blob: a085fea0f81b46224fb656c511be4540ddd93315 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = at_->GetLocations();
285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
293 : kQuickInitializeType;
294 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100296 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100298 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000300
301 // Move the class to the desired location.
302 Location out = locations->Out();
303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 __ B(GetExitLabel());
311 }
312
Alexandre Rames9931f312015-06-19 14:47:01 +0100313 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
314
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
319 // The instruction where this slow path is happening.
320 // (Might be the load class or an initialization check).
321 HInstruction* const at_;
322
323 // The dex PC of `at_`.
324 const uint32_t dex_pc_;
325
326 // Whether to initialize the class.
327 const bool do_clinit_;
328
329 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
330};
331
Vladimir Markoaad75c62016-10-03 08:46:48 +0000332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100334 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
335 : SlowPathCodeARM64(instruction),
336 temp_(temp),
337 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 LocationSummary* locations = instruction_->GetLocations();
341 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
343
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100344 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
345 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
346 temps.Exclude(temp_);
347
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, locations);
350
351 InvokeRuntimeCallingConvention calling_convention;
352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
356 Primitive::Type type = instruction_->GetType();
357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
358
359 RestoreLiveRegisters(codegen, locations);
360
361 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
364 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
367 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
368 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100371 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100376 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 }
378
379 __ B(GetExitLabel());
380 }
381
382 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
383
384 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100385 const Register temp_;
386 vixl::aarch64::Label* adrp_label_;
387
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
389};
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 if (instruction_->CanThrowIntoCatchBlock()) {
399 // Live registers will be restored in the catch block if caught.
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
401 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000402 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
403 instruction_,
404 instruction_->GetDexPc(),
405 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800406 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100407 }
408
Alexandre Rames8158f282015-08-07 10:26:17 +0100409 bool IsFatal() const OVERRIDE { return true; }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
412
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
415};
416
417class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100419 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000420 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 if (successor_ == nullptr) {
428 __ B(GetReturnLabel());
429 } else {
430 __ B(arm64_codegen->GetLabelOf(successor_));
431 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 }
433
Scott Wakeling97c72b72016-06-24 16:19:36 +0100434 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 DCHECK(successor_ == nullptr);
436 return &return_label_;
437 }
438
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100439 HBasicBlock* GetSuccessor() const {
440 return successor_;
441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
444
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 // If not null, the block to branch to after the suspend check.
447 HBasicBlock* const successor_;
448
449 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100450 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100451
452 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
453};
454
Alexandre Rames67555f72014-11-18 10:55:16 +0000455class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000457 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000458 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800462 Location arg0, arg1;
463 if (instruction_->IsInstanceOf()) {
464 arg0 = locations->InAt(1);
465 arg1 = locations->Out();
466 } else {
467 arg0 = locations->InAt(0);
468 arg1 = locations->InAt(1);
469 }
470
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 DCHECK(instruction_->IsCheckCast()
472 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
473 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100474 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475
Alexandre Rames67555f72014-11-18 10:55:16 +0000476 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000478 if (!is_fatal_) {
479 SaveLiveRegisters(codegen, locations);
480 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481
482 // We're moving two locations to locations that could overlap, so we need a parallel
483 // move resolver.
484 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800485 codegen->EmitParallelMoves(arg0,
486 LocationFrom(calling_convention.GetRegisterAt(0)),
487 Primitive::kPrimNot,
488 arg1,
489 LocationFrom(calling_convention.GetRegisterAt(1)),
490 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000491 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000492 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800493 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000494 Primitive::Type ret_type = instruction_->GetType();
495 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
496 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
497 } else {
498 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800499 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
500 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 }
502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000503 if (!is_fatal_) {
504 RestoreLiveRegisters(codegen, locations);
505 __ B(GetExitLabel());
506 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 }
508
Alexandre Rames9931f312015-06-19 14:47:01 +0100509 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100510 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100511
Alexandre Rames67555f72014-11-18 10:55:16 +0000512 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000514
Alexandre Rames67555f72014-11-18 10:55:16 +0000515 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
516};
517
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700518class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
519 public:
Aart Bik42249c32016-01-07 15:33:50 -0800520 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000521 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700522
523 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800524 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000526 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000527 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528 }
529
Alexandre Rames9931f312015-06-19 14:47:01 +0100530 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
531
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700532 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700533 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
534};
535
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100536class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
537 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000538 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100539
540 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
541 LocationSummary* locations = instruction_->GetLocations();
542 __ Bind(GetEntryLabel());
543 SaveLiveRegisters(codegen, locations);
544
545 InvokeRuntimeCallingConvention calling_convention;
546 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
547 parallel_move.AddMove(
548 locations->InAt(0),
549 LocationFrom(calling_convention.GetRegisterAt(0)),
550 Primitive::kPrimNot,
551 nullptr);
552 parallel_move.AddMove(
553 locations->InAt(1),
554 LocationFrom(calling_convention.GetRegisterAt(1)),
555 Primitive::kPrimInt,
556 nullptr);
557 parallel_move.AddMove(
558 locations->InAt(2),
559 LocationFrom(calling_convention.GetRegisterAt(2)),
560 Primitive::kPrimNot,
561 nullptr);
562 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
563
564 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000565 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
567 RestoreLiveRegisters(codegen, locations);
568 __ B(GetExitLabel());
569 }
570
571 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
572
573 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100574 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
575};
576
Zheng Xu3927c8b2015-11-18 17:46:25 +0800577void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
578 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000579 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800580
581 // We are about to use the assembler to place literals directly. Make sure we have enough
582 // underlying code buffer and we have generated the jump table with right size.
583 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
584 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
585
586 __ Bind(&table_start_);
587 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
588 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100589 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800590 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100591 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800592 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
593 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
594 Literal<int32_t> literal(jump_offset);
595 __ place(&literal);
596 }
597}
598
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100599// Slow path marking an object reference `ref` during a read
600// barrier. The field `obj.field` in the object `obj` holding this
601// reference does not get updated by this slow path after marking (see
602// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
603//
604// This means that after the execution of this slow path, `ref` will
605// always be up-to-date, but `obj.field` may not; i.e., after the
606// flip, `ref` will be a to-space reference, but `obj.field` will
607// probably still be a from-space reference (unless it gets updated by
608// another thread, or if another thread installed another object
609// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800610// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
611// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000612class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
613 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800614 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
615 Location ref,
616 Location entrypoint = Location::NoLocation())
617 : SlowPathCodeARM64(instruction),
618 ref_(ref),
619 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000620 DCHECK(kEmitCompilerReadBarrier);
621 }
622
623 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000627 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100628 DCHECK(ref_.IsRegister()) << ref_;
629 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000630 DCHECK(instruction_->IsInstanceFieldGet() ||
631 instruction_->IsStaticFieldGet() ||
632 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100633 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000634 instruction_->IsLoadClass() ||
635 instruction_->IsLoadString() ||
636 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100637 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100638 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
639 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000640 << "Unexpected instruction in read barrier marking slow path: "
641 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000642 // The read barrier instrumentation of object ArrayGet
643 // instructions does not support the HIntermediateAddress
644 // instruction.
645 DCHECK(!(instruction_->IsArrayGet() &&
646 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000647
648 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100649 // No need to save live registers; it's taken care of by the
650 // entrypoint. Also, there is no need to update the stack mask,
651 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000652 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100653 DCHECK_NE(ref_.reg(), LR);
654 DCHECK_NE(ref_.reg(), WSP);
655 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100656 // IP0 is used internally by the ReadBarrierMarkRegX entry point
657 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100658 DCHECK_NE(ref_.reg(), IP0);
659 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100660 // "Compact" slow path, saving two moves.
661 //
662 // Instead of using the standard runtime calling convention (input
663 // and output in W0):
664 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100665 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100666 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100667 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100668 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100669 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100670 // of a dedicated entrypoint:
671 //
672 // rX <- ReadBarrierMarkRegX(rX)
673 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800674 if (entrypoint_.IsValid()) {
675 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
676 __ Blr(XRegisterFrom(entrypoint_));
677 } else {
678 // Entrypoint is not already loaded, load from the thread.
679 int32_t entry_point_offset =
680 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
681 // This runtime call does not require a stack map.
682 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
683 }
Roland Levillain44015862016-01-22 11:47:17 +0000684 __ B(GetExitLabel());
685 }
686
687 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100688 // The location (register) of the marked object reference.
689 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000690
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800691 // The location of the entrypoint if it is already loaded.
692 const Location entrypoint_;
693
Roland Levillain44015862016-01-22 11:47:17 +0000694 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
695};
696
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100697// Slow path marking an object reference `ref` during a read barrier,
698// and if needed, atomically updating the field `obj.field` in the
699// object `obj` holding this reference after marking (contrary to
700// ReadBarrierMarkSlowPathARM64 above, which never tries to update
701// `obj.field`).
702//
703// This means that after the execution of this slow path, both `ref`
704// and `obj.field` will be up-to-date; i.e., after the flip, both will
705// hold the same to-space reference (unless another thread installed
706// another object reference (different from `ref`) in `obj.field`).
707class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
708 public:
709 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
710 Location ref,
711 Register obj,
712 Location field_offset,
713 Register temp)
714 : SlowPathCodeARM64(instruction),
715 ref_(ref),
716 obj_(obj),
717 field_offset_(field_offset),
718 temp_(temp) {
719 DCHECK(kEmitCompilerReadBarrier);
720 }
721
722 const char* GetDescription() const OVERRIDE {
723 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
724 }
725
726 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
727 LocationSummary* locations = instruction_->GetLocations();
728 Register ref_reg = WRegisterFrom(ref_);
729 DCHECK(locations->CanCall());
730 DCHECK(ref_.IsRegister()) << ref_;
731 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
732 // This slow path is only used by the UnsafeCASObject intrinsic.
733 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
734 << "Unexpected instruction in read barrier marking and field updating slow path: "
735 << instruction_->DebugName();
736 DCHECK(instruction_->GetLocations()->Intrinsified());
737 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
738 DCHECK(field_offset_.IsRegister()) << field_offset_;
739
740 __ Bind(GetEntryLabel());
741
742 // Save the old reference.
743 // Note that we cannot use IP to save the old reference, as IP is
744 // used internally by the ReadBarrierMarkRegX entry point, and we
745 // need the old reference after the call to that entry point.
746 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
747 __ Mov(temp_.W(), ref_reg);
748
749 // No need to save live registers; it's taken care of by the
750 // entrypoint. Also, there is no need to update the stack mask,
751 // as this runtime call will not trigger a garbage collection.
752 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
753 DCHECK_NE(ref_.reg(), LR);
754 DCHECK_NE(ref_.reg(), WSP);
755 DCHECK_NE(ref_.reg(), WZR);
756 // IP0 is used internally by the ReadBarrierMarkRegX entry point
757 // as a temporary, it cannot be the entry point's input/output.
758 DCHECK_NE(ref_.reg(), IP0);
759 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
760 // "Compact" slow path, saving two moves.
761 //
762 // Instead of using the standard runtime calling convention (input
763 // and output in W0):
764 //
765 // W0 <- ref
766 // W0 <- ReadBarrierMark(W0)
767 // ref <- W0
768 //
769 // we just use rX (the register containing `ref`) as input and output
770 // of a dedicated entrypoint:
771 //
772 // rX <- ReadBarrierMarkRegX(rX)
773 //
774 int32_t entry_point_offset =
775 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
776 // This runtime call does not require a stack map.
777 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
778
779 // If the new reference is different from the old reference,
780 // update the field in the holder (`*(obj_ + field_offset_)`).
781 //
782 // Note that this field could also hold a different object, if
783 // another thread had concurrently changed it. In that case, the
784 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
785 // (CAS) operation below would abort the CAS, leaving the field
786 // as-is.
787 vixl::aarch64::Label done;
788 __ Cmp(temp_.W(), ref_reg);
789 __ B(eq, &done);
790
791 // Update the the holder's field atomically. This may fail if
792 // mutator updates before us, but it's OK. This is achieved
793 // using a strong compare-and-set (CAS) operation with relaxed
794 // memory synchronization ordering, where the expected value is
795 // the old reference and the desired value is the new reference.
796
797 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
798 UseScratchRegisterScope temps(masm);
799
800 // Convenience aliases.
801 Register base = obj_.W();
802 Register offset = XRegisterFrom(field_offset_);
803 Register expected = temp_.W();
804 Register value = ref_reg;
805 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
806 Register tmp_value = temps.AcquireW(); // Value in memory.
807
808 __ Add(tmp_ptr, base.X(), Operand(offset));
809
810 if (kPoisonHeapReferences) {
811 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
812 if (value.Is(expected)) {
813 // Do not poison `value`, as it is the same register as
814 // `expected`, which has just been poisoned.
815 } else {
816 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
817 }
818 }
819
820 // do {
821 // tmp_value = [tmp_ptr] - expected;
822 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
823
Roland Levillain24a4d112016-10-26 13:10:46 +0100824 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100825 __ Bind(&loop_head);
826 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
827 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100828 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100829 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
830 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100831 __ B(&exit_loop);
832 __ Bind(&comparison_failed);
833 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100834 __ Bind(&exit_loop);
835
836 if (kPoisonHeapReferences) {
837 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
838 if (value.Is(expected)) {
839 // Do not unpoison `value`, as it is the same register as
840 // `expected`, which has just been unpoisoned.
841 } else {
842 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
843 }
844 }
845
846 __ Bind(&done);
847 __ B(GetExitLabel());
848 }
849
850 private:
851 // The location (register) of the marked object reference.
852 const Location ref_;
853 // The register containing the object holding the marked object reference field.
854 const Register obj_;
855 // The location of the offset of the marked reference field within `obj_`.
856 Location field_offset_;
857
858 const Register temp_;
859
860 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
861};
862
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000863// Slow path generating a read barrier for a heap reference.
864class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
865 public:
866 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
867 Location out,
868 Location ref,
869 Location obj,
870 uint32_t offset,
871 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000872 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000873 out_(out),
874 ref_(ref),
875 obj_(obj),
876 offset_(offset),
877 index_(index) {
878 DCHECK(kEmitCompilerReadBarrier);
879 // If `obj` is equal to `out` or `ref`, it means the initial object
880 // has been overwritten by (or after) the heap object reference load
881 // to be instrumented, e.g.:
882 //
883 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000884 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000885 //
886 // In that case, we have lost the information about the original
887 // object, and the emitted read barrier cannot work properly.
888 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
889 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
890 }
891
892 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
893 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
894 LocationSummary* locations = instruction_->GetLocations();
895 Primitive::Type type = Primitive::kPrimNot;
896 DCHECK(locations->CanCall());
897 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100898 DCHECK(instruction_->IsInstanceFieldGet() ||
899 instruction_->IsStaticFieldGet() ||
900 instruction_->IsArrayGet() ||
901 instruction_->IsInstanceOf() ||
902 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100903 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000904 << "Unexpected instruction in read barrier for heap reference slow path: "
905 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000906 // The read barrier instrumentation of object ArrayGet
907 // instructions does not support the HIntermediateAddress
908 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000909 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100910 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000911
912 __ Bind(GetEntryLabel());
913
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000914 SaveLiveRegisters(codegen, locations);
915
916 // We may have to change the index's value, but as `index_` is a
917 // constant member (like other "inputs" of this slow path),
918 // introduce a copy of it, `index`.
919 Location index = index_;
920 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100921 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000922 if (instruction_->IsArrayGet()) {
923 // Compute the actual memory offset and store it in `index`.
924 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
925 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
926 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
927 // We are about to change the value of `index_reg` (see the
928 // calls to vixl::MacroAssembler::Lsl and
929 // vixl::MacroAssembler::Mov below), but it has
930 // not been saved by the previous call to
931 // art::SlowPathCode::SaveLiveRegisters, as it is a
932 // callee-save register --
933 // art::SlowPathCode::SaveLiveRegisters does not consider
934 // callee-save registers, as it has been designed with the
935 // assumption that callee-save registers are supposed to be
936 // handled by the called function. So, as a callee-save
937 // register, `index_reg` _would_ eventually be saved onto
938 // the stack, but it would be too late: we would have
939 // changed its value earlier. Therefore, we manually save
940 // it here into another freely available register,
941 // `free_reg`, chosen of course among the caller-save
942 // registers (as a callee-save `free_reg` register would
943 // exhibit the same problem).
944 //
945 // Note we could have requested a temporary register from
946 // the register allocator instead; but we prefer not to, as
947 // this is a slow path, and we know we can find a
948 // caller-save register that is available.
949 Register free_reg = FindAvailableCallerSaveRegister(codegen);
950 __ Mov(free_reg.W(), index_reg);
951 index_reg = free_reg;
952 index = LocationFrom(index_reg);
953 } else {
954 // The initial register stored in `index_` has already been
955 // saved in the call to art::SlowPathCode::SaveLiveRegisters
956 // (as it is not a callee-save register), so we can freely
957 // use it.
958 }
959 // Shifting the index value contained in `index_reg` by the scale
960 // factor (2) cannot overflow in practice, as the runtime is
961 // unable to allocate object arrays with a size larger than
962 // 2^26 - 1 (that is, 2^28 - 4 bytes).
963 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
964 static_assert(
965 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
966 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
967 __ Add(index_reg, index_reg, Operand(offset_));
968 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100969 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
970 // intrinsics, `index_` is not shifted by a scale factor of 2
971 // (as in the case of ArrayGet), as it is actually an offset
972 // to an object field within an object.
973 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000974 DCHECK(instruction_->GetLocations()->Intrinsified());
975 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
976 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
977 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100978 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100979 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000980 }
981 }
982
983 // We're moving two or three locations to locations that could
984 // overlap, so we need a parallel move resolver.
985 InvokeRuntimeCallingConvention calling_convention;
986 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
987 parallel_move.AddMove(ref_,
988 LocationFrom(calling_convention.GetRegisterAt(0)),
989 type,
990 nullptr);
991 parallel_move.AddMove(obj_,
992 LocationFrom(calling_convention.GetRegisterAt(1)),
993 type,
994 nullptr);
995 if (index.IsValid()) {
996 parallel_move.AddMove(index,
997 LocationFrom(calling_convention.GetRegisterAt(2)),
998 Primitive::kPrimInt,
999 nullptr);
1000 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1001 } else {
1002 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1003 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1004 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001005 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001006 instruction_,
1007 instruction_->GetDexPc(),
1008 this);
1009 CheckEntrypointTypes<
1010 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1011 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1012
1013 RestoreLiveRegisters(codegen, locations);
1014
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001015 __ B(GetExitLabel());
1016 }
1017
1018 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1019
1020 private:
1021 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001022 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1023 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001024 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1025 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1026 return Register(VIXLRegCodeFromART(i), kXRegSize);
1027 }
1028 }
1029 // We shall never fail to find a free caller-save register, as
1030 // there are more than two core caller-save registers on ARM64
1031 // (meaning it is possible to find one which is different from
1032 // `ref` and `obj`).
1033 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1034 LOG(FATAL) << "Could not find a free register";
1035 UNREACHABLE();
1036 }
1037
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001038 const Location out_;
1039 const Location ref_;
1040 const Location obj_;
1041 const uint32_t offset_;
1042 // An additional location containing an index to an array.
1043 // Only used for HArrayGet and the UnsafeGetObject &
1044 // UnsafeGetObjectVolatile intrinsics.
1045 const Location index_;
1046
1047 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1048};
1049
1050// Slow path generating a read barrier for a GC root.
1051class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1052 public:
1053 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001054 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001055 DCHECK(kEmitCompilerReadBarrier);
1056 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001057
1058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1059 LocationSummary* locations = instruction_->GetLocations();
1060 Primitive::Type type = Primitive::kPrimNot;
1061 DCHECK(locations->CanCall());
1062 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001063 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1064 << "Unexpected instruction in read barrier for GC root slow path: "
1065 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001066
1067 __ Bind(GetEntryLabel());
1068 SaveLiveRegisters(codegen, locations);
1069
1070 InvokeRuntimeCallingConvention calling_convention;
1071 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1072 // The argument of the ReadBarrierForRootSlow is not a managed
1073 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1074 // thus we need a 64-bit move here, and we cannot use
1075 //
1076 // arm64_codegen->MoveLocation(
1077 // LocationFrom(calling_convention.GetRegisterAt(0)),
1078 // root_,
1079 // type);
1080 //
1081 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1082 // reference type (`Primitive::kPrimNot`).
1083 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001084 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001085 instruction_,
1086 instruction_->GetDexPc(),
1087 this);
1088 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1089 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1090
1091 RestoreLiveRegisters(codegen, locations);
1092 __ B(GetExitLabel());
1093 }
1094
1095 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1096
1097 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001098 const Location out_;
1099 const Location root_;
1100
1101 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1102};
1103
Alexandre Rames5319def2014-10-23 10:03:10 +01001104#undef __
1105
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001106Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 Location next_location;
1108 if (type == Primitive::kPrimVoid) {
1109 LOG(FATAL) << "Unreachable type " << type;
1110 }
1111
Alexandre Rames542361f2015-01-29 16:57:31 +00001112 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001113 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1114 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001115 } else if (!Primitive::IsFloatingPointType(type) &&
1116 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001117 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1118 } else {
1119 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001120 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1121 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 }
1123
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001124 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001125 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 return next_location;
1127}
1128
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001129Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001130 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001131}
1132
Serban Constantinescu579885a2015-02-22 20:51:33 +00001133CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1134 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001135 const CompilerOptions& compiler_options,
1136 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001137 : CodeGenerator(graph,
1138 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001139 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001140 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001141 callee_saved_core_registers.GetList(),
1142 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001143 compiler_options,
1144 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001145 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001146 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001147 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001148 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001149 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001150 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001151 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001152 uint32_literals_(std::less<uint32_t>(),
1153 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001154 uint64_literals_(std::less<uint64_t>(),
1155 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 method_patches_(MethodReferenceComparator(),
1157 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1158 call_patches_(MethodReferenceComparator(),
1159 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1160 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001161 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1162 boot_image_string_patches_(StringReferenceValueComparator(),
1163 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1164 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001165 boot_image_type_patches_(TypeReferenceValueComparator(),
1166 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1167 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001168 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00001169 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001170 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001171 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001172}
Alexandre Rames5319def2014-10-23 10:03:10 +01001173
Alexandre Rames67555f72014-11-18 10:55:16 +00001174#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001175
Zheng Xu3927c8b2015-11-18 17:46:25 +08001176void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001177 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001178 jump_table->EmitTable(this);
1179 }
1180}
1181
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001182void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001183 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001184 // Ensure we emit the literal pool.
1185 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001186
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001187 CodeGenerator::Finalize(allocator);
1188}
1189
Zheng Xuad4450e2015-04-17 18:48:56 +08001190void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1191 // Note: There are 6 kinds of moves:
1192 // 1. constant -> GPR/FPR (non-cycle)
1193 // 2. constant -> stack (non-cycle)
1194 // 3. GPR/FPR -> GPR/FPR
1195 // 4. GPR/FPR -> stack
1196 // 5. stack -> GPR/FPR
1197 // 6. stack -> stack (non-cycle)
1198 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1199 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1200 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1201 // dependency.
1202 vixl_temps_.Open(GetVIXLAssembler());
1203}
1204
1205void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1206 vixl_temps_.Close();
1207}
1208
1209Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1210 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1211 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1212 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1213 Location scratch = GetScratchLocation(kind);
1214 if (!scratch.Equals(Location::NoLocation())) {
1215 return scratch;
1216 }
1217 // Allocate from VIXL temp registers.
1218 if (kind == Location::kRegister) {
1219 scratch = LocationFrom(vixl_temps_.AcquireX());
1220 } else {
1221 DCHECK(kind == Location::kFpuRegister);
1222 scratch = LocationFrom(vixl_temps_.AcquireD());
1223 }
1224 AddScratchLocation(scratch);
1225 return scratch;
1226}
1227
1228void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1229 if (loc.IsRegister()) {
1230 vixl_temps_.Release(XRegisterFrom(loc));
1231 } else {
1232 DCHECK(loc.IsFpuRegister());
1233 vixl_temps_.Release(DRegisterFrom(loc));
1234 }
1235 RemoveScratchLocation(loc);
1236}
1237
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001239 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001240 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001241}
1242
Alexandre Rames5319def2014-10-23 10:03:10 +01001243void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001244 MacroAssembler* masm = GetVIXLAssembler();
1245 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001246 __ Bind(&frame_entry_label_);
1247
Serban Constantinescu02164b32014-11-13 14:05:07 +00001248 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1249 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001250 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001251 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001252 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001253 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001254 __ Ldr(wzr, MemOperand(temp, 0));
1255 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001256 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001257
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001258 if (!HasEmptyFrame()) {
1259 int frame_size = GetFrameSize();
1260 // Stack layout:
1261 // sp[frame_size - 8] : lr.
1262 // ... : other preserved core registers.
1263 // ... : other preserved fp registers.
1264 // ... : reserved frame space.
1265 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001266
1267 // Save the current method if we need it. Note that we do not
1268 // do this in HCurrentMethod, as the instruction might have been removed
1269 // in the SSA graph.
1270 if (RequiresCurrentMethod()) {
1271 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001272 } else {
1273 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001274 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001275 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001276 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1277 frame_size - GetCoreSpillSize());
1278 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1279 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001280 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001281}
1282
1283void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001284 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001285 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001286 if (!HasEmptyFrame()) {
1287 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001288 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1289 frame_size - FrameEntrySpillSize());
1290 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1291 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001292 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001293 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001294 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001295 __ Ret();
1296 GetAssembler()->cfi().RestoreState();
1297 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001298}
1299
Scott Wakeling97c72b72016-06-24 16:19:36 +01001300CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001301 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001302 return CPURegList(CPURegister::kRegister, kXRegSize,
1303 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001304}
1305
Scott Wakeling97c72b72016-06-24 16:19:36 +01001306CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001307 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1308 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001309 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1310 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001311}
1312
Alexandre Rames5319def2014-10-23 10:03:10 +01001313void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1314 __ Bind(GetLabelOf(block));
1315}
1316
Calin Juravle175dc732015-08-25 15:42:32 +01001317void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1318 DCHECK(location.IsRegister());
1319 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1320}
1321
Calin Juravlee460d1d2015-09-29 04:52:17 +01001322void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1323 if (location.IsRegister()) {
1324 locations->AddTemp(location);
1325 } else {
1326 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1327 }
1328}
1329
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001330void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001331 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001332 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001333 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001334 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001335 if (value_can_be_null) {
1336 __ Cbz(value, &done);
1337 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001338 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001339 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001340 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001341 if (value_can_be_null) {
1342 __ Bind(&done);
1343 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001344}
1345
David Brazdil58282f42016-01-14 12:45:10 +00001346void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001347 // Blocked core registers:
1348 // lr : Runtime reserved.
1349 // tr : Runtime reserved.
1350 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1351 // ip1 : VIXL core temp.
1352 // ip0 : VIXL core temp.
1353 //
1354 // Blocked fp registers:
1355 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001356 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1357 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001358 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001359 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001361
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001362 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001363 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001364 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001365 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001366
David Brazdil58282f42016-01-14 12:45:10 +00001367 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001368 // Stubs do not save callee-save floating point registers. If the graph
1369 // is debuggable, we need to deal with these registers differently. For
1370 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001371 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1372 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001373 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001374 }
1375 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001376}
1377
Alexandre Rames3e69f162014-12-10 10:36:50 +00001378size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1379 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1380 __ Str(reg, MemOperand(sp, stack_index));
1381 return kArm64WordSize;
1382}
1383
1384size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1385 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1386 __ Ldr(reg, MemOperand(sp, stack_index));
1387 return kArm64WordSize;
1388}
1389
1390size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1391 FPRegister reg = FPRegister(reg_id, kDRegSize);
1392 __ Str(reg, MemOperand(sp, stack_index));
1393 return kArm64WordSize;
1394}
1395
1396size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1397 FPRegister reg = FPRegister(reg_id, kDRegSize);
1398 __ Ldr(reg, MemOperand(sp, stack_index));
1399 return kArm64WordSize;
1400}
1401
Alexandre Rames5319def2014-10-23 10:03:10 +01001402void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001403 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001404}
1405
1406void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001407 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001408}
1409
Alexandre Rames67555f72014-11-18 10:55:16 +00001410void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001411 if (constant->IsIntConstant()) {
1412 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1413 } else if (constant->IsLongConstant()) {
1414 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1415 } else if (constant->IsNullConstant()) {
1416 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001417 } else if (constant->IsFloatConstant()) {
1418 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1419 } else {
1420 DCHECK(constant->IsDoubleConstant());
1421 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1422 }
1423}
1424
Alexandre Rames3e69f162014-12-10 10:36:50 +00001425
1426static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1427 DCHECK(constant.IsConstant());
1428 HConstant* cst = constant.GetConstant();
1429 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001430 // Null is mapped to a core W register, which we associate with kPrimInt.
1431 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001432 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1433 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1434 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1435}
1436
Calin Juravlee460d1d2015-09-29 04:52:17 +01001437void CodeGeneratorARM64::MoveLocation(Location destination,
1438 Location source,
1439 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001440 if (source.Equals(destination)) {
1441 return;
1442 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443
1444 // A valid move can always be inferred from the destination and source
1445 // locations. When moving from and to a register, the argument type can be
1446 // used to generate 32bit instead of 64bit moves. In debug mode we also
1447 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001448 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001449
1450 if (destination.IsRegister() || destination.IsFpuRegister()) {
1451 if (unspecified_type) {
1452 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1453 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001454 (src_cst != nullptr && (src_cst->IsIntConstant()
1455 || src_cst->IsFloatConstant()
1456 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001457 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001458 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001459 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001460 // If the source is a double stack slot or a 64bit constant, a 64bit
1461 // type is appropriate. Else the source is a register, and since the
1462 // type has not been specified, we chose a 64bit type to force a 64bit
1463 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001464 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001465 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001466 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001467 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1468 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1469 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001470 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1471 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1472 __ Ldr(dst, StackOperandFrom(source));
1473 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001474 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001475 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001476 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001477 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001478 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001479 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001480 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001481 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1482 ? Primitive::kPrimLong
1483 : Primitive::kPrimInt;
1484 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1485 }
1486 } else {
1487 DCHECK(source.IsFpuRegister());
1488 if (destination.IsRegister()) {
1489 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1490 ? Primitive::kPrimDouble
1491 : Primitive::kPrimFloat;
1492 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1493 } else {
1494 DCHECK(destination.IsFpuRegister());
1495 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496 }
1497 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498 } else { // The destination is not a register. It must be a stack slot.
1499 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1500 if (source.IsRegister() || source.IsFpuRegister()) {
1501 if (unspecified_type) {
1502 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001503 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001504 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001505 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506 }
1507 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001508 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1509 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1510 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001511 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001512 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1513 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001514 UseScratchRegisterScope temps(GetVIXLAssembler());
1515 HConstant* src_cst = source.GetConstant();
1516 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001517 if (src_cst->IsZeroBitPattern()) {
1518 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001519 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001520 if (src_cst->IsIntConstant()) {
1521 temp = temps.AcquireW();
1522 } else if (src_cst->IsLongConstant()) {
1523 temp = temps.AcquireX();
1524 } else if (src_cst->IsFloatConstant()) {
1525 temp = temps.AcquireS();
1526 } else {
1527 DCHECK(src_cst->IsDoubleConstant());
1528 temp = temps.AcquireD();
1529 }
1530 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001532 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001533 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001534 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001535 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001536 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537 // There is generally less pressure on FP registers.
1538 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001539 __ Ldr(temp, StackOperandFrom(source));
1540 __ Str(temp, StackOperandFrom(destination));
1541 }
1542 }
1543}
1544
1545void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001546 CPURegister dst,
1547 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001548 switch (type) {
1549 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001550 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001551 break;
1552 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001553 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001554 break;
1555 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001556 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557 break;
1558 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001559 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001560 break;
1561 case Primitive::kPrimInt:
1562 case Primitive::kPrimNot:
1563 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001564 case Primitive::kPrimFloat:
1565 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001566 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001567 __ Ldr(dst, src);
1568 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001569 case Primitive::kPrimVoid:
1570 LOG(FATAL) << "Unreachable type " << type;
1571 }
1572}
1573
Calin Juravle77520bc2015-01-12 18:45:46 +00001574void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001575 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001576 const MemOperand& src,
1577 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001578 MacroAssembler* masm = GetVIXLAssembler();
1579 BlockPoolsScope block_pools(masm);
1580 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001581 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001582 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001583
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001584 DCHECK(!src.IsPreIndex());
1585 DCHECK(!src.IsPostIndex());
1586
1587 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001588 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001589 MemOperand base = MemOperand(temp_base);
1590 switch (type) {
1591 case Primitive::kPrimBoolean:
1592 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001593 if (needs_null_check) {
1594 MaybeRecordImplicitNullCheck(instruction);
1595 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001596 break;
1597 case Primitive::kPrimByte:
1598 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001599 if (needs_null_check) {
1600 MaybeRecordImplicitNullCheck(instruction);
1601 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001602 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1603 break;
1604 case Primitive::kPrimChar:
1605 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001606 if (needs_null_check) {
1607 MaybeRecordImplicitNullCheck(instruction);
1608 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001609 break;
1610 case Primitive::kPrimShort:
1611 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001612 if (needs_null_check) {
1613 MaybeRecordImplicitNullCheck(instruction);
1614 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001615 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1616 break;
1617 case Primitive::kPrimInt:
1618 case Primitive::kPrimNot:
1619 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001620 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001621 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001622 if (needs_null_check) {
1623 MaybeRecordImplicitNullCheck(instruction);
1624 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001625 break;
1626 case Primitive::kPrimFloat:
1627 case Primitive::kPrimDouble: {
1628 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001629 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001630
1631 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1632 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001633 if (needs_null_check) {
1634 MaybeRecordImplicitNullCheck(instruction);
1635 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001636 __ Fmov(FPRegister(dst), temp);
1637 break;
1638 }
1639 case Primitive::kPrimVoid:
1640 LOG(FATAL) << "Unreachable type " << type;
1641 }
1642}
1643
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001644void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001645 CPURegister src,
1646 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 switch (type) {
1648 case Primitive::kPrimBoolean:
1649 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001650 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001651 break;
1652 case Primitive::kPrimChar:
1653 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001655 break;
1656 case Primitive::kPrimInt:
1657 case Primitive::kPrimNot:
1658 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001659 case Primitive::kPrimFloat:
1660 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001661 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001662 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001663 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001664 case Primitive::kPrimVoid:
1665 LOG(FATAL) << "Unreachable type " << type;
1666 }
1667}
1668
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001669void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1670 CPURegister src,
1671 const MemOperand& dst) {
1672 UseScratchRegisterScope temps(GetVIXLAssembler());
1673 Register temp_base = temps.AcquireX();
1674
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001675 DCHECK(!dst.IsPreIndex());
1676 DCHECK(!dst.IsPostIndex());
1677
1678 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001679 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001680 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001681 MemOperand base = MemOperand(temp_base);
1682 switch (type) {
1683 case Primitive::kPrimBoolean:
1684 case Primitive::kPrimByte:
1685 __ Stlrb(Register(src), base);
1686 break;
1687 case Primitive::kPrimChar:
1688 case Primitive::kPrimShort:
1689 __ Stlrh(Register(src), base);
1690 break;
1691 case Primitive::kPrimInt:
1692 case Primitive::kPrimNot:
1693 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001694 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001695 __ Stlr(Register(src), base);
1696 break;
1697 case Primitive::kPrimFloat:
1698 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001699 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001700 Register temp_src;
1701 if (src.IsZero()) {
1702 // The zero register is used to avoid synthesizing zero constants.
1703 temp_src = Register(src);
1704 } else {
1705 DCHECK(src.IsFPRegister());
1706 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1707 __ Fmov(temp_src, FPRegister(src));
1708 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001709
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001710 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001711 break;
1712 }
1713 case Primitive::kPrimVoid:
1714 LOG(FATAL) << "Unreachable type " << type;
1715 }
1716}
1717
Calin Juravle175dc732015-08-25 15:42:32 +01001718void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1719 HInstruction* instruction,
1720 uint32_t dex_pc,
1721 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001722 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001723 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001724 if (EntrypointRequiresStackMap(entrypoint)) {
1725 RecordPcInfo(instruction, dex_pc, slow_path);
1726 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001727}
1728
Roland Levillaindec8f632016-07-22 17:10:06 +01001729void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1730 HInstruction* instruction,
1731 SlowPathCode* slow_path) {
1732 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001733 GenerateInvokeRuntime(entry_point_offset);
1734}
1735
1736void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001737 BlockPoolsScope block_pools(GetVIXLAssembler());
1738 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1739 __ Blr(lr);
1740}
1741
Alexandre Rames67555f72014-11-18 10:55:16 +00001742void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001743 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001744 UseScratchRegisterScope temps(GetVIXLAssembler());
1745 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001746 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1747
Serban Constantinescu02164b32014-11-13 14:05:07 +00001748 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001749 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1750 __ Add(temp, class_reg, status_offset);
1751 __ Ldar(temp, HeapOperand(temp));
1752 __ Cmp(temp, mirror::Class::kStatusInitialized);
1753 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001754 __ Bind(slow_path->GetExitLabel());
1755}
Alexandre Rames5319def2014-10-23 10:03:10 +01001756
Roland Levillain44015862016-01-22 11:47:17 +00001757void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001758 BarrierType type = BarrierAll;
1759
1760 switch (kind) {
1761 case MemBarrierKind::kAnyAny:
1762 case MemBarrierKind::kAnyStore: {
1763 type = BarrierAll;
1764 break;
1765 }
1766 case MemBarrierKind::kLoadAny: {
1767 type = BarrierReads;
1768 break;
1769 }
1770 case MemBarrierKind::kStoreStore: {
1771 type = BarrierWrites;
1772 break;
1773 }
1774 default:
1775 LOG(FATAL) << "Unexpected memory barrier " << kind;
1776 }
1777 __ Dmb(InnerShareable, type);
1778}
1779
Serban Constantinescu02164b32014-11-13 14:05:07 +00001780void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1781 HBasicBlock* successor) {
1782 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001783 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1784 if (slow_path == nullptr) {
1785 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1786 instruction->SetSlowPath(slow_path);
1787 codegen_->AddSlowPath(slow_path);
1788 if (successor != nullptr) {
1789 DCHECK(successor->IsLoopHeader());
1790 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1791 }
1792 } else {
1793 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1794 }
1795
Serban Constantinescu02164b32014-11-13 14:05:07 +00001796 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1797 Register temp = temps.AcquireW();
1798
Andreas Gampe542451c2016-07-26 09:02:02 -07001799 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001800 if (successor == nullptr) {
1801 __ Cbnz(temp, slow_path->GetEntryLabel());
1802 __ Bind(slow_path->GetReturnLabel());
1803 } else {
1804 __ Cbz(temp, codegen_->GetLabelOf(successor));
1805 __ B(slow_path->GetEntryLabel());
1806 // slow_path will return to GetLabelOf(successor).
1807 }
1808}
1809
Alexandre Rames5319def2014-10-23 10:03:10 +01001810InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1811 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001812 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001813 assembler_(codegen->GetAssembler()),
1814 codegen_(codegen) {}
1815
1816#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001817 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001818
1819#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1820
1821enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001822 // Using a base helps identify when we hit such breakpoints.
1823 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001824#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1825 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1826#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1827};
1828
1829#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001830 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001831 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1832 } \
1833 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1834 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1835 locations->SetOut(Location::Any()); \
1836 }
1837 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1838#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1839
1840#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001841#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001842
Alexandre Rames67555f72014-11-18 10:55:16 +00001843void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001844 DCHECK_EQ(instr->InputCount(), 2U);
1845 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1846 Primitive::Type type = instr->GetResultType();
1847 switch (type) {
1848 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001849 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001850 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001851 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001853 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001854
1855 case Primitive::kPrimFloat:
1856 case Primitive::kPrimDouble:
1857 locations->SetInAt(0, Location::RequiresFpuRegister());
1858 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001859 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001860 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001861
Alexandre Rames5319def2014-10-23 10:03:10 +01001862 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001863 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001864 }
1865}
1866
Alexandre Rames09a99962015-04-15 11:47:56 +01001867void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001868 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1869
1870 bool object_field_get_with_read_barrier =
1871 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001872 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001873 new (GetGraph()->GetArena()) LocationSummary(instruction,
1874 object_field_get_with_read_barrier ?
1875 LocationSummary::kCallOnSlowPath :
1876 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001877 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001878 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001879 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001880 locations->SetInAt(0, Location::RequiresRegister());
1881 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1882 locations->SetOut(Location::RequiresFpuRegister());
1883 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001884 // The output overlaps for an object field get when read barriers
1885 // are enabled: we do not want the load to overwrite the object's
1886 // location, as we need it to emit the read barrier.
1887 locations->SetOut(
1888 Location::RequiresRegister(),
1889 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001890 }
1891}
1892
1893void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1894 const FieldInfo& field_info) {
1895 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001896 LocationSummary* locations = instruction->GetLocations();
1897 Location base_loc = locations->InAt(0);
1898 Location out = locations->Out();
1899 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001900 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001901 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001902 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001903
Roland Levillain44015862016-01-22 11:47:17 +00001904 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1905 // Object FieldGet with Baker's read barrier case.
1906 MacroAssembler* masm = GetVIXLAssembler();
1907 UseScratchRegisterScope temps(masm);
1908 // /* HeapReference<Object> */ out = *(base + offset)
1909 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1910 Register temp = temps.AcquireW();
1911 // Note that potential implicit null checks are handled in this
1912 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1913 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1914 instruction,
1915 out,
1916 base,
1917 offset,
1918 temp,
1919 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001920 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001921 } else {
1922 // General case.
1923 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001924 // Note that a potential implicit null check is handled in this
1925 // CodeGeneratorARM64::LoadAcquire call.
1926 // NB: LoadAcquire will record the pc info if needed.
1927 codegen_->LoadAcquire(
1928 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001929 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001930 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001932 }
Roland Levillain44015862016-01-22 11:47:17 +00001933 if (field_type == Primitive::kPrimNot) {
1934 // If read barriers are enabled, emit read barriers other than
1935 // Baker's using a slow path (and also unpoison the loaded
1936 // reference, if heap poisoning is enabled).
1937 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1938 }
Roland Levillain4d027112015-07-01 15:41:14 +01001939 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001940}
1941
1942void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1943 LocationSummary* locations =
1944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1945 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001946 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1947 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1948 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001949 locations->SetInAt(1, Location::RequiresFpuRegister());
1950 } else {
1951 locations->SetInAt(1, Location::RequiresRegister());
1952 }
1953}
1954
1955void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001956 const FieldInfo& field_info,
1957 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001958 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001959 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001960
1961 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001962 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001963 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001964 Offset offset = field_info.GetFieldOffset();
1965 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001966
Roland Levillain4d027112015-07-01 15:41:14 +01001967 {
1968 // We use a block to end the scratch scope before the write barrier, thus
1969 // freeing the temporary registers so they can be used in `MarkGCCard`.
1970 UseScratchRegisterScope temps(GetVIXLAssembler());
1971
1972 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1973 DCHECK(value.IsW());
1974 Register temp = temps.AcquireW();
1975 __ Mov(temp, value.W());
1976 GetAssembler()->PoisonHeapReference(temp.W());
1977 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001978 }
Roland Levillain4d027112015-07-01 15:41:14 +01001979
1980 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001981 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001983 } else {
1984 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1985 codegen_->MaybeRecordImplicitNullCheck(instruction);
1986 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001987 }
1988
1989 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001990 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001991 }
1992}
1993
Alexandre Rames67555f72014-11-18 10:55:16 +00001994void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001995 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001996
1997 switch (type) {
1998 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001999 case Primitive::kPrimLong: {
2000 Register dst = OutputRegister(instr);
2001 Register lhs = InputRegisterAt(instr, 0);
2002 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002003 if (instr->IsAdd()) {
2004 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002005 } else if (instr->IsAnd()) {
2006 __ And(dst, lhs, rhs);
2007 } else if (instr->IsOr()) {
2008 __ Orr(dst, lhs, rhs);
2009 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002010 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002011 } else if (instr->IsRor()) {
2012 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002013 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002014 __ Ror(dst, lhs, shift);
2015 } else {
2016 // Ensure shift distance is in the same size register as the result. If
2017 // we are rotating a long and the shift comes in a w register originally,
2018 // we don't need to sxtw for use as an x since the shift distances are
2019 // all & reg_bits - 1.
2020 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2021 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002022 } else {
2023 DCHECK(instr->IsXor());
2024 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002025 }
2026 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002027 }
2028 case Primitive::kPrimFloat:
2029 case Primitive::kPrimDouble: {
2030 FPRegister dst = OutputFPRegister(instr);
2031 FPRegister lhs = InputFPRegisterAt(instr, 0);
2032 FPRegister rhs = InputFPRegisterAt(instr, 1);
2033 if (instr->IsAdd()) {
2034 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002035 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002036 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002037 } else {
2038 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002039 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002040 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002041 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002042 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002043 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002044 }
2045}
2046
Serban Constantinescu02164b32014-11-13 14:05:07 +00002047void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2048 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2049
2050 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2051 Primitive::Type type = instr->GetResultType();
2052 switch (type) {
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimLong: {
2055 locations->SetInAt(0, Location::RequiresRegister());
2056 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2057 locations->SetOut(Location::RequiresRegister());
2058 break;
2059 }
2060 default:
2061 LOG(FATAL) << "Unexpected shift type " << type;
2062 }
2063}
2064
2065void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2066 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2067
2068 Primitive::Type type = instr->GetType();
2069 switch (type) {
2070 case Primitive::kPrimInt:
2071 case Primitive::kPrimLong: {
2072 Register dst = OutputRegister(instr);
2073 Register lhs = InputRegisterAt(instr, 0);
2074 Operand rhs = InputOperandAt(instr, 1);
2075 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002076 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002077 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002078 if (instr->IsShl()) {
2079 __ Lsl(dst, lhs, shift_value);
2080 } else if (instr->IsShr()) {
2081 __ Asr(dst, lhs, shift_value);
2082 } else {
2083 __ Lsr(dst, lhs, shift_value);
2084 }
2085 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002086 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002087
2088 if (instr->IsShl()) {
2089 __ Lsl(dst, lhs, rhs_reg);
2090 } else if (instr->IsShr()) {
2091 __ Asr(dst, lhs, rhs_reg);
2092 } else {
2093 __ Lsr(dst, lhs, rhs_reg);
2094 }
2095 }
2096 break;
2097 }
2098 default:
2099 LOG(FATAL) << "Unexpected shift operation type " << type;
2100 }
2101}
2102
Alexandre Rames5319def2014-10-23 10:03:10 +01002103void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002104 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002105}
2106
2107void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002108 HandleBinaryOp(instruction);
2109}
2110
2111void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2112 HandleBinaryOp(instruction);
2113}
2114
2115void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2116 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002117}
2118
Artem Serov7fc63502016-02-09 17:15:29 +00002119void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002120 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2121 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2122 locations->SetInAt(0, Location::RequiresRegister());
2123 // There is no immediate variant of negated bitwise instructions in AArch64.
2124 locations->SetInAt(1, Location::RequiresRegister());
2125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2126}
2127
Artem Serov7fc63502016-02-09 17:15:29 +00002128void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002129 Register dst = OutputRegister(instr);
2130 Register lhs = InputRegisterAt(instr, 0);
2131 Register rhs = InputRegisterAt(instr, 1);
2132
2133 switch (instr->GetOpKind()) {
2134 case HInstruction::kAnd:
2135 __ Bic(dst, lhs, rhs);
2136 break;
2137 case HInstruction::kOr:
2138 __ Orn(dst, lhs, rhs);
2139 break;
2140 case HInstruction::kXor:
2141 __ Eon(dst, lhs, rhs);
2142 break;
2143 default:
2144 LOG(FATAL) << "Unreachable";
2145 }
2146}
2147
Alexandre Rames8626b742015-11-25 16:28:08 +00002148void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2149 HArm64DataProcWithShifterOp* instruction) {
2150 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2151 instruction->GetType() == Primitive::kPrimLong);
2152 LocationSummary* locations =
2153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2154 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2155 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2156 } else {
2157 locations->SetInAt(0, Location::RequiresRegister());
2158 }
2159 locations->SetInAt(1, Location::RequiresRegister());
2160 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2161}
2162
2163void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2164 HArm64DataProcWithShifterOp* instruction) {
2165 Primitive::Type type = instruction->GetType();
2166 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2167 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2168 Register out = OutputRegister(instruction);
2169 Register left;
2170 if (kind != HInstruction::kNeg) {
2171 left = InputRegisterAt(instruction, 0);
2172 }
2173 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2174 // shifter operand operation, the IR generating `right_reg` (input to the type
2175 // conversion) can have a different type from the current instruction's type,
2176 // so we manually indicate the type.
2177 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002178 int64_t shift_amount = instruction->GetShiftAmount() &
2179 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002180
2181 Operand right_operand(0);
2182
2183 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2184 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2185 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2186 } else {
2187 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2188 }
2189
2190 // Logical binary operations do not support extension operations in the
2191 // operand. Note that VIXL would still manage if it was passed by generating
2192 // the extension as a separate instruction.
2193 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2194 DCHECK(!right_operand.IsExtendedRegister() ||
2195 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2196 kind != HInstruction::kNeg));
2197 switch (kind) {
2198 case HInstruction::kAdd:
2199 __ Add(out, left, right_operand);
2200 break;
2201 case HInstruction::kAnd:
2202 __ And(out, left, right_operand);
2203 break;
2204 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002205 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002206 __ Neg(out, right_operand);
2207 break;
2208 case HInstruction::kOr:
2209 __ Orr(out, left, right_operand);
2210 break;
2211 case HInstruction::kSub:
2212 __ Sub(out, left, right_operand);
2213 break;
2214 case HInstruction::kXor:
2215 __ Eor(out, left, right_operand);
2216 break;
2217 default:
2218 LOG(FATAL) << "Unexpected operation kind: " << kind;
2219 UNREACHABLE();
2220 }
2221}
2222
Artem Serov328429f2016-07-06 16:23:04 +01002223void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002224 LocationSummary* locations =
2225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2226 locations->SetInAt(0, Location::RequiresRegister());
2227 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2228 locations->SetOut(Location::RequiresRegister());
2229}
2230
Roland Levillain19c54192016-11-04 13:44:09 +00002231void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002232 __ Add(OutputRegister(instruction),
2233 InputRegisterAt(instruction, 0),
2234 Operand(InputOperandAt(instruction, 1)));
2235}
2236
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002237void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002238 LocationSummary* locations =
2239 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002240 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2241 if (instr->GetOpKind() == HInstruction::kSub &&
2242 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002243 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002244 // Don't allocate register for Mneg instruction.
2245 } else {
2246 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2247 Location::RequiresRegister());
2248 }
2249 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2250 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2252}
2253
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002254void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002255 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002256 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2257 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002258
2259 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2260 // This fixup should be carried out for all multiply-accumulate instructions:
2261 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2262 if (instr->GetType() == Primitive::kPrimLong &&
2263 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2264 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002265 vixl::aarch64::Instruction* prev =
2266 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002267 if (prev->IsLoadOrStore()) {
2268 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002269 vixl::aarch64::CodeBufferCheckScope scope(masm,
2270 kInstructionSize,
2271 vixl::aarch64::CodeBufferCheckScope::kCheck,
2272 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002273 __ nop();
2274 }
2275 }
2276
2277 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002278 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002279 __ Madd(res, mul_left, mul_right, accumulator);
2280 } else {
2281 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002282 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002283 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002284 __ Mneg(res, mul_left, mul_right);
2285 } else {
2286 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2287 __ Msub(res, mul_left, mul_right, accumulator);
2288 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002289 }
2290}
2291
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002292void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002293 bool object_array_get_with_read_barrier =
2294 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002295 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002296 new (GetGraph()->GetArena()) LocationSummary(instruction,
2297 object_array_get_with_read_barrier ?
2298 LocationSummary::kCallOnSlowPath :
2299 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002300 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002301 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002302 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002303 locations->SetInAt(0, Location::RequiresRegister());
2304 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002305 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2307 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002308 // The output overlaps in the case of an object array get with
2309 // read barriers enabled: we do not want the move to overwrite the
2310 // array's location, as we need it to emit the read barrier.
2311 locations->SetOut(
2312 Location::RequiresRegister(),
2313 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002314 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002315}
2316
2317void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002318 Primitive::Type type = instruction->GetType();
2319 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002320 LocationSummary* locations = instruction->GetLocations();
2321 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002322 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002323 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002324 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2325 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002326 MacroAssembler* masm = GetVIXLAssembler();
2327 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002328 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002329 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002330
Roland Levillain19c54192016-11-04 13:44:09 +00002331 // The read barrier instrumentation of object ArrayGet instructions
2332 // does not support the HIntermediateAddress instruction.
2333 DCHECK(!((type == Primitive::kPrimNot) &&
2334 instruction->GetArray()->IsIntermediateAddress() &&
2335 kEmitCompilerReadBarrier));
2336
Roland Levillain44015862016-01-22 11:47:17 +00002337 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2338 // Object ArrayGet with Baker's read barrier case.
2339 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002340 // Note that a potential implicit null check is handled in the
2341 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2342 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2343 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002344 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002345 // General case.
2346 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002347 Register length;
2348 if (maybe_compressed_char_at) {
2349 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2350 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002351 if (instruction->GetArray()->IsIntermediateAddress()) {
2352 DCHECK_LT(count_offset, offset);
2353 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2354 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2355 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2356 } else {
2357 __ Ldr(length, HeapOperand(obj, count_offset));
2358 }
jessicahandojo05765752016-09-09 19:01:32 -07002359 codegen_->MaybeRecordImplicitNullCheck(instruction);
2360 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002361 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002362 if (maybe_compressed_char_at) {
2363 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002364 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2365 "Expecting 0=compressed, 1=uncompressed");
2366 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002367 __ Ldrb(Register(OutputCPURegister(instruction)),
2368 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2369 __ B(&done);
2370 __ Bind(&uncompressed_load);
2371 __ Ldrh(Register(OutputCPURegister(instruction)),
2372 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2373 __ Bind(&done);
2374 } else {
2375 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2376 source = HeapOperand(obj, offset);
2377 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002378 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002379 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002380 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002381 // We do not need to compute the intermediate address from the array: the
2382 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002383 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002384 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002385 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002386 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2387 }
2388 temp = obj;
2389 } else {
2390 __ Add(temp, obj, offset);
2391 }
jessicahandojo05765752016-09-09 19:01:32 -07002392 if (maybe_compressed_char_at) {
2393 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002394 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2395 "Expecting 0=compressed, 1=uncompressed");
2396 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002397 __ Ldrb(Register(OutputCPURegister(instruction)),
2398 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2399 __ B(&done);
2400 __ Bind(&uncompressed_load);
2401 __ Ldrh(Register(OutputCPURegister(instruction)),
2402 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2403 __ Bind(&done);
2404 } else {
2405 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2406 }
Roland Levillain44015862016-01-22 11:47:17 +00002407 }
jessicahandojo05765752016-09-09 19:01:32 -07002408 if (!maybe_compressed_char_at) {
2409 codegen_->Load(type, OutputCPURegister(instruction), source);
2410 codegen_->MaybeRecordImplicitNullCheck(instruction);
2411 }
Roland Levillain44015862016-01-22 11:47:17 +00002412
2413 if (type == Primitive::kPrimNot) {
2414 static_assert(
2415 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2416 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2417 Location obj_loc = locations->InAt(0);
2418 if (index.IsConstant()) {
2419 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2420 } else {
2421 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2422 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002423 }
Roland Levillain4d027112015-07-01 15:41:14 +01002424 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002425}
2426
Alexandre Rames5319def2014-10-23 10:03:10 +01002427void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2428 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2429 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002431}
2432
2433void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002434 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002435 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002436 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002437 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002438 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002439 // Mask out compression flag from String's array length.
2440 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002441 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002442 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002443}
2444
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002445void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002446 Primitive::Type value_type = instruction->GetComponentType();
2447
2448 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2450 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002451 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002452 LocationSummary::kCallOnSlowPath :
2453 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002454 locations->SetInAt(0, Location::RequiresRegister());
2455 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002456 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2457 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2458 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002459 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002460 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002461 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002462 }
2463}
2464
2465void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2466 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002467 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002468 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002469 bool needs_write_barrier =
2470 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002471
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002472 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002473 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002474 CPURegister source = value;
2475 Location index = locations->InAt(1);
2476 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2477 MemOperand destination = HeapOperand(array);
2478 MacroAssembler* masm = GetVIXLAssembler();
2479 BlockPoolsScope block_pools(masm);
2480
2481 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002482 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002483 if (index.IsConstant()) {
2484 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2485 destination = HeapOperand(array, offset);
2486 } else {
2487 UseScratchRegisterScope temps(masm);
2488 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002489 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002490 // We do not need to compute the intermediate address from the array: the
2491 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002492 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002493 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002494 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002495 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2496 }
2497 temp = array;
2498 } else {
2499 __ Add(temp, array, offset);
2500 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002501 destination = HeapOperand(temp,
2502 XRegisterFrom(index),
2503 LSL,
2504 Primitive::ComponentSizeShift(value_type));
2505 }
2506 codegen_->Store(value_type, value, destination);
2507 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002508 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002509 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002510 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002511 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002512 {
2513 // We use a block to end the scratch scope before the write barrier, thus
2514 // freeing the temporary registers so they can be used in `MarkGCCard`.
2515 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002516 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002517 if (index.IsConstant()) {
2518 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002519 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002520 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002521 destination = HeapOperand(temp,
2522 XRegisterFrom(index),
2523 LSL,
2524 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002525 }
2526
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002527 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2528 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2529 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2530
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002531 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002532 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2533 codegen_->AddSlowPath(slow_path);
2534 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002535 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002536 __ Cbnz(Register(value), &non_zero);
2537 if (!index.IsConstant()) {
2538 __ Add(temp, array, offset);
2539 }
2540 __ Str(wzr, destination);
2541 codegen_->MaybeRecordImplicitNullCheck(instruction);
2542 __ B(&done);
2543 __ Bind(&non_zero);
2544 }
2545
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002546 // Note that when Baker read barriers are enabled, the type
2547 // checks are performed without read barriers. This is fine,
2548 // even in the case where a class object is in the from-space
2549 // after the flip, as a comparison involving such a type would
2550 // not produce a false positive; it may of course produce a
2551 // false negative, in which case we would take the ArraySet
2552 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002553
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002554 Register temp2 = temps.AcquireSameSizeAs(array);
2555 // /* HeapReference<Class> */ temp = array->klass_
2556 __ Ldr(temp, HeapOperand(array, class_offset));
2557 codegen_->MaybeRecordImplicitNullCheck(instruction);
2558 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002559
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002560 // /* HeapReference<Class> */ temp = temp->component_type_
2561 __ Ldr(temp, HeapOperand(temp, component_offset));
2562 // /* HeapReference<Class> */ temp2 = value->klass_
2563 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2564 // If heap poisoning is enabled, no need to unpoison `temp`
2565 // nor `temp2`, as we are comparing two poisoned references.
2566 __ Cmp(temp, temp2);
2567 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002568
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002569 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2570 vixl::aarch64::Label do_put;
2571 __ B(eq, &do_put);
2572 // If heap poisoning is enabled, the `temp` reference has
2573 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002574 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2575
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002576 // /* HeapReference<Class> */ temp = temp->super_class_
2577 __ Ldr(temp, HeapOperand(temp, super_offset));
2578 // If heap poisoning is enabled, no need to unpoison
2579 // `temp`, as we are comparing against null below.
2580 __ Cbnz(temp, slow_path->GetEntryLabel());
2581 __ Bind(&do_put);
2582 } else {
2583 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002584 }
2585 }
2586
2587 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002588 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002589 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002590 __ Mov(temp2, value.W());
2591 GetAssembler()->PoisonHeapReference(temp2);
2592 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002593 }
2594
2595 if (!index.IsConstant()) {
2596 __ Add(temp, array, offset);
2597 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002598 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002599
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002600 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002601 codegen_->MaybeRecordImplicitNullCheck(instruction);
2602 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002603 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002604
2605 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2606
2607 if (done.IsLinked()) {
2608 __ Bind(&done);
2609 }
2610
2611 if (slow_path != nullptr) {
2612 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002613 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002614 }
2615}
2616
Alexandre Rames67555f72014-11-18 10:55:16 +00002617void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002618 RegisterSet caller_saves = RegisterSet::Empty();
2619 InvokeRuntimeCallingConvention calling_convention;
2620 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2621 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2622 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002623 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002624 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002625}
2626
2627void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002628 BoundsCheckSlowPathARM64* slow_path =
2629 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002630 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002631 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2632 __ B(slow_path->GetEntryLabel(), hs);
2633}
2634
Alexandre Rames67555f72014-11-18 10:55:16 +00002635void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2636 LocationSummary* locations =
2637 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2638 locations->SetInAt(0, Location::RequiresRegister());
2639 if (check->HasUses()) {
2640 locations->SetOut(Location::SameAsFirstInput());
2641 }
2642}
2643
2644void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2645 // We assume the class is not null.
2646 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2647 check->GetLoadClass(), check, check->GetDexPc(), true);
2648 codegen_->AddSlowPath(slow_path);
2649 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2650}
2651
Roland Levillain1a653882016-03-18 18:05:57 +00002652static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2653 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2654 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2655}
2656
2657void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2658 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2659 Location rhs_loc = instruction->GetLocations()->InAt(1);
2660 if (rhs_loc.IsConstant()) {
2661 // 0.0 is the only immediate that can be encoded directly in
2662 // an FCMP instruction.
2663 //
2664 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2665 // specify that in a floating-point comparison, positive zero
2666 // and negative zero are considered equal, so we can use the
2667 // literal 0.0 for both cases here.
2668 //
2669 // Note however that some methods (Float.equal, Float.compare,
2670 // Float.compareTo, Double.equal, Double.compare,
2671 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2672 // StrictMath.min) consider 0.0 to be (strictly) greater than
2673 // -0.0. So if we ever translate calls to these methods into a
2674 // HCompare instruction, we must handle the -0.0 case with
2675 // care here.
2676 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2677 __ Fcmp(lhs_reg, 0.0);
2678 } else {
2679 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2680 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002681}
2682
Serban Constantinescu02164b32014-11-13 14:05:07 +00002683void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002684 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002685 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2686 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002687 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002688 case Primitive::kPrimBoolean:
2689 case Primitive::kPrimByte:
2690 case Primitive::kPrimShort:
2691 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002692 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002693 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002694 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002695 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2697 break;
2698 }
2699 case Primitive::kPrimFloat:
2700 case Primitive::kPrimDouble: {
2701 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002702 locations->SetInAt(1,
2703 IsFloatingPointZeroConstant(compare->InputAt(1))
2704 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2705 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002706 locations->SetOut(Location::RequiresRegister());
2707 break;
2708 }
2709 default:
2710 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2711 }
2712}
2713
2714void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2715 Primitive::Type in_type = compare->InputAt(0)->GetType();
2716
2717 // 0 if: left == right
2718 // 1 if: left > right
2719 // -1 if: left < right
2720 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002721 case Primitive::kPrimBoolean:
2722 case Primitive::kPrimByte:
2723 case Primitive::kPrimShort:
2724 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002725 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002726 case Primitive::kPrimLong: {
2727 Register result = OutputRegister(compare);
2728 Register left = InputRegisterAt(compare, 0);
2729 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002730 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002731 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2732 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002733 break;
2734 }
2735 case Primitive::kPrimFloat:
2736 case Primitive::kPrimDouble: {
2737 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002738 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002739 __ Cset(result, ne);
2740 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002741 break;
2742 }
2743 default:
2744 LOG(FATAL) << "Unimplemented compare type " << in_type;
2745 }
2746}
2747
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002748void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002750
2751 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2752 locations->SetInAt(0, Location::RequiresFpuRegister());
2753 locations->SetInAt(1,
2754 IsFloatingPointZeroConstant(instruction->InputAt(1))
2755 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2756 : Location::RequiresFpuRegister());
2757 } else {
2758 // Integer cases.
2759 locations->SetInAt(0, Location::RequiresRegister());
2760 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2761 }
2762
David Brazdilb3e773e2016-01-26 11:28:37 +00002763 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002765 }
2766}
2767
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002768void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002769 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002770 return;
2771 }
2772
2773 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002774 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002775 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002776
Roland Levillain7f63c522015-07-13 15:54:55 +00002777 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002778 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002779 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002780 } else {
2781 // Integer cases.
2782 Register lhs = InputRegisterAt(instruction, 0);
2783 Operand rhs = InputOperandAt(instruction, 1);
2784 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002785 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002786 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002787}
2788
2789#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2790 M(Equal) \
2791 M(NotEqual) \
2792 M(LessThan) \
2793 M(LessThanOrEqual) \
2794 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002795 M(GreaterThanOrEqual) \
2796 M(Below) \
2797 M(BelowOrEqual) \
2798 M(Above) \
2799 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002800#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002801void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2802void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002803FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002804#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002805#undef FOR_EACH_CONDITION_INSTRUCTION
2806
Zheng Xuc6667102015-05-15 16:08:45 +08002807void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2808 DCHECK(instruction->IsDiv() || instruction->IsRem());
2809
2810 LocationSummary* locations = instruction->GetLocations();
2811 Location second = locations->InAt(1);
2812 DCHECK(second.IsConstant());
2813
2814 Register out = OutputRegister(instruction);
2815 Register dividend = InputRegisterAt(instruction, 0);
2816 int64_t imm = Int64FromConstant(second.GetConstant());
2817 DCHECK(imm == 1 || imm == -1);
2818
2819 if (instruction->IsRem()) {
2820 __ Mov(out, 0);
2821 } else {
2822 if (imm == 1) {
2823 __ Mov(out, dividend);
2824 } else {
2825 __ Neg(out, dividend);
2826 }
2827 }
2828}
2829
2830void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2831 DCHECK(instruction->IsDiv() || instruction->IsRem());
2832
2833 LocationSummary* locations = instruction->GetLocations();
2834 Location second = locations->InAt(1);
2835 DCHECK(second.IsConstant());
2836
2837 Register out = OutputRegister(instruction);
2838 Register dividend = InputRegisterAt(instruction, 0);
2839 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002840 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002841 int ctz_imm = CTZ(abs_imm);
2842
2843 UseScratchRegisterScope temps(GetVIXLAssembler());
2844 Register temp = temps.AcquireSameSizeAs(out);
2845
2846 if (instruction->IsDiv()) {
2847 __ Add(temp, dividend, abs_imm - 1);
2848 __ Cmp(dividend, 0);
2849 __ Csel(out, temp, dividend, lt);
2850 if (imm > 0) {
2851 __ Asr(out, out, ctz_imm);
2852 } else {
2853 __ Neg(out, Operand(out, ASR, ctz_imm));
2854 }
2855 } else {
2856 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2857 __ Asr(temp, dividend, bits - 1);
2858 __ Lsr(temp, temp, bits - ctz_imm);
2859 __ Add(out, dividend, temp);
2860 __ And(out, out, abs_imm - 1);
2861 __ Sub(out, out, temp);
2862 }
2863}
2864
2865void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2866 DCHECK(instruction->IsDiv() || instruction->IsRem());
2867
2868 LocationSummary* locations = instruction->GetLocations();
2869 Location second = locations->InAt(1);
2870 DCHECK(second.IsConstant());
2871
2872 Register out = OutputRegister(instruction);
2873 Register dividend = InputRegisterAt(instruction, 0);
2874 int64_t imm = Int64FromConstant(second.GetConstant());
2875
2876 Primitive::Type type = instruction->GetResultType();
2877 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2878
2879 int64_t magic;
2880 int shift;
2881 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2882
2883 UseScratchRegisterScope temps(GetVIXLAssembler());
2884 Register temp = temps.AcquireSameSizeAs(out);
2885
2886 // temp = get_high(dividend * magic)
2887 __ Mov(temp, magic);
2888 if (type == Primitive::kPrimLong) {
2889 __ Smulh(temp, dividend, temp);
2890 } else {
2891 __ Smull(temp.X(), dividend, temp);
2892 __ Lsr(temp.X(), temp.X(), 32);
2893 }
2894
2895 if (imm > 0 && magic < 0) {
2896 __ Add(temp, temp, dividend);
2897 } else if (imm < 0 && magic > 0) {
2898 __ Sub(temp, temp, dividend);
2899 }
2900
2901 if (shift != 0) {
2902 __ Asr(temp, temp, shift);
2903 }
2904
2905 if (instruction->IsDiv()) {
2906 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2907 } else {
2908 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2909 // TODO: Strength reduction for msub.
2910 Register temp_imm = temps.AcquireSameSizeAs(out);
2911 __ Mov(temp_imm, imm);
2912 __ Msub(out, temp, temp_imm, dividend);
2913 }
2914}
2915
2916void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2917 DCHECK(instruction->IsDiv() || instruction->IsRem());
2918 Primitive::Type type = instruction->GetResultType();
2919 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2920
2921 LocationSummary* locations = instruction->GetLocations();
2922 Register out = OutputRegister(instruction);
2923 Location second = locations->InAt(1);
2924
2925 if (second.IsConstant()) {
2926 int64_t imm = Int64FromConstant(second.GetConstant());
2927
2928 if (imm == 0) {
2929 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2930 } else if (imm == 1 || imm == -1) {
2931 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002932 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002933 DivRemByPowerOfTwo(instruction);
2934 } else {
2935 DCHECK(imm <= -2 || imm >= 2);
2936 GenerateDivRemWithAnyConstant(instruction);
2937 }
2938 } else {
2939 Register dividend = InputRegisterAt(instruction, 0);
2940 Register divisor = InputRegisterAt(instruction, 1);
2941 if (instruction->IsDiv()) {
2942 __ Sdiv(out, dividend, divisor);
2943 } else {
2944 UseScratchRegisterScope temps(GetVIXLAssembler());
2945 Register temp = temps.AcquireSameSizeAs(out);
2946 __ Sdiv(temp, dividend, divisor);
2947 __ Msub(out, temp, divisor, dividend);
2948 }
2949 }
2950}
2951
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002952void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2953 LocationSummary* locations =
2954 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2955 switch (div->GetResultType()) {
2956 case Primitive::kPrimInt:
2957 case Primitive::kPrimLong:
2958 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002959 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2961 break;
2962
2963 case Primitive::kPrimFloat:
2964 case Primitive::kPrimDouble:
2965 locations->SetInAt(0, Location::RequiresFpuRegister());
2966 locations->SetInAt(1, Location::RequiresFpuRegister());
2967 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2968 break;
2969
2970 default:
2971 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2972 }
2973}
2974
2975void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2976 Primitive::Type type = div->GetResultType();
2977 switch (type) {
2978 case Primitive::kPrimInt:
2979 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002980 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002981 break;
2982
2983 case Primitive::kPrimFloat:
2984 case Primitive::kPrimDouble:
2985 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2986 break;
2987
2988 default:
2989 LOG(FATAL) << "Unexpected div type " << type;
2990 }
2991}
2992
Alexandre Rames67555f72014-11-18 10:55:16 +00002993void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002994 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002995 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002996}
2997
2998void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2999 SlowPathCodeARM64* slow_path =
3000 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3001 codegen_->AddSlowPath(slow_path);
3002 Location value = instruction->GetLocations()->InAt(0);
3003
Alexandre Rames3e69f162014-12-10 10:36:50 +00003004 Primitive::Type type = instruction->GetType();
3005
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003006 if (!Primitive::IsIntegralType(type)) {
3007 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003008 return;
3009 }
3010
Alexandre Rames67555f72014-11-18 10:55:16 +00003011 if (value.IsConstant()) {
3012 int64_t divisor = Int64ConstantFrom(value);
3013 if (divisor == 0) {
3014 __ B(slow_path->GetEntryLabel());
3015 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003016 // A division by a non-null constant is valid. We don't need to perform
3017 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003018 }
3019 } else {
3020 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3021 }
3022}
3023
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003024void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3025 LocationSummary* locations =
3026 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3027 locations->SetOut(Location::ConstantLocation(constant));
3028}
3029
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003030void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3031 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003032 // Will be generated at use site.
3033}
3034
Alexandre Rames5319def2014-10-23 10:03:10 +01003035void LocationsBuilderARM64::VisitExit(HExit* exit) {
3036 exit->SetLocations(nullptr);
3037}
3038
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003039void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003040}
3041
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003042void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3043 LocationSummary* locations =
3044 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3045 locations->SetOut(Location::ConstantLocation(constant));
3046}
3047
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003048void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003049 // Will be generated at use site.
3050}
3051
David Brazdilfc6a86a2015-06-26 10:33:45 +00003052void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003053 DCHECK(!successor->IsExitBlock());
3054 HBasicBlock* block = got->GetBlock();
3055 HInstruction* previous = got->GetPrevious();
3056 HLoopInformation* info = block->GetLoopInformation();
3057
David Brazdil46e2a392015-03-16 17:31:52 +00003058 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003059 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3060 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3061 return;
3062 }
3063 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3064 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3065 }
3066 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003067 __ B(codegen_->GetLabelOf(successor));
3068 }
3069}
3070
David Brazdilfc6a86a2015-06-26 10:33:45 +00003071void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3072 got->SetLocations(nullptr);
3073}
3074
3075void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3076 HandleGoto(got, got->GetSuccessor());
3077}
3078
3079void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3080 try_boundary->SetLocations(nullptr);
3081}
3082
3083void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3084 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3085 if (!successor->IsExitBlock()) {
3086 HandleGoto(try_boundary, successor);
3087 }
3088}
3089
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003090void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003091 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003092 vixl::aarch64::Label* true_target,
3093 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003094 // FP branching requires both targets to be explicit. If either of the targets
3095 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003096 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003097 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003098
David Brazdil0debae72015-11-12 18:37:00 +00003099 if (true_target == nullptr && false_target == nullptr) {
3100 // Nothing to do. The code always falls through.
3101 return;
3102 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003103 // Constant condition, statically compared against "true" (integer value 1).
3104 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003105 if (true_target != nullptr) {
3106 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003107 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003108 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003109 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003110 if (false_target != nullptr) {
3111 __ B(false_target);
3112 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003113 }
David Brazdil0debae72015-11-12 18:37:00 +00003114 return;
3115 }
3116
3117 // The following code generates these patterns:
3118 // (1) true_target == nullptr && false_target != nullptr
3119 // - opposite condition true => branch to false_target
3120 // (2) true_target != nullptr && false_target == nullptr
3121 // - condition true => branch to true_target
3122 // (3) true_target != nullptr && false_target != nullptr
3123 // - condition true => branch to true_target
3124 // - branch to false_target
3125 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003126 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003127 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003128 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003129 if (true_target == nullptr) {
3130 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3131 } else {
3132 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3133 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003134 } else {
3135 // The condition instruction has not been materialized, use its inputs as
3136 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003137 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003138
David Brazdil0debae72015-11-12 18:37:00 +00003139 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003140 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003141 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003142 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003143 IfCondition opposite_condition = condition->GetOppositeCondition();
3144 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003145 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003146 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003147 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003148 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003149 // Integer cases.
3150 Register lhs = InputRegisterAt(condition, 0);
3151 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003152
3153 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003154 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003155 if (true_target == nullptr) {
3156 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3157 non_fallthrough_target = false_target;
3158 } else {
3159 arm64_cond = ARM64Condition(condition->GetCondition());
3160 non_fallthrough_target = true_target;
3161 }
3162
Aart Bik086d27e2016-01-20 17:02:00 -08003163 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003164 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003165 switch (arm64_cond) {
3166 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003167 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003168 break;
3169 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003170 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003171 break;
3172 case lt:
3173 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003174 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003175 break;
3176 case ge:
3177 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003178 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003179 break;
3180 default:
3181 // Without the `static_cast` the compiler throws an error for
3182 // `-Werror=sign-promo`.
3183 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3184 }
3185 } else {
3186 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003187 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003188 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003189 }
3190 }
David Brazdil0debae72015-11-12 18:37:00 +00003191
3192 // If neither branch falls through (case 3), the conditional branch to `true_target`
3193 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3194 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003195 __ B(false_target);
3196 }
David Brazdil0debae72015-11-12 18:37:00 +00003197
3198 if (fallthrough_target.IsLinked()) {
3199 __ Bind(&fallthrough_target);
3200 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003201}
3202
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003203void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3204 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003205 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003206 locations->SetInAt(0, Location::RequiresRegister());
3207 }
3208}
3209
3210void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003211 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3212 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003213 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3214 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3215 true_target = nullptr;
3216 }
3217 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3218 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3219 false_target = nullptr;
3220 }
David Brazdil0debae72015-11-12 18:37:00 +00003221 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003222}
3223
3224void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3225 LocationSummary* locations = new (GetGraph()->GetArena())
3226 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003227 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003228 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003229 locations->SetInAt(0, Location::RequiresRegister());
3230 }
3231}
3232
3233void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003234 SlowPathCodeARM64* slow_path =
3235 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003236 GenerateTestAndBranch(deoptimize,
3237 /* condition_input_index */ 0,
3238 slow_path->GetEntryLabel(),
3239 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003240}
3241
David Brazdilc0b601b2016-02-08 14:20:45 +00003242static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3243 return condition->IsCondition() &&
3244 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3245}
3246
Alexandre Rames880f1192016-06-13 16:04:50 +01003247static inline Condition GetConditionForSelect(HCondition* condition) {
3248 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003249 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3250 : ARM64Condition(cond);
3251}
3252
David Brazdil74eb1b22015-12-14 11:44:01 +00003253void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3254 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003255 if (Primitive::IsFloatingPointType(select->GetType())) {
3256 locations->SetInAt(0, Location::RequiresFpuRegister());
3257 locations->SetInAt(1, Location::RequiresFpuRegister());
3258 locations->SetOut(Location::RequiresFpuRegister());
3259 } else {
3260 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3261 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3262 bool is_true_value_constant = cst_true_value != nullptr;
3263 bool is_false_value_constant = cst_false_value != nullptr;
3264 // Ask VIXL whether we should synthesize constants in registers.
3265 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3266 Operand true_op = is_true_value_constant ?
3267 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3268 Operand false_op = is_false_value_constant ?
3269 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3270 bool true_value_in_register = false;
3271 bool false_value_in_register = false;
3272 MacroAssembler::GetCselSynthesisInformation(
3273 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3274 true_value_in_register |= !is_true_value_constant;
3275 false_value_in_register |= !is_false_value_constant;
3276
3277 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3278 : Location::ConstantLocation(cst_true_value));
3279 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3280 : Location::ConstantLocation(cst_false_value));
3281 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003282 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003283
David Brazdil74eb1b22015-12-14 11:44:01 +00003284 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3285 locations->SetInAt(2, Location::RequiresRegister());
3286 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003287}
3288
3289void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003290 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003291 Condition csel_cond;
3292
3293 if (IsBooleanValueOrMaterializedCondition(cond)) {
3294 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003295 // Use the condition flags set by the previous instruction.
3296 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003297 } else {
3298 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003299 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003300 }
3301 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003302 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003303 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003304 } else {
3305 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003306 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003307 }
3308
Alexandre Rames880f1192016-06-13 16:04:50 +01003309 if (Primitive::IsFloatingPointType(select->GetType())) {
3310 __ Fcsel(OutputFPRegister(select),
3311 InputFPRegisterAt(select, 1),
3312 InputFPRegisterAt(select, 0),
3313 csel_cond);
3314 } else {
3315 __ Csel(OutputRegister(select),
3316 InputOperandAt(select, 1),
3317 InputOperandAt(select, 0),
3318 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003319 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003320}
3321
David Srbecky0cf44932015-12-09 14:09:59 +00003322void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3323 new (GetGraph()->GetArena()) LocationSummary(info);
3324}
3325
David Srbeckyd28f4a02016-03-14 17:14:24 +00003326void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3327 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003328}
3329
3330void CodeGeneratorARM64::GenerateNop() {
3331 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003332}
3333
Alexandre Rames5319def2014-10-23 10:03:10 +01003334void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003335 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003336}
3337
3338void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003339 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003340}
3341
3342void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003343 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003344}
3345
3346void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003347 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003348}
3349
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003350// Temp is used for read barrier.
3351static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3352 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003353 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003354 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3355 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3356 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3357 return 1;
3358 }
3359 return 0;
3360}
3361
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003362// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003363// interface pointer, one for loading the current interface.
3364// The other checks have one temp for loading the object's class.
3365static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3366 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3367 return 3;
3368 }
3369 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003370}
3371
Alexandre Rames67555f72014-11-18 10:55:16 +00003372void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003373 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003374 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003375 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003377 case TypeCheckKind::kExactCheck:
3378 case TypeCheckKind::kAbstractClassCheck:
3379 case TypeCheckKind::kClassHierarchyCheck:
3380 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003381 call_kind =
3382 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003383 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003384 break;
3385 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386 case TypeCheckKind::kUnresolvedCheck:
3387 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003388 call_kind = LocationSummary::kCallOnSlowPath;
3389 break;
3390 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391
Alexandre Rames67555f72014-11-18 10:55:16 +00003392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003393 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003394 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003395 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396 locations->SetInAt(0, Location::RequiresRegister());
3397 locations->SetInAt(1, Location::RequiresRegister());
3398 // The "out" register is used as a temporary, so it overlaps with the inputs.
3399 // Note that TypeCheckSlowPathARM64 uses this register too.
3400 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003401 // Add temps if necessary for read barriers.
3402 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003403}
3404
3405void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003406 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003407 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003408 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003409 Register obj = InputRegisterAt(instruction, 0);
3410 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003411 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003412 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003413 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3414 DCHECK_LE(num_temps, 1u);
3415 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003416 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3417 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3418 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3419 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003420
Scott Wakeling97c72b72016-06-24 16:19:36 +01003421 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003422 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003423
3424 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003425 // Avoid null check if we know `obj` is not null.
3426 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003427 __ Cbz(obj, &zero);
3428 }
3429
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 // /* HeapReference<Class> */ out = obj->klass_
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003431 GenerateReferenceLoadTwoRegisters(instruction,
3432 out_loc,
3433 obj_loc,
3434 class_offset,
3435 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003436 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003437
Roland Levillain44015862016-01-22 11:47:17 +00003438 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003439 case TypeCheckKind::kExactCheck: {
3440 __ Cmp(out, cls);
3441 __ Cset(out, eq);
3442 if (zero.IsLinked()) {
3443 __ B(&done);
3444 }
3445 break;
3446 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003448 case TypeCheckKind::kAbstractClassCheck: {
3449 // If the class is abstract, we eagerly fetch the super class of the
3450 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003451 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003452 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003454 GenerateReferenceLoadOneRegister(instruction,
3455 out_loc,
3456 super_offset,
3457 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003458 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 // If `out` is null, we use it for the result, and jump to `done`.
3460 __ Cbz(out, &done);
3461 __ Cmp(out, cls);
3462 __ B(ne, &loop);
3463 __ Mov(out, 1);
3464 if (zero.IsLinked()) {
3465 __ B(&done);
3466 }
3467 break;
3468 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003470 case TypeCheckKind::kClassHierarchyCheck: {
3471 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003472 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003473 __ Bind(&loop);
3474 __ Cmp(out, cls);
3475 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003476 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003477 GenerateReferenceLoadOneRegister(instruction,
3478 out_loc,
3479 super_offset,
3480 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003481 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003482 __ Cbnz(out, &loop);
3483 // If `out` is null, we use it for the result, and jump to `done`.
3484 __ B(&done);
3485 __ Bind(&success);
3486 __ Mov(out, 1);
3487 if (zero.IsLinked()) {
3488 __ B(&done);
3489 }
3490 break;
3491 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003492
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003493 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003494 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003495 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003496 __ Cmp(out, cls);
3497 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003498 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003499 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003500 GenerateReferenceLoadOneRegister(instruction,
3501 out_loc,
3502 component_offset,
3503 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003504 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003505 // If `out` is null, we use it for the result, and jump to `done`.
3506 __ Cbz(out, &done);
3507 __ Ldrh(out, HeapOperand(out, primitive_offset));
3508 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3509 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003510 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003511 __ Mov(out, 1);
3512 __ B(&done);
3513 break;
3514 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003515
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003516 case TypeCheckKind::kArrayCheck: {
3517 __ Cmp(out, cls);
3518 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003519 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3520 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003521 codegen_->AddSlowPath(slow_path);
3522 __ B(ne, slow_path->GetEntryLabel());
3523 __ Mov(out, 1);
3524 if (zero.IsLinked()) {
3525 __ B(&done);
3526 }
3527 break;
3528 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003529
Calin Juravle98893e12015-10-02 21:05:03 +01003530 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003531 case TypeCheckKind::kInterfaceCheck: {
3532 // Note that we indeed only call on slow path, but we always go
3533 // into the slow path for the unresolved and interface check
3534 // cases.
3535 //
3536 // We cannot directly call the InstanceofNonTrivial runtime
3537 // entry point without resorting to a type checking slow path
3538 // here (i.e. by calling InvokeRuntime directly), as it would
3539 // require to assign fixed registers for the inputs of this
3540 // HInstanceOf instruction (following the runtime calling
3541 // convention), which might be cluttered by the potential first
3542 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003543 //
3544 // TODO: Introduce a new runtime entry point taking the object
3545 // to test (instead of its class) as argument, and let it deal
3546 // with the read barrier issues. This will let us refactor this
3547 // case of the `switch` code as it was previously (with a direct
3548 // call to the runtime not using a type checking slow path).
3549 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003550 DCHECK(locations->OnlyCallsOnSlowPath());
3551 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3552 /* is_fatal */ false);
3553 codegen_->AddSlowPath(slow_path);
3554 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003555 if (zero.IsLinked()) {
3556 __ B(&done);
3557 }
3558 break;
3559 }
3560 }
3561
3562 if (zero.IsLinked()) {
3563 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003564 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003565 }
3566
3567 if (done.IsLinked()) {
3568 __ Bind(&done);
3569 }
3570
3571 if (slow_path != nullptr) {
3572 __ Bind(slow_path->GetExitLabel());
3573 }
3574}
3575
3576void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3577 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3578 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3579
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003580 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3581 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003582 case TypeCheckKind::kExactCheck:
3583 case TypeCheckKind::kAbstractClassCheck:
3584 case TypeCheckKind::kClassHierarchyCheck:
3585 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003586 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3587 LocationSummary::kCallOnSlowPath :
3588 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003589 break;
3590 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003591 case TypeCheckKind::kUnresolvedCheck:
3592 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003593 call_kind = LocationSummary::kCallOnSlowPath;
3594 break;
3595 }
3596
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3598 locations->SetInAt(0, Location::RequiresRegister());
3599 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003600 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3601 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003602}
3603
3604void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003605 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003606 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003607 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003608 Register obj = InputRegisterAt(instruction, 0);
3609 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003610 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3611 DCHECK_GE(num_temps, 1u);
3612 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003613 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003614 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3615 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003616 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003617 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3618 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3619 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3620 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3621 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3622 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3623 const uint32_t object_array_data_offset =
3624 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003625
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003626 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003627 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3628 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3629 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003630 if (!kEmitCompilerReadBarrier) {
3631 is_type_check_slow_path_fatal =
3632 (type_check_kind == TypeCheckKind::kExactCheck ||
3633 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3634 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3635 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3636 !instruction->CanThrowIntoCatchBlock();
3637 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003638 SlowPathCodeARM64* type_check_slow_path =
3639 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3640 is_type_check_slow_path_fatal);
3641 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003642
Scott Wakeling97c72b72016-06-24 16:19:36 +01003643 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003644 // Avoid null check if we know obj is not null.
3645 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003646 __ Cbz(obj, &done);
3647 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003648
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003649 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003650 case TypeCheckKind::kExactCheck:
3651 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003652 // /* HeapReference<Class> */ temp = obj->klass_
3653 GenerateReferenceLoadTwoRegisters(instruction,
3654 temp_loc,
3655 obj_loc,
3656 class_offset,
3657 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003658 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003659
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003660 __ Cmp(temp, cls);
3661 // Jump to slow path for throwing the exception or doing a
3662 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003663 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003664 break;
3665 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003666
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003667 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003668 // /* HeapReference<Class> */ temp = obj->klass_
3669 GenerateReferenceLoadTwoRegisters(instruction,
3670 temp_loc,
3671 obj_loc,
3672 class_offset,
3673 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003674 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003675
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003676 // If the class is abstract, we eagerly fetch the super class of the
3677 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003678 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003679 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003680 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003681 GenerateReferenceLoadOneRegister(instruction,
3682 temp_loc,
3683 super_offset,
3684 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003685 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003686
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003687 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3688 // exception.
3689 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3690 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003691 __ Cmp(temp, cls);
3692 __ B(ne, &loop);
3693 break;
3694 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003695
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003696 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003697 // /* HeapReference<Class> */ temp = obj->klass_
3698 GenerateReferenceLoadTwoRegisters(instruction,
3699 temp_loc,
3700 obj_loc,
3701 class_offset,
3702 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003703 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003704
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003705 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003706 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003707 __ Bind(&loop);
3708 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003709 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003710
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003711 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003712 GenerateReferenceLoadOneRegister(instruction,
3713 temp_loc,
3714 super_offset,
3715 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003716 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003717
3718 // If the class reference currently in `temp` is not null, jump
3719 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003720 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003721 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003722 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003723 break;
3724 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003725
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003726 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003727 // /* HeapReference<Class> */ temp = obj->klass_
3728 GenerateReferenceLoadTwoRegisters(instruction,
3729 temp_loc,
3730 obj_loc,
3731 class_offset,
3732 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003733 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003734
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003735 // Do an exact check.
3736 __ Cmp(temp, cls);
3737 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003738
3739 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003740 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003741 GenerateReferenceLoadOneRegister(instruction,
3742 temp_loc,
3743 component_offset,
3744 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003745 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003746
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003747 // If the component type is null, jump to the slow path to throw the exception.
3748 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3749 // Otherwise, the object is indeed an array. Further check that this component type is not a
3750 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003751 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3752 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003753 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003754 break;
3755 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003756
Calin Juravle98893e12015-10-02 21:05:03 +01003757 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003758 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003759 //
3760 // We cannot directly call the CheckCast runtime entry point
3761 // without resorting to a type checking slow path here (i.e. by
3762 // calling InvokeRuntime directly), as it would require to
3763 // assign fixed registers for the inputs of this HInstanceOf
3764 // instruction (following the runtime calling convention), which
3765 // might be cluttered by the potential first read barrier
3766 // emission at the beginning of this method.
3767 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003768 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003769 case TypeCheckKind::kInterfaceCheck: {
3770 // /* HeapReference<Class> */ temp = obj->klass_
3771 GenerateReferenceLoadTwoRegisters(instruction,
3772 temp_loc,
3773 obj_loc,
3774 class_offset,
3775 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003776 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003777
3778 // /* HeapReference<Class> */ temp = temp->iftable_
3779 GenerateReferenceLoadTwoRegisters(instruction,
3780 temp_loc,
3781 temp_loc,
3782 iftable_offset,
3783 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003784 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003785 // Null iftable means it is empty and will always fail the check.
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003786 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003787
3788 // Loop through the iftable and check if any class matches.
3789 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
3790
3791 vixl::aarch64::Label start_loop;
3792 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003793 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003794 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3795 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003796 // Go to next interface.
3797 __ Add(temp, temp, 2 * kHeapReferenceSize);
3798 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003799 // Compare the classes and continue the loop if they do not match.
3800 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3801 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003802 break;
3803 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003804 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003805 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003806
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003807 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003808}
3809
Alexandre Rames5319def2014-10-23 10:03:10 +01003810void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3811 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3812 locations->SetOut(Location::ConstantLocation(constant));
3813}
3814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003815void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003816 // Will be generated at use site.
3817}
3818
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003819void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3821 locations->SetOut(Location::ConstantLocation(constant));
3822}
3823
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003824void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003825 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003826}
3827
Calin Juravle175dc732015-08-25 15:42:32 +01003828void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3829 // The trampoline uses the same calling convention as dex calling conventions,
3830 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3831 // the method_idx.
3832 HandleInvoke(invoke);
3833}
3834
3835void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3836 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3837}
3838
Alexandre Rames5319def2014-10-23 10:03:10 +01003839void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003840 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003841 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003842}
3843
Alexandre Rames67555f72014-11-18 10:55:16 +00003844void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3845 HandleInvoke(invoke);
3846}
3847
3848void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3849 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003850 LocationSummary* locations = invoke->GetLocations();
3851 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003852 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003853 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003854 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003855
3856 // The register ip1 is required to be used for the hidden argument in
3857 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003858 MacroAssembler* masm = GetVIXLAssembler();
3859 UseScratchRegisterScope scratch_scope(masm);
3860 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003861 scratch_scope.Exclude(ip1);
3862 __ Mov(ip1, invoke->GetDexMethodIndex());
3863
Alexandre Rames67555f72014-11-18 10:55:16 +00003864 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003865 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003866 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003867 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003868 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003870 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003871 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003872 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003873 // Instead of simply (possibly) unpoisoning `temp` here, we should
3874 // emit a read barrier for the previous class reference load.
3875 // However this is not required in practice, as this is an
3876 // intermediate/temporary reference and because the current
3877 // concurrent copying collector keeps the from-space memory
3878 // intact/accessible until the end of the marking phase (the
3879 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003880 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003881 __ Ldr(temp,
3882 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3883 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003884 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003885 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003886 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003887 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003888 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003889 // lr();
3890 __ Blr(lr);
3891 DCHECK(!codegen_->IsLeafMethod());
3892 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3893}
3894
3895void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003896 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3897 if (intrinsic.TryDispatch(invoke)) {
3898 return;
3899 }
3900
Alexandre Rames67555f72014-11-18 10:55:16 +00003901 HandleInvoke(invoke);
3902}
3903
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003904void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003905 // Explicit clinit checks triggered by static invokes must have been pruned by
3906 // art::PrepareForRegisterAllocation.
3907 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003908
Andreas Gampe878d58c2015-01-15 23:24:00 -08003909 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3910 if (intrinsic.TryDispatch(invoke)) {
3911 return;
3912 }
3913
Alexandre Rames67555f72014-11-18 10:55:16 +00003914 HandleInvoke(invoke);
3915}
3916
Andreas Gampe878d58c2015-01-15 23:24:00 -08003917static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3918 if (invoke->GetLocations()->Intrinsified()) {
3919 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3920 intrinsic.Dispatch(invoke);
3921 return true;
3922 }
3923 return false;
3924}
3925
Vladimir Markodc151b22015-10-15 18:02:30 +01003926HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3927 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003928 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003929 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003930 return desired_dispatch_info;
3931}
3932
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003933void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003934 // For better instruction scheduling we load the direct code pointer before the method pointer.
3935 bool direct_code_loaded = false;
3936 switch (invoke->GetCodePtrLocation()) {
3937 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3938 // LR = code address from literal pool with link-time patch.
3939 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3940 direct_code_loaded = true;
3941 break;
3942 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3943 // LR = invoke->GetDirectCodePtr();
3944 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3945 direct_code_loaded = true;
3946 break;
3947 default:
3948 break;
3949 }
3950
Andreas Gampe878d58c2015-01-15 23:24:00 -08003951 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003952 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3953 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003954 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3955 uint32_t offset =
3956 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003957 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003958 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003959 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003960 }
Vladimir Marko58155012015-08-19 12:49:41 +00003961 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003962 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003963 break;
3964 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3965 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003966 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003967 break;
3968 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3969 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003970 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003971 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3972 break;
3973 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3974 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003975 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003976 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003977 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003978 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003979 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003980 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003981 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003982 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003983 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003984 }
Vladimir Marko58155012015-08-19 12:49:41 +00003985 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003986 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003987 Register reg = XRegisterFrom(temp);
3988 Register method_reg;
3989 if (current_method.IsRegister()) {
3990 method_reg = XRegisterFrom(current_method);
3991 } else {
3992 DCHECK(invoke->GetLocations()->Intrinsified());
3993 DCHECK(!current_method.IsValid());
3994 method_reg = reg;
3995 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3996 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003997
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003998 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003999 __ Ldr(reg.X(),
4000 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004001 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004002 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004003 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4004 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004005 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4006 break;
4007 }
4008 }
4009
4010 switch (invoke->GetCodePtrLocation()) {
4011 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4012 __ Bl(&frame_entry_label_);
4013 break;
4014 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004015 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4016 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01004017 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
4018 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00004019 __ Bind(label);
4020 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00004021 break;
4022 }
4023 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4024 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4025 // LR prepared above for better instruction scheduling.
4026 DCHECK(direct_code_loaded);
4027 // lr()
4028 __ Blr(lr);
4029 break;
4030 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4031 // LR = callee_method->entry_point_from_quick_compiled_code_;
4032 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004033 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004034 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004035 // lr()
4036 __ Blr(lr);
4037 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004038 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004039
Andreas Gampe878d58c2015-01-15 23:24:00 -08004040 DCHECK(!IsLeafMethod());
4041}
4042
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004043void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004044 // Use the calling convention instead of the location of the receiver, as
4045 // intrinsics may have put the receiver in a different register. In the intrinsics
4046 // slow path, the arguments have been moved to the right place, so here we are
4047 // guaranteed that the receiver is the first register of the calling convention.
4048 InvokeDexCallingConvention calling_convention;
4049 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004050 Register temp = XRegisterFrom(temp_in);
4051 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4052 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4053 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004054 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004055
4056 BlockPoolsScope block_pools(GetVIXLAssembler());
4057
4058 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004059 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004060 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004061 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004062 // Instead of simply (possibly) unpoisoning `temp` here, we should
4063 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004064 // intermediate/temporary reference and because the current
4065 // concurrent copying collector keeps the from-space memory
4066 // intact/accessible until the end of the marking phase (the
4067 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004068 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4069 // temp = temp->GetMethodAt(method_offset);
4070 __ Ldr(temp, MemOperand(temp, method_offset));
4071 // lr = temp->GetEntryPoint();
4072 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4073 // lr();
4074 __ Blr(lr);
4075}
4076
Scott Wakeling97c72b72016-06-24 16:19:36 +01004077vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4078 const DexFile& dex_file,
4079 uint32_t string_index,
4080 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004081 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4082}
4083
Scott Wakeling97c72b72016-06-24 16:19:36 +01004084vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4085 const DexFile& dex_file,
4086 uint32_t type_index,
4087 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004088 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
4089}
4090
Scott Wakeling97c72b72016-06-24 16:19:36 +01004091vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4092 const DexFile& dex_file,
4093 uint32_t element_offset,
4094 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004095 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4096}
4097
Scott Wakeling97c72b72016-06-24 16:19:36 +01004098vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4099 const DexFile& dex_file,
4100 uint32_t offset_or_index,
4101 vixl::aarch64::Label* adrp_label,
4102 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004103 // Add a patch entry and return the label.
4104 patches->emplace_back(dex_file, offset_or_index);
4105 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004106 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004107 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4108 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4109 return label;
4110}
4111
Scott Wakeling97c72b72016-06-24 16:19:36 +01004112vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004113 const DexFile& dex_file, uint32_t string_index) {
4114 return boot_image_string_patches_.GetOrCreate(
4115 StringReference(&dex_file, string_index),
4116 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4117}
4118
Scott Wakeling97c72b72016-06-24 16:19:36 +01004119vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004120 const DexFile& dex_file, uint32_t type_index) {
4121 return boot_image_type_patches_.GetOrCreate(
4122 TypeReference(&dex_file, type_index),
4123 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4124}
4125
Scott Wakeling97c72b72016-06-24 16:19:36 +01004126vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4127 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004128 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4129 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4130 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4131}
4132
Scott Wakeling97c72b72016-06-24 16:19:36 +01004133vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4134 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004135 return DeduplicateUint64Literal(address);
4136}
4137
Vladimir Markoaad75c62016-10-03 08:46:48 +00004138void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4139 vixl::aarch64::Register reg) {
4140 DCHECK(reg.IsX());
4141 SingleEmissionCheckScope guard(GetVIXLAssembler());
4142 __ Bind(fixup_label);
4143 __ adrp(reg, /* offset placeholder */ 0);
4144}
4145
4146void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4147 vixl::aarch64::Register out,
4148 vixl::aarch64::Register base) {
4149 DCHECK(out.IsX());
4150 DCHECK(base.IsX());
4151 SingleEmissionCheckScope guard(GetVIXLAssembler());
4152 __ Bind(fixup_label);
4153 __ add(out, base, Operand(/* offset placeholder */ 0));
4154}
4155
4156void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4157 vixl::aarch64::Register out,
4158 vixl::aarch64::Register base) {
4159 DCHECK(base.IsX());
4160 SingleEmissionCheckScope guard(GetVIXLAssembler());
4161 __ Bind(fixup_label);
4162 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4163}
4164
4165template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4166inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4167 const ArenaDeque<PcRelativePatchInfo>& infos,
4168 ArenaVector<LinkerPatch>* linker_patches) {
4169 for (const PcRelativePatchInfo& info : infos) {
4170 linker_patches->push_back(Factory(info.label.GetLocation(),
4171 &info.target_dex_file,
4172 info.pc_insn_label->GetLocation(),
4173 info.offset_or_index));
4174 }
4175}
4176
Vladimir Marko58155012015-08-19 12:49:41 +00004177void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4178 DCHECK(linker_patches->empty());
4179 size_t size =
4180 method_patches_.size() +
4181 call_patches_.size() +
4182 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004183 pc_relative_dex_cache_patches_.size() +
4184 boot_image_string_patches_.size() +
4185 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004186 boot_image_type_patches_.size() +
4187 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004188 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004189 linker_patches->reserve(size);
4190 for (const auto& entry : method_patches_) {
4191 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004192 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4193 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004194 target_method.dex_file,
4195 target_method.dex_method_index));
4196 }
4197 for (const auto& entry : call_patches_) {
4198 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004199 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4200 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004201 target_method.dex_file,
4202 target_method.dex_method_index));
4203 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004204 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4205 linker_patches->push_back(
4206 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004207 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004208 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004209 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004210 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004211 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004212 info.offset_or_index));
4213 }
4214 for (const auto& entry : boot_image_string_patches_) {
4215 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004216 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4217 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004218 target_string.dex_file,
4219 target_string.string_index));
4220 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004221 if (!GetCompilerOptions().IsBootImage()) {
4222 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4223 linker_patches);
4224 } else {
4225 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4226 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004227 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004228 for (const auto& entry : boot_image_type_patches_) {
4229 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004230 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4231 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004232 target_type.dex_file,
4233 target_type.type_index));
4234 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004235 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4236 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004237 for (const auto& entry : boot_image_address_patches_) {
4238 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004239 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4240 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004241 }
4242}
4243
Scott Wakeling97c72b72016-06-24 16:19:36 +01004244vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004245 Uint32ToLiteralMap* map) {
4246 return map->GetOrCreate(
4247 value,
4248 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4249}
4250
Scott Wakeling97c72b72016-06-24 16:19:36 +01004251vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004252 return uint64_literals_.GetOrCreate(
4253 value,
4254 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004255}
4256
Scott Wakeling97c72b72016-06-24 16:19:36 +01004257vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004258 MethodReference target_method,
4259 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004260 return map->GetOrCreate(
4261 target_method,
4262 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004263}
4264
Scott Wakeling97c72b72016-06-24 16:19:36 +01004265vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004266 MethodReference target_method) {
4267 return DeduplicateMethodLiteral(target_method, &method_patches_);
4268}
4269
Scott Wakeling97c72b72016-06-24 16:19:36 +01004270vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004271 MethodReference target_method) {
4272 return DeduplicateMethodLiteral(target_method, &call_patches_);
4273}
4274
4275
Andreas Gampe878d58c2015-01-15 23:24:00 -08004276void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004277 // Explicit clinit checks triggered by static invokes must have been pruned by
4278 // art::PrepareForRegisterAllocation.
4279 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004280
Andreas Gampe878d58c2015-01-15 23:24:00 -08004281 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4282 return;
4283 }
4284
Alexandre Ramesd921d642015-04-16 15:07:16 +01004285 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004286 LocationSummary* locations = invoke->GetLocations();
4287 codegen_->GenerateStaticOrDirectCall(
4288 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004289 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004290}
4291
4292void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004293 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4294 return;
4295 }
4296
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004297 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004298 DCHECK(!codegen_->IsLeafMethod());
4299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4300}
4301
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004302HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4303 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004304 switch (desired_class_load_kind) {
4305 case HLoadClass::LoadKind::kReferrersClass:
4306 break;
4307 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4308 DCHECK(!GetCompilerOptions().GetCompilePic());
4309 break;
4310 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4311 DCHECK(GetCompilerOptions().GetCompilePic());
4312 break;
4313 case HLoadClass::LoadKind::kBootImageAddress:
4314 break;
4315 case HLoadClass::LoadKind::kDexCacheAddress:
4316 DCHECK(Runtime::Current()->UseJitCompilation());
4317 break;
4318 case HLoadClass::LoadKind::kDexCachePcRelative:
4319 DCHECK(!Runtime::Current()->UseJitCompilation());
4320 break;
4321 case HLoadClass::LoadKind::kDexCacheViaMethod:
4322 break;
4323 }
4324 return desired_class_load_kind;
4325}
4326
Alexandre Rames67555f72014-11-18 10:55:16 +00004327void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004328 if (cls->NeedsAccessCheck()) {
4329 InvokeRuntimeCallingConvention calling_convention;
4330 CodeGenerator::CreateLoadClassLocationSummary(
4331 cls,
4332 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004333 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004334 /* code_generator_supports_read_barrier */ true);
4335 return;
4336 }
4337
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004338 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4339 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004340 ? LocationSummary::kCallOnSlowPath
4341 : LocationSummary::kNoCall;
4342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004343 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004344 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004345 }
4346
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004347 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4348 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4349 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4350 locations->SetInAt(0, Location::RequiresRegister());
4351 }
4352 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004353}
4354
4355void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004356 if (cls->NeedsAccessCheck()) {
4357 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004358 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004359 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004360 return;
4361 }
4362
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004363 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004364 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004365
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004366 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4367 ? kWithoutReadBarrier
4368 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004369 bool generate_null_check = false;
4370 switch (cls->GetLoadKind()) {
4371 case HLoadClass::LoadKind::kReferrersClass: {
4372 DCHECK(!cls->CanCallRuntime());
4373 DCHECK(!cls->MustGenerateClinitCheck());
4374 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4375 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004376 GenerateGcRootFieldLoad(cls,
4377 out_loc,
4378 current_method,
4379 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004380 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004381 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004382 break;
4383 }
4384 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004385 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004386 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4387 cls->GetTypeIndex()));
4388 break;
4389 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004390 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004391 // Add ADRP with its PC-relative type patch.
4392 const DexFile& dex_file = cls->GetDexFile();
4393 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004394 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004395 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004396 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004397 vixl::aarch64::Label* add_label =
4398 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004399 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004400 break;
4401 }
4402 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004403 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004404 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4405 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4406 break;
4407 }
4408 case HLoadClass::LoadKind::kDexCacheAddress: {
4409 DCHECK_NE(cls->GetAddress(), 0u);
4410 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4411 // that gives a 16KiB range. To try and reduce the number of literals if we load
4412 // multiple types, simply split the dex cache address to a 16KiB aligned base
4413 // loaded from a literal and the remaining offset embedded in the load.
4414 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4415 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4416 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4417 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4418 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4419 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4420 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004421 GenerateGcRootFieldLoad(cls,
4422 out_loc,
4423 out.X(),
4424 offset,
Roland Levillain00468f32016-10-27 18:02:48 +01004425 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004426 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004427 generate_null_check = !cls->IsInDexCache();
4428 break;
4429 }
4430 case HLoadClass::LoadKind::kDexCachePcRelative: {
4431 // Add ADRP with its PC-relative DexCache access patch.
4432 const DexFile& dex_file = cls->GetDexFile();
4433 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004434 vixl::aarch64::Label* adrp_label =
4435 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004436 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004437 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004438 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004439 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4440 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004441 GenerateGcRootFieldLoad(cls,
4442 out_loc,
4443 out.X(),
4444 /* offset placeholder */ 0,
4445 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004446 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004447 generate_null_check = !cls->IsInDexCache();
4448 break;
4449 }
4450 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4451 MemberOffset resolved_types_offset =
4452 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4453 // /* GcRoot<mirror::Class>[] */ out =
4454 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4455 Register current_method = InputRegisterAt(cls, 0);
4456 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4457 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004458 GenerateGcRootFieldLoad(cls,
4459 out_loc,
4460 out.X(),
4461 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
Roland Levillain00468f32016-10-27 18:02:48 +01004462 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004463 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004464 generate_null_check = !cls->IsInDexCache();
4465 break;
4466 }
4467 }
4468
4469 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4470 DCHECK(cls->CanCallRuntime());
4471 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4472 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4473 codegen_->AddSlowPath(slow_path);
4474 if (generate_null_check) {
4475 __ Cbz(out, slow_path->GetEntryLabel());
4476 }
4477 if (cls->MustGenerateClinitCheck()) {
4478 GenerateClassInitializationCheck(slow_path, out);
4479 } else {
4480 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004481 }
4482 }
4483}
4484
David Brazdilcb1c0552015-08-04 16:22:25 +01004485static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004486 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004487}
4488
Alexandre Rames67555f72014-11-18 10:55:16 +00004489void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4490 LocationSummary* locations =
4491 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4492 locations->SetOut(Location::RequiresRegister());
4493}
4494
4495void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004496 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4497}
4498
4499void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4500 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4501}
4502
4503void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4504 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004505}
4506
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004507HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4508 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 switch (desired_string_load_kind) {
4510 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4511 DCHECK(!GetCompilerOptions().GetCompilePic());
4512 break;
4513 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4514 DCHECK(GetCompilerOptions().GetCompilePic());
4515 break;
4516 case HLoadString::LoadKind::kBootImageAddress:
4517 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004518 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004519 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004520 break;
4521 case HLoadString::LoadKind::kDexCacheViaMethod:
4522 break;
4523 }
4524 return desired_string_load_kind;
4525}
4526
Alexandre Rames67555f72014-11-18 10:55:16 +00004527void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004528 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4529 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4530 ? LocationSummary::kCallOnMainOnly
4531 : LocationSummary::kCallOnSlowPath)
4532 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004534 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004535 InvokeRuntimeCallingConvention calling_convention;
4536 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4537 } else {
4538 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004539 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4540 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4541 // Rely on the pResolveString and/or marking to save everything, including temps.
4542 RegisterSet caller_saves = RegisterSet::Empty();
4543 InvokeRuntimeCallingConvention calling_convention;
4544 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4545 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4546 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4547 Primitive::kPrimNot).GetCode());
4548 locations->SetCustomSlowPathCallerSaves(caller_saves);
4549 } else {
4550 // For non-Baker read barrier we have a temp-clobbering call.
4551 }
4552 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004553 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004554}
4555
4556void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004557 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004558
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004559 switch (load->GetLoadKind()) {
4560 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004561 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4562 load->GetStringIndex()));
4563 return; // No dex cache slow path.
4564 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004565 // Add ADRP with its PC-relative String patch.
4566 const DexFile& dex_file = load->GetDexFile();
4567 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004568 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004569 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004570 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004571 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004572 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004573 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004574 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004575 return; // No dex cache slow path.
4576 }
4577 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004578 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4579 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4580 return; // No dex cache slow path.
4581 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004582 case HLoadString::LoadKind::kBssEntry: {
4583 // Add ADRP with its PC-relative String .bss entry patch.
4584 const DexFile& dex_file = load->GetDexFile();
4585 uint32_t string_index = load->GetStringIndex();
4586 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004587 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4588 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004589 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004590 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004591 // Add LDR with its PC-relative String patch.
4592 vixl::aarch64::Label* ldr_label =
4593 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004594 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004595 GenerateGcRootFieldLoad(load,
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004596 load->GetLocations()->Out(),
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004597 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004598 /* offset placeholder */ 0u,
4599 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004600 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004601 SlowPathCodeARM64* slow_path =
4602 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004603 codegen_->AddSlowPath(slow_path);
4604 __ Cbz(out.X(), slow_path->GetEntryLabel());
4605 __ Bind(slow_path->GetExitLabel());
4606 return;
4607 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004608 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004609 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004610 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004611
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004612 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004613 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004614 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004615 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4616 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4617 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004618}
4619
Alexandre Rames5319def2014-10-23 10:03:10 +01004620void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4621 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4622 locations->SetOut(Location::ConstantLocation(constant));
4623}
4624
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004625void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004626 // Will be generated at use site.
4627}
4628
Alexandre Rames67555f72014-11-18 10:55:16 +00004629void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4630 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004631 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004632 InvokeRuntimeCallingConvention calling_convention;
4633 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4634}
4635
4636void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004637 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4638 instruction,
4639 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004640 if (instruction->IsEnter()) {
4641 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4642 } else {
4643 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4644 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004645}
4646
Alexandre Rames42d641b2014-10-27 14:00:51 +00004647void LocationsBuilderARM64::VisitMul(HMul* mul) {
4648 LocationSummary* locations =
4649 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4650 switch (mul->GetResultType()) {
4651 case Primitive::kPrimInt:
4652 case Primitive::kPrimLong:
4653 locations->SetInAt(0, Location::RequiresRegister());
4654 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004656 break;
4657
4658 case Primitive::kPrimFloat:
4659 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004660 locations->SetInAt(0, Location::RequiresFpuRegister());
4661 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004662 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004663 break;
4664
4665 default:
4666 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4667 }
4668}
4669
4670void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4671 switch (mul->GetResultType()) {
4672 case Primitive::kPrimInt:
4673 case Primitive::kPrimLong:
4674 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4675 break;
4676
4677 case Primitive::kPrimFloat:
4678 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004679 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004680 break;
4681
4682 default:
4683 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4684 }
4685}
4686
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004687void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4688 LocationSummary* locations =
4689 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4690 switch (neg->GetResultType()) {
4691 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004692 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004693 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004695 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004696
4697 case Primitive::kPrimFloat:
4698 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004699 locations->SetInAt(0, Location::RequiresFpuRegister());
4700 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004701 break;
4702
4703 default:
4704 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4705 }
4706}
4707
4708void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4709 switch (neg->GetResultType()) {
4710 case Primitive::kPrimInt:
4711 case Primitive::kPrimLong:
4712 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4713 break;
4714
4715 case Primitive::kPrimFloat:
4716 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004717 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004718 break;
4719
4720 default:
4721 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4722 }
4723}
4724
4725void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4726 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004727 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004728 InvokeRuntimeCallingConvention calling_convention;
4729 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004730 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004731 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004732 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004733}
4734
4735void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4736 LocationSummary* locations = instruction->GetLocations();
4737 InvokeRuntimeCallingConvention calling_convention;
4738 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4739 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004740 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004741 // Note: if heap poisoning is enabled, the entry point takes cares
4742 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004743 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004744 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004745}
4746
Alexandre Rames5319def2014-10-23 10:03:10 +01004747void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4748 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004750 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004751 if (instruction->IsStringAlloc()) {
4752 locations->AddTemp(LocationFrom(kArtMethodRegister));
4753 } else {
4754 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4755 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4756 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004757 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4758}
4759
4760void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004761 // Note: if heap poisoning is enabled, the entry point takes cares
4762 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004763 if (instruction->IsStringAlloc()) {
4764 // String is allocated through StringFactory. Call NewEmptyString entry point.
4765 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004766 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004767 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4768 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4769 __ Blr(lr);
4770 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4771 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004772 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004773 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4774 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004775}
4776
4777void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004779 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004780 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004781}
4782
4783void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004784 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004785 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004786 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004787 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004788 break;
4789
4790 default:
4791 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4792 }
4793}
4794
David Brazdil66d126e2015-04-03 16:02:44 +01004795void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4796 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4797 locations->SetInAt(0, Location::RequiresRegister());
4798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4799}
4800
4801void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004802 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004803}
4804
Alexandre Rames5319def2014-10-23 10:03:10 +01004805void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004806 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4807 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004808}
4809
Calin Juravle2ae48182016-03-16 14:05:09 +00004810void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4811 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004812 return;
4813 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004814
Alexandre Ramesd921d642015-04-16 15:07:16 +01004815 BlockPoolsScope block_pools(GetVIXLAssembler());
4816 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004817 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004818 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004819}
4820
Calin Juravle2ae48182016-03-16 14:05:09 +00004821void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004822 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004823 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004824
4825 LocationSummary* locations = instruction->GetLocations();
4826 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004827
4828 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004829}
4830
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004831void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004832 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004833}
4834
Alexandre Rames67555f72014-11-18 10:55:16 +00004835void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4836 HandleBinaryOp(instruction);
4837}
4838
4839void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4840 HandleBinaryOp(instruction);
4841}
4842
Alexandre Rames3e69f162014-12-10 10:36:50 +00004843void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4844 LOG(FATAL) << "Unreachable";
4845}
4846
4847void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4848 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4849}
4850
Alexandre Rames5319def2014-10-23 10:03:10 +01004851void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4852 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4853 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4854 if (location.IsStackSlot()) {
4855 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4856 } else if (location.IsDoubleStackSlot()) {
4857 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4858 }
4859 locations->SetOut(location);
4860}
4861
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004862void InstructionCodeGeneratorARM64::VisitParameterValue(
4863 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004864 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004865}
4866
4867void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4868 LocationSummary* locations =
4869 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004870 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004871}
4872
4873void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4874 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4875 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004876}
4877
4878void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004880 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004881 locations->SetInAt(i, Location::Any());
4882 }
4883 locations->SetOut(Location::Any());
4884}
4885
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004886void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004887 LOG(FATAL) << "Unreachable";
4888}
4889
Serban Constantinescu02164b32014-11-13 14:05:07 +00004890void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004891 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004892 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004893 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4894 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004895 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4896
4897 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004898 case Primitive::kPrimInt:
4899 case Primitive::kPrimLong:
4900 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004901 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4903 break;
4904
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004905 case Primitive::kPrimFloat:
4906 case Primitive::kPrimDouble: {
4907 InvokeRuntimeCallingConvention calling_convention;
4908 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4909 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4910 locations->SetOut(calling_convention.GetReturnLocation(type));
4911
4912 break;
4913 }
4914
Serban Constantinescu02164b32014-11-13 14:05:07 +00004915 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004916 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004917 }
4918}
4919
4920void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4921 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004922
Serban Constantinescu02164b32014-11-13 14:05:07 +00004923 switch (type) {
4924 case Primitive::kPrimInt:
4925 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004926 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004927 break;
4928 }
4929
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004930 case Primitive::kPrimFloat:
4931 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004932 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4933 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004934 if (type == Primitive::kPrimFloat) {
4935 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4936 } else {
4937 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4938 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004939 break;
4940 }
4941
Serban Constantinescu02164b32014-11-13 14:05:07 +00004942 default:
4943 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004944 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004945 }
4946}
4947
Calin Juravle27df7582015-04-17 19:12:31 +01004948void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4949 memory_barrier->SetLocations(nullptr);
4950}
4951
4952void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004953 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004954}
4955
Alexandre Rames5319def2014-10-23 10:03:10 +01004956void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4958 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004959 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004960}
4961
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004962void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004963 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004964}
4965
4966void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4967 instruction->SetLocations(nullptr);
4968}
4969
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004970void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004971 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004972}
4973
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004974void LocationsBuilderARM64::VisitRor(HRor* ror) {
4975 HandleBinaryOp(ror);
4976}
4977
4978void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4979 HandleBinaryOp(ror);
4980}
4981
Serban Constantinescu02164b32014-11-13 14:05:07 +00004982void LocationsBuilderARM64::VisitShl(HShl* shl) {
4983 HandleShift(shl);
4984}
4985
4986void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4987 HandleShift(shl);
4988}
4989
4990void LocationsBuilderARM64::VisitShr(HShr* shr) {
4991 HandleShift(shr);
4992}
4993
4994void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4995 HandleShift(shr);
4996}
4997
Alexandre Rames5319def2014-10-23 10:03:10 +01004998void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004999 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005000}
5001
5002void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005003 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005004}
5005
Alexandre Rames67555f72014-11-18 10:55:16 +00005006void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005007 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005008}
5009
5010void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005011 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005012}
5013
5014void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005015 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005016}
5017
Alexandre Rames67555f72014-11-18 10:55:16 +00005018void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005019 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005020}
5021
Calin Juravlee460d1d2015-09-29 04:52:17 +01005022void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5023 HUnresolvedInstanceFieldGet* instruction) {
5024 FieldAccessCallingConventionARM64 calling_convention;
5025 codegen_->CreateUnresolvedFieldLocationSummary(
5026 instruction, instruction->GetFieldType(), calling_convention);
5027}
5028
5029void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5030 HUnresolvedInstanceFieldGet* instruction) {
5031 FieldAccessCallingConventionARM64 calling_convention;
5032 codegen_->GenerateUnresolvedFieldAccess(instruction,
5033 instruction->GetFieldType(),
5034 instruction->GetFieldIndex(),
5035 instruction->GetDexPc(),
5036 calling_convention);
5037}
5038
5039void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5040 HUnresolvedInstanceFieldSet* instruction) {
5041 FieldAccessCallingConventionARM64 calling_convention;
5042 codegen_->CreateUnresolvedFieldLocationSummary(
5043 instruction, instruction->GetFieldType(), calling_convention);
5044}
5045
5046void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5047 HUnresolvedInstanceFieldSet* instruction) {
5048 FieldAccessCallingConventionARM64 calling_convention;
5049 codegen_->GenerateUnresolvedFieldAccess(instruction,
5050 instruction->GetFieldType(),
5051 instruction->GetFieldIndex(),
5052 instruction->GetDexPc(),
5053 calling_convention);
5054}
5055
5056void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5057 HUnresolvedStaticFieldGet* instruction) {
5058 FieldAccessCallingConventionARM64 calling_convention;
5059 codegen_->CreateUnresolvedFieldLocationSummary(
5060 instruction, instruction->GetFieldType(), calling_convention);
5061}
5062
5063void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5064 HUnresolvedStaticFieldGet* instruction) {
5065 FieldAccessCallingConventionARM64 calling_convention;
5066 codegen_->GenerateUnresolvedFieldAccess(instruction,
5067 instruction->GetFieldType(),
5068 instruction->GetFieldIndex(),
5069 instruction->GetDexPc(),
5070 calling_convention);
5071}
5072
5073void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5074 HUnresolvedStaticFieldSet* instruction) {
5075 FieldAccessCallingConventionARM64 calling_convention;
5076 codegen_->CreateUnresolvedFieldLocationSummary(
5077 instruction, instruction->GetFieldType(), calling_convention);
5078}
5079
5080void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5081 HUnresolvedStaticFieldSet* instruction) {
5082 FieldAccessCallingConventionARM64 calling_convention;
5083 codegen_->GenerateUnresolvedFieldAccess(instruction,
5084 instruction->GetFieldType(),
5085 instruction->GetFieldIndex(),
5086 instruction->GetDexPc(),
5087 calling_convention);
5088}
5089
Alexandre Rames5319def2014-10-23 10:03:10 +01005090void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005091 LocationSummary* locations =
5092 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005093 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005094}
5095
5096void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005097 HBasicBlock* block = instruction->GetBlock();
5098 if (block->GetLoopInformation() != nullptr) {
5099 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5100 // The back edge will generate the suspend check.
5101 return;
5102 }
5103 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5104 // The goto will generate the suspend check.
5105 return;
5106 }
5107 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005108}
5109
Alexandre Rames67555f72014-11-18 10:55:16 +00005110void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5111 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005112 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005113 InvokeRuntimeCallingConvention calling_convention;
5114 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5115}
5116
5117void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005118 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005119 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005120}
5121
5122void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5123 LocationSummary* locations =
5124 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5125 Primitive::Type input_type = conversion->GetInputType();
5126 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005127 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005128 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5129 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5130 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5131 }
5132
Alexandre Rames542361f2015-01-29 16:57:31 +00005133 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005134 locations->SetInAt(0, Location::RequiresFpuRegister());
5135 } else {
5136 locations->SetInAt(0, Location::RequiresRegister());
5137 }
5138
Alexandre Rames542361f2015-01-29 16:57:31 +00005139 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005140 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5141 } else {
5142 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5143 }
5144}
5145
5146void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5147 Primitive::Type result_type = conversion->GetResultType();
5148 Primitive::Type input_type = conversion->GetInputType();
5149
5150 DCHECK_NE(input_type, result_type);
5151
Alexandre Rames542361f2015-01-29 16:57:31 +00005152 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005153 int result_size = Primitive::ComponentSize(result_type);
5154 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005155 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005156 Register output = OutputRegister(conversion);
5157 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005158 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005159 // 'int' values are used directly as W registers, discarding the top
5160 // bits, so we don't need to sign-extend and can just perform a move.
5161 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5162 // top 32 bits of the target register. We theoretically could leave those
5163 // bits unchanged, but we would have to make sure that no code uses a
5164 // 32bit input value as a 64bit value assuming that the top 32 bits are
5165 // zero.
5166 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005167 } else if (result_type == Primitive::kPrimChar ||
5168 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5169 __ Ubfx(output,
5170 output.IsX() ? source.X() : source.W(),
5171 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005172 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005173 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005174 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005175 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005176 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005177 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005178 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5179 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005180 } else if (Primitive::IsFloatingPointType(result_type) &&
5181 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005182 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5183 } else {
5184 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5185 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005186 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005187}
Alexandre Rames67555f72014-11-18 10:55:16 +00005188
Serban Constantinescu02164b32014-11-13 14:05:07 +00005189void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5190 HandleShift(ushr);
5191}
5192
5193void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5194 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005195}
5196
5197void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5198 HandleBinaryOp(instruction);
5199}
5200
5201void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5202 HandleBinaryOp(instruction);
5203}
5204
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005205void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005206 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005207 LOG(FATAL) << "Unreachable";
5208}
5209
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005210void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005211 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005212 LOG(FATAL) << "Unreachable";
5213}
5214
Mark Mendellfe57faa2015-09-18 09:26:15 -04005215// Simple implementation of packed switch - generate cascaded compare/jumps.
5216void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5217 LocationSummary* locations =
5218 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5219 locations->SetInAt(0, Location::RequiresRegister());
5220}
5221
5222void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5223 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005224 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005225 Register value_reg = InputRegisterAt(switch_instr, 0);
5226 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5227
Zheng Xu3927c8b2015-11-18 17:46:25 +08005228 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005229 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005230 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5231 // make sure we don't emit it if the target may run out of range.
5232 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5233 // ranges and emit the tables only as required.
5234 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005235
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005236 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005237 // Current instruction id is an upper bound of the number of HIRs in the graph.
5238 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5239 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005240 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5241 Register temp = temps.AcquireW();
5242 __ Subs(temp, value_reg, Operand(lower_bound));
5243
Zheng Xu3927c8b2015-11-18 17:46:25 +08005244 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005245 // Jump to successors[0] if value == lower_bound.
5246 __ B(eq, codegen_->GetLabelOf(successors[0]));
5247 int32_t last_index = 0;
5248 for (; num_entries - last_index > 2; last_index += 2) {
5249 __ Subs(temp, temp, Operand(2));
5250 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5251 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5252 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5253 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5254 }
5255 if (num_entries - last_index == 2) {
5256 // The last missing case_value.
5257 __ Cmp(temp, Operand(1));
5258 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005259 }
5260
5261 // And the default for any other value.
5262 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5263 __ B(codegen_->GetLabelOf(default_block));
5264 }
5265 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005266 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005267
5268 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5269
5270 // Below instructions should use at most one blocked register. Since there are two blocked
5271 // registers, we are free to block one.
5272 Register temp_w = temps.AcquireW();
5273 Register index;
5274 // Remove the bias.
5275 if (lower_bound != 0) {
5276 index = temp_w;
5277 __ Sub(index, value_reg, Operand(lower_bound));
5278 } else {
5279 index = value_reg;
5280 }
5281
5282 // Jump to default block if index is out of the range.
5283 __ Cmp(index, Operand(num_entries));
5284 __ B(hs, codegen_->GetLabelOf(default_block));
5285
5286 // In current VIXL implementation, it won't require any blocked registers to encode the
5287 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5288 // register pressure.
5289 Register table_base = temps.AcquireX();
5290 // Load jump offset from the table.
5291 __ Adr(table_base, jump_table->GetTableStartLabel());
5292 Register jump_offset = temp_w;
5293 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5294
5295 // Jump to target block by branching to table_base(pc related) + offset.
5296 Register target_address = table_base;
5297 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5298 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005299 }
5300}
5301
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005302void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5303 HInstruction* instruction,
5304 Location out,
5305 uint32_t offset,
5306 Location maybe_temp,
5307 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005308 Primitive::Type type = Primitive::kPrimNot;
5309 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005310 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005311 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005312 Register temp_reg = RegisterFrom(maybe_temp, type);
5313 if (kUseBakerReadBarrier) {
5314 // Load with fast path based Baker's read barrier.
5315 // /* HeapReference<Object> */ out = *(out + offset)
5316 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5317 out,
5318 out_reg,
5319 offset,
5320 temp_reg,
5321 /* needs_null_check */ false,
5322 /* use_load_acquire */ false);
5323 } else {
5324 // Load with slow path based read barrier.
5325 // Save the value of `out` into `maybe_temp` before overwriting it
5326 // in the following move operation, as we will need it for the
5327 // read barrier below.
5328 __ Mov(temp_reg, out_reg);
5329 // /* HeapReference<Object> */ out = *(out + offset)
5330 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5331 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5332 }
5333 } else {
5334 // Plain load with no read barrier.
5335 // /* HeapReference<Object> */ out = *(out + offset)
5336 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5337 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5338 }
5339}
5340
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005341void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5342 HInstruction* instruction,
5343 Location out,
5344 Location obj,
5345 uint32_t offset,
5346 Location maybe_temp,
5347 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005348 Primitive::Type type = Primitive::kPrimNot;
5349 Register out_reg = RegisterFrom(out, type);
5350 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005351 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005352 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005353 if (kUseBakerReadBarrier) {
5354 // Load with fast path based Baker's read barrier.
5355 Register temp_reg = RegisterFrom(maybe_temp, type);
5356 // /* HeapReference<Object> */ out = *(obj + offset)
5357 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5358 out,
5359 obj_reg,
5360 offset,
5361 temp_reg,
5362 /* needs_null_check */ false,
5363 /* use_load_acquire */ false);
5364 } else {
5365 // Load with slow path based read barrier.
5366 // /* HeapReference<Object> */ out = *(obj + offset)
5367 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5368 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5369 }
5370 } else {
5371 // Plain load with no read barrier.
5372 // /* HeapReference<Object> */ out = *(obj + offset)
5373 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5374 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5375 }
5376}
5377
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005378void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5379 HInstruction* instruction,
5380 Location root,
5381 Register obj,
5382 uint32_t offset,
5383 vixl::aarch64::Label* fixup_label,
5384 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005385 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005386 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005387 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005388 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005389 if (kUseBakerReadBarrier) {
5390 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5391 // Baker's read barrier are used:
5392 //
5393 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005394 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5395 // if (temp != null) {
5396 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005397 // }
5398
5399 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005400 if (fixup_label == nullptr) {
5401 __ Ldr(root_reg, MemOperand(obj, offset));
5402 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005403 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005404 }
Roland Levillain44015862016-01-22 11:47:17 +00005405 static_assert(
5406 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5407 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5408 "have different sizes.");
5409 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5410 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5411 "have different sizes.");
5412
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005413 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005414
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005415 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5416 SlowPathCodeARM64* slow_path =
5417 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5418 root,
5419 LocationFrom(temp));
5420 codegen_->AddSlowPath(slow_path);
5421 const int32_t entry_point_offset =
5422 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5423 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5424 // Loading the entrypoint does not require a load acquire since it is only changed when
5425 // threads are suspended or running a checkpoint.
5426 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5427 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5428 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005429 __ Cbnz(temp, slow_path->GetEntryLabel());
5430 __ Bind(slow_path->GetExitLabel());
5431 } else {
5432 // GC root loaded through a slow path for read barriers other
5433 // than Baker's.
5434 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005435 if (fixup_label == nullptr) {
5436 __ Add(root_reg.X(), obj.X(), offset);
5437 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005438 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005439 }
Roland Levillain44015862016-01-22 11:47:17 +00005440 // /* mirror::Object* */ root = root->Read()
5441 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5442 }
5443 } else {
5444 // Plain GC root load with no read barrier.
5445 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005446 if (fixup_label == nullptr) {
5447 __ Ldr(root_reg, MemOperand(obj, offset));
5448 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005449 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005450 }
Roland Levillain44015862016-01-22 11:47:17 +00005451 // Note that GC roots are not affected by heap poisoning, thus we
5452 // do not have to unpoison `root_reg` here.
5453 }
5454}
5455
5456void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5457 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005458 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005459 uint32_t offset,
5460 Register temp,
5461 bool needs_null_check,
5462 bool use_load_acquire) {
5463 DCHECK(kEmitCompilerReadBarrier);
5464 DCHECK(kUseBakerReadBarrier);
5465
5466 // /* HeapReference<Object> */ ref = *(obj + offset)
5467 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005468 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005469 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5470 ref,
5471 obj,
5472 offset,
5473 no_index,
5474 no_scale_factor,
5475 temp,
5476 needs_null_check,
5477 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005478}
5479
5480void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5481 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005482 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005483 uint32_t data_offset,
5484 Location index,
5485 Register temp,
5486 bool needs_null_check) {
5487 DCHECK(kEmitCompilerReadBarrier);
5488 DCHECK(kUseBakerReadBarrier);
5489
5490 // Array cells are never volatile variables, therefore array loads
5491 // never use Load-Acquire instructions on ARM64.
5492 const bool use_load_acquire = false;
5493
Roland Levillainbfea3352016-06-23 13:48:47 +01005494 static_assert(
5495 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5496 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005497 // /* HeapReference<Object> */ ref =
5498 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005499 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5500 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5501 ref,
5502 obj,
5503 data_offset,
5504 index,
5505 scale_factor,
5506 temp,
5507 needs_null_check,
5508 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005509}
5510
5511void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5512 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005513 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005514 uint32_t offset,
5515 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005516 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005517 Register temp,
5518 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005519 bool use_load_acquire,
5520 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005521 DCHECK(kEmitCompilerReadBarrier);
5522 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005523 // If we are emitting an array load, we should not be using a
5524 // Load Acquire instruction. In other words:
5525 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5526 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005527
5528 MacroAssembler* masm = GetVIXLAssembler();
5529 UseScratchRegisterScope temps(masm);
5530
5531 // In slow path based read barriers, the read barrier call is
5532 // inserted after the original load. However, in fast path based
5533 // Baker's read barriers, we need to perform the load of
5534 // mirror::Object::monitor_ *before* the original reference load.
5535 // This load-load ordering is required by the read barrier.
5536 // The fast path/slow path (for Baker's algorithm) should look like:
5537 //
5538 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5539 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5540 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005541 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005542 // if (is_gray) {
5543 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5544 // }
5545 //
5546 // Note: the original implementation in ReadBarrier::Barrier is
5547 // slightly more complex as it performs additional checks that we do
5548 // not do here for performance reasons.
5549
5550 Primitive::Type type = Primitive::kPrimNot;
5551 Register ref_reg = RegisterFrom(ref, type);
5552 DCHECK(obj.IsW());
5553 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5554
5555 // /* int32_t */ monitor = obj->monitor_
5556 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5557 if (needs_null_check) {
5558 MaybeRecordImplicitNullCheck(instruction);
5559 }
5560 // /* LockWord */ lock_word = LockWord(monitor)
5561 static_assert(sizeof(LockWord) == sizeof(int32_t),
5562 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005563
Vladimir Marko877a0332016-07-11 19:30:56 +01005564 // Introduce a dependency on the lock_word including rb_state,
5565 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005566 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005567 // `obj` is unchanged by this operation, but its value now depends
5568 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005569 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005570
5571 // The actual reference load.
5572 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005573 // Load types involving an "index": ArrayGet,
5574 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5575 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005576 if (use_load_acquire) {
5577 // UnsafeGetObjectVolatile intrinsic case.
5578 // Register `index` is not an index in an object array, but an
5579 // offset to an object reference field within object `obj`.
5580 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5581 DCHECK(instruction->GetLocations()->Intrinsified());
5582 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5583 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005584 DCHECK_EQ(offset, 0u);
5585 DCHECK_EQ(scale_factor, 0u);
5586 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005587 // /* HeapReference<Object> */ ref = *(obj + index)
5588 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5589 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005590 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005591 // ArrayGet and UnsafeGetObject intrinsics cases.
5592 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5593 if (index.IsConstant()) {
5594 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5595 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5596 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005597 Register temp3 = temps.AcquireW();
5598 __ Add(temp3, obj, offset);
5599 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5600 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005601 }
Roland Levillain44015862016-01-22 11:47:17 +00005602 }
Roland Levillain44015862016-01-22 11:47:17 +00005603 } else {
5604 // /* HeapReference<Object> */ ref = *(obj + offset)
5605 MemOperand field = HeapOperand(obj, offset);
5606 if (use_load_acquire) {
5607 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5608 } else {
5609 Load(type, ref_reg, field);
5610 }
5611 }
5612
5613 // Object* ref = ref_addr->AsMirrorPtr()
5614 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5615
Vladimir Marko953437b2016-08-24 08:30:46 +00005616 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005617 SlowPathCodeARM64* slow_path;
5618 if (always_update_field) {
5619 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5620 // address of the form `obj + field_offset`, where `obj` is a
5621 // register and `field_offset` is a register. Thus `offset` and
5622 // `scale_factor` above are expected to be null in this code path.
5623 DCHECK_EQ(offset, 0u);
5624 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5625 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5626 instruction, ref, obj, /* field_offset */ index, temp);
5627 } else {
5628 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5629 }
Roland Levillain44015862016-01-22 11:47:17 +00005630 AddSlowPath(slow_path);
5631
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005632 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005633 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005634 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005635 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5636 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005637 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005638 __ Bind(slow_path->GetExitLabel());
5639}
5640
5641void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5642 Location out,
5643 Location ref,
5644 Location obj,
5645 uint32_t offset,
5646 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005647 DCHECK(kEmitCompilerReadBarrier);
5648
Roland Levillain44015862016-01-22 11:47:17 +00005649 // Insert a slow path based read barrier *after* the reference load.
5650 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005651 // If heap poisoning is enabled, the unpoisoning of the loaded
5652 // reference will be carried out by the runtime within the slow
5653 // path.
5654 //
5655 // Note that `ref` currently does not get unpoisoned (when heap
5656 // poisoning is enabled), which is alright as the `ref` argument is
5657 // not used by the artReadBarrierSlow entry point.
5658 //
5659 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5660 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5661 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5662 AddSlowPath(slow_path);
5663
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005664 __ B(slow_path->GetEntryLabel());
5665 __ Bind(slow_path->GetExitLabel());
5666}
5667
Roland Levillain44015862016-01-22 11:47:17 +00005668void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5669 Location out,
5670 Location ref,
5671 Location obj,
5672 uint32_t offset,
5673 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005674 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005675 // Baker's read barriers shall be handled by the fast path
5676 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5677 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005678 // If heap poisoning is enabled, unpoisoning will be taken care of
5679 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005680 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005681 } else if (kPoisonHeapReferences) {
5682 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5683 }
5684}
5685
Roland Levillain44015862016-01-22 11:47:17 +00005686void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5687 Location out,
5688 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005689 DCHECK(kEmitCompilerReadBarrier);
5690
Roland Levillain44015862016-01-22 11:47:17 +00005691 // Insert a slow path based read barrier *after* the GC root load.
5692 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005693 // Note that GC roots are not affected by heap poisoning, so we do
5694 // not need to do anything special for this here.
5695 SlowPathCodeARM64* slow_path =
5696 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5697 AddSlowPath(slow_path);
5698
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005699 __ B(slow_path->GetEntryLabel());
5700 __ Bind(slow_path->GetExitLabel());
5701}
5702
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005703void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5704 LocationSummary* locations =
5705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5706 locations->SetInAt(0, Location::RequiresRegister());
5707 locations->SetOut(Location::RequiresRegister());
5708}
5709
5710void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5711 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005713 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005714 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005715 __ Ldr(XRegisterFrom(locations->Out()),
5716 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005717 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005718 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005719 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005720 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5721 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005722 __ Ldr(XRegisterFrom(locations->Out()),
5723 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005724 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005725}
5726
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00005727
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005728
Alexandre Rames67555f72014-11-18 10:55:16 +00005729#undef __
5730#undef QUICK_ENTRY_POINT
5731
Alexandre Rames5319def2014-10-23 10:03:10 +01005732} // namespace arm64
5733} // namespace art