blob: 017dd366b31ab58da03eb899802b310e697eeb0f [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`).
Roland Levillaind966ce72017-02-09 16:20:14 +0000622//
623// If `entrypoint` is a valid location it is assumed to already be
624// holding the entrypoint. The case where the entrypoint is passed in
625// is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000626class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
627 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800628 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
629 Location ref,
630 Location entrypoint = Location::NoLocation())
631 : SlowPathCodeARM64(instruction),
632 ref_(ref),
633 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000634 DCHECK(kEmitCompilerReadBarrier);
635 }
636
637 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
638
639 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
640 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000641 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100642 DCHECK(ref_.IsRegister()) << ref_;
643 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000644 DCHECK(instruction_->IsInstanceFieldGet() ||
645 instruction_->IsStaticFieldGet() ||
646 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100647 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000648 instruction_->IsLoadClass() ||
649 instruction_->IsLoadString() ||
650 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100651 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100652 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
653 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000654 << "Unexpected instruction in read barrier marking slow path: "
655 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000656 // The read barrier instrumentation of object ArrayGet
657 // instructions does not support the HIntermediateAddress
658 // instruction.
659 DCHECK(!(instruction_->IsArrayGet() &&
660 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000661
662 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100663 // No need to save live registers; it's taken care of by the
664 // entrypoint. Also, there is no need to update the stack mask,
665 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000666 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100667 DCHECK_NE(ref_.reg(), LR);
668 DCHECK_NE(ref_.reg(), WSP);
669 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100670 // IP0 is used internally by the ReadBarrierMarkRegX entry point
671 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100672 DCHECK_NE(ref_.reg(), IP0);
673 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100674 // "Compact" slow path, saving two moves.
675 //
676 // Instead of using the standard runtime calling convention (input
677 // and output in W0):
678 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100679 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100680 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100681 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100682 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100683 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100684 // of a dedicated entrypoint:
685 //
686 // rX <- ReadBarrierMarkRegX(rX)
687 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800688 if (entrypoint_.IsValid()) {
689 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
690 __ Blr(XRegisterFrom(entrypoint_));
691 } else {
692 // Entrypoint is not already loaded, load from the thread.
693 int32_t entry_point_offset =
694 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
695 // This runtime call does not require a stack map.
696 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
697 }
Roland Levillain44015862016-01-22 11:47:17 +0000698 __ B(GetExitLabel());
699 }
700
701 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100702 // The location (register) of the marked object reference.
703 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000704
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800705 // The location of the entrypoint if it is already loaded.
706 const Location entrypoint_;
707
Roland Levillain44015862016-01-22 11:47:17 +0000708 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
709};
710
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100711// Slow path marking an object reference `ref` during a read barrier,
712// and if needed, atomically updating the field `obj.field` in the
713// object `obj` holding this reference after marking (contrary to
714// ReadBarrierMarkSlowPathARM64 above, which never tries to update
715// `obj.field`).
716//
717// This means that after the execution of this slow path, both `ref`
718// and `obj.field` will be up-to-date; i.e., after the flip, both will
719// hold the same to-space reference (unless another thread installed
720// another object reference (different from `ref`) in `obj.field`).
721class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
722 public:
723 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
724 Location ref,
725 Register obj,
726 Location field_offset,
727 Register temp)
728 : SlowPathCodeARM64(instruction),
729 ref_(ref),
730 obj_(obj),
731 field_offset_(field_offset),
732 temp_(temp) {
733 DCHECK(kEmitCompilerReadBarrier);
734 }
735
736 const char* GetDescription() const OVERRIDE {
737 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
738 }
739
740 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
741 LocationSummary* locations = instruction_->GetLocations();
742 Register ref_reg = WRegisterFrom(ref_);
743 DCHECK(locations->CanCall());
744 DCHECK(ref_.IsRegister()) << ref_;
745 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
746 // This slow path is only used by the UnsafeCASObject intrinsic.
747 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
748 << "Unexpected instruction in read barrier marking and field updating slow path: "
749 << instruction_->DebugName();
750 DCHECK(instruction_->GetLocations()->Intrinsified());
751 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
752 DCHECK(field_offset_.IsRegister()) << field_offset_;
753
754 __ Bind(GetEntryLabel());
755
756 // Save the old reference.
757 // Note that we cannot use IP to save the old reference, as IP is
758 // used internally by the ReadBarrierMarkRegX entry point, and we
759 // need the old reference after the call to that entry point.
760 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
761 __ Mov(temp_.W(), ref_reg);
762
763 // No need to save live registers; it's taken care of by the
764 // entrypoint. Also, there is no need to update the stack mask,
765 // as this runtime call will not trigger a garbage collection.
766 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
767 DCHECK_NE(ref_.reg(), LR);
768 DCHECK_NE(ref_.reg(), WSP);
769 DCHECK_NE(ref_.reg(), WZR);
770 // IP0 is used internally by the ReadBarrierMarkRegX entry point
771 // as a temporary, it cannot be the entry point's input/output.
772 DCHECK_NE(ref_.reg(), IP0);
773 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
774 // "Compact" slow path, saving two moves.
775 //
776 // Instead of using the standard runtime calling convention (input
777 // and output in W0):
778 //
779 // W0 <- ref
780 // W0 <- ReadBarrierMark(W0)
781 // ref <- W0
782 //
783 // we just use rX (the register containing `ref`) as input and output
784 // of a dedicated entrypoint:
785 //
786 // rX <- ReadBarrierMarkRegX(rX)
787 //
788 int32_t entry_point_offset =
789 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
790 // This runtime call does not require a stack map.
791 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
792
793 // If the new reference is different from the old reference,
794 // update the field in the holder (`*(obj_ + field_offset_)`).
795 //
796 // Note that this field could also hold a different object, if
797 // another thread had concurrently changed it. In that case, the
798 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
799 // (CAS) operation below would abort the CAS, leaving the field
800 // as-is.
801 vixl::aarch64::Label done;
802 __ Cmp(temp_.W(), ref_reg);
803 __ B(eq, &done);
804
805 // Update the the holder's field atomically. This may fail if
806 // mutator updates before us, but it's OK. This is achieved
807 // using a strong compare-and-set (CAS) operation with relaxed
808 // memory synchronization ordering, where the expected value is
809 // the old reference and the desired value is the new reference.
810
811 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
812 UseScratchRegisterScope temps(masm);
813
814 // Convenience aliases.
815 Register base = obj_.W();
816 Register offset = XRegisterFrom(field_offset_);
817 Register expected = temp_.W();
818 Register value = ref_reg;
819 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
820 Register tmp_value = temps.AcquireW(); // Value in memory.
821
822 __ Add(tmp_ptr, base.X(), Operand(offset));
823
824 if (kPoisonHeapReferences) {
825 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
826 if (value.Is(expected)) {
827 // Do not poison `value`, as it is the same register as
828 // `expected`, which has just been poisoned.
829 } else {
830 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
831 }
832 }
833
834 // do {
835 // tmp_value = [tmp_ptr] - expected;
836 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
837
Roland Levillain24a4d112016-10-26 13:10:46 +0100838 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100839 __ Bind(&loop_head);
840 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
841 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100842 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100843 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
844 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100845 __ B(&exit_loop);
846 __ Bind(&comparison_failed);
847 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100848 __ Bind(&exit_loop);
849
850 if (kPoisonHeapReferences) {
851 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
852 if (value.Is(expected)) {
853 // Do not unpoison `value`, as it is the same register as
854 // `expected`, which has just been unpoisoned.
855 } else {
856 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
857 }
858 }
859
860 __ Bind(&done);
861 __ B(GetExitLabel());
862 }
863
864 private:
865 // The location (register) of the marked object reference.
866 const Location ref_;
867 // The register containing the object holding the marked object reference field.
868 const Register obj_;
869 // The location of the offset of the marked reference field within `obj_`.
870 Location field_offset_;
871
872 const Register temp_;
873
874 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
875};
876
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000877// Slow path generating a read barrier for a heap reference.
878class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
879 public:
880 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
881 Location out,
882 Location ref,
883 Location obj,
884 uint32_t offset,
885 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000886 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000887 out_(out),
888 ref_(ref),
889 obj_(obj),
890 offset_(offset),
891 index_(index) {
892 DCHECK(kEmitCompilerReadBarrier);
893 // If `obj` is equal to `out` or `ref`, it means the initial object
894 // has been overwritten by (or after) the heap object reference load
895 // to be instrumented, e.g.:
896 //
897 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000898 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000899 //
900 // In that case, we have lost the information about the original
901 // object, and the emitted read barrier cannot work properly.
902 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
903 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
904 }
905
906 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
907 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
908 LocationSummary* locations = instruction_->GetLocations();
909 Primitive::Type type = Primitive::kPrimNot;
910 DCHECK(locations->CanCall());
911 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100912 DCHECK(instruction_->IsInstanceFieldGet() ||
913 instruction_->IsStaticFieldGet() ||
914 instruction_->IsArrayGet() ||
915 instruction_->IsInstanceOf() ||
916 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100917 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000918 << "Unexpected instruction in read barrier for heap reference slow path: "
919 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000920 // The read barrier instrumentation of object ArrayGet
921 // instructions does not support the HIntermediateAddress
922 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000923 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100924 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000925
926 __ Bind(GetEntryLabel());
927
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000928 SaveLiveRegisters(codegen, locations);
929
930 // We may have to change the index's value, but as `index_` is a
931 // constant member (like other "inputs" of this slow path),
932 // introduce a copy of it, `index`.
933 Location index = index_;
934 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100935 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000936 if (instruction_->IsArrayGet()) {
937 // Compute the actual memory offset and store it in `index`.
938 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
939 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
940 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
941 // We are about to change the value of `index_reg` (see the
942 // calls to vixl::MacroAssembler::Lsl and
943 // vixl::MacroAssembler::Mov below), but it has
944 // not been saved by the previous call to
945 // art::SlowPathCode::SaveLiveRegisters, as it is a
946 // callee-save register --
947 // art::SlowPathCode::SaveLiveRegisters does not consider
948 // callee-save registers, as it has been designed with the
949 // assumption that callee-save registers are supposed to be
950 // handled by the called function. So, as a callee-save
951 // register, `index_reg` _would_ eventually be saved onto
952 // the stack, but it would be too late: we would have
953 // changed its value earlier. Therefore, we manually save
954 // it here into another freely available register,
955 // `free_reg`, chosen of course among the caller-save
956 // registers (as a callee-save `free_reg` register would
957 // exhibit the same problem).
958 //
959 // Note we could have requested a temporary register from
960 // the register allocator instead; but we prefer not to, as
961 // this is a slow path, and we know we can find a
962 // caller-save register that is available.
963 Register free_reg = FindAvailableCallerSaveRegister(codegen);
964 __ Mov(free_reg.W(), index_reg);
965 index_reg = free_reg;
966 index = LocationFrom(index_reg);
967 } else {
968 // The initial register stored in `index_` has already been
969 // saved in the call to art::SlowPathCode::SaveLiveRegisters
970 // (as it is not a callee-save register), so we can freely
971 // use it.
972 }
973 // Shifting the index value contained in `index_reg` by the scale
974 // factor (2) cannot overflow in practice, as the runtime is
975 // unable to allocate object arrays with a size larger than
976 // 2^26 - 1 (that is, 2^28 - 4 bytes).
977 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
978 static_assert(
979 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
980 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
981 __ Add(index_reg, index_reg, Operand(offset_));
982 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100983 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
984 // intrinsics, `index_` is not shifted by a scale factor of 2
985 // (as in the case of ArrayGet), as it is actually an offset
986 // to an object field within an object.
987 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000988 DCHECK(instruction_->GetLocations()->Intrinsified());
989 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
990 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
991 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100992 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100993 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000994 }
995 }
996
997 // We're moving two or three locations to locations that could
998 // overlap, so we need a parallel move resolver.
999 InvokeRuntimeCallingConvention calling_convention;
1000 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1001 parallel_move.AddMove(ref_,
1002 LocationFrom(calling_convention.GetRegisterAt(0)),
1003 type,
1004 nullptr);
1005 parallel_move.AddMove(obj_,
1006 LocationFrom(calling_convention.GetRegisterAt(1)),
1007 type,
1008 nullptr);
1009 if (index.IsValid()) {
1010 parallel_move.AddMove(index,
1011 LocationFrom(calling_convention.GetRegisterAt(2)),
1012 Primitive::kPrimInt,
1013 nullptr);
1014 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1015 } else {
1016 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1017 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1018 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001019 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001020 instruction_,
1021 instruction_->GetDexPc(),
1022 this);
1023 CheckEntrypointTypes<
1024 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1025 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1026
1027 RestoreLiveRegisters(codegen, locations);
1028
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001029 __ B(GetExitLabel());
1030 }
1031
1032 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1033
1034 private:
1035 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001036 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1037 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001038 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1039 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1040 return Register(VIXLRegCodeFromART(i), kXRegSize);
1041 }
1042 }
1043 // We shall never fail to find a free caller-save register, as
1044 // there are more than two core caller-save registers on ARM64
1045 // (meaning it is possible to find one which is different from
1046 // `ref` and `obj`).
1047 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1048 LOG(FATAL) << "Could not find a free register";
1049 UNREACHABLE();
1050 }
1051
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001052 const Location out_;
1053 const Location ref_;
1054 const Location obj_;
1055 const uint32_t offset_;
1056 // An additional location containing an index to an array.
1057 // Only used for HArrayGet and the UnsafeGetObject &
1058 // UnsafeGetObjectVolatile intrinsics.
1059 const Location index_;
1060
1061 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1062};
1063
1064// Slow path generating a read barrier for a GC root.
1065class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1066 public:
1067 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001068 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001069 DCHECK(kEmitCompilerReadBarrier);
1070 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001071
1072 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1073 LocationSummary* locations = instruction_->GetLocations();
1074 Primitive::Type type = Primitive::kPrimNot;
1075 DCHECK(locations->CanCall());
1076 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001077 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1078 << "Unexpected instruction in read barrier for GC root slow path: "
1079 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001080
1081 __ Bind(GetEntryLabel());
1082 SaveLiveRegisters(codegen, locations);
1083
1084 InvokeRuntimeCallingConvention calling_convention;
1085 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1086 // The argument of the ReadBarrierForRootSlow is not a managed
1087 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1088 // thus we need a 64-bit move here, and we cannot use
1089 //
1090 // arm64_codegen->MoveLocation(
1091 // LocationFrom(calling_convention.GetRegisterAt(0)),
1092 // root_,
1093 // type);
1094 //
1095 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1096 // reference type (`Primitive::kPrimNot`).
1097 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001098 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001099 instruction_,
1100 instruction_->GetDexPc(),
1101 this);
1102 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1103 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1104
1105 RestoreLiveRegisters(codegen, locations);
1106 __ B(GetExitLabel());
1107 }
1108
1109 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1110
1111 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001112 const Location out_;
1113 const Location root_;
1114
1115 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1116};
1117
Alexandre Rames5319def2014-10-23 10:03:10 +01001118#undef __
1119
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001120Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 Location next_location;
1122 if (type == Primitive::kPrimVoid) {
1123 LOG(FATAL) << "Unreachable type " << type;
1124 }
1125
Alexandre Rames542361f2015-01-29 16:57:31 +00001126 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001127 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1128 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001129 } else if (!Primitive::IsFloatingPointType(type) &&
1130 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1132 } else {
1133 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001134 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1135 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001136 }
1137
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001138 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001139 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001140 return next_location;
1141}
1142
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001144 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001145}
1146
Serban Constantinescu579885a2015-02-22 20:51:33 +00001147CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1148 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001149 const CompilerOptions& compiler_options,
1150 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001151 : CodeGenerator(graph,
1152 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001153 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001154 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001155 callee_saved_core_registers.GetList(),
1156 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001157 compiler_options,
1158 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001159 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001160 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001161 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001162 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001163 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001164 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001165 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001166 uint32_literals_(std::less<uint32_t>(),
1167 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001168 uint64_literals_(std::less<uint64_t>(),
1169 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001170 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1171 boot_image_string_patches_(StringReferenceValueComparator(),
1172 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1173 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001174 boot_image_type_patches_(TypeReferenceValueComparator(),
1175 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1176 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001177 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001178 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001179 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1180 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001181 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1182 jit_class_patches_(TypeReferenceValueComparator(),
1183 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001184 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001185 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001186}
Alexandre Rames5319def2014-10-23 10:03:10 +01001187
Alexandre Rames67555f72014-11-18 10:55:16 +00001188#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001189
Zheng Xu3927c8b2015-11-18 17:46:25 +08001190void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001191 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001192 jump_table->EmitTable(this);
1193 }
1194}
1195
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001196void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001197 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001198 // Ensure we emit the literal pool.
1199 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001200
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001201 CodeGenerator::Finalize(allocator);
1202}
1203
Zheng Xuad4450e2015-04-17 18:48:56 +08001204void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1205 // Note: There are 6 kinds of moves:
1206 // 1. constant -> GPR/FPR (non-cycle)
1207 // 2. constant -> stack (non-cycle)
1208 // 3. GPR/FPR -> GPR/FPR
1209 // 4. GPR/FPR -> stack
1210 // 5. stack -> GPR/FPR
1211 // 6. stack -> stack (non-cycle)
1212 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1213 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1214 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1215 // dependency.
1216 vixl_temps_.Open(GetVIXLAssembler());
1217}
1218
1219void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1220 vixl_temps_.Close();
1221}
1222
1223Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1224 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1225 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1226 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1227 Location scratch = GetScratchLocation(kind);
1228 if (!scratch.Equals(Location::NoLocation())) {
1229 return scratch;
1230 }
1231 // Allocate from VIXL temp registers.
1232 if (kind == Location::kRegister) {
1233 scratch = LocationFrom(vixl_temps_.AcquireX());
1234 } else {
1235 DCHECK(kind == Location::kFpuRegister);
1236 scratch = LocationFrom(vixl_temps_.AcquireD());
1237 }
1238 AddScratchLocation(scratch);
1239 return scratch;
1240}
1241
1242void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1243 if (loc.IsRegister()) {
1244 vixl_temps_.Release(XRegisterFrom(loc));
1245 } else {
1246 DCHECK(loc.IsFpuRegister());
1247 vixl_temps_.Release(DRegisterFrom(loc));
1248 }
1249 RemoveScratchLocation(loc);
1250}
1251
Alexandre Rames3e69f162014-12-10 10:36:50 +00001252void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001253 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001255}
1256
Alexandre Rames5319def2014-10-23 10:03:10 +01001257void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001258 MacroAssembler* masm = GetVIXLAssembler();
1259 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001260 __ Bind(&frame_entry_label_);
1261
Serban Constantinescu02164b32014-11-13 14:05:07 +00001262 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1263 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001264 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001265 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001266 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001267 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001268 __ Ldr(wzr, MemOperand(temp, 0));
1269 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001270 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001271
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001272 if (!HasEmptyFrame()) {
1273 int frame_size = GetFrameSize();
1274 // Stack layout:
1275 // sp[frame_size - 8] : lr.
1276 // ... : other preserved core registers.
1277 // ... : other preserved fp registers.
1278 // ... : reserved frame space.
1279 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001280
1281 // Save the current method if we need it. Note that we do not
1282 // do this in HCurrentMethod, as the instruction might have been removed
1283 // in the SSA graph.
1284 if (RequiresCurrentMethod()) {
1285 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001286 } else {
1287 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001288 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001289 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001290 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1291 frame_size - GetCoreSpillSize());
1292 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1293 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001294
1295 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1296 // Initialize should_deoptimize flag to 0.
1297 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1298 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1299 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001300 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001301}
1302
1303void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001304 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001305 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001306 if (!HasEmptyFrame()) {
1307 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001308 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1309 frame_size - FrameEntrySpillSize());
1310 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1311 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001312 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001313 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001314 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001315 __ Ret();
1316 GetAssembler()->cfi().RestoreState();
1317 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001318}
1319
Scott Wakeling97c72b72016-06-24 16:19:36 +01001320CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001321 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001322 return CPURegList(CPURegister::kRegister, kXRegSize,
1323 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001324}
1325
Scott Wakeling97c72b72016-06-24 16:19:36 +01001326CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001327 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1328 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001329 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1330 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001331}
1332
Alexandre Rames5319def2014-10-23 10:03:10 +01001333void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1334 __ Bind(GetLabelOf(block));
1335}
1336
Calin Juravle175dc732015-08-25 15:42:32 +01001337void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1338 DCHECK(location.IsRegister());
1339 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1340}
1341
Calin Juravlee460d1d2015-09-29 04:52:17 +01001342void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1343 if (location.IsRegister()) {
1344 locations->AddTemp(location);
1345 } else {
1346 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1347 }
1348}
1349
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001350void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001351 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001353 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001354 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001355 if (value_can_be_null) {
1356 __ Cbz(value, &done);
1357 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001358 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001359 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001360 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001361 if (value_can_be_null) {
1362 __ Bind(&done);
1363 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001364}
1365
David Brazdil58282f42016-01-14 12:45:10 +00001366void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001367 // Blocked core registers:
1368 // lr : Runtime reserved.
1369 // tr : Runtime reserved.
1370 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1371 // ip1 : VIXL core temp.
1372 // ip0 : VIXL core temp.
1373 //
1374 // Blocked fp registers:
1375 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001376 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1377 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001378 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001379 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001381
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001382 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001383 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001384 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001385 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001386
David Brazdil58282f42016-01-14 12:45:10 +00001387 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001388 // Stubs do not save callee-save floating point registers. If the graph
1389 // is debuggable, we need to deal with these registers differently. For
1390 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001391 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1392 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001393 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001394 }
1395 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001396}
1397
Alexandre Rames3e69f162014-12-10 10:36:50 +00001398size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1399 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1400 __ Str(reg, MemOperand(sp, stack_index));
1401 return kArm64WordSize;
1402}
1403
1404size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1405 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1406 __ Ldr(reg, MemOperand(sp, stack_index));
1407 return kArm64WordSize;
1408}
1409
1410size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1411 FPRegister reg = FPRegister(reg_id, kDRegSize);
1412 __ Str(reg, MemOperand(sp, stack_index));
1413 return kArm64WordSize;
1414}
1415
1416size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1417 FPRegister reg = FPRegister(reg_id, kDRegSize);
1418 __ Ldr(reg, MemOperand(sp, stack_index));
1419 return kArm64WordSize;
1420}
1421
Alexandre Rames5319def2014-10-23 10:03:10 +01001422void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001423 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001424}
1425
1426void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001427 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001428}
1429
Alexandre Rames67555f72014-11-18 10:55:16 +00001430void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001431 if (constant->IsIntConstant()) {
1432 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1433 } else if (constant->IsLongConstant()) {
1434 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1435 } else if (constant->IsNullConstant()) {
1436 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001437 } else if (constant->IsFloatConstant()) {
1438 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1439 } else {
1440 DCHECK(constant->IsDoubleConstant());
1441 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1442 }
1443}
1444
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445
1446static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1447 DCHECK(constant.IsConstant());
1448 HConstant* cst = constant.GetConstant();
1449 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001450 // Null is mapped to a core W register, which we associate with kPrimInt.
1451 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001452 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1453 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1454 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1455}
1456
Roland Levillain558dea12017-01-27 19:40:44 +00001457// Allocate a scratch register from the VIXL pool, querying first into
1458// the floating-point register pool, and then the the core register
1459// pool. This is essentially a reimplementation of
1460// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1461// using a different allocation strategy.
1462static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1463 vixl::aarch64::UseScratchRegisterScope* temps,
1464 int size_in_bits) {
1465 return masm->GetScratchFPRegisterList()->IsEmpty()
1466 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1467 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1468}
1469
Calin Juravlee460d1d2015-09-29 04:52:17 +01001470void CodeGeneratorARM64::MoveLocation(Location destination,
1471 Location source,
1472 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001473 if (source.Equals(destination)) {
1474 return;
1475 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001476
1477 // A valid move can always be inferred from the destination and source
1478 // locations. When moving from and to a register, the argument type can be
1479 // used to generate 32bit instead of 64bit moves. In debug mode we also
1480 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001481 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001482
1483 if (destination.IsRegister() || destination.IsFpuRegister()) {
1484 if (unspecified_type) {
1485 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1486 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001487 (src_cst != nullptr && (src_cst->IsIntConstant()
1488 || src_cst->IsFloatConstant()
1489 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001490 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001491 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001492 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001493 // If the source is a double stack slot or a 64bit constant, a 64bit
1494 // type is appropriate. Else the source is a register, and since the
1495 // type has not been specified, we chose a 64bit type to force a 64bit
1496 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001497 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001498 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001499 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001500 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1501 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1502 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1504 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1505 __ Ldr(dst, StackOperandFrom(source));
1506 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001507 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001508 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001509 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001510 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001511 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001512 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001513 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001514 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1515 ? Primitive::kPrimLong
1516 : Primitive::kPrimInt;
1517 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1518 }
1519 } else {
1520 DCHECK(source.IsFpuRegister());
1521 if (destination.IsRegister()) {
1522 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1523 ? Primitive::kPrimDouble
1524 : Primitive::kPrimFloat;
1525 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1526 } else {
1527 DCHECK(destination.IsFpuRegister());
1528 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 }
1530 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531 } else { // The destination is not a register. It must be a stack slot.
1532 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1533 if (source.IsRegister() || source.IsFpuRegister()) {
1534 if (unspecified_type) {
1535 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001536 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001538 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 }
1540 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001541 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1542 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1543 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001544 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001545 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1546 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001547 UseScratchRegisterScope temps(GetVIXLAssembler());
1548 HConstant* src_cst = source.GetConstant();
1549 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001550 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001551 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1552 ? Register(xzr)
1553 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001554 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001555 if (src_cst->IsIntConstant()) {
1556 temp = temps.AcquireW();
1557 } else if (src_cst->IsLongConstant()) {
1558 temp = temps.AcquireX();
1559 } else if (src_cst->IsFloatConstant()) {
1560 temp = temps.AcquireS();
1561 } else {
1562 DCHECK(src_cst->IsDoubleConstant());
1563 temp = temps.AcquireD();
1564 }
1565 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001566 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001567 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001570 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001571 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001572 // Use any scratch register (a core or a floating-point one)
1573 // from VIXL scratch register pools as a temporary.
1574 //
1575 // We used to only use the FP scratch register pool, but in some
1576 // rare cases the only register from this pool (D31) would
1577 // already be used (e.g. within a ParallelMove instruction, when
1578 // a move is blocked by a another move requiring a scratch FP
1579 // register, which would reserve D31). To prevent this issue, we
1580 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001581 //
1582 // Also, we start by asking for a FP scratch register first, as the
1583 // demand of scratch core registers is higher. This is why we
1584 // use AcquireFPOrCoreCPURegisterOfSize instead of
1585 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1586 // allocates core scratch registers first.
1587 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1588 GetVIXLAssembler(),
1589 &temps,
1590 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001591 __ Ldr(temp, StackOperandFrom(source));
1592 __ Str(temp, StackOperandFrom(destination));
1593 }
1594 }
1595}
1596
1597void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001598 CPURegister dst,
1599 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001600 switch (type) {
1601 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001602 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001603 break;
1604 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001605 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001606 break;
1607 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001608 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001609 break;
1610 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001611 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001612 break;
1613 case Primitive::kPrimInt:
1614 case Primitive::kPrimNot:
1615 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001616 case Primitive::kPrimFloat:
1617 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001618 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001619 __ Ldr(dst, src);
1620 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001621 case Primitive::kPrimVoid:
1622 LOG(FATAL) << "Unreachable type " << type;
1623 }
1624}
1625
Calin Juravle77520bc2015-01-12 18:45:46 +00001626void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001627 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001628 const MemOperand& src,
1629 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001630 MacroAssembler* masm = GetVIXLAssembler();
1631 BlockPoolsScope block_pools(masm);
1632 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001633 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001634 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001635
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001636 DCHECK(!src.IsPreIndex());
1637 DCHECK(!src.IsPostIndex());
1638
1639 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001640 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001641 MemOperand base = MemOperand(temp_base);
1642 switch (type) {
1643 case Primitive::kPrimBoolean:
1644 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001645 if (needs_null_check) {
1646 MaybeRecordImplicitNullCheck(instruction);
1647 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001648 break;
1649 case Primitive::kPrimByte:
1650 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001651 if (needs_null_check) {
1652 MaybeRecordImplicitNullCheck(instruction);
1653 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1655 break;
1656 case Primitive::kPrimChar:
1657 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001658 if (needs_null_check) {
1659 MaybeRecordImplicitNullCheck(instruction);
1660 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001661 break;
1662 case Primitive::kPrimShort:
1663 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001664 if (needs_null_check) {
1665 MaybeRecordImplicitNullCheck(instruction);
1666 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001667 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1668 break;
1669 case Primitive::kPrimInt:
1670 case Primitive::kPrimNot:
1671 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001672 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001673 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001674 if (needs_null_check) {
1675 MaybeRecordImplicitNullCheck(instruction);
1676 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001677 break;
1678 case Primitive::kPrimFloat:
1679 case Primitive::kPrimDouble: {
1680 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001681 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001682
1683 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1684 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001685 if (needs_null_check) {
1686 MaybeRecordImplicitNullCheck(instruction);
1687 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001688 __ Fmov(FPRegister(dst), temp);
1689 break;
1690 }
1691 case Primitive::kPrimVoid:
1692 LOG(FATAL) << "Unreachable type " << type;
1693 }
1694}
1695
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001696void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001697 CPURegister src,
1698 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001699 switch (type) {
1700 case Primitive::kPrimBoolean:
1701 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001702 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001703 break;
1704 case Primitive::kPrimChar:
1705 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001706 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001707 break;
1708 case Primitive::kPrimInt:
1709 case Primitive::kPrimNot:
1710 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001711 case Primitive::kPrimFloat:
1712 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001713 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001714 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001715 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001716 case Primitive::kPrimVoid:
1717 LOG(FATAL) << "Unreachable type " << type;
1718 }
1719}
1720
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001721void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1722 CPURegister src,
1723 const MemOperand& dst) {
1724 UseScratchRegisterScope temps(GetVIXLAssembler());
1725 Register temp_base = temps.AcquireX();
1726
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001727 DCHECK(!dst.IsPreIndex());
1728 DCHECK(!dst.IsPostIndex());
1729
1730 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001731 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001732 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001733 MemOperand base = MemOperand(temp_base);
1734 switch (type) {
1735 case Primitive::kPrimBoolean:
1736 case Primitive::kPrimByte:
1737 __ Stlrb(Register(src), base);
1738 break;
1739 case Primitive::kPrimChar:
1740 case Primitive::kPrimShort:
1741 __ Stlrh(Register(src), base);
1742 break;
1743 case Primitive::kPrimInt:
1744 case Primitive::kPrimNot:
1745 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001746 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001747 __ Stlr(Register(src), base);
1748 break;
1749 case Primitive::kPrimFloat:
1750 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001751 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001752 Register temp_src;
1753 if (src.IsZero()) {
1754 // The zero register is used to avoid synthesizing zero constants.
1755 temp_src = Register(src);
1756 } else {
1757 DCHECK(src.IsFPRegister());
1758 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1759 __ Fmov(temp_src, FPRegister(src));
1760 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001761
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001762 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001763 break;
1764 }
1765 case Primitive::kPrimVoid:
1766 LOG(FATAL) << "Unreachable type " << type;
1767 }
1768}
1769
Calin Juravle175dc732015-08-25 15:42:32 +01001770void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1771 HInstruction* instruction,
1772 uint32_t dex_pc,
1773 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001774 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001775 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001776 if (EntrypointRequiresStackMap(entrypoint)) {
1777 RecordPcInfo(instruction, dex_pc, slow_path);
1778 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001779}
1780
Roland Levillaindec8f632016-07-22 17:10:06 +01001781void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1782 HInstruction* instruction,
1783 SlowPathCode* slow_path) {
1784 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001785 GenerateInvokeRuntime(entry_point_offset);
1786}
1787
1788void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001789 BlockPoolsScope block_pools(GetVIXLAssembler());
1790 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1791 __ Blr(lr);
1792}
1793
Alexandre Rames67555f72014-11-18 10:55:16 +00001794void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001795 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001796 UseScratchRegisterScope temps(GetVIXLAssembler());
1797 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001798 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1799
Serban Constantinescu02164b32014-11-13 14:05:07 +00001800 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001801 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1802 __ Add(temp, class_reg, status_offset);
1803 __ Ldar(temp, HeapOperand(temp));
1804 __ Cmp(temp, mirror::Class::kStatusInitialized);
1805 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001806 __ Bind(slow_path->GetExitLabel());
1807}
Alexandre Rames5319def2014-10-23 10:03:10 +01001808
Roland Levillain44015862016-01-22 11:47:17 +00001809void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001810 BarrierType type = BarrierAll;
1811
1812 switch (kind) {
1813 case MemBarrierKind::kAnyAny:
1814 case MemBarrierKind::kAnyStore: {
1815 type = BarrierAll;
1816 break;
1817 }
1818 case MemBarrierKind::kLoadAny: {
1819 type = BarrierReads;
1820 break;
1821 }
1822 case MemBarrierKind::kStoreStore: {
1823 type = BarrierWrites;
1824 break;
1825 }
1826 default:
1827 LOG(FATAL) << "Unexpected memory barrier " << kind;
1828 }
1829 __ Dmb(InnerShareable, type);
1830}
1831
Serban Constantinescu02164b32014-11-13 14:05:07 +00001832void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1833 HBasicBlock* successor) {
1834 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001835 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1836 if (slow_path == nullptr) {
1837 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1838 instruction->SetSlowPath(slow_path);
1839 codegen_->AddSlowPath(slow_path);
1840 if (successor != nullptr) {
1841 DCHECK(successor->IsLoopHeader());
1842 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1843 }
1844 } else {
1845 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1846 }
1847
Serban Constantinescu02164b32014-11-13 14:05:07 +00001848 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1849 Register temp = temps.AcquireW();
1850
Andreas Gampe542451c2016-07-26 09:02:02 -07001851 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001852 if (successor == nullptr) {
1853 __ Cbnz(temp, slow_path->GetEntryLabel());
1854 __ Bind(slow_path->GetReturnLabel());
1855 } else {
1856 __ Cbz(temp, codegen_->GetLabelOf(successor));
1857 __ B(slow_path->GetEntryLabel());
1858 // slow_path will return to GetLabelOf(successor).
1859 }
1860}
1861
Alexandre Rames5319def2014-10-23 10:03:10 +01001862InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1863 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001864 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001865 assembler_(codegen->GetAssembler()),
1866 codegen_(codegen) {}
1867
1868#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001869 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001870
1871#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1872
1873enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001874 // Using a base helps identify when we hit such breakpoints.
1875 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001876#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1877 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1878#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1879};
1880
1881#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001882 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001883 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1884 } \
1885 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1887 locations->SetOut(Location::Any()); \
1888 }
1889 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1890#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1891
1892#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001893#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001894
Alexandre Rames67555f72014-11-18 10:55:16 +00001895void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001896 DCHECK_EQ(instr->InputCount(), 2U);
1897 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1898 Primitive::Type type = instr->GetResultType();
1899 switch (type) {
1900 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001901 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001902 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001903 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001905 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001906
1907 case Primitive::kPrimFloat:
1908 case Primitive::kPrimDouble:
1909 locations->SetInAt(0, Location::RequiresFpuRegister());
1910 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001911 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001912 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001913
Alexandre Rames5319def2014-10-23 10:03:10 +01001914 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001915 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001916 }
1917}
1918
Alexandre Rames09a99962015-04-15 11:47:56 +01001919void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001920 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1921
1922 bool object_field_get_with_read_barrier =
1923 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001924 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001925 new (GetGraph()->GetArena()) LocationSummary(instruction,
1926 object_field_get_with_read_barrier ?
1927 LocationSummary::kCallOnSlowPath :
1928 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001929 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001930 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00001931 // We need a temporary register for the read barrier marking slow
1932 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
1933 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01001934 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001935 locations->SetInAt(0, Location::RequiresRegister());
1936 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1937 locations->SetOut(Location::RequiresFpuRegister());
1938 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001939 // The output overlaps for an object field get when read barriers
1940 // are enabled: we do not want the load to overwrite the object's
1941 // location, as we need it to emit the read barrier.
1942 locations->SetOut(
1943 Location::RequiresRegister(),
1944 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001945 }
1946}
1947
1948void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1949 const FieldInfo& field_info) {
1950 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001951 LocationSummary* locations = instruction->GetLocations();
1952 Location base_loc = locations->InAt(0);
1953 Location out = locations->Out();
1954 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001955 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001956 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001957 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001958
Roland Levillain44015862016-01-22 11:47:17 +00001959 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1960 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001961 // /* HeapReference<Object> */ out = *(base + offset)
1962 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00001963 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00001964 // Note that potential implicit null checks are handled in this
1965 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1966 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1967 instruction,
1968 out,
1969 base,
1970 offset,
1971 temp,
1972 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001973 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001974 } else {
1975 // General case.
1976 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001977 // Note that a potential implicit null check is handled in this
1978 // CodeGeneratorARM64::LoadAcquire call.
1979 // NB: LoadAcquire will record the pc info if needed.
1980 codegen_->LoadAcquire(
1981 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001982 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001983 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001984 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001985 }
Roland Levillain44015862016-01-22 11:47:17 +00001986 if (field_type == Primitive::kPrimNot) {
1987 // If read barriers are enabled, emit read barriers other than
1988 // Baker's using a slow path (and also unpoison the loaded
1989 // reference, if heap poisoning is enabled).
1990 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1991 }
Roland Levillain4d027112015-07-01 15:41:14 +01001992 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001993}
1994
1995void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1996 LocationSummary* locations =
1997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1998 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001999 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2000 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2001 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002002 locations->SetInAt(1, Location::RequiresFpuRegister());
2003 } else {
2004 locations->SetInAt(1, Location::RequiresRegister());
2005 }
2006}
2007
2008void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002009 const FieldInfo& field_info,
2010 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002011 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01002012 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01002013
2014 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002015 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002016 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002017 Offset offset = field_info.GetFieldOffset();
2018 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002019
Roland Levillain4d027112015-07-01 15:41:14 +01002020 {
2021 // We use a block to end the scratch scope before the write barrier, thus
2022 // freeing the temporary registers so they can be used in `MarkGCCard`.
2023 UseScratchRegisterScope temps(GetVIXLAssembler());
2024
2025 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2026 DCHECK(value.IsW());
2027 Register temp = temps.AcquireW();
2028 __ Mov(temp, value.W());
2029 GetAssembler()->PoisonHeapReference(temp.W());
2030 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002031 }
Roland Levillain4d027112015-07-01 15:41:14 +01002032
2033 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002034 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
2035 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002036 } else {
2037 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2038 codegen_->MaybeRecordImplicitNullCheck(instruction);
2039 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002040 }
2041
2042 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002043 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002044 }
2045}
2046
Alexandre Rames67555f72014-11-18 10:55:16 +00002047void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002048 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002049
2050 switch (type) {
2051 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002052 case Primitive::kPrimLong: {
2053 Register dst = OutputRegister(instr);
2054 Register lhs = InputRegisterAt(instr, 0);
2055 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002056 if (instr->IsAdd()) {
2057 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002058 } else if (instr->IsAnd()) {
2059 __ And(dst, lhs, rhs);
2060 } else if (instr->IsOr()) {
2061 __ Orr(dst, lhs, rhs);
2062 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002063 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002064 } else if (instr->IsRor()) {
2065 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002066 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002067 __ Ror(dst, lhs, shift);
2068 } else {
2069 // Ensure shift distance is in the same size register as the result. If
2070 // we are rotating a long and the shift comes in a w register originally,
2071 // we don't need to sxtw for use as an x since the shift distances are
2072 // all & reg_bits - 1.
2073 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2074 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002075 } else {
2076 DCHECK(instr->IsXor());
2077 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002078 }
2079 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002080 }
2081 case Primitive::kPrimFloat:
2082 case Primitive::kPrimDouble: {
2083 FPRegister dst = OutputFPRegister(instr);
2084 FPRegister lhs = InputFPRegisterAt(instr, 0);
2085 FPRegister rhs = InputFPRegisterAt(instr, 1);
2086 if (instr->IsAdd()) {
2087 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002088 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002089 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002090 } else {
2091 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002092 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002093 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002094 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002095 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002096 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002097 }
2098}
2099
Serban Constantinescu02164b32014-11-13 14:05:07 +00002100void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2101 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2102
2103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2104 Primitive::Type type = instr->GetResultType();
2105 switch (type) {
2106 case Primitive::kPrimInt:
2107 case Primitive::kPrimLong: {
2108 locations->SetInAt(0, Location::RequiresRegister());
2109 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2110 locations->SetOut(Location::RequiresRegister());
2111 break;
2112 }
2113 default:
2114 LOG(FATAL) << "Unexpected shift type " << type;
2115 }
2116}
2117
2118void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2119 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2120
2121 Primitive::Type type = instr->GetType();
2122 switch (type) {
2123 case Primitive::kPrimInt:
2124 case Primitive::kPrimLong: {
2125 Register dst = OutputRegister(instr);
2126 Register lhs = InputRegisterAt(instr, 0);
2127 Operand rhs = InputOperandAt(instr, 1);
2128 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002129 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002130 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002131 if (instr->IsShl()) {
2132 __ Lsl(dst, lhs, shift_value);
2133 } else if (instr->IsShr()) {
2134 __ Asr(dst, lhs, shift_value);
2135 } else {
2136 __ Lsr(dst, lhs, shift_value);
2137 }
2138 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002139 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002140
2141 if (instr->IsShl()) {
2142 __ Lsl(dst, lhs, rhs_reg);
2143 } else if (instr->IsShr()) {
2144 __ Asr(dst, lhs, rhs_reg);
2145 } else {
2146 __ Lsr(dst, lhs, rhs_reg);
2147 }
2148 }
2149 break;
2150 }
2151 default:
2152 LOG(FATAL) << "Unexpected shift operation type " << type;
2153 }
2154}
2155
Alexandre Rames5319def2014-10-23 10:03:10 +01002156void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002157 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002158}
2159
2160void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002161 HandleBinaryOp(instruction);
2162}
2163
2164void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2165 HandleBinaryOp(instruction);
2166}
2167
2168void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2169 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002170}
2171
Artem Serov7fc63502016-02-09 17:15:29 +00002172void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002173 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2175 locations->SetInAt(0, Location::RequiresRegister());
2176 // There is no immediate variant of negated bitwise instructions in AArch64.
2177 locations->SetInAt(1, Location::RequiresRegister());
2178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2179}
2180
Artem Serov7fc63502016-02-09 17:15:29 +00002181void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002182 Register dst = OutputRegister(instr);
2183 Register lhs = InputRegisterAt(instr, 0);
2184 Register rhs = InputRegisterAt(instr, 1);
2185
2186 switch (instr->GetOpKind()) {
2187 case HInstruction::kAnd:
2188 __ Bic(dst, lhs, rhs);
2189 break;
2190 case HInstruction::kOr:
2191 __ Orn(dst, lhs, rhs);
2192 break;
2193 case HInstruction::kXor:
2194 __ Eon(dst, lhs, rhs);
2195 break;
2196 default:
2197 LOG(FATAL) << "Unreachable";
2198 }
2199}
2200
Alexandre Rames8626b742015-11-25 16:28:08 +00002201void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2202 HArm64DataProcWithShifterOp* instruction) {
2203 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2204 instruction->GetType() == Primitive::kPrimLong);
2205 LocationSummary* locations =
2206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2207 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2208 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2209 } else {
2210 locations->SetInAt(0, Location::RequiresRegister());
2211 }
2212 locations->SetInAt(1, Location::RequiresRegister());
2213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2214}
2215
2216void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2217 HArm64DataProcWithShifterOp* instruction) {
2218 Primitive::Type type = instruction->GetType();
2219 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2220 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2221 Register out = OutputRegister(instruction);
2222 Register left;
2223 if (kind != HInstruction::kNeg) {
2224 left = InputRegisterAt(instruction, 0);
2225 }
2226 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2227 // shifter operand operation, the IR generating `right_reg` (input to the type
2228 // conversion) can have a different type from the current instruction's type,
2229 // so we manually indicate the type.
2230 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002231 int64_t shift_amount = instruction->GetShiftAmount() &
2232 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002233
2234 Operand right_operand(0);
2235
2236 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2237 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2238 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2239 } else {
2240 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2241 }
2242
2243 // Logical binary operations do not support extension operations in the
2244 // operand. Note that VIXL would still manage if it was passed by generating
2245 // the extension as a separate instruction.
2246 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2247 DCHECK(!right_operand.IsExtendedRegister() ||
2248 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2249 kind != HInstruction::kNeg));
2250 switch (kind) {
2251 case HInstruction::kAdd:
2252 __ Add(out, left, right_operand);
2253 break;
2254 case HInstruction::kAnd:
2255 __ And(out, left, right_operand);
2256 break;
2257 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002258 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002259 __ Neg(out, right_operand);
2260 break;
2261 case HInstruction::kOr:
2262 __ Orr(out, left, right_operand);
2263 break;
2264 case HInstruction::kSub:
2265 __ Sub(out, left, right_operand);
2266 break;
2267 case HInstruction::kXor:
2268 __ Eor(out, left, right_operand);
2269 break;
2270 default:
2271 LOG(FATAL) << "Unexpected operation kind: " << kind;
2272 UNREACHABLE();
2273 }
2274}
2275
Artem Serov328429f2016-07-06 16:23:04 +01002276void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002277 LocationSummary* locations =
2278 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2279 locations->SetInAt(0, Location::RequiresRegister());
2280 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2281 locations->SetOut(Location::RequiresRegister());
2282}
2283
Roland Levillain19c54192016-11-04 13:44:09 +00002284void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002285 __ Add(OutputRegister(instruction),
2286 InputRegisterAt(instruction, 0),
2287 Operand(InputOperandAt(instruction, 1)));
2288}
2289
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002290void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002291 LocationSummary* locations =
2292 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002293 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2294 if (instr->GetOpKind() == HInstruction::kSub &&
2295 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002296 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002297 // Don't allocate register for Mneg instruction.
2298 } else {
2299 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2300 Location::RequiresRegister());
2301 }
2302 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2303 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2305}
2306
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002307void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002308 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002309 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2310 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002311
2312 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2313 // This fixup should be carried out for all multiply-accumulate instructions:
2314 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2315 if (instr->GetType() == Primitive::kPrimLong &&
2316 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2317 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002318 vixl::aarch64::Instruction* prev =
2319 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002320 if (prev->IsLoadOrStore()) {
2321 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002322 vixl::CodeBufferCheckScope scope(masm,
2323 kInstructionSize,
2324 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2325 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002326 __ nop();
2327 }
2328 }
2329
2330 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002331 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002332 __ Madd(res, mul_left, mul_right, accumulator);
2333 } else {
2334 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002335 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002336 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002337 __ Mneg(res, mul_left, mul_right);
2338 } else {
2339 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2340 __ Msub(res, mul_left, mul_right, accumulator);
2341 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002342 }
2343}
2344
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002345void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002346 bool object_array_get_with_read_barrier =
2347 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002348 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002349 new (GetGraph()->GetArena()) LocationSummary(instruction,
2350 object_array_get_with_read_barrier ?
2351 LocationSummary::kCallOnSlowPath :
2352 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002353 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002354 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002355 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002356 locations->SetInAt(0, Location::RequiresRegister());
2357 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002358 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2359 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2360 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002361 // The output overlaps in the case of an object array get with
2362 // read barriers enabled: we do not want the move to overwrite the
2363 // array's location, as we need it to emit the read barrier.
2364 locations->SetOut(
2365 Location::RequiresRegister(),
2366 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002367 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002368}
2369
2370void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002371 Primitive::Type type = instruction->GetType();
2372 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002373 LocationSummary* locations = instruction->GetLocations();
2374 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002375 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002376 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002377 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2378 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002379 MacroAssembler* masm = GetVIXLAssembler();
2380 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002381 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002382 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002383
Roland Levillain19c54192016-11-04 13:44:09 +00002384 // The read barrier instrumentation of object ArrayGet instructions
2385 // does not support the HIntermediateAddress instruction.
2386 DCHECK(!((type == Primitive::kPrimNot) &&
2387 instruction->GetArray()->IsIntermediateAddress() &&
2388 kEmitCompilerReadBarrier));
2389
Roland Levillain44015862016-01-22 11:47:17 +00002390 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2391 // Object ArrayGet with Baker's read barrier case.
2392 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002393 // Note that a potential implicit null check is handled in the
2394 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2395 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2396 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002397 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002398 // General case.
2399 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002400 Register length;
2401 if (maybe_compressed_char_at) {
2402 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2403 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002404 if (instruction->GetArray()->IsIntermediateAddress()) {
2405 DCHECK_LT(count_offset, offset);
2406 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2407 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2408 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2409 } else {
2410 __ Ldr(length, HeapOperand(obj, count_offset));
2411 }
jessicahandojo05765752016-09-09 19:01:32 -07002412 codegen_->MaybeRecordImplicitNullCheck(instruction);
2413 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002414 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002415 if (maybe_compressed_char_at) {
2416 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002417 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2418 "Expecting 0=compressed, 1=uncompressed");
2419 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002420 __ Ldrb(Register(OutputCPURegister(instruction)),
2421 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2422 __ B(&done);
2423 __ Bind(&uncompressed_load);
2424 __ Ldrh(Register(OutputCPURegister(instruction)),
2425 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2426 __ Bind(&done);
2427 } else {
2428 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2429 source = HeapOperand(obj, offset);
2430 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002431 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002432 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002433 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002434 // We do not need to compute the intermediate address from the array: the
2435 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002436 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002437 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002438 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002439 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2440 }
2441 temp = obj;
2442 } else {
2443 __ Add(temp, obj, offset);
2444 }
jessicahandojo05765752016-09-09 19:01:32 -07002445 if (maybe_compressed_char_at) {
2446 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002447 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2448 "Expecting 0=compressed, 1=uncompressed");
2449 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002450 __ Ldrb(Register(OutputCPURegister(instruction)),
2451 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2452 __ B(&done);
2453 __ Bind(&uncompressed_load);
2454 __ Ldrh(Register(OutputCPURegister(instruction)),
2455 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2456 __ Bind(&done);
2457 } else {
2458 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2459 }
Roland Levillain44015862016-01-22 11:47:17 +00002460 }
jessicahandojo05765752016-09-09 19:01:32 -07002461 if (!maybe_compressed_char_at) {
2462 codegen_->Load(type, OutputCPURegister(instruction), source);
2463 codegen_->MaybeRecordImplicitNullCheck(instruction);
2464 }
Roland Levillain44015862016-01-22 11:47:17 +00002465
2466 if (type == Primitive::kPrimNot) {
2467 static_assert(
2468 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2469 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2470 Location obj_loc = locations->InAt(0);
2471 if (index.IsConstant()) {
2472 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2473 } else {
2474 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2475 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002476 }
Roland Levillain4d027112015-07-01 15:41:14 +01002477 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002478}
2479
Alexandre Rames5319def2014-10-23 10:03:10 +01002480void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2482 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002484}
2485
2486void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002487 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002488 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002489 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002490 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002491 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002492 // Mask out compression flag from String's array length.
2493 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002494 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002495 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002496}
2497
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002498void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002499 Primitive::Type value_type = instruction->GetComponentType();
2500
2501 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002502 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2503 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002504 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002505 LocationSummary::kCallOnSlowPath :
2506 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002507 locations->SetInAt(0, Location::RequiresRegister());
2508 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002509 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2510 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2511 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002512 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002513 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002514 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002515 }
2516}
2517
2518void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2519 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002520 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002521 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002522 bool needs_write_barrier =
2523 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002524
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002525 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002526 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002527 CPURegister source = value;
2528 Location index = locations->InAt(1);
2529 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2530 MemOperand destination = HeapOperand(array);
2531 MacroAssembler* masm = GetVIXLAssembler();
2532 BlockPoolsScope block_pools(masm);
2533
2534 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002535 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002536 if (index.IsConstant()) {
2537 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2538 destination = HeapOperand(array, offset);
2539 } else {
2540 UseScratchRegisterScope temps(masm);
2541 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002542 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002543 // We do not need to compute the intermediate address from the array: the
2544 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002545 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002546 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002547 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002548 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2549 }
2550 temp = array;
2551 } else {
2552 __ Add(temp, array, offset);
2553 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002554 destination = HeapOperand(temp,
2555 XRegisterFrom(index),
2556 LSL,
2557 Primitive::ComponentSizeShift(value_type));
2558 }
2559 codegen_->Store(value_type, value, destination);
2560 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002561 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002562 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002563 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002564 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002565 {
2566 // We use a block to end the scratch scope before the write barrier, thus
2567 // freeing the temporary registers so they can be used in `MarkGCCard`.
2568 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002569 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002570 if (index.IsConstant()) {
2571 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002572 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002573 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002574 destination = HeapOperand(temp,
2575 XRegisterFrom(index),
2576 LSL,
2577 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002578 }
2579
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002580 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2581 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2582 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2583
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002584 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002585 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2586 codegen_->AddSlowPath(slow_path);
2587 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002588 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002589 __ Cbnz(Register(value), &non_zero);
2590 if (!index.IsConstant()) {
2591 __ Add(temp, array, offset);
2592 }
2593 __ Str(wzr, destination);
2594 codegen_->MaybeRecordImplicitNullCheck(instruction);
2595 __ B(&done);
2596 __ Bind(&non_zero);
2597 }
2598
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002599 // Note that when Baker read barriers are enabled, the type
2600 // checks are performed without read barriers. This is fine,
2601 // even in the case where a class object is in the from-space
2602 // after the flip, as a comparison involving such a type would
2603 // not produce a false positive; it may of course produce a
2604 // false negative, in which case we would take the ArraySet
2605 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002606
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002607 Register temp2 = temps.AcquireSameSizeAs(array);
2608 // /* HeapReference<Class> */ temp = array->klass_
2609 __ Ldr(temp, HeapOperand(array, class_offset));
2610 codegen_->MaybeRecordImplicitNullCheck(instruction);
2611 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002612
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002613 // /* HeapReference<Class> */ temp = temp->component_type_
2614 __ Ldr(temp, HeapOperand(temp, component_offset));
2615 // /* HeapReference<Class> */ temp2 = value->klass_
2616 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2617 // If heap poisoning is enabled, no need to unpoison `temp`
2618 // nor `temp2`, as we are comparing two poisoned references.
2619 __ Cmp(temp, temp2);
2620 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002621
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002622 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2623 vixl::aarch64::Label do_put;
2624 __ B(eq, &do_put);
2625 // If heap poisoning is enabled, the `temp` reference has
2626 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002627 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2628
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002629 // /* HeapReference<Class> */ temp = temp->super_class_
2630 __ Ldr(temp, HeapOperand(temp, super_offset));
2631 // If heap poisoning is enabled, no need to unpoison
2632 // `temp`, as we are comparing against null below.
2633 __ Cbnz(temp, slow_path->GetEntryLabel());
2634 __ Bind(&do_put);
2635 } else {
2636 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002637 }
2638 }
2639
2640 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002641 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002642 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002643 __ Mov(temp2, value.W());
2644 GetAssembler()->PoisonHeapReference(temp2);
2645 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002646 }
2647
2648 if (!index.IsConstant()) {
2649 __ Add(temp, array, offset);
2650 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002651 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002652
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002653 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002654 codegen_->MaybeRecordImplicitNullCheck(instruction);
2655 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002656 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002657
2658 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2659
2660 if (done.IsLinked()) {
2661 __ Bind(&done);
2662 }
2663
2664 if (slow_path != nullptr) {
2665 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002666 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002667 }
2668}
2669
Alexandre Rames67555f72014-11-18 10:55:16 +00002670void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002671 RegisterSet caller_saves = RegisterSet::Empty();
2672 InvokeRuntimeCallingConvention calling_convention;
2673 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2674 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2675 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002676 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002677 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002678}
2679
2680void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002681 BoundsCheckSlowPathARM64* slow_path =
2682 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002683 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002684 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2685 __ B(slow_path->GetEntryLabel(), hs);
2686}
2687
Alexandre Rames67555f72014-11-18 10:55:16 +00002688void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2689 LocationSummary* locations =
2690 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2691 locations->SetInAt(0, Location::RequiresRegister());
2692 if (check->HasUses()) {
2693 locations->SetOut(Location::SameAsFirstInput());
2694 }
2695}
2696
2697void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2698 // We assume the class is not null.
2699 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2700 check->GetLoadClass(), check, check->GetDexPc(), true);
2701 codegen_->AddSlowPath(slow_path);
2702 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2703}
2704
Roland Levillain1a653882016-03-18 18:05:57 +00002705static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2706 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2707 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2708}
2709
2710void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2711 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2712 Location rhs_loc = instruction->GetLocations()->InAt(1);
2713 if (rhs_loc.IsConstant()) {
2714 // 0.0 is the only immediate that can be encoded directly in
2715 // an FCMP instruction.
2716 //
2717 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2718 // specify that in a floating-point comparison, positive zero
2719 // and negative zero are considered equal, so we can use the
2720 // literal 0.0 for both cases here.
2721 //
2722 // Note however that some methods (Float.equal, Float.compare,
2723 // Float.compareTo, Double.equal, Double.compare,
2724 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2725 // StrictMath.min) consider 0.0 to be (strictly) greater than
2726 // -0.0. So if we ever translate calls to these methods into a
2727 // HCompare instruction, we must handle the -0.0 case with
2728 // care here.
2729 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2730 __ Fcmp(lhs_reg, 0.0);
2731 } else {
2732 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2733 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002734}
2735
Serban Constantinescu02164b32014-11-13 14:05:07 +00002736void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002737 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002738 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2739 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002740 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002741 case Primitive::kPrimBoolean:
2742 case Primitive::kPrimByte:
2743 case Primitive::kPrimShort:
2744 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002745 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002746 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002747 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002748 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002749 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2750 break;
2751 }
2752 case Primitive::kPrimFloat:
2753 case Primitive::kPrimDouble: {
2754 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002755 locations->SetInAt(1,
2756 IsFloatingPointZeroConstant(compare->InputAt(1))
2757 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2758 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002759 locations->SetOut(Location::RequiresRegister());
2760 break;
2761 }
2762 default:
2763 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2764 }
2765}
2766
2767void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2768 Primitive::Type in_type = compare->InputAt(0)->GetType();
2769
2770 // 0 if: left == right
2771 // 1 if: left > right
2772 // -1 if: left < right
2773 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002774 case Primitive::kPrimBoolean:
2775 case Primitive::kPrimByte:
2776 case Primitive::kPrimShort:
2777 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002778 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002779 case Primitive::kPrimLong: {
2780 Register result = OutputRegister(compare);
2781 Register left = InputRegisterAt(compare, 0);
2782 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002783 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002784 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2785 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002786 break;
2787 }
2788 case Primitive::kPrimFloat:
2789 case Primitive::kPrimDouble: {
2790 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002791 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002792 __ Cset(result, ne);
2793 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002794 break;
2795 }
2796 default:
2797 LOG(FATAL) << "Unimplemented compare type " << in_type;
2798 }
2799}
2800
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002801void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002803
2804 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2805 locations->SetInAt(0, Location::RequiresFpuRegister());
2806 locations->SetInAt(1,
2807 IsFloatingPointZeroConstant(instruction->InputAt(1))
2808 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2809 : Location::RequiresFpuRegister());
2810 } else {
2811 // Integer cases.
2812 locations->SetInAt(0, Location::RequiresRegister());
2813 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2814 }
2815
David Brazdilb3e773e2016-01-26 11:28:37 +00002816 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002818 }
2819}
2820
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002821void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002822 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002823 return;
2824 }
2825
2826 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002827 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002828 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002829
Roland Levillain7f63c522015-07-13 15:54:55 +00002830 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002831 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002832 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002833 } else {
2834 // Integer cases.
2835 Register lhs = InputRegisterAt(instruction, 0);
2836 Operand rhs = InputOperandAt(instruction, 1);
2837 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002838 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002839 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002840}
2841
2842#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2843 M(Equal) \
2844 M(NotEqual) \
2845 M(LessThan) \
2846 M(LessThanOrEqual) \
2847 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002848 M(GreaterThanOrEqual) \
2849 M(Below) \
2850 M(BelowOrEqual) \
2851 M(Above) \
2852 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002853#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002854void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2855void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002856FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002857#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002858#undef FOR_EACH_CONDITION_INSTRUCTION
2859
Zheng Xuc6667102015-05-15 16:08:45 +08002860void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2861 DCHECK(instruction->IsDiv() || instruction->IsRem());
2862
2863 LocationSummary* locations = instruction->GetLocations();
2864 Location second = locations->InAt(1);
2865 DCHECK(second.IsConstant());
2866
2867 Register out = OutputRegister(instruction);
2868 Register dividend = InputRegisterAt(instruction, 0);
2869 int64_t imm = Int64FromConstant(second.GetConstant());
2870 DCHECK(imm == 1 || imm == -1);
2871
2872 if (instruction->IsRem()) {
2873 __ Mov(out, 0);
2874 } else {
2875 if (imm == 1) {
2876 __ Mov(out, dividend);
2877 } else {
2878 __ Neg(out, dividend);
2879 }
2880 }
2881}
2882
2883void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2884 DCHECK(instruction->IsDiv() || instruction->IsRem());
2885
2886 LocationSummary* locations = instruction->GetLocations();
2887 Location second = locations->InAt(1);
2888 DCHECK(second.IsConstant());
2889
2890 Register out = OutputRegister(instruction);
2891 Register dividend = InputRegisterAt(instruction, 0);
2892 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002893 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002894 int ctz_imm = CTZ(abs_imm);
2895
2896 UseScratchRegisterScope temps(GetVIXLAssembler());
2897 Register temp = temps.AcquireSameSizeAs(out);
2898
2899 if (instruction->IsDiv()) {
2900 __ Add(temp, dividend, abs_imm - 1);
2901 __ Cmp(dividend, 0);
2902 __ Csel(out, temp, dividend, lt);
2903 if (imm > 0) {
2904 __ Asr(out, out, ctz_imm);
2905 } else {
2906 __ Neg(out, Operand(out, ASR, ctz_imm));
2907 }
2908 } else {
2909 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2910 __ Asr(temp, dividend, bits - 1);
2911 __ Lsr(temp, temp, bits - ctz_imm);
2912 __ Add(out, dividend, temp);
2913 __ And(out, out, abs_imm - 1);
2914 __ Sub(out, out, temp);
2915 }
2916}
2917
2918void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2919 DCHECK(instruction->IsDiv() || instruction->IsRem());
2920
2921 LocationSummary* locations = instruction->GetLocations();
2922 Location second = locations->InAt(1);
2923 DCHECK(second.IsConstant());
2924
2925 Register out = OutputRegister(instruction);
2926 Register dividend = InputRegisterAt(instruction, 0);
2927 int64_t imm = Int64FromConstant(second.GetConstant());
2928
2929 Primitive::Type type = instruction->GetResultType();
2930 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2931
2932 int64_t magic;
2933 int shift;
2934 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2935
2936 UseScratchRegisterScope temps(GetVIXLAssembler());
2937 Register temp = temps.AcquireSameSizeAs(out);
2938
2939 // temp = get_high(dividend * magic)
2940 __ Mov(temp, magic);
2941 if (type == Primitive::kPrimLong) {
2942 __ Smulh(temp, dividend, temp);
2943 } else {
2944 __ Smull(temp.X(), dividend, temp);
2945 __ Lsr(temp.X(), temp.X(), 32);
2946 }
2947
2948 if (imm > 0 && magic < 0) {
2949 __ Add(temp, temp, dividend);
2950 } else if (imm < 0 && magic > 0) {
2951 __ Sub(temp, temp, dividend);
2952 }
2953
2954 if (shift != 0) {
2955 __ Asr(temp, temp, shift);
2956 }
2957
2958 if (instruction->IsDiv()) {
2959 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2960 } else {
2961 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2962 // TODO: Strength reduction for msub.
2963 Register temp_imm = temps.AcquireSameSizeAs(out);
2964 __ Mov(temp_imm, imm);
2965 __ Msub(out, temp, temp_imm, dividend);
2966 }
2967}
2968
2969void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2970 DCHECK(instruction->IsDiv() || instruction->IsRem());
2971 Primitive::Type type = instruction->GetResultType();
2972 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2973
2974 LocationSummary* locations = instruction->GetLocations();
2975 Register out = OutputRegister(instruction);
2976 Location second = locations->InAt(1);
2977
2978 if (second.IsConstant()) {
2979 int64_t imm = Int64FromConstant(second.GetConstant());
2980
2981 if (imm == 0) {
2982 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2983 } else if (imm == 1 || imm == -1) {
2984 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002985 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002986 DivRemByPowerOfTwo(instruction);
2987 } else {
2988 DCHECK(imm <= -2 || imm >= 2);
2989 GenerateDivRemWithAnyConstant(instruction);
2990 }
2991 } else {
2992 Register dividend = InputRegisterAt(instruction, 0);
2993 Register divisor = InputRegisterAt(instruction, 1);
2994 if (instruction->IsDiv()) {
2995 __ Sdiv(out, dividend, divisor);
2996 } else {
2997 UseScratchRegisterScope temps(GetVIXLAssembler());
2998 Register temp = temps.AcquireSameSizeAs(out);
2999 __ Sdiv(temp, dividend, divisor);
3000 __ Msub(out, temp, divisor, dividend);
3001 }
3002 }
3003}
3004
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003005void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3006 LocationSummary* locations =
3007 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3008 switch (div->GetResultType()) {
3009 case Primitive::kPrimInt:
3010 case Primitive::kPrimLong:
3011 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003012 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3014 break;
3015
3016 case Primitive::kPrimFloat:
3017 case Primitive::kPrimDouble:
3018 locations->SetInAt(0, Location::RequiresFpuRegister());
3019 locations->SetInAt(1, Location::RequiresFpuRegister());
3020 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3021 break;
3022
3023 default:
3024 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3025 }
3026}
3027
3028void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3029 Primitive::Type type = div->GetResultType();
3030 switch (type) {
3031 case Primitive::kPrimInt:
3032 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003033 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003034 break;
3035
3036 case Primitive::kPrimFloat:
3037 case Primitive::kPrimDouble:
3038 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3039 break;
3040
3041 default:
3042 LOG(FATAL) << "Unexpected div type " << type;
3043 }
3044}
3045
Alexandre Rames67555f72014-11-18 10:55:16 +00003046void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003047 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003048 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003049}
3050
3051void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3052 SlowPathCodeARM64* slow_path =
3053 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3054 codegen_->AddSlowPath(slow_path);
3055 Location value = instruction->GetLocations()->InAt(0);
3056
Alexandre Rames3e69f162014-12-10 10:36:50 +00003057 Primitive::Type type = instruction->GetType();
3058
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003059 if (!Primitive::IsIntegralType(type)) {
3060 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003061 return;
3062 }
3063
Alexandre Rames67555f72014-11-18 10:55:16 +00003064 if (value.IsConstant()) {
3065 int64_t divisor = Int64ConstantFrom(value);
3066 if (divisor == 0) {
3067 __ B(slow_path->GetEntryLabel());
3068 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003069 // A division by a non-null constant is valid. We don't need to perform
3070 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003071 }
3072 } else {
3073 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3074 }
3075}
3076
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003077void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3080 locations->SetOut(Location::ConstantLocation(constant));
3081}
3082
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003083void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3084 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003085 // Will be generated at use site.
3086}
3087
Alexandre Rames5319def2014-10-23 10:03:10 +01003088void LocationsBuilderARM64::VisitExit(HExit* exit) {
3089 exit->SetLocations(nullptr);
3090}
3091
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003092void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003093}
3094
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003095void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3096 LocationSummary* locations =
3097 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3098 locations->SetOut(Location::ConstantLocation(constant));
3099}
3100
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003101void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003102 // Will be generated at use site.
3103}
3104
David Brazdilfc6a86a2015-06-26 10:33:45 +00003105void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003106 DCHECK(!successor->IsExitBlock());
3107 HBasicBlock* block = got->GetBlock();
3108 HInstruction* previous = got->GetPrevious();
3109 HLoopInformation* info = block->GetLoopInformation();
3110
David Brazdil46e2a392015-03-16 17:31:52 +00003111 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003112 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3113 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3114 return;
3115 }
3116 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3117 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3118 }
3119 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003120 __ B(codegen_->GetLabelOf(successor));
3121 }
3122}
3123
David Brazdilfc6a86a2015-06-26 10:33:45 +00003124void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3125 got->SetLocations(nullptr);
3126}
3127
3128void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3129 HandleGoto(got, got->GetSuccessor());
3130}
3131
3132void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3133 try_boundary->SetLocations(nullptr);
3134}
3135
3136void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3137 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3138 if (!successor->IsExitBlock()) {
3139 HandleGoto(try_boundary, successor);
3140 }
3141}
3142
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003143void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003144 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003145 vixl::aarch64::Label* true_target,
3146 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003147 // FP branching requires both targets to be explicit. If either of the targets
3148 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003149 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003150 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003151
David Brazdil0debae72015-11-12 18:37:00 +00003152 if (true_target == nullptr && false_target == nullptr) {
3153 // Nothing to do. The code always falls through.
3154 return;
3155 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003156 // Constant condition, statically compared against "true" (integer value 1).
3157 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003158 if (true_target != nullptr) {
3159 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003160 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003161 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003162 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003163 if (false_target != nullptr) {
3164 __ B(false_target);
3165 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003166 }
David Brazdil0debae72015-11-12 18:37:00 +00003167 return;
3168 }
3169
3170 // The following code generates these patterns:
3171 // (1) true_target == nullptr && false_target != nullptr
3172 // - opposite condition true => branch to false_target
3173 // (2) true_target != nullptr && false_target == nullptr
3174 // - condition true => branch to true_target
3175 // (3) true_target != nullptr && false_target != nullptr
3176 // - condition true => branch to true_target
3177 // - branch to false_target
3178 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003179 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003180 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003181 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003182 if (true_target == nullptr) {
3183 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3184 } else {
3185 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3186 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003187 } else {
3188 // The condition instruction has not been materialized, use its inputs as
3189 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003190 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003191
David Brazdil0debae72015-11-12 18:37:00 +00003192 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003193 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003194 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003195 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003196 IfCondition opposite_condition = condition->GetOppositeCondition();
3197 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003198 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003199 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003200 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003201 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003202 // Integer cases.
3203 Register lhs = InputRegisterAt(condition, 0);
3204 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003205
3206 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003207 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003208 if (true_target == nullptr) {
3209 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3210 non_fallthrough_target = false_target;
3211 } else {
3212 arm64_cond = ARM64Condition(condition->GetCondition());
3213 non_fallthrough_target = true_target;
3214 }
3215
Aart Bik086d27e2016-01-20 17:02:00 -08003216 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003217 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003218 switch (arm64_cond) {
3219 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003220 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003221 break;
3222 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003223 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003224 break;
3225 case lt:
3226 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003227 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003228 break;
3229 case ge:
3230 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003231 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003232 break;
3233 default:
3234 // Without the `static_cast` the compiler throws an error for
3235 // `-Werror=sign-promo`.
3236 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3237 }
3238 } else {
3239 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003240 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003241 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003242 }
3243 }
David Brazdil0debae72015-11-12 18:37:00 +00003244
3245 // If neither branch falls through (case 3), the conditional branch to `true_target`
3246 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3247 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003248 __ B(false_target);
3249 }
David Brazdil0debae72015-11-12 18:37:00 +00003250
3251 if (fallthrough_target.IsLinked()) {
3252 __ Bind(&fallthrough_target);
3253 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003254}
3255
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003256void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003258 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003259 locations->SetInAt(0, Location::RequiresRegister());
3260 }
3261}
3262
3263void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003264 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3265 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003266 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3267 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3268 true_target = nullptr;
3269 }
3270 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3271 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3272 false_target = nullptr;
3273 }
David Brazdil0debae72015-11-12 18:37:00 +00003274 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003275}
3276
3277void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3278 LocationSummary* locations = new (GetGraph()->GetArena())
3279 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003280 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003281 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003282 locations->SetInAt(0, Location::RequiresRegister());
3283 }
3284}
3285
3286void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003287 SlowPathCodeARM64* slow_path =
3288 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003289 GenerateTestAndBranch(deoptimize,
3290 /* condition_input_index */ 0,
3291 slow_path->GetEntryLabel(),
3292 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003293}
3294
Mingyao Yang063fc772016-08-02 11:02:54 -07003295void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3296 LocationSummary* locations = new (GetGraph()->GetArena())
3297 LocationSummary(flag, LocationSummary::kNoCall);
3298 locations->SetOut(Location::RequiresRegister());
3299}
3300
3301void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3302 __ Ldr(OutputRegister(flag),
3303 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3304}
3305
David Brazdilc0b601b2016-02-08 14:20:45 +00003306static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3307 return condition->IsCondition() &&
3308 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3309}
3310
Alexandre Rames880f1192016-06-13 16:04:50 +01003311static inline Condition GetConditionForSelect(HCondition* condition) {
3312 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003313 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3314 : ARM64Condition(cond);
3315}
3316
David Brazdil74eb1b22015-12-14 11:44:01 +00003317void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003319 if (Primitive::IsFloatingPointType(select->GetType())) {
3320 locations->SetInAt(0, Location::RequiresFpuRegister());
3321 locations->SetInAt(1, Location::RequiresFpuRegister());
3322 locations->SetOut(Location::RequiresFpuRegister());
3323 } else {
3324 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3325 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3326 bool is_true_value_constant = cst_true_value != nullptr;
3327 bool is_false_value_constant = cst_false_value != nullptr;
3328 // Ask VIXL whether we should synthesize constants in registers.
3329 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3330 Operand true_op = is_true_value_constant ?
3331 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3332 Operand false_op = is_false_value_constant ?
3333 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3334 bool true_value_in_register = false;
3335 bool false_value_in_register = false;
3336 MacroAssembler::GetCselSynthesisInformation(
3337 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3338 true_value_in_register |= !is_true_value_constant;
3339 false_value_in_register |= !is_false_value_constant;
3340
3341 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3342 : Location::ConstantLocation(cst_true_value));
3343 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3344 : Location::ConstantLocation(cst_false_value));
3345 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003346 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003347
David Brazdil74eb1b22015-12-14 11:44:01 +00003348 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3349 locations->SetInAt(2, Location::RequiresRegister());
3350 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003351}
3352
3353void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003354 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003355 Condition csel_cond;
3356
3357 if (IsBooleanValueOrMaterializedCondition(cond)) {
3358 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003359 // Use the condition flags set by the previous instruction.
3360 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003361 } else {
3362 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003363 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003364 }
3365 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003366 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003367 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003368 } else {
3369 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003370 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003371 }
3372
Alexandre Rames880f1192016-06-13 16:04:50 +01003373 if (Primitive::IsFloatingPointType(select->GetType())) {
3374 __ Fcsel(OutputFPRegister(select),
3375 InputFPRegisterAt(select, 1),
3376 InputFPRegisterAt(select, 0),
3377 csel_cond);
3378 } else {
3379 __ Csel(OutputRegister(select),
3380 InputOperandAt(select, 1),
3381 InputOperandAt(select, 0),
3382 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003383 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003384}
3385
David Srbecky0cf44932015-12-09 14:09:59 +00003386void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3387 new (GetGraph()->GetArena()) LocationSummary(info);
3388}
3389
David Srbeckyd28f4a02016-03-14 17:14:24 +00003390void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3391 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003392}
3393
3394void CodeGeneratorARM64::GenerateNop() {
3395 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003396}
3397
Alexandre Rames5319def2014-10-23 10:03:10 +01003398void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003399 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003400}
3401
3402void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003403 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003404}
3405
3406void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003407 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003408}
3409
3410void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003411 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003412}
3413
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003414// Temp is used for read barrier.
3415static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3416 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003417 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003418 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3419 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3420 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3421 return 1;
3422 }
3423 return 0;
3424}
3425
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003426// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003427// interface pointer, one for loading the current interface.
3428// The other checks have one temp for loading the object's class.
3429static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3430 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3431 return 3;
3432 }
3433 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003434}
3435
Alexandre Rames67555f72014-11-18 10:55:16 +00003436void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003437 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003439 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003440 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003441 case TypeCheckKind::kExactCheck:
3442 case TypeCheckKind::kAbstractClassCheck:
3443 case TypeCheckKind::kClassHierarchyCheck:
3444 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003445 call_kind =
3446 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003447 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003448 break;
3449 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003450 case TypeCheckKind::kUnresolvedCheck:
3451 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003452 call_kind = LocationSummary::kCallOnSlowPath;
3453 break;
3454 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003455
Alexandre Rames67555f72014-11-18 10:55:16 +00003456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003457 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003458 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003459 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003460 locations->SetInAt(0, Location::RequiresRegister());
3461 locations->SetInAt(1, Location::RequiresRegister());
3462 // The "out" register is used as a temporary, so it overlaps with the inputs.
3463 // Note that TypeCheckSlowPathARM64 uses this register too.
3464 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003465 // Add temps if necessary for read barriers.
3466 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003467}
3468
3469void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003470 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003471 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003472 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003473 Register obj = InputRegisterAt(instruction, 0);
3474 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003475 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003476 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003477 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3478 DCHECK_LE(num_temps, 1u);
3479 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003480 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3481 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3482 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3483 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003484
Scott Wakeling97c72b72016-06-24 16:19:36 +01003485 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003486 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003487
3488 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003489 // Avoid null check if we know `obj` is not null.
3490 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003491 __ Cbz(obj, &zero);
3492 }
3493
Roland Levillain44015862016-01-22 11:47:17 +00003494 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003496 // /* HeapReference<Class> */ out = obj->klass_
3497 GenerateReferenceLoadTwoRegisters(instruction,
3498 out_loc,
3499 obj_loc,
3500 class_offset,
3501 maybe_temp_loc,
3502 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003503 __ Cmp(out, cls);
3504 __ Cset(out, eq);
3505 if (zero.IsLinked()) {
3506 __ B(&done);
3507 }
3508 break;
3509 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003510
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003511 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003512 // /* HeapReference<Class> */ out = obj->klass_
3513 GenerateReferenceLoadTwoRegisters(instruction,
3514 out_loc,
3515 obj_loc,
3516 class_offset,
3517 maybe_temp_loc,
3518 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003519 // If the class is abstract, we eagerly fetch the super class of the
3520 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003521 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003522 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003523 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003524 GenerateReferenceLoadOneRegister(instruction,
3525 out_loc,
3526 super_offset,
3527 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003528 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003529 // If `out` is null, we use it for the result, and jump to `done`.
3530 __ Cbz(out, &done);
3531 __ Cmp(out, cls);
3532 __ B(ne, &loop);
3533 __ Mov(out, 1);
3534 if (zero.IsLinked()) {
3535 __ B(&done);
3536 }
3537 break;
3538 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003539
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003540 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003541 // /* HeapReference<Class> */ out = obj->klass_
3542 GenerateReferenceLoadTwoRegisters(instruction,
3543 out_loc,
3544 obj_loc,
3545 class_offset,
3546 maybe_temp_loc,
3547 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003548 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003549 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003550 __ Bind(&loop);
3551 __ Cmp(out, cls);
3552 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003553 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003554 GenerateReferenceLoadOneRegister(instruction,
3555 out_loc,
3556 super_offset,
3557 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003558 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003559 __ Cbnz(out, &loop);
3560 // If `out` is null, we use it for the result, and jump to `done`.
3561 __ B(&done);
3562 __ Bind(&success);
3563 __ Mov(out, 1);
3564 if (zero.IsLinked()) {
3565 __ B(&done);
3566 }
3567 break;
3568 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003569
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003570 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003571 // /* HeapReference<Class> */ out = obj->klass_
3572 GenerateReferenceLoadTwoRegisters(instruction,
3573 out_loc,
3574 obj_loc,
3575 class_offset,
3576 maybe_temp_loc,
3577 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003578 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003579 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003580 __ Cmp(out, cls);
3581 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003582 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003583 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003584 GenerateReferenceLoadOneRegister(instruction,
3585 out_loc,
3586 component_offset,
3587 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003588 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003589 // If `out` is null, we use it for the result, and jump to `done`.
3590 __ Cbz(out, &done);
3591 __ Ldrh(out, HeapOperand(out, primitive_offset));
3592 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3593 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003594 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003595 __ Mov(out, 1);
3596 __ B(&done);
3597 break;
3598 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003599
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003600 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003601 // No read barrier since the slow path will retry upon failure.
3602 // /* HeapReference<Class> */ out = obj->klass_
3603 GenerateReferenceLoadTwoRegisters(instruction,
3604 out_loc,
3605 obj_loc,
3606 class_offset,
3607 maybe_temp_loc,
3608 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003609 __ Cmp(out, cls);
3610 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003611 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3612 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003613 codegen_->AddSlowPath(slow_path);
3614 __ B(ne, slow_path->GetEntryLabel());
3615 __ Mov(out, 1);
3616 if (zero.IsLinked()) {
3617 __ B(&done);
3618 }
3619 break;
3620 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003621
Calin Juravle98893e12015-10-02 21:05:03 +01003622 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003623 case TypeCheckKind::kInterfaceCheck: {
3624 // Note that we indeed only call on slow path, but we always go
3625 // into the slow path for the unresolved and interface check
3626 // cases.
3627 //
3628 // We cannot directly call the InstanceofNonTrivial runtime
3629 // entry point without resorting to a type checking slow path
3630 // here (i.e. by calling InvokeRuntime directly), as it would
3631 // require to assign fixed registers for the inputs of this
3632 // HInstanceOf instruction (following the runtime calling
3633 // convention), which might be cluttered by the potential first
3634 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003635 //
3636 // TODO: Introduce a new runtime entry point taking the object
3637 // to test (instead of its class) as argument, and let it deal
3638 // with the read barrier issues. This will let us refactor this
3639 // case of the `switch` code as it was previously (with a direct
3640 // call to the runtime not using a type checking slow path).
3641 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003642 DCHECK(locations->OnlyCallsOnSlowPath());
3643 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3644 /* is_fatal */ false);
3645 codegen_->AddSlowPath(slow_path);
3646 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003647 if (zero.IsLinked()) {
3648 __ B(&done);
3649 }
3650 break;
3651 }
3652 }
3653
3654 if (zero.IsLinked()) {
3655 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003656 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003657 }
3658
3659 if (done.IsLinked()) {
3660 __ Bind(&done);
3661 }
3662
3663 if (slow_path != nullptr) {
3664 __ Bind(slow_path->GetExitLabel());
3665 }
3666}
3667
3668void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3669 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3670 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3671
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003672 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3673 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003674 case TypeCheckKind::kExactCheck:
3675 case TypeCheckKind::kAbstractClassCheck:
3676 case TypeCheckKind::kClassHierarchyCheck:
3677 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003678 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3679 LocationSummary::kCallOnSlowPath :
3680 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003681 break;
3682 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003683 case TypeCheckKind::kUnresolvedCheck:
3684 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003685 call_kind = LocationSummary::kCallOnSlowPath;
3686 break;
3687 }
3688
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003689 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3690 locations->SetInAt(0, Location::RequiresRegister());
3691 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003692 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3693 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003694}
3695
3696void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003697 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003698 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003699 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003700 Register obj = InputRegisterAt(instruction, 0);
3701 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003702 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3703 DCHECK_GE(num_temps, 1u);
3704 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003705 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003706 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3707 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003708 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003709 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3710 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3711 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3712 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3713 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3714 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3715 const uint32_t object_array_data_offset =
3716 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003717
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003718 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003719 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3720 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3721 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003722 if (!kEmitCompilerReadBarrier) {
3723 is_type_check_slow_path_fatal =
3724 (type_check_kind == TypeCheckKind::kExactCheck ||
3725 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3726 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3727 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3728 !instruction->CanThrowIntoCatchBlock();
3729 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003730 SlowPathCodeARM64* type_check_slow_path =
3731 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3732 is_type_check_slow_path_fatal);
3733 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003734
Scott Wakeling97c72b72016-06-24 16:19:36 +01003735 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003736 // Avoid null check if we know obj is not null.
3737 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003738 __ Cbz(obj, &done);
3739 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003740
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003741 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003742 case TypeCheckKind::kExactCheck:
3743 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003744 // /* HeapReference<Class> */ temp = obj->klass_
3745 GenerateReferenceLoadTwoRegisters(instruction,
3746 temp_loc,
3747 obj_loc,
3748 class_offset,
3749 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003750 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003751
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003752 __ Cmp(temp, cls);
3753 // Jump to slow path for throwing the exception or doing a
3754 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003755 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003756 break;
3757 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003758
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003759 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003760 // /* HeapReference<Class> */ temp = obj->klass_
3761 GenerateReferenceLoadTwoRegisters(instruction,
3762 temp_loc,
3763 obj_loc,
3764 class_offset,
3765 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003766 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003767
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003768 // If the class is abstract, we eagerly fetch the super class of the
3769 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003770 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003771 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003772 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003773 GenerateReferenceLoadOneRegister(instruction,
3774 temp_loc,
3775 super_offset,
3776 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003777 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003778
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003779 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3780 // exception.
3781 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3782 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003783 __ Cmp(temp, cls);
3784 __ B(ne, &loop);
3785 break;
3786 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003787
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003788 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003789 // /* HeapReference<Class> */ temp = obj->klass_
3790 GenerateReferenceLoadTwoRegisters(instruction,
3791 temp_loc,
3792 obj_loc,
3793 class_offset,
3794 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003795 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003796
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003797 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003798 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003799 __ Bind(&loop);
3800 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003801 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003802
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003803 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003804 GenerateReferenceLoadOneRegister(instruction,
3805 temp_loc,
3806 super_offset,
3807 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003808 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003809
3810 // If the class reference currently in `temp` is not null, jump
3811 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003812 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003813 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003814 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003815 break;
3816 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003817
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003818 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003819 // /* HeapReference<Class> */ temp = obj->klass_
3820 GenerateReferenceLoadTwoRegisters(instruction,
3821 temp_loc,
3822 obj_loc,
3823 class_offset,
3824 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003825 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003826
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003827 // Do an exact check.
3828 __ Cmp(temp, cls);
3829 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003830
3831 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003832 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003833 GenerateReferenceLoadOneRegister(instruction,
3834 temp_loc,
3835 component_offset,
3836 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003837 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003838
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003839 // If the component type is null, jump to the slow path to throw the exception.
3840 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3841 // Otherwise, the object is indeed an array. Further check that this component type is not a
3842 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003843 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3844 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003845 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846 break;
3847 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003848
Calin Juravle98893e12015-10-02 21:05:03 +01003849 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003850 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003851 //
3852 // We cannot directly call the CheckCast runtime entry point
3853 // without resorting to a type checking slow path here (i.e. by
3854 // calling InvokeRuntime directly), as it would require to
3855 // assign fixed registers for the inputs of this HInstanceOf
3856 // instruction (following the runtime calling convention), which
3857 // might be cluttered by the potential first read barrier
3858 // emission at the beginning of this method.
3859 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003860 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003861 case TypeCheckKind::kInterfaceCheck: {
3862 // /* HeapReference<Class> */ temp = obj->klass_
3863 GenerateReferenceLoadTwoRegisters(instruction,
3864 temp_loc,
3865 obj_loc,
3866 class_offset,
3867 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003868 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003869
3870 // /* HeapReference<Class> */ temp = temp->iftable_
3871 GenerateReferenceLoadTwoRegisters(instruction,
3872 temp_loc,
3873 temp_loc,
3874 iftable_offset,
3875 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003876 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003877 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003878 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003879 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003880 vixl::aarch64::Label start_loop;
3881 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003882 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003883 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3884 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003885 // Go to next interface.
3886 __ Add(temp, temp, 2 * kHeapReferenceSize);
3887 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003888 // Compare the classes and continue the loop if they do not match.
3889 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3890 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003891 break;
3892 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003893 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003894 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003895
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003897}
3898
Alexandre Rames5319def2014-10-23 10:03:10 +01003899void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3900 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3901 locations->SetOut(Location::ConstantLocation(constant));
3902}
3903
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003904void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003905 // Will be generated at use site.
3906}
3907
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003908void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3910 locations->SetOut(Location::ConstantLocation(constant));
3911}
3912
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003913void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003914 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003915}
3916
Calin Juravle175dc732015-08-25 15:42:32 +01003917void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3918 // The trampoline uses the same calling convention as dex calling conventions,
3919 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3920 // the method_idx.
3921 HandleInvoke(invoke);
3922}
3923
3924void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3925 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3926}
3927
Alexandre Rames5319def2014-10-23 10:03:10 +01003928void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003929 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003930 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003931}
3932
Alexandre Rames67555f72014-11-18 10:55:16 +00003933void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3934 HandleInvoke(invoke);
3935}
3936
3937void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3938 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939 LocationSummary* locations = invoke->GetLocations();
3940 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003941 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003942 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003943 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003944
3945 // The register ip1 is required to be used for the hidden argument in
3946 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003947 MacroAssembler* masm = GetVIXLAssembler();
3948 UseScratchRegisterScope scratch_scope(masm);
3949 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003950 scratch_scope.Exclude(ip1);
3951 __ Mov(ip1, invoke->GetDexMethodIndex());
3952
Alexandre Rames67555f72014-11-18 10:55:16 +00003953 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003954 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003955 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003956 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003957 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003958 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003959 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003960 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003961 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003962 // Instead of simply (possibly) unpoisoning `temp` here, we should
3963 // emit a read barrier for the previous class reference load.
3964 // However this is not required in practice, as this is an
3965 // intermediate/temporary reference and because the current
3966 // concurrent copying collector keeps the from-space memory
3967 // intact/accessible until the end of the marking phase (the
3968 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003969 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003970 __ Ldr(temp,
3971 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3972 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003973 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003974 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003975 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003976 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003977 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003978 // lr();
3979 __ Blr(lr);
3980 DCHECK(!codegen_->IsLeafMethod());
3981 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3982}
3983
3984void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003985 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3986 if (intrinsic.TryDispatch(invoke)) {
3987 return;
3988 }
3989
Alexandre Rames67555f72014-11-18 10:55:16 +00003990 HandleInvoke(invoke);
3991}
3992
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003993void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003994 // Explicit clinit checks triggered by static invokes must have been pruned by
3995 // art::PrepareForRegisterAllocation.
3996 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003997
Andreas Gampe878d58c2015-01-15 23:24:00 -08003998 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3999 if (intrinsic.TryDispatch(invoke)) {
4000 return;
4001 }
4002
Alexandre Rames67555f72014-11-18 10:55:16 +00004003 HandleInvoke(invoke);
4004}
4005
Andreas Gampe878d58c2015-01-15 23:24:00 -08004006static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4007 if (invoke->GetLocations()->Intrinsified()) {
4008 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4009 intrinsic.Dispatch(invoke);
4010 return true;
4011 }
4012 return false;
4013}
4014
Vladimir Markodc151b22015-10-15 18:02:30 +01004015HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4016 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004017 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004018 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004019 return desired_dispatch_info;
4020}
4021
TatWai Chongd8c052a2016-11-02 16:12:48 +08004022Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4023 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004024 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004025 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4026 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004027 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4028 uint32_t offset =
4029 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004030 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004031 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004032 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004033 }
Vladimir Marko58155012015-08-19 12:49:41 +00004034 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004035 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004036 break;
4037 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4038 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004039 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004040 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004041 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4042 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004043 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004044 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004045 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004046 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004047 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004048 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004049 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004050 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004051 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004052 }
Vladimir Marko58155012015-08-19 12:49:41 +00004053 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004054 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004055 Register reg = XRegisterFrom(temp);
4056 Register method_reg;
4057 if (current_method.IsRegister()) {
4058 method_reg = XRegisterFrom(current_method);
4059 } else {
4060 DCHECK(invoke->GetLocations()->Intrinsified());
4061 DCHECK(!current_method.IsValid());
4062 method_reg = reg;
4063 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4064 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004065
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004066 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004067 __ Ldr(reg.X(),
4068 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004069 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004070 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004071 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4072 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004073 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4074 break;
4075 }
4076 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004077 return callee_method;
4078}
4079
4080void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4081 // All registers are assumed to be correctly set up.
4082 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004083
4084 switch (invoke->GetCodePtrLocation()) {
4085 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4086 __ Bl(&frame_entry_label_);
4087 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004088 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4089 // LR = callee_method->entry_point_from_quick_compiled_code_;
4090 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004091 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004092 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004093 // lr()
4094 __ Blr(lr);
4095 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004096 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004097
Andreas Gampe878d58c2015-01-15 23:24:00 -08004098 DCHECK(!IsLeafMethod());
4099}
4100
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004101void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004102 // Use the calling convention instead of the location of the receiver, as
4103 // intrinsics may have put the receiver in a different register. In the intrinsics
4104 // slow path, the arguments have been moved to the right place, so here we are
4105 // guaranteed that the receiver is the first register of the calling convention.
4106 InvokeDexCallingConvention calling_convention;
4107 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004108 Register temp = XRegisterFrom(temp_in);
4109 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4110 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4111 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004112 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004113
4114 BlockPoolsScope block_pools(GetVIXLAssembler());
4115
4116 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004118 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004119 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 // Instead of simply (possibly) unpoisoning `temp` here, we should
4121 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004122 // intermediate/temporary reference and because the current
4123 // concurrent copying collector keeps the from-space memory
4124 // intact/accessible until the end of the marking phase (the
4125 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004126 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4127 // temp = temp->GetMethodAt(method_offset);
4128 __ Ldr(temp, MemOperand(temp, method_offset));
4129 // lr = temp->GetEntryPoint();
4130 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4131 // lr();
4132 __ Blr(lr);
4133}
4134
Orion Hodsonac141392017-01-13 11:53:47 +00004135void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4136 HandleInvoke(invoke);
4137}
4138
4139void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4140 codegen_->GenerateInvokePolymorphicCall(invoke);
4141}
4142
Scott Wakeling97c72b72016-06-24 16:19:36 +01004143vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4144 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004145 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004146 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004147 return
4148 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004149}
4150
Scott Wakeling97c72b72016-06-24 16:19:36 +01004151vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4152 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004153 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004154 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004155 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004156}
4157
Vladimir Marko1998cd02017-01-13 13:02:58 +00004158vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4159 const DexFile& dex_file,
4160 dex::TypeIndex type_index,
4161 vixl::aarch64::Label* adrp_label) {
4162 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4163}
4164
Scott Wakeling97c72b72016-06-24 16:19:36 +01004165vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4166 const DexFile& dex_file,
4167 uint32_t element_offset,
4168 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004169 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4170}
4171
Scott Wakeling97c72b72016-06-24 16:19:36 +01004172vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4173 const DexFile& dex_file,
4174 uint32_t offset_or_index,
4175 vixl::aarch64::Label* adrp_label,
4176 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004177 // Add a patch entry and return the label.
4178 patches->emplace_back(dex_file, offset_or_index);
4179 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004180 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004181 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4182 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4183 return label;
4184}
4185
Scott Wakeling97c72b72016-06-24 16:19:36 +01004186vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004187 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004188 return boot_image_string_patches_.GetOrCreate(
4189 StringReference(&dex_file, string_index),
4190 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4191}
4192
Scott Wakeling97c72b72016-06-24 16:19:36 +01004193vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004194 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004195 return boot_image_type_patches_.GetOrCreate(
4196 TypeReference(&dex_file, type_index),
4197 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4198}
4199
Scott Wakeling97c72b72016-06-24 16:19:36 +01004200vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4201 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004202 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4203 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4204 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4205}
4206
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004207vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004208 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4209 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4210 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004211 return jit_string_patches_.GetOrCreate(
4212 StringReference(&dex_file, string_index),
4213 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4214}
4215
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004216vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004217 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4218 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4219 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004220 return jit_class_patches_.GetOrCreate(
4221 TypeReference(&dex_file, type_index),
4222 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4223}
4224
Vladimir Markoaad75c62016-10-03 08:46:48 +00004225void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4226 vixl::aarch64::Register reg) {
4227 DCHECK(reg.IsX());
4228 SingleEmissionCheckScope guard(GetVIXLAssembler());
4229 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004230 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004231}
4232
4233void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4234 vixl::aarch64::Register out,
4235 vixl::aarch64::Register base) {
4236 DCHECK(out.IsX());
4237 DCHECK(base.IsX());
4238 SingleEmissionCheckScope guard(GetVIXLAssembler());
4239 __ Bind(fixup_label);
4240 __ add(out, base, Operand(/* offset placeholder */ 0));
4241}
4242
4243void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4244 vixl::aarch64::Register out,
4245 vixl::aarch64::Register base) {
4246 DCHECK(base.IsX());
4247 SingleEmissionCheckScope guard(GetVIXLAssembler());
4248 __ Bind(fixup_label);
4249 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4250}
4251
4252template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4253inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4254 const ArenaDeque<PcRelativePatchInfo>& infos,
4255 ArenaVector<LinkerPatch>* linker_patches) {
4256 for (const PcRelativePatchInfo& info : infos) {
4257 linker_patches->push_back(Factory(info.label.GetLocation(),
4258 &info.target_dex_file,
4259 info.pc_insn_label->GetLocation(),
4260 info.offset_or_index));
4261 }
4262}
4263
Vladimir Marko58155012015-08-19 12:49:41 +00004264void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4265 DCHECK(linker_patches->empty());
4266 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004267 pc_relative_dex_cache_patches_.size() +
4268 boot_image_string_patches_.size() +
4269 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004270 boot_image_type_patches_.size() +
4271 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004272 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004273 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004274 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004275 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004276 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004277 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004278 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004279 info.offset_or_index));
4280 }
4281 for (const auto& entry : boot_image_string_patches_) {
4282 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004283 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4284 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004285 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004286 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004287 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004288 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004289 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004290 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4291 linker_patches);
4292 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004293 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4294 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004295 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4296 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004297 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004298 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4299 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004300 for (const auto& entry : boot_image_type_patches_) {
4301 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004302 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4303 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004304 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004305 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004306 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004307 for (const auto& entry : boot_image_address_patches_) {
4308 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004309 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4310 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004311 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004312 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004313}
4314
Scott Wakeling97c72b72016-06-24 16:19:36 +01004315vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004316 Uint32ToLiteralMap* map) {
4317 return map->GetOrCreate(
4318 value,
4319 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4320}
4321
Scott Wakeling97c72b72016-06-24 16:19:36 +01004322vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004323 return uint64_literals_.GetOrCreate(
4324 value,
4325 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004326}
4327
Scott Wakeling97c72b72016-06-24 16:19:36 +01004328vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004329 MethodReference target_method,
4330 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004331 return map->GetOrCreate(
4332 target_method,
4333 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004334}
4335
Andreas Gampe878d58c2015-01-15 23:24:00 -08004336void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004337 // Explicit clinit checks triggered by static invokes must have been pruned by
4338 // art::PrepareForRegisterAllocation.
4339 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004340
Andreas Gampe878d58c2015-01-15 23:24:00 -08004341 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4342 return;
4343 }
4344
Alexandre Ramesd921d642015-04-16 15:07:16 +01004345 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004346 LocationSummary* locations = invoke->GetLocations();
4347 codegen_->GenerateStaticOrDirectCall(
4348 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004349 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004350}
4351
4352void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004353 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4354 return;
4355 }
4356
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004357 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004358 DCHECK(!codegen_->IsLeafMethod());
4359 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4360}
4361
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004362HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4363 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004364 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004365 case HLoadClass::LoadKind::kInvalid:
4366 LOG(FATAL) << "UNREACHABLE";
4367 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004368 case HLoadClass::LoadKind::kReferrersClass:
4369 break;
4370 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4371 DCHECK(!GetCompilerOptions().GetCompilePic());
4372 break;
4373 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4374 DCHECK(GetCompilerOptions().GetCompilePic());
4375 break;
4376 case HLoadClass::LoadKind::kBootImageAddress:
4377 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004378 case HLoadClass::LoadKind::kBssEntry:
4379 DCHECK(!Runtime::Current()->UseJitCompilation());
4380 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004381 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004382 DCHECK(Runtime::Current()->UseJitCompilation());
4383 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004384 case HLoadClass::LoadKind::kDexCacheViaMethod:
4385 break;
4386 }
4387 return desired_class_load_kind;
4388}
4389
Alexandre Rames67555f72014-11-18 10:55:16 +00004390void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004391 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4392 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004393 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004394 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004395 cls,
4396 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004397 LocationFrom(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004398 return;
4399 }
Vladimir Marko41559982017-01-06 14:04:23 +00004400 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004401
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004402 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4403 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004404 ? LocationSummary::kCallOnSlowPath
4405 : LocationSummary::kNoCall;
4406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004407 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004408 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004409 }
4410
Vladimir Marko41559982017-01-06 14:04:23 +00004411 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004412 locations->SetInAt(0, Location::RequiresRegister());
4413 }
4414 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004415}
4416
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004417// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4418// move.
4419void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004420 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4421 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4422 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004423 return;
4424 }
Vladimir Marko41559982017-01-06 14:04:23 +00004425 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004426
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004427 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004428 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004429
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004430 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4431 ? kWithoutReadBarrier
4432 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004433 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004434 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004435 case HLoadClass::LoadKind::kReferrersClass: {
4436 DCHECK(!cls->CanCallRuntime());
4437 DCHECK(!cls->MustGenerateClinitCheck());
4438 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4439 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004440 GenerateGcRootFieldLoad(cls,
4441 out_loc,
4442 current_method,
4443 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004444 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004445 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004446 break;
4447 }
4448 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004449 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004450 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4451 cls->GetTypeIndex()));
4452 break;
4453 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004454 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004455 // Add ADRP with its PC-relative type patch.
4456 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004457 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004458 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004459 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004460 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004461 vixl::aarch64::Label* add_label =
4462 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004463 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004464 break;
4465 }
4466 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004467 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004468 uint32_t address = dchecked_integral_cast<uint32_t>(
4469 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4470 DCHECK_NE(address, 0u);
4471 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004472 break;
4473 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004474 case HLoadClass::LoadKind::kBssEntry: {
4475 // Add ADRP with its PC-relative Class .bss entry patch.
4476 const DexFile& dex_file = cls->GetDexFile();
4477 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko1998cd02017-01-13 13:02:58 +00004478 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004479 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4480 // Add LDR with its PC-relative Class patch.
4481 vixl::aarch64::Label* ldr_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +00004482 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004483 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4484 GenerateGcRootFieldLoad(cls,
4485 cls->GetLocations()->Out(),
4486 out.X(),
4487 /* placeholder */ 0u,
4488 ldr_label,
4489 kCompilerReadBarrierOption);
4490 generate_null_check = true;
4491 break;
4492 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004493 case HLoadClass::LoadKind::kJitTableAddress: {
4494 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4495 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004496 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004497 GenerateGcRootFieldLoad(cls,
4498 out_loc,
4499 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004500 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004501 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004502 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004503 break;
4504 }
Vladimir Marko41559982017-01-06 14:04:23 +00004505 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004506 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004507 LOG(FATAL) << "UNREACHABLE";
4508 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004509 }
4510
4511 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4512 DCHECK(cls->CanCallRuntime());
4513 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4514 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4515 codegen_->AddSlowPath(slow_path);
4516 if (generate_null_check) {
4517 __ Cbz(out, slow_path->GetEntryLabel());
4518 }
4519 if (cls->MustGenerateClinitCheck()) {
4520 GenerateClassInitializationCheck(slow_path, out);
4521 } else {
4522 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004523 }
4524 }
4525}
4526
David Brazdilcb1c0552015-08-04 16:22:25 +01004527static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004528 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004529}
4530
Alexandre Rames67555f72014-11-18 10:55:16 +00004531void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4532 LocationSummary* locations =
4533 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4534 locations->SetOut(Location::RequiresRegister());
4535}
4536
4537void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004538 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4539}
4540
4541void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4542 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4543}
4544
4545void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4546 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004547}
4548
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004549HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4550 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004551 switch (desired_string_load_kind) {
4552 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4553 DCHECK(!GetCompilerOptions().GetCompilePic());
4554 break;
4555 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4556 DCHECK(GetCompilerOptions().GetCompilePic());
4557 break;
4558 case HLoadString::LoadKind::kBootImageAddress:
4559 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004560 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004561 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004562 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004563 case HLoadString::LoadKind::kJitTableAddress:
4564 DCHECK(Runtime::Current()->UseJitCompilation());
4565 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004566 case HLoadString::LoadKind::kDexCacheViaMethod:
4567 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004568 }
4569 return desired_string_load_kind;
4570}
4571
Alexandre Rames67555f72014-11-18 10:55:16 +00004572void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004573 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004574 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004575 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004576 InvokeRuntimeCallingConvention calling_convention;
4577 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4578 } else {
4579 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004580 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4581 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4582 // Rely on the pResolveString and/or marking to save everything, including temps.
4583 RegisterSet caller_saves = RegisterSet::Empty();
4584 InvokeRuntimeCallingConvention calling_convention;
4585 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4586 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4587 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4588 Primitive::kPrimNot).GetCode());
4589 locations->SetCustomSlowPathCallerSaves(caller_saves);
4590 } else {
4591 // For non-Baker read barrier we have a temp-clobbering call.
4592 }
4593 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004594 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004595}
4596
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004597// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4598// move.
4599void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004600 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004601 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004602
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004603 switch (load->GetLoadKind()) {
4604 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004605 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4606 load->GetStringIndex()));
4607 return; // No dex cache slow path.
4608 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004609 // Add ADRP with its PC-relative String patch.
4610 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004611 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004612 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004613 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004614 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004615 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004616 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004617 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004618 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004619 return; // No dex cache slow path.
4620 }
4621 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004622 uint32_t address = dchecked_integral_cast<uint32_t>(
4623 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4624 DCHECK_NE(address, 0u);
4625 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004626 return; // No dex cache slow path.
4627 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004628 case HLoadString::LoadKind::kBssEntry: {
4629 // Add ADRP with its PC-relative String .bss entry patch.
4630 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004631 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004632 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004633 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4634 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004635 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004636 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004637 // Add LDR with its PC-relative String patch.
4638 vixl::aarch64::Label* ldr_label =
4639 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004640 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004641 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004642 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004643 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004644 /* offset placeholder */ 0u,
4645 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004646 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004647 SlowPathCodeARM64* slow_path =
4648 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004649 codegen_->AddSlowPath(slow_path);
4650 __ Cbz(out.X(), slow_path->GetEntryLabel());
4651 __ Bind(slow_path->GetExitLabel());
4652 return;
4653 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004654 case HLoadString::LoadKind::kJitTableAddress: {
4655 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004656 load->GetStringIndex(),
4657 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004658 GenerateGcRootFieldLoad(load,
4659 out_loc,
4660 out.X(),
4661 /* offset */ 0,
4662 /* fixup_label */ nullptr,
4663 kCompilerReadBarrierOption);
4664 return;
4665 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004666 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004667 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004668 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004669
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004670 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004671 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004672 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004673 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004674 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4675 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004676}
4677
Alexandre Rames5319def2014-10-23 10:03:10 +01004678void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4680 locations->SetOut(Location::ConstantLocation(constant));
4681}
4682
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004683void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004684 // Will be generated at use site.
4685}
4686
Alexandre Rames67555f72014-11-18 10:55:16 +00004687void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4688 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004689 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004690 InvokeRuntimeCallingConvention calling_convention;
4691 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4692}
4693
4694void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004695 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004696 instruction,
4697 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004698 if (instruction->IsEnter()) {
4699 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4700 } else {
4701 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4702 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004703}
4704
Alexandre Rames42d641b2014-10-27 14:00:51 +00004705void LocationsBuilderARM64::VisitMul(HMul* mul) {
4706 LocationSummary* locations =
4707 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4708 switch (mul->GetResultType()) {
4709 case Primitive::kPrimInt:
4710 case Primitive::kPrimLong:
4711 locations->SetInAt(0, Location::RequiresRegister());
4712 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004713 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004714 break;
4715
4716 case Primitive::kPrimFloat:
4717 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004718 locations->SetInAt(0, Location::RequiresFpuRegister());
4719 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004720 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004721 break;
4722
4723 default:
4724 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4725 }
4726}
4727
4728void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4729 switch (mul->GetResultType()) {
4730 case Primitive::kPrimInt:
4731 case Primitive::kPrimLong:
4732 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4733 break;
4734
4735 case Primitive::kPrimFloat:
4736 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004737 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004738 break;
4739
4740 default:
4741 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4742 }
4743}
4744
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004745void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4746 LocationSummary* locations =
4747 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4748 switch (neg->GetResultType()) {
4749 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004750 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004751 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004753 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004754
4755 case Primitive::kPrimFloat:
4756 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004757 locations->SetInAt(0, Location::RequiresFpuRegister());
4758 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004759 break;
4760
4761 default:
4762 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4763 }
4764}
4765
4766void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4767 switch (neg->GetResultType()) {
4768 case Primitive::kPrimInt:
4769 case Primitive::kPrimLong:
4770 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4771 break;
4772
4773 case Primitive::kPrimFloat:
4774 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004775 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004776 break;
4777
4778 default:
4779 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4780 }
4781}
4782
4783void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4784 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004785 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004786 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004787 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004788 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4789 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004790}
4791
4792void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004793 // Note: if heap poisoning is enabled, the entry point takes cares
4794 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004795 QuickEntrypointEnum entrypoint =
4796 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4797 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004798 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004799}
4800
Alexandre Rames5319def2014-10-23 10:03:10 +01004801void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4802 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004804 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004805 if (instruction->IsStringAlloc()) {
4806 locations->AddTemp(LocationFrom(kArtMethodRegister));
4807 } else {
4808 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004809 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004810 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4811}
4812
4813void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004814 // Note: if heap poisoning is enabled, the entry point takes cares
4815 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004816 if (instruction->IsStringAlloc()) {
4817 // String is allocated through StringFactory. Call NewEmptyString entry point.
4818 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004819 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004820 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4821 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4822 __ Blr(lr);
4823 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4824 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004825 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004826 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004827 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004828}
4829
4830void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004832 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004834}
4835
4836void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004837 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004838 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004839 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004840 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004841 break;
4842
4843 default:
4844 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4845 }
4846}
4847
David Brazdil66d126e2015-04-03 16:02:44 +01004848void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4849 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4850 locations->SetInAt(0, Location::RequiresRegister());
4851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4852}
4853
4854void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004855 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004856}
4857
Alexandre Rames5319def2014-10-23 10:03:10 +01004858void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004859 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4860 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004861}
4862
Calin Juravle2ae48182016-03-16 14:05:09 +00004863void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4864 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 return;
4866 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004867
Alexandre Ramesd921d642015-04-16 15:07:16 +01004868 BlockPoolsScope block_pools(GetVIXLAssembler());
4869 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004870 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004871 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004872}
4873
Calin Juravle2ae48182016-03-16 14:05:09 +00004874void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004875 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004876 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004877
4878 LocationSummary* locations = instruction->GetLocations();
4879 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004880
4881 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004882}
4883
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004884void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004885 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004886}
4887
Alexandre Rames67555f72014-11-18 10:55:16 +00004888void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4889 HandleBinaryOp(instruction);
4890}
4891
4892void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4893 HandleBinaryOp(instruction);
4894}
4895
Alexandre Rames3e69f162014-12-10 10:36:50 +00004896void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4897 LOG(FATAL) << "Unreachable";
4898}
4899
4900void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4901 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4902}
4903
Alexandre Rames5319def2014-10-23 10:03:10 +01004904void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4905 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4906 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4907 if (location.IsStackSlot()) {
4908 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4909 } else if (location.IsDoubleStackSlot()) {
4910 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4911 }
4912 locations->SetOut(location);
4913}
4914
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004915void InstructionCodeGeneratorARM64::VisitParameterValue(
4916 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004917 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004918}
4919
4920void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4921 LocationSummary* locations =
4922 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004923 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004924}
4925
4926void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4927 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4928 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004929}
4930
4931void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004933 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004934 locations->SetInAt(i, Location::Any());
4935 }
4936 locations->SetOut(Location::Any());
4937}
4938
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004939void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004940 LOG(FATAL) << "Unreachable";
4941}
4942
Serban Constantinescu02164b32014-11-13 14:05:07 +00004943void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004944 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004945 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004946 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4947 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004948 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4949
4950 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004951 case Primitive::kPrimInt:
4952 case Primitive::kPrimLong:
4953 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004954 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4956 break;
4957
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004958 case Primitive::kPrimFloat:
4959 case Primitive::kPrimDouble: {
4960 InvokeRuntimeCallingConvention calling_convention;
4961 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4962 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4963 locations->SetOut(calling_convention.GetReturnLocation(type));
4964
4965 break;
4966 }
4967
Serban Constantinescu02164b32014-11-13 14:05:07 +00004968 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004969 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004970 }
4971}
4972
4973void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4974 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004975
Serban Constantinescu02164b32014-11-13 14:05:07 +00004976 switch (type) {
4977 case Primitive::kPrimInt:
4978 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004979 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004980 break;
4981 }
4982
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004983 case Primitive::kPrimFloat:
4984 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004985 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4986 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004987 if (type == Primitive::kPrimFloat) {
4988 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4989 } else {
4990 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4991 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004992 break;
4993 }
4994
Serban Constantinescu02164b32014-11-13 14:05:07 +00004995 default:
4996 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004997 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004998 }
4999}
5000
Calin Juravle27df7582015-04-17 19:12:31 +01005001void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5002 memory_barrier->SetLocations(nullptr);
5003}
5004
5005void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005006 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005007}
5008
Alexandre Rames5319def2014-10-23 10:03:10 +01005009void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5011 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005012 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005013}
5014
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005015void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005016 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005017}
5018
5019void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5020 instruction->SetLocations(nullptr);
5021}
5022
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005023void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005024 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005025}
5026
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005027void LocationsBuilderARM64::VisitRor(HRor* ror) {
5028 HandleBinaryOp(ror);
5029}
5030
5031void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5032 HandleBinaryOp(ror);
5033}
5034
Serban Constantinescu02164b32014-11-13 14:05:07 +00005035void LocationsBuilderARM64::VisitShl(HShl* shl) {
5036 HandleShift(shl);
5037}
5038
5039void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5040 HandleShift(shl);
5041}
5042
5043void LocationsBuilderARM64::VisitShr(HShr* shr) {
5044 HandleShift(shr);
5045}
5046
5047void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5048 HandleShift(shr);
5049}
5050
Alexandre Rames5319def2014-10-23 10:03:10 +01005051void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005052 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005053}
5054
5055void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005056 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005057}
5058
Alexandre Rames67555f72014-11-18 10:55:16 +00005059void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005060 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005061}
5062
5063void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005064 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005065}
5066
5067void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005068 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005069}
5070
Alexandre Rames67555f72014-11-18 10:55:16 +00005071void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005072 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005073}
5074
Calin Juravlee460d1d2015-09-29 04:52:17 +01005075void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5076 HUnresolvedInstanceFieldGet* instruction) {
5077 FieldAccessCallingConventionARM64 calling_convention;
5078 codegen_->CreateUnresolvedFieldLocationSummary(
5079 instruction, instruction->GetFieldType(), calling_convention);
5080}
5081
5082void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5083 HUnresolvedInstanceFieldGet* instruction) {
5084 FieldAccessCallingConventionARM64 calling_convention;
5085 codegen_->GenerateUnresolvedFieldAccess(instruction,
5086 instruction->GetFieldType(),
5087 instruction->GetFieldIndex(),
5088 instruction->GetDexPc(),
5089 calling_convention);
5090}
5091
5092void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5093 HUnresolvedInstanceFieldSet* instruction) {
5094 FieldAccessCallingConventionARM64 calling_convention;
5095 codegen_->CreateUnresolvedFieldLocationSummary(
5096 instruction, instruction->GetFieldType(), calling_convention);
5097}
5098
5099void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5100 HUnresolvedInstanceFieldSet* instruction) {
5101 FieldAccessCallingConventionARM64 calling_convention;
5102 codegen_->GenerateUnresolvedFieldAccess(instruction,
5103 instruction->GetFieldType(),
5104 instruction->GetFieldIndex(),
5105 instruction->GetDexPc(),
5106 calling_convention);
5107}
5108
5109void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5110 HUnresolvedStaticFieldGet* instruction) {
5111 FieldAccessCallingConventionARM64 calling_convention;
5112 codegen_->CreateUnresolvedFieldLocationSummary(
5113 instruction, instruction->GetFieldType(), calling_convention);
5114}
5115
5116void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5117 HUnresolvedStaticFieldGet* instruction) {
5118 FieldAccessCallingConventionARM64 calling_convention;
5119 codegen_->GenerateUnresolvedFieldAccess(instruction,
5120 instruction->GetFieldType(),
5121 instruction->GetFieldIndex(),
5122 instruction->GetDexPc(),
5123 calling_convention);
5124}
5125
5126void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5127 HUnresolvedStaticFieldSet* instruction) {
5128 FieldAccessCallingConventionARM64 calling_convention;
5129 codegen_->CreateUnresolvedFieldLocationSummary(
5130 instruction, instruction->GetFieldType(), calling_convention);
5131}
5132
5133void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5134 HUnresolvedStaticFieldSet* instruction) {
5135 FieldAccessCallingConventionARM64 calling_convention;
5136 codegen_->GenerateUnresolvedFieldAccess(instruction,
5137 instruction->GetFieldType(),
5138 instruction->GetFieldIndex(),
5139 instruction->GetDexPc(),
5140 calling_convention);
5141}
5142
Alexandre Rames5319def2014-10-23 10:03:10 +01005143void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005144 LocationSummary* locations =
5145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005146 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005147}
5148
5149void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005150 HBasicBlock* block = instruction->GetBlock();
5151 if (block->GetLoopInformation() != nullptr) {
5152 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5153 // The back edge will generate the suspend check.
5154 return;
5155 }
5156 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5157 // The goto will generate the suspend check.
5158 return;
5159 }
5160 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005161}
5162
Alexandre Rames67555f72014-11-18 10:55:16 +00005163void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5164 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005165 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005166 InvokeRuntimeCallingConvention calling_convention;
5167 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5168}
5169
5170void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005171 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005172 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005173}
5174
5175void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5176 LocationSummary* locations =
5177 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5178 Primitive::Type input_type = conversion->GetInputType();
5179 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005180 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005181 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5182 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5183 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5184 }
5185
Alexandre Rames542361f2015-01-29 16:57:31 +00005186 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005187 locations->SetInAt(0, Location::RequiresFpuRegister());
5188 } else {
5189 locations->SetInAt(0, Location::RequiresRegister());
5190 }
5191
Alexandre Rames542361f2015-01-29 16:57:31 +00005192 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005193 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5194 } else {
5195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5196 }
5197}
5198
5199void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5200 Primitive::Type result_type = conversion->GetResultType();
5201 Primitive::Type input_type = conversion->GetInputType();
5202
5203 DCHECK_NE(input_type, result_type);
5204
Alexandre Rames542361f2015-01-29 16:57:31 +00005205 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005206 int result_size = Primitive::ComponentSize(result_type);
5207 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005208 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005209 Register output = OutputRegister(conversion);
5210 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005211 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005212 // 'int' values are used directly as W registers, discarding the top
5213 // bits, so we don't need to sign-extend and can just perform a move.
5214 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5215 // top 32 bits of the target register. We theoretically could leave those
5216 // bits unchanged, but we would have to make sure that no code uses a
5217 // 32bit input value as a 64bit value assuming that the top 32 bits are
5218 // zero.
5219 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005220 } else if (result_type == Primitive::kPrimChar ||
5221 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5222 __ Ubfx(output,
5223 output.IsX() ? source.X() : source.W(),
5224 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005225 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005226 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005227 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005228 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005229 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005230 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005231 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5232 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005233 } else if (Primitive::IsFloatingPointType(result_type) &&
5234 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005235 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5236 } else {
5237 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5238 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005239 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005240}
Alexandre Rames67555f72014-11-18 10:55:16 +00005241
Serban Constantinescu02164b32014-11-13 14:05:07 +00005242void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5243 HandleShift(ushr);
5244}
5245
5246void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5247 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005248}
5249
5250void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5251 HandleBinaryOp(instruction);
5252}
5253
5254void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5255 HandleBinaryOp(instruction);
5256}
5257
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005258void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005259 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005260 LOG(FATAL) << "Unreachable";
5261}
5262
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005263void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005264 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005265 LOG(FATAL) << "Unreachable";
5266}
5267
Mark Mendellfe57faa2015-09-18 09:26:15 -04005268// Simple implementation of packed switch - generate cascaded compare/jumps.
5269void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5270 LocationSummary* locations =
5271 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5272 locations->SetInAt(0, Location::RequiresRegister());
5273}
5274
5275void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5276 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005277 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005278 Register value_reg = InputRegisterAt(switch_instr, 0);
5279 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5280
Zheng Xu3927c8b2015-11-18 17:46:25 +08005281 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005282 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005283 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5284 // make sure we don't emit it if the target may run out of range.
5285 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5286 // ranges and emit the tables only as required.
5287 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005288
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005289 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005290 // Current instruction id is an upper bound of the number of HIRs in the graph.
5291 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5292 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005293 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5294 Register temp = temps.AcquireW();
5295 __ Subs(temp, value_reg, Operand(lower_bound));
5296
Zheng Xu3927c8b2015-11-18 17:46:25 +08005297 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005298 // Jump to successors[0] if value == lower_bound.
5299 __ B(eq, codegen_->GetLabelOf(successors[0]));
5300 int32_t last_index = 0;
5301 for (; num_entries - last_index > 2; last_index += 2) {
5302 __ Subs(temp, temp, Operand(2));
5303 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5304 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5305 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5306 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5307 }
5308 if (num_entries - last_index == 2) {
5309 // The last missing case_value.
5310 __ Cmp(temp, Operand(1));
5311 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005312 }
5313
5314 // And the default for any other value.
5315 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5316 __ B(codegen_->GetLabelOf(default_block));
5317 }
5318 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005319 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005320
5321 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5322
5323 // Below instructions should use at most one blocked register. Since there are two blocked
5324 // registers, we are free to block one.
5325 Register temp_w = temps.AcquireW();
5326 Register index;
5327 // Remove the bias.
5328 if (lower_bound != 0) {
5329 index = temp_w;
5330 __ Sub(index, value_reg, Operand(lower_bound));
5331 } else {
5332 index = value_reg;
5333 }
5334
5335 // Jump to default block if index is out of the range.
5336 __ Cmp(index, Operand(num_entries));
5337 __ B(hs, codegen_->GetLabelOf(default_block));
5338
5339 // In current VIXL implementation, it won't require any blocked registers to encode the
5340 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5341 // register pressure.
5342 Register table_base = temps.AcquireX();
5343 // Load jump offset from the table.
5344 __ Adr(table_base, jump_table->GetTableStartLabel());
5345 Register jump_offset = temp_w;
5346 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5347
5348 // Jump to target block by branching to table_base(pc related) + offset.
5349 Register target_address = table_base;
5350 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5351 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005352 }
5353}
5354
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005355void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5356 HInstruction* instruction,
5357 Location out,
5358 uint32_t offset,
5359 Location maybe_temp,
5360 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005361 Primitive::Type type = Primitive::kPrimNot;
5362 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005363 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005364 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005365 Register temp_reg = RegisterFrom(maybe_temp, type);
5366 if (kUseBakerReadBarrier) {
5367 // Load with fast path based Baker's read barrier.
5368 // /* HeapReference<Object> */ out = *(out + offset)
5369 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5370 out,
5371 out_reg,
5372 offset,
5373 temp_reg,
5374 /* needs_null_check */ false,
5375 /* use_load_acquire */ false);
5376 } else {
5377 // Load with slow path based read barrier.
5378 // Save the value of `out` into `maybe_temp` before overwriting it
5379 // in the following move operation, as we will need it for the
5380 // read barrier below.
5381 __ Mov(temp_reg, out_reg);
5382 // /* HeapReference<Object> */ out = *(out + offset)
5383 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5384 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5385 }
5386 } else {
5387 // Plain load with no read barrier.
5388 // /* HeapReference<Object> */ out = *(out + offset)
5389 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5390 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5391 }
5392}
5393
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005394void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5395 HInstruction* instruction,
5396 Location out,
5397 Location obj,
5398 uint32_t offset,
5399 Location maybe_temp,
5400 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005401 Primitive::Type type = Primitive::kPrimNot;
5402 Register out_reg = RegisterFrom(out, type);
5403 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005404 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005405 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005406 if (kUseBakerReadBarrier) {
5407 // Load with fast path based Baker's read barrier.
5408 Register temp_reg = RegisterFrom(maybe_temp, type);
5409 // /* HeapReference<Object> */ out = *(obj + offset)
5410 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5411 out,
5412 obj_reg,
5413 offset,
5414 temp_reg,
5415 /* needs_null_check */ false,
5416 /* use_load_acquire */ false);
5417 } else {
5418 // Load with slow path based read barrier.
5419 // /* HeapReference<Object> */ out = *(obj + offset)
5420 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5421 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5422 }
5423 } else {
5424 // Plain load with no read barrier.
5425 // /* HeapReference<Object> */ out = *(obj + offset)
5426 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5427 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5428 }
5429}
5430
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005431void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5432 HInstruction* instruction,
5433 Location root,
5434 Register obj,
5435 uint32_t offset,
5436 vixl::aarch64::Label* fixup_label,
5437 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005438 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005439 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005440 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005441 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005442 if (kUseBakerReadBarrier) {
5443 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5444 // Baker's read barrier are used:
5445 //
5446 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005447 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5448 // if (temp != null) {
5449 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005450 // }
5451
5452 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005453 if (fixup_label == nullptr) {
5454 __ Ldr(root_reg, MemOperand(obj, offset));
5455 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005456 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005457 }
Roland Levillain44015862016-01-22 11:47:17 +00005458 static_assert(
5459 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5460 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5461 "have different sizes.");
5462 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5463 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5464 "have different sizes.");
5465
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005466 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005467
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005468 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5469 SlowPathCodeARM64* slow_path =
5470 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5471 root,
5472 LocationFrom(temp));
5473 codegen_->AddSlowPath(slow_path);
5474 const int32_t entry_point_offset =
5475 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5476 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5477 // Loading the entrypoint does not require a load acquire since it is only changed when
5478 // threads are suspended or running a checkpoint.
5479 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5480 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5481 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005482 __ Cbnz(temp, slow_path->GetEntryLabel());
5483 __ Bind(slow_path->GetExitLabel());
5484 } else {
5485 // GC root loaded through a slow path for read barriers other
5486 // than Baker's.
5487 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005488 if (fixup_label == nullptr) {
5489 __ Add(root_reg.X(), obj.X(), offset);
5490 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005491 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005492 }
Roland Levillain44015862016-01-22 11:47:17 +00005493 // /* mirror::Object* */ root = root->Read()
5494 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5495 }
5496 } else {
5497 // Plain GC root load with no read barrier.
5498 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005499 if (fixup_label == nullptr) {
5500 __ Ldr(root_reg, MemOperand(obj, offset));
5501 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005502 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005503 }
Roland Levillain44015862016-01-22 11:47:17 +00005504 // Note that GC roots are not affected by heap poisoning, thus we
5505 // do not have to unpoison `root_reg` here.
5506 }
5507}
5508
5509void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5510 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005511 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005512 uint32_t offset,
5513 Register temp,
5514 bool needs_null_check,
5515 bool use_load_acquire) {
5516 DCHECK(kEmitCompilerReadBarrier);
5517 DCHECK(kUseBakerReadBarrier);
5518
5519 // /* HeapReference<Object> */ ref = *(obj + offset)
5520 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005521 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005522 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5523 ref,
5524 obj,
5525 offset,
5526 no_index,
5527 no_scale_factor,
5528 temp,
5529 needs_null_check,
5530 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005531}
5532
5533void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5534 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005535 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005536 uint32_t data_offset,
5537 Location index,
5538 Register temp,
5539 bool needs_null_check) {
5540 DCHECK(kEmitCompilerReadBarrier);
5541 DCHECK(kUseBakerReadBarrier);
5542
5543 // Array cells are never volatile variables, therefore array loads
5544 // never use Load-Acquire instructions on ARM64.
5545 const bool use_load_acquire = false;
5546
Roland Levillainbfea3352016-06-23 13:48:47 +01005547 static_assert(
5548 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5549 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005550 // /* HeapReference<Object> */ ref =
5551 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005552 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5553 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5554 ref,
5555 obj,
5556 data_offset,
5557 index,
5558 scale_factor,
5559 temp,
5560 needs_null_check,
5561 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005562}
5563
5564void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5565 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005566 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005567 uint32_t offset,
5568 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005569 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005570 Register temp,
5571 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005572 bool use_load_acquire,
5573 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005574 DCHECK(kEmitCompilerReadBarrier);
5575 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005576 // If we are emitting an array load, we should not be using a
5577 // Load Acquire instruction. In other words:
5578 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5579 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005580
5581 MacroAssembler* masm = GetVIXLAssembler();
5582 UseScratchRegisterScope temps(masm);
5583
5584 // In slow path based read barriers, the read barrier call is
5585 // inserted after the original load. However, in fast path based
5586 // Baker's read barriers, we need to perform the load of
5587 // mirror::Object::monitor_ *before* the original reference load.
5588 // This load-load ordering is required by the read barrier.
5589 // The fast path/slow path (for Baker's algorithm) should look like:
5590 //
5591 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5592 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5593 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005594 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005595 // if (is_gray) {
5596 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5597 // }
5598 //
5599 // Note: the original implementation in ReadBarrier::Barrier is
5600 // slightly more complex as it performs additional checks that we do
5601 // not do here for performance reasons.
5602
5603 Primitive::Type type = Primitive::kPrimNot;
5604 Register ref_reg = RegisterFrom(ref, type);
5605 DCHECK(obj.IsW());
5606 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5607
5608 // /* int32_t */ monitor = obj->monitor_
5609 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5610 if (needs_null_check) {
5611 MaybeRecordImplicitNullCheck(instruction);
5612 }
5613 // /* LockWord */ lock_word = LockWord(monitor)
5614 static_assert(sizeof(LockWord) == sizeof(int32_t),
5615 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005616
Vladimir Marko877a0332016-07-11 19:30:56 +01005617 // Introduce a dependency on the lock_word including rb_state,
5618 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005619 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005620 // `obj` is unchanged by this operation, but its value now depends
5621 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005622 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005623
5624 // The actual reference load.
5625 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005626 // Load types involving an "index": ArrayGet,
5627 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5628 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005629 if (use_load_acquire) {
5630 // UnsafeGetObjectVolatile intrinsic case.
5631 // Register `index` is not an index in an object array, but an
5632 // offset to an object reference field within object `obj`.
5633 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5634 DCHECK(instruction->GetLocations()->Intrinsified());
5635 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5636 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005637 DCHECK_EQ(offset, 0u);
5638 DCHECK_EQ(scale_factor, 0u);
5639 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005640 // /* HeapReference<Object> */ ref = *(obj + index)
5641 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5642 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005643 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005644 // ArrayGet and UnsafeGetObject intrinsics cases.
5645 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5646 if (index.IsConstant()) {
5647 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5648 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5649 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005650 Register temp3 = temps.AcquireW();
5651 __ Add(temp3, obj, offset);
5652 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5653 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005654 }
Roland Levillain44015862016-01-22 11:47:17 +00005655 }
Roland Levillain44015862016-01-22 11:47:17 +00005656 } else {
5657 // /* HeapReference<Object> */ ref = *(obj + offset)
5658 MemOperand field = HeapOperand(obj, offset);
5659 if (use_load_acquire) {
5660 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5661 } else {
5662 Load(type, ref_reg, field);
5663 }
5664 }
5665
5666 // Object* ref = ref_addr->AsMirrorPtr()
5667 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5668
Vladimir Marko953437b2016-08-24 08:30:46 +00005669 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005670 SlowPathCodeARM64* slow_path;
5671 if (always_update_field) {
5672 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5673 // address of the form `obj + field_offset`, where `obj` is a
5674 // register and `field_offset` is a register. Thus `offset` and
5675 // `scale_factor` above are expected to be null in this code path.
5676 DCHECK_EQ(offset, 0u);
5677 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5678 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5679 instruction, ref, obj, /* field_offset */ index, temp);
5680 } else {
5681 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5682 }
Roland Levillain44015862016-01-22 11:47:17 +00005683 AddSlowPath(slow_path);
5684
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005685 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005686 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005687 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005688 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5689 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005690 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005691 __ Bind(slow_path->GetExitLabel());
5692}
5693
5694void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5695 Location out,
5696 Location ref,
5697 Location obj,
5698 uint32_t offset,
5699 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005700 DCHECK(kEmitCompilerReadBarrier);
5701
Roland Levillain44015862016-01-22 11:47:17 +00005702 // Insert a slow path based read barrier *after* the reference load.
5703 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005704 // If heap poisoning is enabled, the unpoisoning of the loaded
5705 // reference will be carried out by the runtime within the slow
5706 // path.
5707 //
5708 // Note that `ref` currently does not get unpoisoned (when heap
5709 // poisoning is enabled), which is alright as the `ref` argument is
5710 // not used by the artReadBarrierSlow entry point.
5711 //
5712 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5713 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5714 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5715 AddSlowPath(slow_path);
5716
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005717 __ B(slow_path->GetEntryLabel());
5718 __ Bind(slow_path->GetExitLabel());
5719}
5720
Roland Levillain44015862016-01-22 11:47:17 +00005721void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5722 Location out,
5723 Location ref,
5724 Location obj,
5725 uint32_t offset,
5726 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005727 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005728 // Baker's read barriers shall be handled by the fast path
5729 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5730 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005731 // If heap poisoning is enabled, unpoisoning will be taken care of
5732 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005733 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005734 } else if (kPoisonHeapReferences) {
5735 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5736 }
5737}
5738
Roland Levillain44015862016-01-22 11:47:17 +00005739void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5740 Location out,
5741 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005742 DCHECK(kEmitCompilerReadBarrier);
5743
Roland Levillain44015862016-01-22 11:47:17 +00005744 // Insert a slow path based read barrier *after* the GC root load.
5745 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005746 // Note that GC roots are not affected by heap poisoning, so we do
5747 // not need to do anything special for this here.
5748 SlowPathCodeARM64* slow_path =
5749 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5750 AddSlowPath(slow_path);
5751
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005752 __ B(slow_path->GetEntryLabel());
5753 __ Bind(slow_path->GetExitLabel());
5754}
5755
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005756void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5757 LocationSummary* locations =
5758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5759 locations->SetInAt(0, Location::RequiresRegister());
5760 locations->SetOut(Location::RequiresRegister());
5761}
5762
5763void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5764 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005765 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005766 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005767 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005768 __ Ldr(XRegisterFrom(locations->Out()),
5769 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005770 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005771 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005772 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005773 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5774 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005775 __ Ldr(XRegisterFrom(locations->Out()),
5776 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005777 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005778}
5779
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005780static void PatchJitRootUse(uint8_t* code,
5781 const uint8_t* roots_data,
5782 vixl::aarch64::Literal<uint32_t>* literal,
5783 uint64_t index_in_table) {
5784 uint32_t literal_offset = literal->GetOffset();
5785 uintptr_t address =
5786 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5787 uint8_t* data = code + literal_offset;
5788 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5789}
5790
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005791void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5792 for (const auto& entry : jit_string_patches_) {
5793 const auto& it = jit_string_roots_.find(entry.first);
5794 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005795 PatchJitRootUse(code, roots_data, entry.second, it->second);
5796 }
5797 for (const auto& entry : jit_class_patches_) {
5798 const auto& it = jit_class_roots_.find(entry.first);
5799 DCHECK(it != jit_class_roots_.end());
5800 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005801 }
5802}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005803
Alexandre Rames67555f72014-11-18 10:55:16 +00005804#undef __
5805#undef QUICK_ENTRY_POINT
5806
Alexandre Rames5319def2014-10-23 10:03:10 +01005807} // namespace arm64
5808} // namespace art