blob: 4ab6065819a50583b40f5176f9d3b51e8e62d28f [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
Alexandre Rames3e69f162014-12-10 10:36:50 +0000463 DCHECK(instruction_->IsCheckCast()
464 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100466 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000470 if (!is_fatal_) {
471 SaveLiveRegisters(codegen, locations);
472 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473
474 // We're moving two locations to locations that could overlap, so we need a parallel
475 // move resolver.
476 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800477 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800478 LocationFrom(calling_convention.GetRegisterAt(0)),
479 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800480 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800481 LocationFrom(calling_convention.GetRegisterAt(1)),
482 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000483 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000484 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800485 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000486 Primitive::Type ret_type = instruction_->GetType();
487 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
488 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
489 } else {
490 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800491 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
492 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493 }
494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000495 if (!is_fatal_) {
496 RestoreLiveRegisters(codegen, locations);
497 __ B(GetExitLabel());
498 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 }
500
Alexandre Rames9931f312015-06-19 14:47:01 +0100501 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100502 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000506
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
508};
509
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
511 public:
Aart Bik42249c32016-01-07 15:33:50 -0800512 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000513 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800516 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700517 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000518 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000519 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700520 }
521
Alexandre Rames9931f312015-06-19 14:47:01 +0100522 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
526};
527
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100528class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
533 LocationSummary* locations = instruction_->GetLocations();
534 __ Bind(GetEntryLabel());
535 SaveLiveRegisters(codegen, locations);
536
537 InvokeRuntimeCallingConvention calling_convention;
538 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
539 parallel_move.AddMove(
540 locations->InAt(0),
541 LocationFrom(calling_convention.GetRegisterAt(0)),
542 Primitive::kPrimNot,
543 nullptr);
544 parallel_move.AddMove(
545 locations->InAt(1),
546 LocationFrom(calling_convention.GetRegisterAt(1)),
547 Primitive::kPrimInt,
548 nullptr);
549 parallel_move.AddMove(
550 locations->InAt(2),
551 LocationFrom(calling_convention.GetRegisterAt(2)),
552 Primitive::kPrimNot,
553 nullptr);
554 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
555
556 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000557 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
559 RestoreLiveRegisters(codegen, locations);
560 __ B(GetExitLabel());
561 }
562
563 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
564
565 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
567};
568
Zheng Xu3927c8b2015-11-18 17:46:25 +0800569void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
570 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000571 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800572
573 // We are about to use the assembler to place literals directly. Make sure we have enough
574 // underlying code buffer and we have generated the jump table with right size.
575 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
576 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
577
578 __ Bind(&table_start_);
579 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
580 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100581 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800582 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100583 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800584 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
585 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
586 Literal<int32_t> literal(jump_offset);
587 __ place(&literal);
588 }
589}
590
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100591// Slow path marking an object reference `ref` during a read
592// barrier. The field `obj.field` in the object `obj` holding this
593// reference does not get updated by this slow path after marking (see
594// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
595//
596// This means that after the execution of this slow path, `ref` will
597// always be up-to-date, but `obj.field` may not; i.e., after the
598// flip, `ref` will be a to-space reference, but `obj.field` will
599// probably still be a from-space reference (unless it gets updated by
600// another thread, or if another thread installed another object
601// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800602// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
603// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000604class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
605 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800606 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
607 Location ref,
608 Location entrypoint = Location::NoLocation())
609 : SlowPathCodeARM64(instruction),
610 ref_(ref),
611 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000612 DCHECK(kEmitCompilerReadBarrier);
613 }
614
615 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000619 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100620 DCHECK(ref_.IsRegister()) << ref_;
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000622 DCHECK(instruction_->IsInstanceFieldGet() ||
623 instruction_->IsStaticFieldGet() ||
624 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100625 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000626 instruction_->IsLoadClass() ||
627 instruction_->IsLoadString() ||
628 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100629 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100630 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
631 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000632 << "Unexpected instruction in read barrier marking slow path: "
633 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000634 // The read barrier instrumentation of object ArrayGet
635 // instructions does not support the HIntermediateAddress
636 // instruction.
637 DCHECK(!(instruction_->IsArrayGet() &&
638 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000639
640 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100641 // No need to save live registers; it's taken care of by the
642 // entrypoint. Also, there is no need to update the stack mask,
643 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000644 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100645 DCHECK_NE(ref_.reg(), LR);
646 DCHECK_NE(ref_.reg(), WSP);
647 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100648 // IP0 is used internally by the ReadBarrierMarkRegX entry point
649 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100650 DCHECK_NE(ref_.reg(), IP0);
651 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100652 // "Compact" slow path, saving two moves.
653 //
654 // Instead of using the standard runtime calling convention (input
655 // and output in W0):
656 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100657 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100658 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100659 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100660 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100661 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100662 // of a dedicated entrypoint:
663 //
664 // rX <- ReadBarrierMarkRegX(rX)
665 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800666 if (entrypoint_.IsValid()) {
667 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
668 __ Blr(XRegisterFrom(entrypoint_));
669 } else {
670 // Entrypoint is not already loaded, load from the thread.
671 int32_t entry_point_offset =
672 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
673 // This runtime call does not require a stack map.
674 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
675 }
Roland Levillain44015862016-01-22 11:47:17 +0000676 __ B(GetExitLabel());
677 }
678
679 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 // The location (register) of the marked object reference.
681 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000682
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800683 // The location of the entrypoint if it is already loaded.
684 const Location entrypoint_;
685
Roland Levillain44015862016-01-22 11:47:17 +0000686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
687};
688
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100689// Slow path marking an object reference `ref` during a read barrier,
690// and if needed, atomically updating the field `obj.field` in the
691// object `obj` holding this reference after marking (contrary to
692// ReadBarrierMarkSlowPathARM64 above, which never tries to update
693// `obj.field`).
694//
695// This means that after the execution of this slow path, both `ref`
696// and `obj.field` will be up-to-date; i.e., after the flip, both will
697// hold the same to-space reference (unless another thread installed
698// another object reference (different from `ref`) in `obj.field`).
699class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
700 public:
701 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
702 Location ref,
703 Register obj,
704 Location field_offset,
705 Register temp)
706 : SlowPathCodeARM64(instruction),
707 ref_(ref),
708 obj_(obj),
709 field_offset_(field_offset),
710 temp_(temp) {
711 DCHECK(kEmitCompilerReadBarrier);
712 }
713
714 const char* GetDescription() const OVERRIDE {
715 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
716 }
717
718 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
719 LocationSummary* locations = instruction_->GetLocations();
720 Register ref_reg = WRegisterFrom(ref_);
721 DCHECK(locations->CanCall());
722 DCHECK(ref_.IsRegister()) << ref_;
723 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
724 // This slow path is only used by the UnsafeCASObject intrinsic.
725 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
726 << "Unexpected instruction in read barrier marking and field updating slow path: "
727 << instruction_->DebugName();
728 DCHECK(instruction_->GetLocations()->Intrinsified());
729 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
730 DCHECK(field_offset_.IsRegister()) << field_offset_;
731
732 __ Bind(GetEntryLabel());
733
734 // Save the old reference.
735 // Note that we cannot use IP to save the old reference, as IP is
736 // used internally by the ReadBarrierMarkRegX entry point, and we
737 // need the old reference after the call to that entry point.
738 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
739 __ Mov(temp_.W(), ref_reg);
740
741 // No need to save live registers; it's taken care of by the
742 // entrypoint. Also, there is no need to update the stack mask,
743 // as this runtime call will not trigger a garbage collection.
744 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
745 DCHECK_NE(ref_.reg(), LR);
746 DCHECK_NE(ref_.reg(), WSP);
747 DCHECK_NE(ref_.reg(), WZR);
748 // IP0 is used internally by the ReadBarrierMarkRegX entry point
749 // as a temporary, it cannot be the entry point's input/output.
750 DCHECK_NE(ref_.reg(), IP0);
751 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
752 // "Compact" slow path, saving two moves.
753 //
754 // Instead of using the standard runtime calling convention (input
755 // and output in W0):
756 //
757 // W0 <- ref
758 // W0 <- ReadBarrierMark(W0)
759 // ref <- W0
760 //
761 // we just use rX (the register containing `ref`) as input and output
762 // of a dedicated entrypoint:
763 //
764 // rX <- ReadBarrierMarkRegX(rX)
765 //
766 int32_t entry_point_offset =
767 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
768 // This runtime call does not require a stack map.
769 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
770
771 // If the new reference is different from the old reference,
772 // update the field in the holder (`*(obj_ + field_offset_)`).
773 //
774 // Note that this field could also hold a different object, if
775 // another thread had concurrently changed it. In that case, the
776 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
777 // (CAS) operation below would abort the CAS, leaving the field
778 // as-is.
779 vixl::aarch64::Label done;
780 __ Cmp(temp_.W(), ref_reg);
781 __ B(eq, &done);
782
783 // Update the the holder's field atomically. This may fail if
784 // mutator updates before us, but it's OK. This is achieved
785 // using a strong compare-and-set (CAS) operation with relaxed
786 // memory synchronization ordering, where the expected value is
787 // the old reference and the desired value is the new reference.
788
789 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
790 UseScratchRegisterScope temps(masm);
791
792 // Convenience aliases.
793 Register base = obj_.W();
794 Register offset = XRegisterFrom(field_offset_);
795 Register expected = temp_.W();
796 Register value = ref_reg;
797 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
798 Register tmp_value = temps.AcquireW(); // Value in memory.
799
800 __ Add(tmp_ptr, base.X(), Operand(offset));
801
802 if (kPoisonHeapReferences) {
803 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
804 if (value.Is(expected)) {
805 // Do not poison `value`, as it is the same register as
806 // `expected`, which has just been poisoned.
807 } else {
808 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
809 }
810 }
811
812 // do {
813 // tmp_value = [tmp_ptr] - expected;
814 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
815
Roland Levillain24a4d112016-10-26 13:10:46 +0100816 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100817 __ Bind(&loop_head);
818 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
819 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100820 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100821 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
822 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100823 __ B(&exit_loop);
824 __ Bind(&comparison_failed);
825 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100826 __ Bind(&exit_loop);
827
828 if (kPoisonHeapReferences) {
829 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
830 if (value.Is(expected)) {
831 // Do not unpoison `value`, as it is the same register as
832 // `expected`, which has just been unpoisoned.
833 } else {
834 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
835 }
836 }
837
838 __ Bind(&done);
839 __ B(GetExitLabel());
840 }
841
842 private:
843 // The location (register) of the marked object reference.
844 const Location ref_;
845 // The register containing the object holding the marked object reference field.
846 const Register obj_;
847 // The location of the offset of the marked reference field within `obj_`.
848 Location field_offset_;
849
850 const Register temp_;
851
852 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
853};
854
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000855// Slow path generating a read barrier for a heap reference.
856class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
857 public:
858 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
859 Location out,
860 Location ref,
861 Location obj,
862 uint32_t offset,
863 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000864 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000865 out_(out),
866 ref_(ref),
867 obj_(obj),
868 offset_(offset),
869 index_(index) {
870 DCHECK(kEmitCompilerReadBarrier);
871 // If `obj` is equal to `out` or `ref`, it means the initial object
872 // has been overwritten by (or after) the heap object reference load
873 // to be instrumented, e.g.:
874 //
875 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000876 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000877 //
878 // In that case, we have lost the information about the original
879 // object, and the emitted read barrier cannot work properly.
880 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
881 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
882 }
883
884 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
885 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
886 LocationSummary* locations = instruction_->GetLocations();
887 Primitive::Type type = Primitive::kPrimNot;
888 DCHECK(locations->CanCall());
889 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100890 DCHECK(instruction_->IsInstanceFieldGet() ||
891 instruction_->IsStaticFieldGet() ||
892 instruction_->IsArrayGet() ||
893 instruction_->IsInstanceOf() ||
894 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100895 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000896 << "Unexpected instruction in read barrier for heap reference slow path: "
897 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000898 // The read barrier instrumentation of object ArrayGet
899 // instructions does not support the HIntermediateAddress
900 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000901 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100902 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000903
904 __ Bind(GetEntryLabel());
905
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000906 SaveLiveRegisters(codegen, locations);
907
908 // We may have to change the index's value, but as `index_` is a
909 // constant member (like other "inputs" of this slow path),
910 // introduce a copy of it, `index`.
911 Location index = index_;
912 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100913 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000914 if (instruction_->IsArrayGet()) {
915 // Compute the actual memory offset and store it in `index`.
916 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
917 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
918 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
919 // We are about to change the value of `index_reg` (see the
920 // calls to vixl::MacroAssembler::Lsl and
921 // vixl::MacroAssembler::Mov below), but it has
922 // not been saved by the previous call to
923 // art::SlowPathCode::SaveLiveRegisters, as it is a
924 // callee-save register --
925 // art::SlowPathCode::SaveLiveRegisters does not consider
926 // callee-save registers, as it has been designed with the
927 // assumption that callee-save registers are supposed to be
928 // handled by the called function. So, as a callee-save
929 // register, `index_reg` _would_ eventually be saved onto
930 // the stack, but it would be too late: we would have
931 // changed its value earlier. Therefore, we manually save
932 // it here into another freely available register,
933 // `free_reg`, chosen of course among the caller-save
934 // registers (as a callee-save `free_reg` register would
935 // exhibit the same problem).
936 //
937 // Note we could have requested a temporary register from
938 // the register allocator instead; but we prefer not to, as
939 // this is a slow path, and we know we can find a
940 // caller-save register that is available.
941 Register free_reg = FindAvailableCallerSaveRegister(codegen);
942 __ Mov(free_reg.W(), index_reg);
943 index_reg = free_reg;
944 index = LocationFrom(index_reg);
945 } else {
946 // The initial register stored in `index_` has already been
947 // saved in the call to art::SlowPathCode::SaveLiveRegisters
948 // (as it is not a callee-save register), so we can freely
949 // use it.
950 }
951 // Shifting the index value contained in `index_reg` by the scale
952 // factor (2) cannot overflow in practice, as the runtime is
953 // unable to allocate object arrays with a size larger than
954 // 2^26 - 1 (that is, 2^28 - 4 bytes).
955 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
956 static_assert(
957 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
958 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
959 __ Add(index_reg, index_reg, Operand(offset_));
960 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100961 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
962 // intrinsics, `index_` is not shifted by a scale factor of 2
963 // (as in the case of ArrayGet), as it is actually an offset
964 // to an object field within an object.
965 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000966 DCHECK(instruction_->GetLocations()->Intrinsified());
967 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
968 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
969 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100971 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000972 }
973 }
974
975 // We're moving two or three locations to locations that could
976 // overlap, so we need a parallel move resolver.
977 InvokeRuntimeCallingConvention calling_convention;
978 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
979 parallel_move.AddMove(ref_,
980 LocationFrom(calling_convention.GetRegisterAt(0)),
981 type,
982 nullptr);
983 parallel_move.AddMove(obj_,
984 LocationFrom(calling_convention.GetRegisterAt(1)),
985 type,
986 nullptr);
987 if (index.IsValid()) {
988 parallel_move.AddMove(index,
989 LocationFrom(calling_convention.GetRegisterAt(2)),
990 Primitive::kPrimInt,
991 nullptr);
992 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
993 } else {
994 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
995 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
996 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000997 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000998 instruction_,
999 instruction_->GetDexPc(),
1000 this);
1001 CheckEntrypointTypes<
1002 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1003 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1004
1005 RestoreLiveRegisters(codegen, locations);
1006
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001007 __ B(GetExitLabel());
1008 }
1009
1010 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1011
1012 private:
1013 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001014 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1015 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001016 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1017 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1018 return Register(VIXLRegCodeFromART(i), kXRegSize);
1019 }
1020 }
1021 // We shall never fail to find a free caller-save register, as
1022 // there are more than two core caller-save registers on ARM64
1023 // (meaning it is possible to find one which is different from
1024 // `ref` and `obj`).
1025 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1026 LOG(FATAL) << "Could not find a free register";
1027 UNREACHABLE();
1028 }
1029
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001030 const Location out_;
1031 const Location ref_;
1032 const Location obj_;
1033 const uint32_t offset_;
1034 // An additional location containing an index to an array.
1035 // Only used for HArrayGet and the UnsafeGetObject &
1036 // UnsafeGetObjectVolatile intrinsics.
1037 const Location index_;
1038
1039 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1040};
1041
1042// Slow path generating a read barrier for a GC root.
1043class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1044 public:
1045 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001046 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001047 DCHECK(kEmitCompilerReadBarrier);
1048 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001049
1050 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1051 LocationSummary* locations = instruction_->GetLocations();
1052 Primitive::Type type = Primitive::kPrimNot;
1053 DCHECK(locations->CanCall());
1054 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001055 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1056 << "Unexpected instruction in read barrier for GC root slow path: "
1057 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001058
1059 __ Bind(GetEntryLabel());
1060 SaveLiveRegisters(codegen, locations);
1061
1062 InvokeRuntimeCallingConvention calling_convention;
1063 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1064 // The argument of the ReadBarrierForRootSlow is not a managed
1065 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1066 // thus we need a 64-bit move here, and we cannot use
1067 //
1068 // arm64_codegen->MoveLocation(
1069 // LocationFrom(calling_convention.GetRegisterAt(0)),
1070 // root_,
1071 // type);
1072 //
1073 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1074 // reference type (`Primitive::kPrimNot`).
1075 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001076 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001077 instruction_,
1078 instruction_->GetDexPc(),
1079 this);
1080 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1081 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1082
1083 RestoreLiveRegisters(codegen, locations);
1084 __ B(GetExitLabel());
1085 }
1086
1087 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1088
1089 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001090 const Location out_;
1091 const Location root_;
1092
1093 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1094};
1095
Alexandre Rames5319def2014-10-23 10:03:10 +01001096#undef __
1097
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001098Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001099 Location next_location;
1100 if (type == Primitive::kPrimVoid) {
1101 LOG(FATAL) << "Unreachable type " << type;
1102 }
1103
Alexandre Rames542361f2015-01-29 16:57:31 +00001104 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001105 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1106 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001107 } else if (!Primitive::IsFloatingPointType(type) &&
1108 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001109 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1110 } else {
1111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001112 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1113 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001114 }
1115
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001116 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001117 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 return next_location;
1119}
1120
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001121Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001122 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001123}
1124
Serban Constantinescu579885a2015-02-22 20:51:33 +00001125CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1126 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001127 const CompilerOptions& compiler_options,
1128 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001129 : CodeGenerator(graph,
1130 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001132 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001133 callee_saved_core_registers.GetList(),
1134 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001135 compiler_options,
1136 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001137 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001138 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001139 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001140 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001141 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001142 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001143 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001144 uint32_literals_(std::less<uint32_t>(),
1145 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001146 uint64_literals_(std::less<uint64_t>(),
1147 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1148 method_patches_(MethodReferenceComparator(),
1149 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1150 call_patches_(MethodReferenceComparator(),
1151 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1152 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001153 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1154 boot_image_string_patches_(StringReferenceValueComparator(),
1155 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001157 boot_image_type_patches_(TypeReferenceValueComparator(),
1158 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1159 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001160 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00001161 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001162 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001163 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001164}
Alexandre Rames5319def2014-10-23 10:03:10 +01001165
Alexandre Rames67555f72014-11-18 10:55:16 +00001166#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001167
Zheng Xu3927c8b2015-11-18 17:46:25 +08001168void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001169 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001170 jump_table->EmitTable(this);
1171 }
1172}
1173
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001174void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001175 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001176 // Ensure we emit the literal pool.
1177 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001178
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001179 CodeGenerator::Finalize(allocator);
1180}
1181
Zheng Xuad4450e2015-04-17 18:48:56 +08001182void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1183 // Note: There are 6 kinds of moves:
1184 // 1. constant -> GPR/FPR (non-cycle)
1185 // 2. constant -> stack (non-cycle)
1186 // 3. GPR/FPR -> GPR/FPR
1187 // 4. GPR/FPR -> stack
1188 // 5. stack -> GPR/FPR
1189 // 6. stack -> stack (non-cycle)
1190 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1191 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1192 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1193 // dependency.
1194 vixl_temps_.Open(GetVIXLAssembler());
1195}
1196
1197void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1198 vixl_temps_.Close();
1199}
1200
1201Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1202 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1203 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1204 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1205 Location scratch = GetScratchLocation(kind);
1206 if (!scratch.Equals(Location::NoLocation())) {
1207 return scratch;
1208 }
1209 // Allocate from VIXL temp registers.
1210 if (kind == Location::kRegister) {
1211 scratch = LocationFrom(vixl_temps_.AcquireX());
1212 } else {
1213 DCHECK(kind == Location::kFpuRegister);
1214 scratch = LocationFrom(vixl_temps_.AcquireD());
1215 }
1216 AddScratchLocation(scratch);
1217 return scratch;
1218}
1219
1220void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1221 if (loc.IsRegister()) {
1222 vixl_temps_.Release(XRegisterFrom(loc));
1223 } else {
1224 DCHECK(loc.IsFpuRegister());
1225 vixl_temps_.Release(DRegisterFrom(loc));
1226 }
1227 RemoveScratchLocation(loc);
1228}
1229
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001231 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001232 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001233}
1234
Alexandre Rames5319def2014-10-23 10:03:10 +01001235void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001236 MacroAssembler* masm = GetVIXLAssembler();
1237 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001238 __ Bind(&frame_entry_label_);
1239
Serban Constantinescu02164b32014-11-13 14:05:07 +00001240 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1241 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001242 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001243 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001244 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001245 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001246 __ Ldr(wzr, MemOperand(temp, 0));
1247 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001248 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001249
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001250 if (!HasEmptyFrame()) {
1251 int frame_size = GetFrameSize();
1252 // Stack layout:
1253 // sp[frame_size - 8] : lr.
1254 // ... : other preserved core registers.
1255 // ... : other preserved fp registers.
1256 // ... : reserved frame space.
1257 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001258
1259 // Save the current method if we need it. Note that we do not
1260 // do this in HCurrentMethod, as the instruction might have been removed
1261 // in the SSA graph.
1262 if (RequiresCurrentMethod()) {
1263 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001264 } else {
1265 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001266 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001267 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001268 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1269 frame_size - GetCoreSpillSize());
1270 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1271 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001272 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001273}
1274
1275void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001276 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001277 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001278 if (!HasEmptyFrame()) {
1279 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001280 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1281 frame_size - FrameEntrySpillSize());
1282 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1283 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001284 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001285 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001286 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001287 __ Ret();
1288 GetAssembler()->cfi().RestoreState();
1289 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001290}
1291
Scott Wakeling97c72b72016-06-24 16:19:36 +01001292CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001293 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001294 return CPURegList(CPURegister::kRegister, kXRegSize,
1295 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001296}
1297
Scott Wakeling97c72b72016-06-24 16:19:36 +01001298CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001299 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1300 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001301 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1302 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001303}
1304
Alexandre Rames5319def2014-10-23 10:03:10 +01001305void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1306 __ Bind(GetLabelOf(block));
1307}
1308
Calin Juravle175dc732015-08-25 15:42:32 +01001309void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1310 DCHECK(location.IsRegister());
1311 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1312}
1313
Calin Juravlee460d1d2015-09-29 04:52:17 +01001314void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1315 if (location.IsRegister()) {
1316 locations->AddTemp(location);
1317 } else {
1318 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1319 }
1320}
1321
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001322void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001324 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001325 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001326 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001327 if (value_can_be_null) {
1328 __ Cbz(value, &done);
1329 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001330 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001331 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001332 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001333 if (value_can_be_null) {
1334 __ Bind(&done);
1335 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001336}
1337
David Brazdil58282f42016-01-14 12:45:10 +00001338void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001339 // Blocked core registers:
1340 // lr : Runtime reserved.
1341 // tr : Runtime reserved.
1342 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1343 // ip1 : VIXL core temp.
1344 // ip0 : VIXL core temp.
1345 //
1346 // Blocked fp registers:
1347 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001348 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1349 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001353
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001354 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001355 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001356 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001357 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001358
David Brazdil58282f42016-01-14 12:45:10 +00001359 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001360 // Stubs do not save callee-save floating point registers. If the graph
1361 // is debuggable, we need to deal with these registers differently. For
1362 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001363 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1364 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001365 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001366 }
1367 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001368}
1369
Alexandre Rames3e69f162014-12-10 10:36:50 +00001370size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1371 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1372 __ Str(reg, MemOperand(sp, stack_index));
1373 return kArm64WordSize;
1374}
1375
1376size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1377 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1378 __ Ldr(reg, MemOperand(sp, stack_index));
1379 return kArm64WordSize;
1380}
1381
1382size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1383 FPRegister reg = FPRegister(reg_id, kDRegSize);
1384 __ Str(reg, MemOperand(sp, stack_index));
1385 return kArm64WordSize;
1386}
1387
1388size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1389 FPRegister reg = FPRegister(reg_id, kDRegSize);
1390 __ Ldr(reg, MemOperand(sp, stack_index));
1391 return kArm64WordSize;
1392}
1393
Alexandre Rames5319def2014-10-23 10:03:10 +01001394void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001395 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001396}
1397
1398void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001399 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001400}
1401
Alexandre Rames67555f72014-11-18 10:55:16 +00001402void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001403 if (constant->IsIntConstant()) {
1404 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1405 } else if (constant->IsLongConstant()) {
1406 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1407 } else if (constant->IsNullConstant()) {
1408 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001409 } else if (constant->IsFloatConstant()) {
1410 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1411 } else {
1412 DCHECK(constant->IsDoubleConstant());
1413 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1414 }
1415}
1416
Alexandre Rames3e69f162014-12-10 10:36:50 +00001417
1418static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1419 DCHECK(constant.IsConstant());
1420 HConstant* cst = constant.GetConstant();
1421 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001422 // Null is mapped to a core W register, which we associate with kPrimInt.
1423 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001424 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1425 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1426 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1427}
1428
Calin Juravlee460d1d2015-09-29 04:52:17 +01001429void CodeGeneratorARM64::MoveLocation(Location destination,
1430 Location source,
1431 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001432 if (source.Equals(destination)) {
1433 return;
1434 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001435
1436 // A valid move can always be inferred from the destination and source
1437 // locations. When moving from and to a register, the argument type can be
1438 // used to generate 32bit instead of 64bit moves. In debug mode we also
1439 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001440 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001441
1442 if (destination.IsRegister() || destination.IsFpuRegister()) {
1443 if (unspecified_type) {
1444 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1445 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001446 (src_cst != nullptr && (src_cst->IsIntConstant()
1447 || src_cst->IsFloatConstant()
1448 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001449 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001450 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001451 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001452 // If the source is a double stack slot or a 64bit constant, a 64bit
1453 // type is appropriate. Else the source is a register, and since the
1454 // type has not been specified, we chose a 64bit type to force a 64bit
1455 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001456 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001457 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001458 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001459 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1460 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1461 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001462 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1463 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1464 __ Ldr(dst, StackOperandFrom(source));
1465 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001466 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001467 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001470 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001471 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001472 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001473 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1474 ? Primitive::kPrimLong
1475 : Primitive::kPrimInt;
1476 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1477 }
1478 } else {
1479 DCHECK(source.IsFpuRegister());
1480 if (destination.IsRegister()) {
1481 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1482 ? Primitive::kPrimDouble
1483 : Primitive::kPrimFloat;
1484 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1485 } else {
1486 DCHECK(destination.IsFpuRegister());
1487 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488 }
1489 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001490 } else { // The destination is not a register. It must be a stack slot.
1491 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1492 if (source.IsRegister() || source.IsFpuRegister()) {
1493 if (unspecified_type) {
1494 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001495 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001497 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498 }
1499 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001500 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1501 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1502 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001504 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1505 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506 UseScratchRegisterScope temps(GetVIXLAssembler());
1507 HConstant* src_cst = source.GetConstant();
1508 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001509 if (src_cst->IsZeroBitPattern()) {
1510 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001511 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001512 if (src_cst->IsIntConstant()) {
1513 temp = temps.AcquireW();
1514 } else if (src_cst->IsLongConstant()) {
1515 temp = temps.AcquireX();
1516 } else if (src_cst->IsFloatConstant()) {
1517 temp = temps.AcquireS();
1518 } else {
1519 DCHECK(src_cst->IsDoubleConstant());
1520 temp = temps.AcquireD();
1521 }
1522 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001523 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001524 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001526 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001528 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 // There is generally less pressure on FP registers.
1530 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001531 __ Ldr(temp, StackOperandFrom(source));
1532 __ Str(temp, StackOperandFrom(destination));
1533 }
1534 }
1535}
1536
1537void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001538 CPURegister dst,
1539 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001540 switch (type) {
1541 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001542 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001543 break;
1544 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001545 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001546 break;
1547 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001548 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001549 break;
1550 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001551 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001552 break;
1553 case Primitive::kPrimInt:
1554 case Primitive::kPrimNot:
1555 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556 case Primitive::kPrimFloat:
1557 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001558 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001559 __ Ldr(dst, src);
1560 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001561 case Primitive::kPrimVoid:
1562 LOG(FATAL) << "Unreachable type " << type;
1563 }
1564}
1565
Calin Juravle77520bc2015-01-12 18:45:46 +00001566void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001567 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001568 const MemOperand& src,
1569 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001570 MacroAssembler* masm = GetVIXLAssembler();
1571 BlockPoolsScope block_pools(masm);
1572 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001573 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001574 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001575
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001576 DCHECK(!src.IsPreIndex());
1577 DCHECK(!src.IsPostIndex());
1578
1579 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001580 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001581 MemOperand base = MemOperand(temp_base);
1582 switch (type) {
1583 case Primitive::kPrimBoolean:
1584 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001585 if (needs_null_check) {
1586 MaybeRecordImplicitNullCheck(instruction);
1587 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001588 break;
1589 case Primitive::kPrimByte:
1590 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001591 if (needs_null_check) {
1592 MaybeRecordImplicitNullCheck(instruction);
1593 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001594 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1595 break;
1596 case Primitive::kPrimChar:
1597 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001598 if (needs_null_check) {
1599 MaybeRecordImplicitNullCheck(instruction);
1600 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001601 break;
1602 case Primitive::kPrimShort:
1603 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001604 if (needs_null_check) {
1605 MaybeRecordImplicitNullCheck(instruction);
1606 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001607 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1608 break;
1609 case Primitive::kPrimInt:
1610 case Primitive::kPrimNot:
1611 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001612 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001613 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001614 if (needs_null_check) {
1615 MaybeRecordImplicitNullCheck(instruction);
1616 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001617 break;
1618 case Primitive::kPrimFloat:
1619 case Primitive::kPrimDouble: {
1620 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001621 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001622
1623 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1624 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001625 if (needs_null_check) {
1626 MaybeRecordImplicitNullCheck(instruction);
1627 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001628 __ Fmov(FPRegister(dst), temp);
1629 break;
1630 }
1631 case Primitive::kPrimVoid:
1632 LOG(FATAL) << "Unreachable type " << type;
1633 }
1634}
1635
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001636void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001637 CPURegister src,
1638 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001639 switch (type) {
1640 case Primitive::kPrimBoolean:
1641 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001642 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001643 break;
1644 case Primitive::kPrimChar:
1645 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001646 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 break;
1648 case Primitive::kPrimInt:
1649 case Primitive::kPrimNot:
1650 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001651 case Primitive::kPrimFloat:
1652 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001653 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001655 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001656 case Primitive::kPrimVoid:
1657 LOG(FATAL) << "Unreachable type " << type;
1658 }
1659}
1660
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001661void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1662 CPURegister src,
1663 const MemOperand& dst) {
1664 UseScratchRegisterScope temps(GetVIXLAssembler());
1665 Register temp_base = temps.AcquireX();
1666
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001667 DCHECK(!dst.IsPreIndex());
1668 DCHECK(!dst.IsPostIndex());
1669
1670 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001671 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001672 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001673 MemOperand base = MemOperand(temp_base);
1674 switch (type) {
1675 case Primitive::kPrimBoolean:
1676 case Primitive::kPrimByte:
1677 __ Stlrb(Register(src), base);
1678 break;
1679 case Primitive::kPrimChar:
1680 case Primitive::kPrimShort:
1681 __ Stlrh(Register(src), base);
1682 break;
1683 case Primitive::kPrimInt:
1684 case Primitive::kPrimNot:
1685 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001686 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001687 __ Stlr(Register(src), base);
1688 break;
1689 case Primitive::kPrimFloat:
1690 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001691 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001692 Register temp_src;
1693 if (src.IsZero()) {
1694 // The zero register is used to avoid synthesizing zero constants.
1695 temp_src = Register(src);
1696 } else {
1697 DCHECK(src.IsFPRegister());
1698 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1699 __ Fmov(temp_src, FPRegister(src));
1700 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001701
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001702 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001703 break;
1704 }
1705 case Primitive::kPrimVoid:
1706 LOG(FATAL) << "Unreachable type " << type;
1707 }
1708}
1709
Calin Juravle175dc732015-08-25 15:42:32 +01001710void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1711 HInstruction* instruction,
1712 uint32_t dex_pc,
1713 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001714 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001715 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001716 if (EntrypointRequiresStackMap(entrypoint)) {
1717 RecordPcInfo(instruction, dex_pc, slow_path);
1718 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001719}
1720
Roland Levillaindec8f632016-07-22 17:10:06 +01001721void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1722 HInstruction* instruction,
1723 SlowPathCode* slow_path) {
1724 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001725 GenerateInvokeRuntime(entry_point_offset);
1726}
1727
1728void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001729 BlockPoolsScope block_pools(GetVIXLAssembler());
1730 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1731 __ Blr(lr);
1732}
1733
Alexandre Rames67555f72014-11-18 10:55:16 +00001734void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001735 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001736 UseScratchRegisterScope temps(GetVIXLAssembler());
1737 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1739
Serban Constantinescu02164b32014-11-13 14:05:07 +00001740 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001741 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1742 __ Add(temp, class_reg, status_offset);
1743 __ Ldar(temp, HeapOperand(temp));
1744 __ Cmp(temp, mirror::Class::kStatusInitialized);
1745 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001746 __ Bind(slow_path->GetExitLabel());
1747}
Alexandre Rames5319def2014-10-23 10:03:10 +01001748
Roland Levillain44015862016-01-22 11:47:17 +00001749void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001750 BarrierType type = BarrierAll;
1751
1752 switch (kind) {
1753 case MemBarrierKind::kAnyAny:
1754 case MemBarrierKind::kAnyStore: {
1755 type = BarrierAll;
1756 break;
1757 }
1758 case MemBarrierKind::kLoadAny: {
1759 type = BarrierReads;
1760 break;
1761 }
1762 case MemBarrierKind::kStoreStore: {
1763 type = BarrierWrites;
1764 break;
1765 }
1766 default:
1767 LOG(FATAL) << "Unexpected memory barrier " << kind;
1768 }
1769 __ Dmb(InnerShareable, type);
1770}
1771
Serban Constantinescu02164b32014-11-13 14:05:07 +00001772void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1773 HBasicBlock* successor) {
1774 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001775 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1776 if (slow_path == nullptr) {
1777 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1778 instruction->SetSlowPath(slow_path);
1779 codegen_->AddSlowPath(slow_path);
1780 if (successor != nullptr) {
1781 DCHECK(successor->IsLoopHeader());
1782 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1783 }
1784 } else {
1785 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1786 }
1787
Serban Constantinescu02164b32014-11-13 14:05:07 +00001788 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1789 Register temp = temps.AcquireW();
1790
Andreas Gampe542451c2016-07-26 09:02:02 -07001791 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001792 if (successor == nullptr) {
1793 __ Cbnz(temp, slow_path->GetEntryLabel());
1794 __ Bind(slow_path->GetReturnLabel());
1795 } else {
1796 __ Cbz(temp, codegen_->GetLabelOf(successor));
1797 __ B(slow_path->GetEntryLabel());
1798 // slow_path will return to GetLabelOf(successor).
1799 }
1800}
1801
Alexandre Rames5319def2014-10-23 10:03:10 +01001802InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1803 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001804 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 assembler_(codegen->GetAssembler()),
1806 codegen_(codegen) {}
1807
1808#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001810
1811#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1812
1813enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001814 // Using a base helps identify when we hit such breakpoints.
1815 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001816#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1817 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1818#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1819};
1820
1821#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001822 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001823 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1824 } \
1825 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1827 locations->SetOut(Location::Any()); \
1828 }
1829 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1830#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1831
1832#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001833#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001834
Alexandre Rames67555f72014-11-18 10:55:16 +00001835void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001836 DCHECK_EQ(instr->InputCount(), 2U);
1837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1838 Primitive::Type type = instr->GetResultType();
1839 switch (type) {
1840 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001841 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001842 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001843 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001845 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001846
1847 case Primitive::kPrimFloat:
1848 case Primitive::kPrimDouble:
1849 locations->SetInAt(0, Location::RequiresFpuRegister());
1850 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001851 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001852 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001853
Alexandre Rames5319def2014-10-23 10:03:10 +01001854 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001855 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001856 }
1857}
1858
Alexandre Rames09a99962015-04-15 11:47:56 +01001859void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001860 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1861
1862 bool object_field_get_with_read_barrier =
1863 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001864 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001865 new (GetGraph()->GetArena()) LocationSummary(instruction,
1866 object_field_get_with_read_barrier ?
1867 LocationSummary::kCallOnSlowPath :
1868 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001869 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001870 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001871 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001872 locations->SetInAt(0, Location::RequiresRegister());
1873 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1874 locations->SetOut(Location::RequiresFpuRegister());
1875 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001876 // The output overlaps for an object field get when read barriers
1877 // are enabled: we do not want the load to overwrite the object's
1878 // location, as we need it to emit the read barrier.
1879 locations->SetOut(
1880 Location::RequiresRegister(),
1881 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001882 }
1883}
1884
1885void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1886 const FieldInfo& field_info) {
1887 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001888 LocationSummary* locations = instruction->GetLocations();
1889 Location base_loc = locations->InAt(0);
1890 Location out = locations->Out();
1891 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001892 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001893 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001894 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001895
Roland Levillain44015862016-01-22 11:47:17 +00001896 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1897 // Object FieldGet with Baker's read barrier case.
1898 MacroAssembler* masm = GetVIXLAssembler();
1899 UseScratchRegisterScope temps(masm);
1900 // /* HeapReference<Object> */ out = *(base + offset)
1901 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1902 Register temp = temps.AcquireW();
1903 // Note that potential implicit null checks are handled in this
1904 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1905 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1906 instruction,
1907 out,
1908 base,
1909 offset,
1910 temp,
1911 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001912 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001913 } else {
1914 // General case.
1915 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001916 // Note that a potential implicit null check is handled in this
1917 // CodeGeneratorARM64::LoadAcquire call.
1918 // NB: LoadAcquire will record the pc info if needed.
1919 codegen_->LoadAcquire(
1920 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001921 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001922 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001923 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001924 }
Roland Levillain44015862016-01-22 11:47:17 +00001925 if (field_type == Primitive::kPrimNot) {
1926 // If read barriers are enabled, emit read barriers other than
1927 // Baker's using a slow path (and also unpoison the loaded
1928 // reference, if heap poisoning is enabled).
1929 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1930 }
Roland Levillain4d027112015-07-01 15:41:14 +01001931 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001932}
1933
1934void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1935 LocationSummary* locations =
1936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1937 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001938 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1939 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1940 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001941 locations->SetInAt(1, Location::RequiresFpuRegister());
1942 } else {
1943 locations->SetInAt(1, Location::RequiresRegister());
1944 }
1945}
1946
1947void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001948 const FieldInfo& field_info,
1949 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001950 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001951 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001952
1953 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001954 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001955 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001956 Offset offset = field_info.GetFieldOffset();
1957 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001958
Roland Levillain4d027112015-07-01 15:41:14 +01001959 {
1960 // We use a block to end the scratch scope before the write barrier, thus
1961 // freeing the temporary registers so they can be used in `MarkGCCard`.
1962 UseScratchRegisterScope temps(GetVIXLAssembler());
1963
1964 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1965 DCHECK(value.IsW());
1966 Register temp = temps.AcquireW();
1967 __ Mov(temp, value.W());
1968 GetAssembler()->PoisonHeapReference(temp.W());
1969 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001970 }
Roland Levillain4d027112015-07-01 15:41:14 +01001971
1972 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001973 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001975 } else {
1976 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1977 codegen_->MaybeRecordImplicitNullCheck(instruction);
1978 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001979 }
1980
1981 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001982 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001983 }
1984}
1985
Alexandre Rames67555f72014-11-18 10:55:16 +00001986void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001987 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001988
1989 switch (type) {
1990 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001991 case Primitive::kPrimLong: {
1992 Register dst = OutputRegister(instr);
1993 Register lhs = InputRegisterAt(instr, 0);
1994 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001995 if (instr->IsAdd()) {
1996 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001997 } else if (instr->IsAnd()) {
1998 __ And(dst, lhs, rhs);
1999 } else if (instr->IsOr()) {
2000 __ Orr(dst, lhs, rhs);
2001 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002002 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002003 } else if (instr->IsRor()) {
2004 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002005 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002006 __ Ror(dst, lhs, shift);
2007 } else {
2008 // Ensure shift distance is in the same size register as the result. If
2009 // we are rotating a long and the shift comes in a w register originally,
2010 // we don't need to sxtw for use as an x since the shift distances are
2011 // all & reg_bits - 1.
2012 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2013 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002014 } else {
2015 DCHECK(instr->IsXor());
2016 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002017 }
2018 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002019 }
2020 case Primitive::kPrimFloat:
2021 case Primitive::kPrimDouble: {
2022 FPRegister dst = OutputFPRegister(instr);
2023 FPRegister lhs = InputFPRegisterAt(instr, 0);
2024 FPRegister rhs = InputFPRegisterAt(instr, 1);
2025 if (instr->IsAdd()) {
2026 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002027 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002028 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002029 } else {
2030 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002031 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002032 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002033 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002034 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002035 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002036 }
2037}
2038
Serban Constantinescu02164b32014-11-13 14:05:07 +00002039void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2040 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2041
2042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2043 Primitive::Type type = instr->GetResultType();
2044 switch (type) {
2045 case Primitive::kPrimInt:
2046 case Primitive::kPrimLong: {
2047 locations->SetInAt(0, Location::RequiresRegister());
2048 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2049 locations->SetOut(Location::RequiresRegister());
2050 break;
2051 }
2052 default:
2053 LOG(FATAL) << "Unexpected shift type " << type;
2054 }
2055}
2056
2057void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2058 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2059
2060 Primitive::Type type = instr->GetType();
2061 switch (type) {
2062 case Primitive::kPrimInt:
2063 case Primitive::kPrimLong: {
2064 Register dst = OutputRegister(instr);
2065 Register lhs = InputRegisterAt(instr, 0);
2066 Operand rhs = InputOperandAt(instr, 1);
2067 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002068 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002069 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002070 if (instr->IsShl()) {
2071 __ Lsl(dst, lhs, shift_value);
2072 } else if (instr->IsShr()) {
2073 __ Asr(dst, lhs, shift_value);
2074 } else {
2075 __ Lsr(dst, lhs, shift_value);
2076 }
2077 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002078 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002079
2080 if (instr->IsShl()) {
2081 __ Lsl(dst, lhs, rhs_reg);
2082 } else if (instr->IsShr()) {
2083 __ Asr(dst, lhs, rhs_reg);
2084 } else {
2085 __ Lsr(dst, lhs, rhs_reg);
2086 }
2087 }
2088 break;
2089 }
2090 default:
2091 LOG(FATAL) << "Unexpected shift operation type " << type;
2092 }
2093}
2094
Alexandre Rames5319def2014-10-23 10:03:10 +01002095void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002096 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002097}
2098
2099void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002100 HandleBinaryOp(instruction);
2101}
2102
2103void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2104 HandleBinaryOp(instruction);
2105}
2106
2107void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2108 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002109}
2110
Artem Serov7fc63502016-02-09 17:15:29 +00002111void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002112 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2114 locations->SetInAt(0, Location::RequiresRegister());
2115 // There is no immediate variant of negated bitwise instructions in AArch64.
2116 locations->SetInAt(1, Location::RequiresRegister());
2117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2118}
2119
Artem Serov7fc63502016-02-09 17:15:29 +00002120void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002121 Register dst = OutputRegister(instr);
2122 Register lhs = InputRegisterAt(instr, 0);
2123 Register rhs = InputRegisterAt(instr, 1);
2124
2125 switch (instr->GetOpKind()) {
2126 case HInstruction::kAnd:
2127 __ Bic(dst, lhs, rhs);
2128 break;
2129 case HInstruction::kOr:
2130 __ Orn(dst, lhs, rhs);
2131 break;
2132 case HInstruction::kXor:
2133 __ Eon(dst, lhs, rhs);
2134 break;
2135 default:
2136 LOG(FATAL) << "Unreachable";
2137 }
2138}
2139
Alexandre Rames8626b742015-11-25 16:28:08 +00002140void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2141 HArm64DataProcWithShifterOp* instruction) {
2142 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2143 instruction->GetType() == Primitive::kPrimLong);
2144 LocationSummary* locations =
2145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2146 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2147 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2148 } else {
2149 locations->SetInAt(0, Location::RequiresRegister());
2150 }
2151 locations->SetInAt(1, Location::RequiresRegister());
2152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2153}
2154
2155void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2156 HArm64DataProcWithShifterOp* instruction) {
2157 Primitive::Type type = instruction->GetType();
2158 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2159 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2160 Register out = OutputRegister(instruction);
2161 Register left;
2162 if (kind != HInstruction::kNeg) {
2163 left = InputRegisterAt(instruction, 0);
2164 }
2165 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2166 // shifter operand operation, the IR generating `right_reg` (input to the type
2167 // conversion) can have a different type from the current instruction's type,
2168 // so we manually indicate the type.
2169 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002170 int64_t shift_amount = instruction->GetShiftAmount() &
2171 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002172
2173 Operand right_operand(0);
2174
2175 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2176 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2177 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2178 } else {
2179 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2180 }
2181
2182 // Logical binary operations do not support extension operations in the
2183 // operand. Note that VIXL would still manage if it was passed by generating
2184 // the extension as a separate instruction.
2185 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2186 DCHECK(!right_operand.IsExtendedRegister() ||
2187 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2188 kind != HInstruction::kNeg));
2189 switch (kind) {
2190 case HInstruction::kAdd:
2191 __ Add(out, left, right_operand);
2192 break;
2193 case HInstruction::kAnd:
2194 __ And(out, left, right_operand);
2195 break;
2196 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002197 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002198 __ Neg(out, right_operand);
2199 break;
2200 case HInstruction::kOr:
2201 __ Orr(out, left, right_operand);
2202 break;
2203 case HInstruction::kSub:
2204 __ Sub(out, left, right_operand);
2205 break;
2206 case HInstruction::kXor:
2207 __ Eor(out, left, right_operand);
2208 break;
2209 default:
2210 LOG(FATAL) << "Unexpected operation kind: " << kind;
2211 UNREACHABLE();
2212 }
2213}
2214
Artem Serov328429f2016-07-06 16:23:04 +01002215void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002216 LocationSummary* locations =
2217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2218 locations->SetInAt(0, Location::RequiresRegister());
2219 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2220 locations->SetOut(Location::RequiresRegister());
2221}
2222
Roland Levillain19c54192016-11-04 13:44:09 +00002223void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002224 __ Add(OutputRegister(instruction),
2225 InputRegisterAt(instruction, 0),
2226 Operand(InputOperandAt(instruction, 1)));
2227}
2228
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002229void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002230 LocationSummary* locations =
2231 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002232 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2233 if (instr->GetOpKind() == HInstruction::kSub &&
2234 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002235 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002236 // Don't allocate register for Mneg instruction.
2237 } else {
2238 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2239 Location::RequiresRegister());
2240 }
2241 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2242 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2244}
2245
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002246void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002247 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002248 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2249 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002250
2251 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2252 // This fixup should be carried out for all multiply-accumulate instructions:
2253 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2254 if (instr->GetType() == Primitive::kPrimLong &&
2255 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2256 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002257 vixl::aarch64::Instruction* prev =
2258 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002259 if (prev->IsLoadOrStore()) {
2260 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002261 vixl::aarch64::CodeBufferCheckScope scope(masm,
2262 kInstructionSize,
2263 vixl::aarch64::CodeBufferCheckScope::kCheck,
2264 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002265 __ nop();
2266 }
2267 }
2268
2269 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002270 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002271 __ Madd(res, mul_left, mul_right, accumulator);
2272 } else {
2273 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002274 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002275 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002276 __ Mneg(res, mul_left, mul_right);
2277 } else {
2278 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2279 __ Msub(res, mul_left, mul_right, accumulator);
2280 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002281 }
2282}
2283
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002284void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002285 bool object_array_get_with_read_barrier =
2286 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002287 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002288 new (GetGraph()->GetArena()) LocationSummary(instruction,
2289 object_array_get_with_read_barrier ?
2290 LocationSummary::kCallOnSlowPath :
2291 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002292 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002293 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002294 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002295 locations->SetInAt(0, Location::RequiresRegister());
2296 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002297 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2298 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2299 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002300 // The output overlaps in the case of an object array get with
2301 // read barriers enabled: we do not want the move to overwrite the
2302 // array's location, as we need it to emit the read barrier.
2303 locations->SetOut(
2304 Location::RequiresRegister(),
2305 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002306 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002307}
2308
2309void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002310 Primitive::Type type = instruction->GetType();
2311 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002312 LocationSummary* locations = instruction->GetLocations();
2313 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002314 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002315 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002316 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2317 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002318 MacroAssembler* masm = GetVIXLAssembler();
2319 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002320 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002321 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002322
Roland Levillain19c54192016-11-04 13:44:09 +00002323 // The read barrier instrumentation of object ArrayGet instructions
2324 // does not support the HIntermediateAddress instruction.
2325 DCHECK(!((type == Primitive::kPrimNot) &&
2326 instruction->GetArray()->IsIntermediateAddress() &&
2327 kEmitCompilerReadBarrier));
2328
Roland Levillain44015862016-01-22 11:47:17 +00002329 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2330 // Object ArrayGet with Baker's read barrier case.
2331 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002332 // Note that a potential implicit null check is handled in the
2333 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2334 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2335 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002336 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002337 // General case.
2338 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002339 Register length;
2340 if (maybe_compressed_char_at) {
2341 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2342 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002343 if (instruction->GetArray()->IsIntermediateAddress()) {
2344 DCHECK_LT(count_offset, offset);
2345 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2346 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2347 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2348 } else {
2349 __ Ldr(length, HeapOperand(obj, count_offset));
2350 }
jessicahandojo05765752016-09-09 19:01:32 -07002351 codegen_->MaybeRecordImplicitNullCheck(instruction);
2352 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002353 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002354 if (maybe_compressed_char_at) {
2355 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002356 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2357 "Expecting 0=compressed, 1=uncompressed");
2358 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002359 __ Ldrb(Register(OutputCPURegister(instruction)),
2360 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2361 __ B(&done);
2362 __ Bind(&uncompressed_load);
2363 __ Ldrh(Register(OutputCPURegister(instruction)),
2364 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2365 __ Bind(&done);
2366 } else {
2367 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2368 source = HeapOperand(obj, offset);
2369 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002370 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002371 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002372 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002373 // We do not need to compute the intermediate address from the array: the
2374 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002375 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002376 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002377 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002378 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2379 }
2380 temp = obj;
2381 } else {
2382 __ Add(temp, obj, offset);
2383 }
jessicahandojo05765752016-09-09 19:01:32 -07002384 if (maybe_compressed_char_at) {
2385 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002386 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2387 "Expecting 0=compressed, 1=uncompressed");
2388 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002389 __ Ldrb(Register(OutputCPURegister(instruction)),
2390 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2391 __ B(&done);
2392 __ Bind(&uncompressed_load);
2393 __ Ldrh(Register(OutputCPURegister(instruction)),
2394 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2395 __ Bind(&done);
2396 } else {
2397 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2398 }
Roland Levillain44015862016-01-22 11:47:17 +00002399 }
jessicahandojo05765752016-09-09 19:01:32 -07002400 if (!maybe_compressed_char_at) {
2401 codegen_->Load(type, OutputCPURegister(instruction), source);
2402 codegen_->MaybeRecordImplicitNullCheck(instruction);
2403 }
Roland Levillain44015862016-01-22 11:47:17 +00002404
2405 if (type == Primitive::kPrimNot) {
2406 static_assert(
2407 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2408 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2409 Location obj_loc = locations->InAt(0);
2410 if (index.IsConstant()) {
2411 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2412 } else {
2413 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2414 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002415 }
Roland Levillain4d027112015-07-01 15:41:14 +01002416 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002417}
2418
Alexandre Rames5319def2014-10-23 10:03:10 +01002419void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2420 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2421 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002423}
2424
2425void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002426 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002427 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002428 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002429 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002430 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002431 // Mask out compression flag from String's array length.
2432 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002433 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002434 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002435}
2436
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002437void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002438 Primitive::Type value_type = instruction->GetComponentType();
2439
2440 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2442 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002443 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002444 LocationSummary::kCallOnSlowPath :
2445 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002448 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2449 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2450 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002451 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002452 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002453 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002454 }
2455}
2456
2457void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2458 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002459 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002460 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002461 bool needs_write_barrier =
2462 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002463
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002464 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002465 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002466 CPURegister source = value;
2467 Location index = locations->InAt(1);
2468 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2469 MemOperand destination = HeapOperand(array);
2470 MacroAssembler* masm = GetVIXLAssembler();
2471 BlockPoolsScope block_pools(masm);
2472
2473 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002474 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002475 if (index.IsConstant()) {
2476 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2477 destination = HeapOperand(array, offset);
2478 } else {
2479 UseScratchRegisterScope temps(masm);
2480 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002481 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002482 // We do not need to compute the intermediate address from the array: the
2483 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002484 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002485 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002486 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002487 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2488 }
2489 temp = array;
2490 } else {
2491 __ Add(temp, array, offset);
2492 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002493 destination = HeapOperand(temp,
2494 XRegisterFrom(index),
2495 LSL,
2496 Primitive::ComponentSizeShift(value_type));
2497 }
2498 codegen_->Store(value_type, value, destination);
2499 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002500 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002501 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002502 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002503 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002504 {
2505 // We use a block to end the scratch scope before the write barrier, thus
2506 // freeing the temporary registers so they can be used in `MarkGCCard`.
2507 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002508 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002509 if (index.IsConstant()) {
2510 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002511 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002512 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002513 destination = HeapOperand(temp,
2514 XRegisterFrom(index),
2515 LSL,
2516 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002517 }
2518
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002519 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2520 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2521 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2522
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002523 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002524 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2525 codegen_->AddSlowPath(slow_path);
2526 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002527 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002528 __ Cbnz(Register(value), &non_zero);
2529 if (!index.IsConstant()) {
2530 __ Add(temp, array, offset);
2531 }
2532 __ Str(wzr, destination);
2533 codegen_->MaybeRecordImplicitNullCheck(instruction);
2534 __ B(&done);
2535 __ Bind(&non_zero);
2536 }
2537
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002538 // Note that when Baker read barriers are enabled, the type
2539 // checks are performed without read barriers. This is fine,
2540 // even in the case where a class object is in the from-space
2541 // after the flip, as a comparison involving such a type would
2542 // not produce a false positive; it may of course produce a
2543 // false negative, in which case we would take the ArraySet
2544 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002545
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002546 Register temp2 = temps.AcquireSameSizeAs(array);
2547 // /* HeapReference<Class> */ temp = array->klass_
2548 __ Ldr(temp, HeapOperand(array, class_offset));
2549 codegen_->MaybeRecordImplicitNullCheck(instruction);
2550 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002551
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002552 // /* HeapReference<Class> */ temp = temp->component_type_
2553 __ Ldr(temp, HeapOperand(temp, component_offset));
2554 // /* HeapReference<Class> */ temp2 = value->klass_
2555 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2556 // If heap poisoning is enabled, no need to unpoison `temp`
2557 // nor `temp2`, as we are comparing two poisoned references.
2558 __ Cmp(temp, temp2);
2559 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002560
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002561 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2562 vixl::aarch64::Label do_put;
2563 __ B(eq, &do_put);
2564 // If heap poisoning is enabled, the `temp` reference has
2565 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002566 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2567
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002568 // /* HeapReference<Class> */ temp = temp->super_class_
2569 __ Ldr(temp, HeapOperand(temp, super_offset));
2570 // If heap poisoning is enabled, no need to unpoison
2571 // `temp`, as we are comparing against null below.
2572 __ Cbnz(temp, slow_path->GetEntryLabel());
2573 __ Bind(&do_put);
2574 } else {
2575 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002576 }
2577 }
2578
2579 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002580 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002581 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002582 __ Mov(temp2, value.W());
2583 GetAssembler()->PoisonHeapReference(temp2);
2584 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002585 }
2586
2587 if (!index.IsConstant()) {
2588 __ Add(temp, array, offset);
2589 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002590 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002591
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002592 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002593 codegen_->MaybeRecordImplicitNullCheck(instruction);
2594 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002595 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002596
2597 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2598
2599 if (done.IsLinked()) {
2600 __ Bind(&done);
2601 }
2602
2603 if (slow_path != nullptr) {
2604 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002605 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002606 }
2607}
2608
Alexandre Rames67555f72014-11-18 10:55:16 +00002609void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002610 RegisterSet caller_saves = RegisterSet::Empty();
2611 InvokeRuntimeCallingConvention calling_convention;
2612 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2613 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2614 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002615 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002616 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002617}
2618
2619void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002620 BoundsCheckSlowPathARM64* slow_path =
2621 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002622 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002623 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2624 __ B(slow_path->GetEntryLabel(), hs);
2625}
2626
Alexandre Rames67555f72014-11-18 10:55:16 +00002627void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2628 LocationSummary* locations =
2629 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2630 locations->SetInAt(0, Location::RequiresRegister());
2631 if (check->HasUses()) {
2632 locations->SetOut(Location::SameAsFirstInput());
2633 }
2634}
2635
2636void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2637 // We assume the class is not null.
2638 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2639 check->GetLoadClass(), check, check->GetDexPc(), true);
2640 codegen_->AddSlowPath(slow_path);
2641 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2642}
2643
Roland Levillain1a653882016-03-18 18:05:57 +00002644static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2645 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2646 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2647}
2648
2649void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2650 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2651 Location rhs_loc = instruction->GetLocations()->InAt(1);
2652 if (rhs_loc.IsConstant()) {
2653 // 0.0 is the only immediate that can be encoded directly in
2654 // an FCMP instruction.
2655 //
2656 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2657 // specify that in a floating-point comparison, positive zero
2658 // and negative zero are considered equal, so we can use the
2659 // literal 0.0 for both cases here.
2660 //
2661 // Note however that some methods (Float.equal, Float.compare,
2662 // Float.compareTo, Double.equal, Double.compare,
2663 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2664 // StrictMath.min) consider 0.0 to be (strictly) greater than
2665 // -0.0. So if we ever translate calls to these methods into a
2666 // HCompare instruction, we must handle the -0.0 case with
2667 // care here.
2668 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2669 __ Fcmp(lhs_reg, 0.0);
2670 } else {
2671 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2672 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002673}
2674
Serban Constantinescu02164b32014-11-13 14:05:07 +00002675void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002676 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002677 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2678 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002679 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002680 case Primitive::kPrimBoolean:
2681 case Primitive::kPrimByte:
2682 case Primitive::kPrimShort:
2683 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002684 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002685 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002686 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002687 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002688 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2689 break;
2690 }
2691 case Primitive::kPrimFloat:
2692 case Primitive::kPrimDouble: {
2693 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002694 locations->SetInAt(1,
2695 IsFloatingPointZeroConstant(compare->InputAt(1))
2696 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2697 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002698 locations->SetOut(Location::RequiresRegister());
2699 break;
2700 }
2701 default:
2702 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2703 }
2704}
2705
2706void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2707 Primitive::Type in_type = compare->InputAt(0)->GetType();
2708
2709 // 0 if: left == right
2710 // 1 if: left > right
2711 // -1 if: left < right
2712 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002713 case Primitive::kPrimBoolean:
2714 case Primitive::kPrimByte:
2715 case Primitive::kPrimShort:
2716 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002717 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002718 case Primitive::kPrimLong: {
2719 Register result = OutputRegister(compare);
2720 Register left = InputRegisterAt(compare, 0);
2721 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002722 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002723 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2724 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002725 break;
2726 }
2727 case Primitive::kPrimFloat:
2728 case Primitive::kPrimDouble: {
2729 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002730 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002731 __ Cset(result, ne);
2732 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002733 break;
2734 }
2735 default:
2736 LOG(FATAL) << "Unimplemented compare type " << in_type;
2737 }
2738}
2739
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002740void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002741 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002742
2743 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2744 locations->SetInAt(0, Location::RequiresFpuRegister());
2745 locations->SetInAt(1,
2746 IsFloatingPointZeroConstant(instruction->InputAt(1))
2747 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2748 : Location::RequiresFpuRegister());
2749 } else {
2750 // Integer cases.
2751 locations->SetInAt(0, Location::RequiresRegister());
2752 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2753 }
2754
David Brazdilb3e773e2016-01-26 11:28:37 +00002755 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002757 }
2758}
2759
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002760void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002761 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002762 return;
2763 }
2764
2765 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002766 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002767 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002768
Roland Levillain7f63c522015-07-13 15:54:55 +00002769 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002770 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002771 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002772 } else {
2773 // Integer cases.
2774 Register lhs = InputRegisterAt(instruction, 0);
2775 Operand rhs = InputOperandAt(instruction, 1);
2776 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002777 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002778 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002779}
2780
2781#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2782 M(Equal) \
2783 M(NotEqual) \
2784 M(LessThan) \
2785 M(LessThanOrEqual) \
2786 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002787 M(GreaterThanOrEqual) \
2788 M(Below) \
2789 M(BelowOrEqual) \
2790 M(Above) \
2791 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002792#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002793void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2794void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002795FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002796#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002797#undef FOR_EACH_CONDITION_INSTRUCTION
2798
Zheng Xuc6667102015-05-15 16:08:45 +08002799void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2800 DCHECK(instruction->IsDiv() || instruction->IsRem());
2801
2802 LocationSummary* locations = instruction->GetLocations();
2803 Location second = locations->InAt(1);
2804 DCHECK(second.IsConstant());
2805
2806 Register out = OutputRegister(instruction);
2807 Register dividend = InputRegisterAt(instruction, 0);
2808 int64_t imm = Int64FromConstant(second.GetConstant());
2809 DCHECK(imm == 1 || imm == -1);
2810
2811 if (instruction->IsRem()) {
2812 __ Mov(out, 0);
2813 } else {
2814 if (imm == 1) {
2815 __ Mov(out, dividend);
2816 } else {
2817 __ Neg(out, dividend);
2818 }
2819 }
2820}
2821
2822void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2823 DCHECK(instruction->IsDiv() || instruction->IsRem());
2824
2825 LocationSummary* locations = instruction->GetLocations();
2826 Location second = locations->InAt(1);
2827 DCHECK(second.IsConstant());
2828
2829 Register out = OutputRegister(instruction);
2830 Register dividend = InputRegisterAt(instruction, 0);
2831 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002832 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002833 int ctz_imm = CTZ(abs_imm);
2834
2835 UseScratchRegisterScope temps(GetVIXLAssembler());
2836 Register temp = temps.AcquireSameSizeAs(out);
2837
2838 if (instruction->IsDiv()) {
2839 __ Add(temp, dividend, abs_imm - 1);
2840 __ Cmp(dividend, 0);
2841 __ Csel(out, temp, dividend, lt);
2842 if (imm > 0) {
2843 __ Asr(out, out, ctz_imm);
2844 } else {
2845 __ Neg(out, Operand(out, ASR, ctz_imm));
2846 }
2847 } else {
2848 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2849 __ Asr(temp, dividend, bits - 1);
2850 __ Lsr(temp, temp, bits - ctz_imm);
2851 __ Add(out, dividend, temp);
2852 __ And(out, out, abs_imm - 1);
2853 __ Sub(out, out, temp);
2854 }
2855}
2856
2857void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2858 DCHECK(instruction->IsDiv() || instruction->IsRem());
2859
2860 LocationSummary* locations = instruction->GetLocations();
2861 Location second = locations->InAt(1);
2862 DCHECK(second.IsConstant());
2863
2864 Register out = OutputRegister(instruction);
2865 Register dividend = InputRegisterAt(instruction, 0);
2866 int64_t imm = Int64FromConstant(second.GetConstant());
2867
2868 Primitive::Type type = instruction->GetResultType();
2869 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2870
2871 int64_t magic;
2872 int shift;
2873 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2874
2875 UseScratchRegisterScope temps(GetVIXLAssembler());
2876 Register temp = temps.AcquireSameSizeAs(out);
2877
2878 // temp = get_high(dividend * magic)
2879 __ Mov(temp, magic);
2880 if (type == Primitive::kPrimLong) {
2881 __ Smulh(temp, dividend, temp);
2882 } else {
2883 __ Smull(temp.X(), dividend, temp);
2884 __ Lsr(temp.X(), temp.X(), 32);
2885 }
2886
2887 if (imm > 0 && magic < 0) {
2888 __ Add(temp, temp, dividend);
2889 } else if (imm < 0 && magic > 0) {
2890 __ Sub(temp, temp, dividend);
2891 }
2892
2893 if (shift != 0) {
2894 __ Asr(temp, temp, shift);
2895 }
2896
2897 if (instruction->IsDiv()) {
2898 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2899 } else {
2900 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2901 // TODO: Strength reduction for msub.
2902 Register temp_imm = temps.AcquireSameSizeAs(out);
2903 __ Mov(temp_imm, imm);
2904 __ Msub(out, temp, temp_imm, dividend);
2905 }
2906}
2907
2908void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2909 DCHECK(instruction->IsDiv() || instruction->IsRem());
2910 Primitive::Type type = instruction->GetResultType();
2911 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2912
2913 LocationSummary* locations = instruction->GetLocations();
2914 Register out = OutputRegister(instruction);
2915 Location second = locations->InAt(1);
2916
2917 if (second.IsConstant()) {
2918 int64_t imm = Int64FromConstant(second.GetConstant());
2919
2920 if (imm == 0) {
2921 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2922 } else if (imm == 1 || imm == -1) {
2923 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002924 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002925 DivRemByPowerOfTwo(instruction);
2926 } else {
2927 DCHECK(imm <= -2 || imm >= 2);
2928 GenerateDivRemWithAnyConstant(instruction);
2929 }
2930 } else {
2931 Register dividend = InputRegisterAt(instruction, 0);
2932 Register divisor = InputRegisterAt(instruction, 1);
2933 if (instruction->IsDiv()) {
2934 __ Sdiv(out, dividend, divisor);
2935 } else {
2936 UseScratchRegisterScope temps(GetVIXLAssembler());
2937 Register temp = temps.AcquireSameSizeAs(out);
2938 __ Sdiv(temp, dividend, divisor);
2939 __ Msub(out, temp, divisor, dividend);
2940 }
2941 }
2942}
2943
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002944void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2945 LocationSummary* locations =
2946 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2947 switch (div->GetResultType()) {
2948 case Primitive::kPrimInt:
2949 case Primitive::kPrimLong:
2950 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002951 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2953 break;
2954
2955 case Primitive::kPrimFloat:
2956 case Primitive::kPrimDouble:
2957 locations->SetInAt(0, Location::RequiresFpuRegister());
2958 locations->SetInAt(1, Location::RequiresFpuRegister());
2959 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2960 break;
2961
2962 default:
2963 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2964 }
2965}
2966
2967void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2968 Primitive::Type type = div->GetResultType();
2969 switch (type) {
2970 case Primitive::kPrimInt:
2971 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002972 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002973 break;
2974
2975 case Primitive::kPrimFloat:
2976 case Primitive::kPrimDouble:
2977 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2978 break;
2979
2980 default:
2981 LOG(FATAL) << "Unexpected div type " << type;
2982 }
2983}
2984
Alexandre Rames67555f72014-11-18 10:55:16 +00002985void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002986 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002987 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002988}
2989
2990void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2991 SlowPathCodeARM64* slow_path =
2992 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2993 codegen_->AddSlowPath(slow_path);
2994 Location value = instruction->GetLocations()->InAt(0);
2995
Alexandre Rames3e69f162014-12-10 10:36:50 +00002996 Primitive::Type type = instruction->GetType();
2997
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002998 if (!Primitive::IsIntegralType(type)) {
2999 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003000 return;
3001 }
3002
Alexandre Rames67555f72014-11-18 10:55:16 +00003003 if (value.IsConstant()) {
3004 int64_t divisor = Int64ConstantFrom(value);
3005 if (divisor == 0) {
3006 __ B(slow_path->GetEntryLabel());
3007 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003008 // A division by a non-null constant is valid. We don't need to perform
3009 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003010 }
3011 } else {
3012 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3013 }
3014}
3015
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003016void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3019 locations->SetOut(Location::ConstantLocation(constant));
3020}
3021
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003022void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3023 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003024 // Will be generated at use site.
3025}
3026
Alexandre Rames5319def2014-10-23 10:03:10 +01003027void LocationsBuilderARM64::VisitExit(HExit* exit) {
3028 exit->SetLocations(nullptr);
3029}
3030
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003031void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003032}
3033
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003034void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3035 LocationSummary* locations =
3036 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3037 locations->SetOut(Location::ConstantLocation(constant));
3038}
3039
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003040void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003041 // Will be generated at use site.
3042}
3043
David Brazdilfc6a86a2015-06-26 10:33:45 +00003044void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003045 DCHECK(!successor->IsExitBlock());
3046 HBasicBlock* block = got->GetBlock();
3047 HInstruction* previous = got->GetPrevious();
3048 HLoopInformation* info = block->GetLoopInformation();
3049
David Brazdil46e2a392015-03-16 17:31:52 +00003050 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003051 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3052 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3053 return;
3054 }
3055 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3056 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3057 }
3058 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003059 __ B(codegen_->GetLabelOf(successor));
3060 }
3061}
3062
David Brazdilfc6a86a2015-06-26 10:33:45 +00003063void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3064 got->SetLocations(nullptr);
3065}
3066
3067void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3068 HandleGoto(got, got->GetSuccessor());
3069}
3070
3071void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3072 try_boundary->SetLocations(nullptr);
3073}
3074
3075void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3076 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3077 if (!successor->IsExitBlock()) {
3078 HandleGoto(try_boundary, successor);
3079 }
3080}
3081
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003082void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003083 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003084 vixl::aarch64::Label* true_target,
3085 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003086 // FP branching requires both targets to be explicit. If either of the targets
3087 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003088 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003089 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003090
David Brazdil0debae72015-11-12 18:37:00 +00003091 if (true_target == nullptr && false_target == nullptr) {
3092 // Nothing to do. The code always falls through.
3093 return;
3094 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003095 // Constant condition, statically compared against "true" (integer value 1).
3096 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003097 if (true_target != nullptr) {
3098 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003099 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003100 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003101 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003102 if (false_target != nullptr) {
3103 __ B(false_target);
3104 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003105 }
David Brazdil0debae72015-11-12 18:37:00 +00003106 return;
3107 }
3108
3109 // The following code generates these patterns:
3110 // (1) true_target == nullptr && false_target != nullptr
3111 // - opposite condition true => branch to false_target
3112 // (2) true_target != nullptr && false_target == nullptr
3113 // - condition true => branch to true_target
3114 // (3) true_target != nullptr && false_target != nullptr
3115 // - condition true => branch to true_target
3116 // - branch to false_target
3117 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003118 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003119 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003120 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003121 if (true_target == nullptr) {
3122 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3123 } else {
3124 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3125 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003126 } else {
3127 // The condition instruction has not been materialized, use its inputs as
3128 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003129 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003130
David Brazdil0debae72015-11-12 18:37:00 +00003131 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003132 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003133 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003134 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003135 IfCondition opposite_condition = condition->GetOppositeCondition();
3136 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003137 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003138 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003139 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003140 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003141 // Integer cases.
3142 Register lhs = InputRegisterAt(condition, 0);
3143 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003144
3145 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003146 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003147 if (true_target == nullptr) {
3148 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3149 non_fallthrough_target = false_target;
3150 } else {
3151 arm64_cond = ARM64Condition(condition->GetCondition());
3152 non_fallthrough_target = true_target;
3153 }
3154
Aart Bik086d27e2016-01-20 17:02:00 -08003155 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003156 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003157 switch (arm64_cond) {
3158 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003159 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003160 break;
3161 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003162 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003163 break;
3164 case lt:
3165 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003166 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003167 break;
3168 case ge:
3169 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003170 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003171 break;
3172 default:
3173 // Without the `static_cast` the compiler throws an error for
3174 // `-Werror=sign-promo`.
3175 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3176 }
3177 } else {
3178 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003179 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003180 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003181 }
3182 }
David Brazdil0debae72015-11-12 18:37:00 +00003183
3184 // If neither branch falls through (case 3), the conditional branch to `true_target`
3185 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3186 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003187 __ B(false_target);
3188 }
David Brazdil0debae72015-11-12 18:37:00 +00003189
3190 if (fallthrough_target.IsLinked()) {
3191 __ Bind(&fallthrough_target);
3192 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003193}
3194
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003195void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003197 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003198 locations->SetInAt(0, Location::RequiresRegister());
3199 }
3200}
3201
3202void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003203 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3204 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003205 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3206 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3207 true_target = nullptr;
3208 }
3209 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3210 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3211 false_target = nullptr;
3212 }
David Brazdil0debae72015-11-12 18:37:00 +00003213 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003214}
3215
3216void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3217 LocationSummary* locations = new (GetGraph()->GetArena())
3218 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003219 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003220 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221 locations->SetInAt(0, Location::RequiresRegister());
3222 }
3223}
3224
3225void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003226 SlowPathCodeARM64* slow_path =
3227 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003228 GenerateTestAndBranch(deoptimize,
3229 /* condition_input_index */ 0,
3230 slow_path->GetEntryLabel(),
3231 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003232}
3233
David Brazdilc0b601b2016-02-08 14:20:45 +00003234static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3235 return condition->IsCondition() &&
3236 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3237}
3238
Alexandre Rames880f1192016-06-13 16:04:50 +01003239static inline Condition GetConditionForSelect(HCondition* condition) {
3240 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003241 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3242 : ARM64Condition(cond);
3243}
3244
David Brazdil74eb1b22015-12-14 11:44:01 +00003245void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003247 if (Primitive::IsFloatingPointType(select->GetType())) {
3248 locations->SetInAt(0, Location::RequiresFpuRegister());
3249 locations->SetInAt(1, Location::RequiresFpuRegister());
3250 locations->SetOut(Location::RequiresFpuRegister());
3251 } else {
3252 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3253 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3254 bool is_true_value_constant = cst_true_value != nullptr;
3255 bool is_false_value_constant = cst_false_value != nullptr;
3256 // Ask VIXL whether we should synthesize constants in registers.
3257 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3258 Operand true_op = is_true_value_constant ?
3259 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3260 Operand false_op = is_false_value_constant ?
3261 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3262 bool true_value_in_register = false;
3263 bool false_value_in_register = false;
3264 MacroAssembler::GetCselSynthesisInformation(
3265 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3266 true_value_in_register |= !is_true_value_constant;
3267 false_value_in_register |= !is_false_value_constant;
3268
3269 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3270 : Location::ConstantLocation(cst_true_value));
3271 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3272 : Location::ConstantLocation(cst_false_value));
3273 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003274 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003275
David Brazdil74eb1b22015-12-14 11:44:01 +00003276 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3277 locations->SetInAt(2, Location::RequiresRegister());
3278 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003279}
3280
3281void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003282 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003283 Condition csel_cond;
3284
3285 if (IsBooleanValueOrMaterializedCondition(cond)) {
3286 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003287 // Use the condition flags set by the previous instruction.
3288 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003289 } else {
3290 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003291 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003292 }
3293 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003294 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003295 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003296 } else {
3297 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003298 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003299 }
3300
Alexandre Rames880f1192016-06-13 16:04:50 +01003301 if (Primitive::IsFloatingPointType(select->GetType())) {
3302 __ Fcsel(OutputFPRegister(select),
3303 InputFPRegisterAt(select, 1),
3304 InputFPRegisterAt(select, 0),
3305 csel_cond);
3306 } else {
3307 __ Csel(OutputRegister(select),
3308 InputOperandAt(select, 1),
3309 InputOperandAt(select, 0),
3310 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003311 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003312}
3313
David Srbecky0cf44932015-12-09 14:09:59 +00003314void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3315 new (GetGraph()->GetArena()) LocationSummary(info);
3316}
3317
David Srbeckyd28f4a02016-03-14 17:14:24 +00003318void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3319 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003320}
3321
3322void CodeGeneratorARM64::GenerateNop() {
3323 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003324}
3325
Alexandre Rames5319def2014-10-23 10:03:10 +01003326void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003327 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003328}
3329
3330void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003331 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003332}
3333
3334void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003335 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003336}
3337
3338void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003339 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003340}
3341
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003342// Temp is used for read barrier.
3343static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3344 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003345 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003346 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3347 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3348 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3349 return 1;
3350 }
3351 return 0;
3352}
3353
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003354// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003355// interface pointer, one for loading the current interface.
3356// The other checks have one temp for loading the object's class.
3357static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3358 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3359 return 3;
3360 }
3361 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003362}
3363
Alexandre Rames67555f72014-11-18 10:55:16 +00003364void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003366 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003367 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 case TypeCheckKind::kExactCheck:
3370 case TypeCheckKind::kAbstractClassCheck:
3371 case TypeCheckKind::kClassHierarchyCheck:
3372 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003373 call_kind =
3374 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003375 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003376 break;
3377 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378 case TypeCheckKind::kUnresolvedCheck:
3379 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003380 call_kind = LocationSummary::kCallOnSlowPath;
3381 break;
3382 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003383
Alexandre Rames67555f72014-11-18 10:55:16 +00003384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003385 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003386 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003387 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003388 locations->SetInAt(0, Location::RequiresRegister());
3389 locations->SetInAt(1, Location::RequiresRegister());
3390 // The "out" register is used as a temporary, so it overlaps with the inputs.
3391 // Note that TypeCheckSlowPathARM64 uses this register too.
3392 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003393 // Add temps if necessary for read barriers.
3394 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003395}
3396
3397void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003398 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003399 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003400 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003401 Register obj = InputRegisterAt(instruction, 0);
3402 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003403 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003404 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003405 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3406 DCHECK_LE(num_temps, 1u);
3407 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003408 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3409 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3410 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3411 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003412
Scott Wakeling97c72b72016-06-24 16:19:36 +01003413 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003415
3416 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003417 // Avoid null check if we know `obj` is not null.
3418 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003419 __ Cbz(obj, &zero);
3420 }
3421
Roland Levillain44015862016-01-22 11:47:17 +00003422 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003424 // /* HeapReference<Class> */ out = obj->klass_
3425 GenerateReferenceLoadTwoRegisters(instruction,
3426 out_loc,
3427 obj_loc,
3428 class_offset,
3429 maybe_temp_loc,
3430 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 __ Cmp(out, cls);
3432 __ Cset(out, eq);
3433 if (zero.IsLinked()) {
3434 __ B(&done);
3435 }
3436 break;
3437 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003439 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003440 // /* HeapReference<Class> */ out = obj->klass_
3441 GenerateReferenceLoadTwoRegisters(instruction,
3442 out_loc,
3443 obj_loc,
3444 class_offset,
3445 maybe_temp_loc,
3446 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447 // If the class is abstract, we eagerly fetch the super class of the
3448 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003449 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003451 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003452 GenerateReferenceLoadOneRegister(instruction,
3453 out_loc,
3454 super_offset,
3455 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003456 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003457 // If `out` is null, we use it for the result, and jump to `done`.
3458 __ Cbz(out, &done);
3459 __ Cmp(out, cls);
3460 __ B(ne, &loop);
3461 __ Mov(out, 1);
3462 if (zero.IsLinked()) {
3463 __ B(&done);
3464 }
3465 break;
3466 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003468 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003469 // /* HeapReference<Class> */ out = obj->klass_
3470 GenerateReferenceLoadTwoRegisters(instruction,
3471 out_loc,
3472 obj_loc,
3473 class_offset,
3474 maybe_temp_loc,
3475 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003476 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003477 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003478 __ Bind(&loop);
3479 __ Cmp(out, cls);
3480 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003481 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003482 GenerateReferenceLoadOneRegister(instruction,
3483 out_loc,
3484 super_offset,
3485 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003486 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003487 __ Cbnz(out, &loop);
3488 // If `out` is null, we use it for the result, and jump to `done`.
3489 __ B(&done);
3490 __ Bind(&success);
3491 __ Mov(out, 1);
3492 if (zero.IsLinked()) {
3493 __ B(&done);
3494 }
3495 break;
3496 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003497
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003498 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003499 // /* HeapReference<Class> */ out = obj->klass_
3500 GenerateReferenceLoadTwoRegisters(instruction,
3501 out_loc,
3502 obj_loc,
3503 class_offset,
3504 maybe_temp_loc,
3505 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003506 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003507 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003508 __ Cmp(out, cls);
3509 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003510 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003511 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003512 GenerateReferenceLoadOneRegister(instruction,
3513 out_loc,
3514 component_offset,
3515 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003516 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003517 // If `out` is null, we use it for the result, and jump to `done`.
3518 __ Cbz(out, &done);
3519 __ Ldrh(out, HeapOperand(out, primitive_offset));
3520 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3521 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003522 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003523 __ Mov(out, 1);
3524 __ B(&done);
3525 break;
3526 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003527
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003528 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003529 // No read barrier since the slow path will retry upon failure.
3530 // /* HeapReference<Class> */ out = obj->klass_
3531 GenerateReferenceLoadTwoRegisters(instruction,
3532 out_loc,
3533 obj_loc,
3534 class_offset,
3535 maybe_temp_loc,
3536 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003537 __ Cmp(out, cls);
3538 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003539 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3540 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003541 codegen_->AddSlowPath(slow_path);
3542 __ B(ne, slow_path->GetEntryLabel());
3543 __ Mov(out, 1);
3544 if (zero.IsLinked()) {
3545 __ B(&done);
3546 }
3547 break;
3548 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003549
Calin Juravle98893e12015-10-02 21:05:03 +01003550 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003551 case TypeCheckKind::kInterfaceCheck: {
3552 // Note that we indeed only call on slow path, but we always go
3553 // into the slow path for the unresolved and interface check
3554 // cases.
3555 //
3556 // We cannot directly call the InstanceofNonTrivial runtime
3557 // entry point without resorting to a type checking slow path
3558 // here (i.e. by calling InvokeRuntime directly), as it would
3559 // require to assign fixed registers for the inputs of this
3560 // HInstanceOf instruction (following the runtime calling
3561 // convention), which might be cluttered by the potential first
3562 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003563 //
3564 // TODO: Introduce a new runtime entry point taking the object
3565 // to test (instead of its class) as argument, and let it deal
3566 // with the read barrier issues. This will let us refactor this
3567 // case of the `switch` code as it was previously (with a direct
3568 // call to the runtime not using a type checking slow path).
3569 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003570 DCHECK(locations->OnlyCallsOnSlowPath());
3571 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3572 /* is_fatal */ false);
3573 codegen_->AddSlowPath(slow_path);
3574 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003575 if (zero.IsLinked()) {
3576 __ B(&done);
3577 }
3578 break;
3579 }
3580 }
3581
3582 if (zero.IsLinked()) {
3583 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003584 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003585 }
3586
3587 if (done.IsLinked()) {
3588 __ Bind(&done);
3589 }
3590
3591 if (slow_path != nullptr) {
3592 __ Bind(slow_path->GetExitLabel());
3593 }
3594}
3595
3596void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3597 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3598 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3599
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003600 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3601 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003602 case TypeCheckKind::kExactCheck:
3603 case TypeCheckKind::kAbstractClassCheck:
3604 case TypeCheckKind::kClassHierarchyCheck:
3605 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003606 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3607 LocationSummary::kCallOnSlowPath :
3608 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003609 break;
3610 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003611 case TypeCheckKind::kUnresolvedCheck:
3612 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003613 call_kind = LocationSummary::kCallOnSlowPath;
3614 break;
3615 }
3616
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3618 locations->SetInAt(0, Location::RequiresRegister());
3619 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003620 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3621 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003622}
3623
3624void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003625 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003626 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003627 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003628 Register obj = InputRegisterAt(instruction, 0);
3629 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003630 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3631 DCHECK_GE(num_temps, 1u);
3632 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003633 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003634 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3635 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003636 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003637 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3638 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3639 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3640 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3641 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3642 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3643 const uint32_t object_array_data_offset =
3644 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003645
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003646 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003647 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3648 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3649 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003650 if (!kEmitCompilerReadBarrier) {
3651 is_type_check_slow_path_fatal =
3652 (type_check_kind == TypeCheckKind::kExactCheck ||
3653 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3654 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3655 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3656 !instruction->CanThrowIntoCatchBlock();
3657 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003658 SlowPathCodeARM64* type_check_slow_path =
3659 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3660 is_type_check_slow_path_fatal);
3661 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003662
Scott Wakeling97c72b72016-06-24 16:19:36 +01003663 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003664 // Avoid null check if we know obj is not null.
3665 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003666 __ Cbz(obj, &done);
3667 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003668
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003669 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003670 case TypeCheckKind::kExactCheck:
3671 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003672 // /* HeapReference<Class> */ temp = obj->klass_
3673 GenerateReferenceLoadTwoRegisters(instruction,
3674 temp_loc,
3675 obj_loc,
3676 class_offset,
3677 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003678 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003679
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003680 __ Cmp(temp, cls);
3681 // Jump to slow path for throwing the exception or doing a
3682 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003683 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003684 break;
3685 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003686
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003687 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003688 // /* HeapReference<Class> */ temp = obj->klass_
3689 GenerateReferenceLoadTwoRegisters(instruction,
3690 temp_loc,
3691 obj_loc,
3692 class_offset,
3693 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003694 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003695
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003696 // If the class is abstract, we eagerly fetch the super class of the
3697 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003698 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003699 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003700 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003701 GenerateReferenceLoadOneRegister(instruction,
3702 temp_loc,
3703 super_offset,
3704 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003705 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003706
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003707 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3708 // exception.
3709 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3710 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003711 __ Cmp(temp, cls);
3712 __ B(ne, &loop);
3713 break;
3714 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003715
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003716 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003717 // /* HeapReference<Class> */ temp = obj->klass_
3718 GenerateReferenceLoadTwoRegisters(instruction,
3719 temp_loc,
3720 obj_loc,
3721 class_offset,
3722 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003723 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003724
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003725 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003726 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003727 __ Bind(&loop);
3728 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003729 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003730
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003731 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003732 GenerateReferenceLoadOneRegister(instruction,
3733 temp_loc,
3734 super_offset,
3735 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003736 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003737
3738 // If the class reference currently in `temp` is not null, jump
3739 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003740 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003741 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003742 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003743 break;
3744 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003745
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003746 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003747 // /* HeapReference<Class> */ temp = obj->klass_
3748 GenerateReferenceLoadTwoRegisters(instruction,
3749 temp_loc,
3750 obj_loc,
3751 class_offset,
3752 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003753 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003754
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003755 // Do an exact check.
3756 __ Cmp(temp, cls);
3757 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003758
3759 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003760 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003761 GenerateReferenceLoadOneRegister(instruction,
3762 temp_loc,
3763 component_offset,
3764 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003765 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003766
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003767 // If the component type is null, jump to the slow path to throw the exception.
3768 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3769 // Otherwise, the object is indeed an array. Further check that this component type is not a
3770 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003771 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3772 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003773 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003774 break;
3775 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003776
Calin Juravle98893e12015-10-02 21:05:03 +01003777 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003778 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003779 //
3780 // We cannot directly call the CheckCast runtime entry point
3781 // without resorting to a type checking slow path here (i.e. by
3782 // calling InvokeRuntime directly), as it would require to
3783 // assign fixed registers for the inputs of this HInstanceOf
3784 // instruction (following the runtime calling convention), which
3785 // might be cluttered by the potential first read barrier
3786 // emission at the beginning of this method.
3787 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003788 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003789 case TypeCheckKind::kInterfaceCheck: {
3790 // /* HeapReference<Class> */ temp = obj->klass_
3791 GenerateReferenceLoadTwoRegisters(instruction,
3792 temp_loc,
3793 obj_loc,
3794 class_offset,
3795 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003796 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003797
3798 // /* HeapReference<Class> */ temp = temp->iftable_
3799 GenerateReferenceLoadTwoRegisters(instruction,
3800 temp_loc,
3801 temp_loc,
3802 iftable_offset,
3803 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003804 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003805 // Null iftable means it is empty and will always fail the check.
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003806 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003807
3808 // Loop through the iftable and check if any class matches.
3809 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
3810
3811 vixl::aarch64::Label start_loop;
3812 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003813 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003814 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3815 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003816 // Go to next interface.
3817 __ Add(temp, temp, 2 * kHeapReferenceSize);
3818 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003819 // Compare the classes and continue the loop if they do not match.
3820 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3821 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003822 break;
3823 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003824 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003825 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003826
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003827 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003828}
3829
Alexandre Rames5319def2014-10-23 10:03:10 +01003830void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3832 locations->SetOut(Location::ConstantLocation(constant));
3833}
3834
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003835void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003836 // Will be generated at use site.
3837}
3838
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003839void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3841 locations->SetOut(Location::ConstantLocation(constant));
3842}
3843
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003844void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003845 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003846}
3847
Calin Juravle175dc732015-08-25 15:42:32 +01003848void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3849 // The trampoline uses the same calling convention as dex calling conventions,
3850 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3851 // the method_idx.
3852 HandleInvoke(invoke);
3853}
3854
3855void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3856 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3857}
3858
Alexandre Rames5319def2014-10-23 10:03:10 +01003859void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003860 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003861 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003862}
3863
Alexandre Rames67555f72014-11-18 10:55:16 +00003864void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3865 HandleInvoke(invoke);
3866}
3867
3868void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3869 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003870 LocationSummary* locations = invoke->GetLocations();
3871 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003872 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003873 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003874 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003875
3876 // The register ip1 is required to be used for the hidden argument in
3877 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003878 MacroAssembler* masm = GetVIXLAssembler();
3879 UseScratchRegisterScope scratch_scope(masm);
3880 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003881 scratch_scope.Exclude(ip1);
3882 __ Mov(ip1, invoke->GetDexMethodIndex());
3883
Alexandre Rames67555f72014-11-18 10:55:16 +00003884 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003885 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003886 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003887 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003888 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003889 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003890 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003891 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003892 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003893 // Instead of simply (possibly) unpoisoning `temp` here, we should
3894 // emit a read barrier for the previous class reference load.
3895 // However this is not required in practice, as this is an
3896 // intermediate/temporary reference and because the current
3897 // concurrent copying collector keeps the from-space memory
3898 // intact/accessible until the end of the marking phase (the
3899 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003900 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003901 __ Ldr(temp,
3902 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3903 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003904 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003905 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003906 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003907 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003908 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003909 // lr();
3910 __ Blr(lr);
3911 DCHECK(!codegen_->IsLeafMethod());
3912 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3913}
3914
3915void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003916 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3917 if (intrinsic.TryDispatch(invoke)) {
3918 return;
3919 }
3920
Alexandre Rames67555f72014-11-18 10:55:16 +00003921 HandleInvoke(invoke);
3922}
3923
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003924void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003925 // Explicit clinit checks triggered by static invokes must have been pruned by
3926 // art::PrepareForRegisterAllocation.
3927 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003928
Andreas Gampe878d58c2015-01-15 23:24:00 -08003929 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3930 if (intrinsic.TryDispatch(invoke)) {
3931 return;
3932 }
3933
Alexandre Rames67555f72014-11-18 10:55:16 +00003934 HandleInvoke(invoke);
3935}
3936
Andreas Gampe878d58c2015-01-15 23:24:00 -08003937static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3938 if (invoke->GetLocations()->Intrinsified()) {
3939 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3940 intrinsic.Dispatch(invoke);
3941 return true;
3942 }
3943 return false;
3944}
3945
Vladimir Markodc151b22015-10-15 18:02:30 +01003946HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3947 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003948 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003949 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003950 return desired_dispatch_info;
3951}
3952
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003953void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003954 // For better instruction scheduling we load the direct code pointer before the method pointer.
3955 bool direct_code_loaded = false;
3956 switch (invoke->GetCodePtrLocation()) {
3957 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3958 // LR = code address from literal pool with link-time patch.
3959 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3960 direct_code_loaded = true;
3961 break;
3962 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3963 // LR = invoke->GetDirectCodePtr();
3964 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3965 direct_code_loaded = true;
3966 break;
3967 default:
3968 break;
3969 }
3970
Andreas Gampe878d58c2015-01-15 23:24:00 -08003971 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003972 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3973 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003974 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3975 uint32_t offset =
3976 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003977 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003978 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003979 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003980 }
Vladimir Marko58155012015-08-19 12:49:41 +00003981 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003982 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003983 break;
3984 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3985 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003986 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003987 break;
3988 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3989 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003990 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003991 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3992 break;
3993 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3994 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003995 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003996 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003997 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003998 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003999 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004000 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004001 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004002 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004003 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004004 }
Vladimir Marko58155012015-08-19 12:49:41 +00004005 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004006 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004007 Register reg = XRegisterFrom(temp);
4008 Register method_reg;
4009 if (current_method.IsRegister()) {
4010 method_reg = XRegisterFrom(current_method);
4011 } else {
4012 DCHECK(invoke->GetLocations()->Intrinsified());
4013 DCHECK(!current_method.IsValid());
4014 method_reg = reg;
4015 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4016 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004017
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004018 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004019 __ Ldr(reg.X(),
4020 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004021 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004022 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004023 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4024 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004025 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4026 break;
4027 }
4028 }
4029
4030 switch (invoke->GetCodePtrLocation()) {
4031 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4032 __ Bl(&frame_entry_label_);
4033 break;
4034 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004035 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4036 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01004037 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
4038 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00004039 __ Bind(label);
4040 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00004041 break;
4042 }
4043 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4044 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4045 // LR prepared above for better instruction scheduling.
4046 DCHECK(direct_code_loaded);
4047 // lr()
4048 __ Blr(lr);
4049 break;
4050 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4051 // LR = callee_method->entry_point_from_quick_compiled_code_;
4052 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004053 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004054 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004055 // lr()
4056 __ Blr(lr);
4057 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004058 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004059
Andreas Gampe878d58c2015-01-15 23:24:00 -08004060 DCHECK(!IsLeafMethod());
4061}
4062
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004063void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004064 // Use the calling convention instead of the location of the receiver, as
4065 // intrinsics may have put the receiver in a different register. In the intrinsics
4066 // slow path, the arguments have been moved to the right place, so here we are
4067 // guaranteed that the receiver is the first register of the calling convention.
4068 InvokeDexCallingConvention calling_convention;
4069 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004070 Register temp = XRegisterFrom(temp_in);
4071 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4072 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4073 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004074 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004075
4076 BlockPoolsScope block_pools(GetVIXLAssembler());
4077
4078 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004079 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004080 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004081 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004082 // Instead of simply (possibly) unpoisoning `temp` here, we should
4083 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004084 // intermediate/temporary reference and because the current
4085 // concurrent copying collector keeps the from-space memory
4086 // intact/accessible until the end of the marking phase (the
4087 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004088 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4089 // temp = temp->GetMethodAt(method_offset);
4090 __ Ldr(temp, MemOperand(temp, method_offset));
4091 // lr = temp->GetEntryPoint();
4092 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4093 // lr();
4094 __ Blr(lr);
4095}
4096
Scott Wakeling97c72b72016-06-24 16:19:36 +01004097vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4098 const DexFile& dex_file,
4099 uint32_t string_index,
4100 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004101 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4102}
4103
Scott Wakeling97c72b72016-06-24 16:19:36 +01004104vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4105 const DexFile& dex_file,
4106 uint32_t type_index,
4107 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004108 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
4109}
4110
Scott Wakeling97c72b72016-06-24 16:19:36 +01004111vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4112 const DexFile& dex_file,
4113 uint32_t element_offset,
4114 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004115 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4116}
4117
Scott Wakeling97c72b72016-06-24 16:19:36 +01004118vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4119 const DexFile& dex_file,
4120 uint32_t offset_or_index,
4121 vixl::aarch64::Label* adrp_label,
4122 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004123 // Add a patch entry and return the label.
4124 patches->emplace_back(dex_file, offset_or_index);
4125 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004126 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004127 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4128 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4129 return label;
4130}
4131
Scott Wakeling97c72b72016-06-24 16:19:36 +01004132vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004133 const DexFile& dex_file, uint32_t string_index) {
4134 return boot_image_string_patches_.GetOrCreate(
4135 StringReference(&dex_file, string_index),
4136 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4137}
4138
Scott Wakeling97c72b72016-06-24 16:19:36 +01004139vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004140 const DexFile& dex_file, uint32_t type_index) {
4141 return boot_image_type_patches_.GetOrCreate(
4142 TypeReference(&dex_file, type_index),
4143 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4144}
4145
Scott Wakeling97c72b72016-06-24 16:19:36 +01004146vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4147 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004148 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4149 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4150 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4151}
4152
Scott Wakeling97c72b72016-06-24 16:19:36 +01004153vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4154 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004155 return DeduplicateUint64Literal(address);
4156}
4157
Vladimir Markoaad75c62016-10-03 08:46:48 +00004158void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4159 vixl::aarch64::Register reg) {
4160 DCHECK(reg.IsX());
4161 SingleEmissionCheckScope guard(GetVIXLAssembler());
4162 __ Bind(fixup_label);
4163 __ adrp(reg, /* offset placeholder */ 0);
4164}
4165
4166void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4167 vixl::aarch64::Register out,
4168 vixl::aarch64::Register base) {
4169 DCHECK(out.IsX());
4170 DCHECK(base.IsX());
4171 SingleEmissionCheckScope guard(GetVIXLAssembler());
4172 __ Bind(fixup_label);
4173 __ add(out, base, Operand(/* offset placeholder */ 0));
4174}
4175
4176void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4177 vixl::aarch64::Register out,
4178 vixl::aarch64::Register base) {
4179 DCHECK(base.IsX());
4180 SingleEmissionCheckScope guard(GetVIXLAssembler());
4181 __ Bind(fixup_label);
4182 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4183}
4184
4185template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4186inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4187 const ArenaDeque<PcRelativePatchInfo>& infos,
4188 ArenaVector<LinkerPatch>* linker_patches) {
4189 for (const PcRelativePatchInfo& info : infos) {
4190 linker_patches->push_back(Factory(info.label.GetLocation(),
4191 &info.target_dex_file,
4192 info.pc_insn_label->GetLocation(),
4193 info.offset_or_index));
4194 }
4195}
4196
Vladimir Marko58155012015-08-19 12:49:41 +00004197void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4198 DCHECK(linker_patches->empty());
4199 size_t size =
4200 method_patches_.size() +
4201 call_patches_.size() +
4202 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004203 pc_relative_dex_cache_patches_.size() +
4204 boot_image_string_patches_.size() +
4205 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004206 boot_image_type_patches_.size() +
4207 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004208 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004209 linker_patches->reserve(size);
4210 for (const auto& entry : method_patches_) {
4211 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004212 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4213 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004214 target_method.dex_file,
4215 target_method.dex_method_index));
4216 }
4217 for (const auto& entry : call_patches_) {
4218 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004219 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4220 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004221 target_method.dex_file,
4222 target_method.dex_method_index));
4223 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004224 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4225 linker_patches->push_back(
4226 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004227 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004228 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004229 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004230 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004231 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004232 info.offset_or_index));
4233 }
4234 for (const auto& entry : boot_image_string_patches_) {
4235 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004236 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4237 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004238 target_string.dex_file,
4239 target_string.string_index));
4240 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004241 if (!GetCompilerOptions().IsBootImage()) {
4242 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4243 linker_patches);
4244 } else {
4245 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4246 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004247 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004248 for (const auto& entry : boot_image_type_patches_) {
4249 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004250 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4251 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004252 target_type.dex_file,
4253 target_type.type_index));
4254 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004255 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4256 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004257 for (const auto& entry : boot_image_address_patches_) {
4258 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004259 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4260 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004261 }
4262}
4263
Scott Wakeling97c72b72016-06-24 16:19:36 +01004264vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004265 Uint32ToLiteralMap* map) {
4266 return map->GetOrCreate(
4267 value,
4268 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4269}
4270
Scott Wakeling97c72b72016-06-24 16:19:36 +01004271vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004272 return uint64_literals_.GetOrCreate(
4273 value,
4274 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004275}
4276
Scott Wakeling97c72b72016-06-24 16:19:36 +01004277vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004278 MethodReference target_method,
4279 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004280 return map->GetOrCreate(
4281 target_method,
4282 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004283}
4284
Scott Wakeling97c72b72016-06-24 16:19:36 +01004285vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004286 MethodReference target_method) {
4287 return DeduplicateMethodLiteral(target_method, &method_patches_);
4288}
4289
Scott Wakeling97c72b72016-06-24 16:19:36 +01004290vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004291 MethodReference target_method) {
4292 return DeduplicateMethodLiteral(target_method, &call_patches_);
4293}
4294
4295
Andreas Gampe878d58c2015-01-15 23:24:00 -08004296void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004297 // Explicit clinit checks triggered by static invokes must have been pruned by
4298 // art::PrepareForRegisterAllocation.
4299 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004300
Andreas Gampe878d58c2015-01-15 23:24:00 -08004301 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4302 return;
4303 }
4304
Alexandre Ramesd921d642015-04-16 15:07:16 +01004305 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004306 LocationSummary* locations = invoke->GetLocations();
4307 codegen_->GenerateStaticOrDirectCall(
4308 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004309 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004310}
4311
4312void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004313 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4314 return;
4315 }
4316
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004317 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004318 DCHECK(!codegen_->IsLeafMethod());
4319 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4320}
4321
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004322HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4323 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004324 switch (desired_class_load_kind) {
4325 case HLoadClass::LoadKind::kReferrersClass:
4326 break;
4327 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4328 DCHECK(!GetCompilerOptions().GetCompilePic());
4329 break;
4330 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4331 DCHECK(GetCompilerOptions().GetCompilePic());
4332 break;
4333 case HLoadClass::LoadKind::kBootImageAddress:
4334 break;
4335 case HLoadClass::LoadKind::kDexCacheAddress:
4336 DCHECK(Runtime::Current()->UseJitCompilation());
4337 break;
4338 case HLoadClass::LoadKind::kDexCachePcRelative:
4339 DCHECK(!Runtime::Current()->UseJitCompilation());
4340 break;
4341 case HLoadClass::LoadKind::kDexCacheViaMethod:
4342 break;
4343 }
4344 return desired_class_load_kind;
4345}
4346
Alexandre Rames67555f72014-11-18 10:55:16 +00004347void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004348 if (cls->NeedsAccessCheck()) {
4349 InvokeRuntimeCallingConvention calling_convention;
4350 CodeGenerator::CreateLoadClassLocationSummary(
4351 cls,
4352 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004353 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004354 /* code_generator_supports_read_barrier */ true);
4355 return;
4356 }
4357
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004358 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4359 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004360 ? LocationSummary::kCallOnSlowPath
4361 : LocationSummary::kNoCall;
4362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004363 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004364 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004365 }
4366
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004367 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4368 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4369 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4370 locations->SetInAt(0, Location::RequiresRegister());
4371 }
4372 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004373}
4374
4375void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004376 if (cls->NeedsAccessCheck()) {
4377 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004378 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004379 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004380 return;
4381 }
4382
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004383 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004384 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004385
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004386 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4387 ? kWithoutReadBarrier
4388 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004389 bool generate_null_check = false;
4390 switch (cls->GetLoadKind()) {
4391 case HLoadClass::LoadKind::kReferrersClass: {
4392 DCHECK(!cls->CanCallRuntime());
4393 DCHECK(!cls->MustGenerateClinitCheck());
4394 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4395 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004396 GenerateGcRootFieldLoad(cls,
4397 out_loc,
4398 current_method,
4399 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004400 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004401 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004402 break;
4403 }
4404 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004405 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004406 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4407 cls->GetTypeIndex()));
4408 break;
4409 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004410 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004411 // Add ADRP with its PC-relative type patch.
4412 const DexFile& dex_file = cls->GetDexFile();
4413 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004414 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004415 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004416 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004417 vixl::aarch64::Label* add_label =
4418 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004419 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004420 break;
4421 }
4422 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004423 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004424 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4425 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4426 break;
4427 }
4428 case HLoadClass::LoadKind::kDexCacheAddress: {
4429 DCHECK_NE(cls->GetAddress(), 0u);
4430 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4431 // that gives a 16KiB range. To try and reduce the number of literals if we load
4432 // multiple types, simply split the dex cache address to a 16KiB aligned base
4433 // loaded from a literal and the remaining offset embedded in the load.
4434 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4435 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4436 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4437 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4438 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4439 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4440 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004441 GenerateGcRootFieldLoad(cls,
4442 out_loc,
4443 out.X(),
4444 offset,
Roland Levillain00468f32016-10-27 18:02:48 +01004445 /* fixup_label */ nullptr,
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::kDexCachePcRelative: {
4451 // Add ADRP with its PC-relative DexCache access patch.
4452 const DexFile& dex_file = cls->GetDexFile();
4453 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004454 vixl::aarch64::Label* adrp_label =
4455 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004456 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004457 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004458 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004459 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4460 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004461 GenerateGcRootFieldLoad(cls,
4462 out_loc,
4463 out.X(),
4464 /* offset placeholder */ 0,
4465 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004466 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004467 generate_null_check = !cls->IsInDexCache();
4468 break;
4469 }
4470 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4471 MemberOffset resolved_types_offset =
4472 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4473 // /* GcRoot<mirror::Class>[] */ out =
4474 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4475 Register current_method = InputRegisterAt(cls, 0);
4476 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4477 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004478 GenerateGcRootFieldLoad(cls,
4479 out_loc,
4480 out.X(),
4481 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
Roland Levillain00468f32016-10-27 18:02:48 +01004482 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004483 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004484 generate_null_check = !cls->IsInDexCache();
4485 break;
4486 }
4487 }
4488
4489 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4490 DCHECK(cls->CanCallRuntime());
4491 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4492 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4493 codegen_->AddSlowPath(slow_path);
4494 if (generate_null_check) {
4495 __ Cbz(out, slow_path->GetEntryLabel());
4496 }
4497 if (cls->MustGenerateClinitCheck()) {
4498 GenerateClassInitializationCheck(slow_path, out);
4499 } else {
4500 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004501 }
4502 }
4503}
4504
David Brazdilcb1c0552015-08-04 16:22:25 +01004505static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004506 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004507}
4508
Alexandre Rames67555f72014-11-18 10:55:16 +00004509void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4510 LocationSummary* locations =
4511 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4512 locations->SetOut(Location::RequiresRegister());
4513}
4514
4515void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004516 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4517}
4518
4519void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4520 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4521}
4522
4523void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4524 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004525}
4526
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004527HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4528 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004529 switch (desired_string_load_kind) {
4530 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4531 DCHECK(!GetCompilerOptions().GetCompilePic());
4532 break;
4533 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4534 DCHECK(GetCompilerOptions().GetCompilePic());
4535 break;
4536 case HLoadString::LoadKind::kBootImageAddress:
4537 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004538 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004539 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004540 break;
4541 case HLoadString::LoadKind::kDexCacheViaMethod:
4542 break;
4543 }
4544 return desired_string_load_kind;
4545}
4546
Alexandre Rames67555f72014-11-18 10:55:16 +00004547void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004548 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4549 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4550 ? LocationSummary::kCallOnMainOnly
4551 : LocationSummary::kCallOnSlowPath)
4552 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004553 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004554 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004555 InvokeRuntimeCallingConvention calling_convention;
4556 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4557 } else {
4558 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004559 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4560 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4561 // Rely on the pResolveString and/or marking to save everything, including temps.
4562 RegisterSet caller_saves = RegisterSet::Empty();
4563 InvokeRuntimeCallingConvention calling_convention;
4564 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4565 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4566 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4567 Primitive::kPrimNot).GetCode());
4568 locations->SetCustomSlowPathCallerSaves(caller_saves);
4569 } else {
4570 // For non-Baker read barrier we have a temp-clobbering call.
4571 }
4572 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004573 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004574}
4575
4576void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004577 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004578
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004579 switch (load->GetLoadKind()) {
4580 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004581 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4582 load->GetStringIndex()));
4583 return; // No dex cache slow path.
4584 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004585 // Add ADRP with its PC-relative String patch.
4586 const DexFile& dex_file = load->GetDexFile();
4587 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004588 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004589 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004590 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004591 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004592 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004593 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004594 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004595 return; // No dex cache slow path.
4596 }
4597 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004598 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4599 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4600 return; // No dex cache slow path.
4601 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004602 case HLoadString::LoadKind::kBssEntry: {
4603 // Add ADRP with its PC-relative String .bss entry patch.
4604 const DexFile& dex_file = load->GetDexFile();
4605 uint32_t string_index = load->GetStringIndex();
4606 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004607 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4608 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004609 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004610 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004611 // Add LDR with its PC-relative String patch.
4612 vixl::aarch64::Label* ldr_label =
4613 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004614 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004615 GenerateGcRootFieldLoad(load,
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00004616 load->GetLocations()->Out(),
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004617 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004618 /* offset placeholder */ 0u,
4619 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004620 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004621 SlowPathCodeARM64* slow_path =
4622 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004623 codegen_->AddSlowPath(slow_path);
4624 __ Cbz(out.X(), slow_path->GetEntryLabel());
4625 __ Bind(slow_path->GetExitLabel());
4626 return;
4627 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004628 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004629 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004630 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004631
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004632 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004633 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004634 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004635 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4636 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4637 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004638}
4639
Alexandre Rames5319def2014-10-23 10:03:10 +01004640void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4642 locations->SetOut(Location::ConstantLocation(constant));
4643}
4644
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004645void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004646 // Will be generated at use site.
4647}
4648
Alexandre Rames67555f72014-11-18 10:55:16 +00004649void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4650 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004652 InvokeRuntimeCallingConvention calling_convention;
4653 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4654}
4655
4656void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004657 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4658 instruction,
4659 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004660 if (instruction->IsEnter()) {
4661 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4662 } else {
4663 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4664 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004665}
4666
Alexandre Rames42d641b2014-10-27 14:00:51 +00004667void LocationsBuilderARM64::VisitMul(HMul* mul) {
4668 LocationSummary* locations =
4669 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4670 switch (mul->GetResultType()) {
4671 case Primitive::kPrimInt:
4672 case Primitive::kPrimLong:
4673 locations->SetInAt(0, Location::RequiresRegister());
4674 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004676 break;
4677
4678 case Primitive::kPrimFloat:
4679 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004680 locations->SetInAt(0, Location::RequiresFpuRegister());
4681 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004682 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004683 break;
4684
4685 default:
4686 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4687 }
4688}
4689
4690void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4691 switch (mul->GetResultType()) {
4692 case Primitive::kPrimInt:
4693 case Primitive::kPrimLong:
4694 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4695 break;
4696
4697 case Primitive::kPrimFloat:
4698 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004699 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004700 break;
4701
4702 default:
4703 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4704 }
4705}
4706
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004707void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4708 LocationSummary* locations =
4709 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4710 switch (neg->GetResultType()) {
4711 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004712 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004713 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004715 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004716
4717 case Primitive::kPrimFloat:
4718 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004719 locations->SetInAt(0, Location::RequiresFpuRegister());
4720 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004721 break;
4722
4723 default:
4724 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4725 }
4726}
4727
4728void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4729 switch (neg->GetResultType()) {
4730 case Primitive::kPrimInt:
4731 case Primitive::kPrimLong:
4732 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4733 break;
4734
4735 case Primitive::kPrimFloat:
4736 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004737 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004738 break;
4739
4740 default:
4741 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4742 }
4743}
4744
4745void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4746 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004748 InvokeRuntimeCallingConvention calling_convention;
4749 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004750 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004751 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004752 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004753}
4754
4755void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4756 LocationSummary* locations = instruction->GetLocations();
4757 InvokeRuntimeCallingConvention calling_convention;
4758 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4759 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004760 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004761 // Note: if heap poisoning is enabled, the entry point takes cares
4762 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004763 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004764 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004765}
4766
Alexandre Rames5319def2014-10-23 10:03:10 +01004767void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4768 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004769 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004770 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004771 if (instruction->IsStringAlloc()) {
4772 locations->AddTemp(LocationFrom(kArtMethodRegister));
4773 } else {
4774 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4775 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4776 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004777 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4778}
4779
4780void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004781 // Note: if heap poisoning is enabled, the entry point takes cares
4782 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004783 if (instruction->IsStringAlloc()) {
4784 // String is allocated through StringFactory. Call NewEmptyString entry point.
4785 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004786 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004787 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4788 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4789 __ Blr(lr);
4790 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4791 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004792 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004793 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4794 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004795}
4796
4797void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4798 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004799 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004801}
4802
4803void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004804 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004805 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004806 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004807 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004808 break;
4809
4810 default:
4811 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4812 }
4813}
4814
David Brazdil66d126e2015-04-03 16:02:44 +01004815void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4816 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4817 locations->SetInAt(0, Location::RequiresRegister());
4818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4819}
4820
4821void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004822 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004823}
4824
Alexandre Rames5319def2014-10-23 10:03:10 +01004825void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004826 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4827 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004828}
4829
Calin Juravle2ae48182016-03-16 14:05:09 +00004830void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4831 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004832 return;
4833 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004834
Alexandre Ramesd921d642015-04-16 15:07:16 +01004835 BlockPoolsScope block_pools(GetVIXLAssembler());
4836 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004837 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004838 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004839}
4840
Calin Juravle2ae48182016-03-16 14:05:09 +00004841void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004842 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004843 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004844
4845 LocationSummary* locations = instruction->GetLocations();
4846 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004847
4848 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004849}
4850
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004851void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004852 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004853}
4854
Alexandre Rames67555f72014-11-18 10:55:16 +00004855void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4856 HandleBinaryOp(instruction);
4857}
4858
4859void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4860 HandleBinaryOp(instruction);
4861}
4862
Alexandre Rames3e69f162014-12-10 10:36:50 +00004863void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4864 LOG(FATAL) << "Unreachable";
4865}
4866
4867void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4868 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4869}
4870
Alexandre Rames5319def2014-10-23 10:03:10 +01004871void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4873 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4874 if (location.IsStackSlot()) {
4875 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4876 } else if (location.IsDoubleStackSlot()) {
4877 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4878 }
4879 locations->SetOut(location);
4880}
4881
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004882void InstructionCodeGeneratorARM64::VisitParameterValue(
4883 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004884 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004885}
4886
4887void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4888 LocationSummary* locations =
4889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004890 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004891}
4892
4893void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4894 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4895 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004896}
4897
4898void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004900 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004901 locations->SetInAt(i, Location::Any());
4902 }
4903 locations->SetOut(Location::Any());
4904}
4905
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004906void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004907 LOG(FATAL) << "Unreachable";
4908}
4909
Serban Constantinescu02164b32014-11-13 14:05:07 +00004910void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004911 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004912 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004913 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4914 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4916
4917 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004918 case Primitive::kPrimInt:
4919 case Primitive::kPrimLong:
4920 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004921 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4923 break;
4924
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004925 case Primitive::kPrimFloat:
4926 case Primitive::kPrimDouble: {
4927 InvokeRuntimeCallingConvention calling_convention;
4928 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4929 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4930 locations->SetOut(calling_convention.GetReturnLocation(type));
4931
4932 break;
4933 }
4934
Serban Constantinescu02164b32014-11-13 14:05:07 +00004935 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004936 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004937 }
4938}
4939
4940void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4941 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004942
Serban Constantinescu02164b32014-11-13 14:05:07 +00004943 switch (type) {
4944 case Primitive::kPrimInt:
4945 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004946 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004947 break;
4948 }
4949
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004950 case Primitive::kPrimFloat:
4951 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004952 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4953 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004954 if (type == Primitive::kPrimFloat) {
4955 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4956 } else {
4957 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4958 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004959 break;
4960 }
4961
Serban Constantinescu02164b32014-11-13 14:05:07 +00004962 default:
4963 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004964 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004965 }
4966}
4967
Calin Juravle27df7582015-04-17 19:12:31 +01004968void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4969 memory_barrier->SetLocations(nullptr);
4970}
4971
4972void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004973 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004974}
4975
Alexandre Rames5319def2014-10-23 10:03:10 +01004976void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4978 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004979 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004980}
4981
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004982void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004983 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004984}
4985
4986void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4987 instruction->SetLocations(nullptr);
4988}
4989
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004990void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004991 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004992}
4993
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004994void LocationsBuilderARM64::VisitRor(HRor* ror) {
4995 HandleBinaryOp(ror);
4996}
4997
4998void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4999 HandleBinaryOp(ror);
5000}
5001
Serban Constantinescu02164b32014-11-13 14:05:07 +00005002void LocationsBuilderARM64::VisitShl(HShl* shl) {
5003 HandleShift(shl);
5004}
5005
5006void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5007 HandleShift(shl);
5008}
5009
5010void LocationsBuilderARM64::VisitShr(HShr* shr) {
5011 HandleShift(shr);
5012}
5013
5014void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5015 HandleShift(shr);
5016}
5017
Alexandre Rames5319def2014-10-23 10:03:10 +01005018void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005019 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005020}
5021
5022void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005023 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005024}
5025
Alexandre Rames67555f72014-11-18 10:55:16 +00005026void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005027 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005028}
5029
5030void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005031 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005032}
5033
5034void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005035 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005036}
5037
Alexandre Rames67555f72014-11-18 10:55:16 +00005038void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005039 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005040}
5041
Calin Juravlee460d1d2015-09-29 04:52:17 +01005042void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5043 HUnresolvedInstanceFieldGet* instruction) {
5044 FieldAccessCallingConventionARM64 calling_convention;
5045 codegen_->CreateUnresolvedFieldLocationSummary(
5046 instruction, instruction->GetFieldType(), calling_convention);
5047}
5048
5049void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5050 HUnresolvedInstanceFieldGet* instruction) {
5051 FieldAccessCallingConventionARM64 calling_convention;
5052 codegen_->GenerateUnresolvedFieldAccess(instruction,
5053 instruction->GetFieldType(),
5054 instruction->GetFieldIndex(),
5055 instruction->GetDexPc(),
5056 calling_convention);
5057}
5058
5059void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5060 HUnresolvedInstanceFieldSet* instruction) {
5061 FieldAccessCallingConventionARM64 calling_convention;
5062 codegen_->CreateUnresolvedFieldLocationSummary(
5063 instruction, instruction->GetFieldType(), calling_convention);
5064}
5065
5066void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5067 HUnresolvedInstanceFieldSet* instruction) {
5068 FieldAccessCallingConventionARM64 calling_convention;
5069 codegen_->GenerateUnresolvedFieldAccess(instruction,
5070 instruction->GetFieldType(),
5071 instruction->GetFieldIndex(),
5072 instruction->GetDexPc(),
5073 calling_convention);
5074}
5075
5076void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5077 HUnresolvedStaticFieldGet* instruction) {
5078 FieldAccessCallingConventionARM64 calling_convention;
5079 codegen_->CreateUnresolvedFieldLocationSummary(
5080 instruction, instruction->GetFieldType(), calling_convention);
5081}
5082
5083void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5084 HUnresolvedStaticFieldGet* instruction) {
5085 FieldAccessCallingConventionARM64 calling_convention;
5086 codegen_->GenerateUnresolvedFieldAccess(instruction,
5087 instruction->GetFieldType(),
5088 instruction->GetFieldIndex(),
5089 instruction->GetDexPc(),
5090 calling_convention);
5091}
5092
5093void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5094 HUnresolvedStaticFieldSet* instruction) {
5095 FieldAccessCallingConventionARM64 calling_convention;
5096 codegen_->CreateUnresolvedFieldLocationSummary(
5097 instruction, instruction->GetFieldType(), calling_convention);
5098}
5099
5100void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5101 HUnresolvedStaticFieldSet* instruction) {
5102 FieldAccessCallingConventionARM64 calling_convention;
5103 codegen_->GenerateUnresolvedFieldAccess(instruction,
5104 instruction->GetFieldType(),
5105 instruction->GetFieldIndex(),
5106 instruction->GetDexPc(),
5107 calling_convention);
5108}
5109
Alexandre Rames5319def2014-10-23 10:03:10 +01005110void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005111 LocationSummary* locations =
5112 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005113 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005114}
5115
5116void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005117 HBasicBlock* block = instruction->GetBlock();
5118 if (block->GetLoopInformation() != nullptr) {
5119 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5120 // The back edge will generate the suspend check.
5121 return;
5122 }
5123 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5124 // The goto will generate the suspend check.
5125 return;
5126 }
5127 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005128}
5129
Alexandre Rames67555f72014-11-18 10:55:16 +00005130void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5131 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005133 InvokeRuntimeCallingConvention calling_convention;
5134 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5135}
5136
5137void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005138 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005139 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005140}
5141
5142void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5143 LocationSummary* locations =
5144 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5145 Primitive::Type input_type = conversion->GetInputType();
5146 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005147 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005148 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5149 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5150 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5151 }
5152
Alexandre Rames542361f2015-01-29 16:57:31 +00005153 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005154 locations->SetInAt(0, Location::RequiresFpuRegister());
5155 } else {
5156 locations->SetInAt(0, Location::RequiresRegister());
5157 }
5158
Alexandre Rames542361f2015-01-29 16:57:31 +00005159 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005160 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5161 } else {
5162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5163 }
5164}
5165
5166void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5167 Primitive::Type result_type = conversion->GetResultType();
5168 Primitive::Type input_type = conversion->GetInputType();
5169
5170 DCHECK_NE(input_type, result_type);
5171
Alexandre Rames542361f2015-01-29 16:57:31 +00005172 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005173 int result_size = Primitive::ComponentSize(result_type);
5174 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005175 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005176 Register output = OutputRegister(conversion);
5177 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005178 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005179 // 'int' values are used directly as W registers, discarding the top
5180 // bits, so we don't need to sign-extend and can just perform a move.
5181 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5182 // top 32 bits of the target register. We theoretically could leave those
5183 // bits unchanged, but we would have to make sure that no code uses a
5184 // 32bit input value as a 64bit value assuming that the top 32 bits are
5185 // zero.
5186 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005187 } else if (result_type == Primitive::kPrimChar ||
5188 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5189 __ Ubfx(output,
5190 output.IsX() ? source.X() : source.W(),
5191 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005192 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005193 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005194 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005195 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005196 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005197 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005198 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5199 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005200 } else if (Primitive::IsFloatingPointType(result_type) &&
5201 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005202 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5203 } else {
5204 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5205 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005206 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005207}
Alexandre Rames67555f72014-11-18 10:55:16 +00005208
Serban Constantinescu02164b32014-11-13 14:05:07 +00005209void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5210 HandleShift(ushr);
5211}
5212
5213void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5214 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005215}
5216
5217void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5218 HandleBinaryOp(instruction);
5219}
5220
5221void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5222 HandleBinaryOp(instruction);
5223}
5224
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005225void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005226 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005227 LOG(FATAL) << "Unreachable";
5228}
5229
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005230void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005231 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005232 LOG(FATAL) << "Unreachable";
5233}
5234
Mark Mendellfe57faa2015-09-18 09:26:15 -04005235// Simple implementation of packed switch - generate cascaded compare/jumps.
5236void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5237 LocationSummary* locations =
5238 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5239 locations->SetInAt(0, Location::RequiresRegister());
5240}
5241
5242void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5243 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005244 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005245 Register value_reg = InputRegisterAt(switch_instr, 0);
5246 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5247
Zheng Xu3927c8b2015-11-18 17:46:25 +08005248 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005249 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005250 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5251 // make sure we don't emit it if the target may run out of range.
5252 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5253 // ranges and emit the tables only as required.
5254 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005255
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005256 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005257 // Current instruction id is an upper bound of the number of HIRs in the graph.
5258 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5259 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005260 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5261 Register temp = temps.AcquireW();
5262 __ Subs(temp, value_reg, Operand(lower_bound));
5263
Zheng Xu3927c8b2015-11-18 17:46:25 +08005264 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005265 // Jump to successors[0] if value == lower_bound.
5266 __ B(eq, codegen_->GetLabelOf(successors[0]));
5267 int32_t last_index = 0;
5268 for (; num_entries - last_index > 2; last_index += 2) {
5269 __ Subs(temp, temp, Operand(2));
5270 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5271 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5272 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5273 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5274 }
5275 if (num_entries - last_index == 2) {
5276 // The last missing case_value.
5277 __ Cmp(temp, Operand(1));
5278 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005279 }
5280
5281 // And the default for any other value.
5282 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5283 __ B(codegen_->GetLabelOf(default_block));
5284 }
5285 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005286 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005287
5288 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5289
5290 // Below instructions should use at most one blocked register. Since there are two blocked
5291 // registers, we are free to block one.
5292 Register temp_w = temps.AcquireW();
5293 Register index;
5294 // Remove the bias.
5295 if (lower_bound != 0) {
5296 index = temp_w;
5297 __ Sub(index, value_reg, Operand(lower_bound));
5298 } else {
5299 index = value_reg;
5300 }
5301
5302 // Jump to default block if index is out of the range.
5303 __ Cmp(index, Operand(num_entries));
5304 __ B(hs, codegen_->GetLabelOf(default_block));
5305
5306 // In current VIXL implementation, it won't require any blocked registers to encode the
5307 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5308 // register pressure.
5309 Register table_base = temps.AcquireX();
5310 // Load jump offset from the table.
5311 __ Adr(table_base, jump_table->GetTableStartLabel());
5312 Register jump_offset = temp_w;
5313 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5314
5315 // Jump to target block by branching to table_base(pc related) + offset.
5316 Register target_address = table_base;
5317 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5318 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005319 }
5320}
5321
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005322void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5323 HInstruction* instruction,
5324 Location out,
5325 uint32_t offset,
5326 Location maybe_temp,
5327 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005328 Primitive::Type type = Primitive::kPrimNot;
5329 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005330 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005331 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005332 Register temp_reg = RegisterFrom(maybe_temp, type);
5333 if (kUseBakerReadBarrier) {
5334 // Load with fast path based Baker's read barrier.
5335 // /* HeapReference<Object> */ out = *(out + offset)
5336 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5337 out,
5338 out_reg,
5339 offset,
5340 temp_reg,
5341 /* needs_null_check */ false,
5342 /* use_load_acquire */ false);
5343 } else {
5344 // Load with slow path based read barrier.
5345 // Save the value of `out` into `maybe_temp` before overwriting it
5346 // in the following move operation, as we will need it for the
5347 // read barrier below.
5348 __ Mov(temp_reg, out_reg);
5349 // /* HeapReference<Object> */ out = *(out + offset)
5350 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5351 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5352 }
5353 } else {
5354 // Plain load with no read barrier.
5355 // /* HeapReference<Object> */ out = *(out + offset)
5356 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5357 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5358 }
5359}
5360
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005361void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5362 HInstruction* instruction,
5363 Location out,
5364 Location obj,
5365 uint32_t offset,
5366 Location maybe_temp,
5367 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005368 Primitive::Type type = Primitive::kPrimNot;
5369 Register out_reg = RegisterFrom(out, type);
5370 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005371 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005372 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005373 if (kUseBakerReadBarrier) {
5374 // Load with fast path based Baker's read barrier.
5375 Register temp_reg = RegisterFrom(maybe_temp, type);
5376 // /* HeapReference<Object> */ out = *(obj + offset)
5377 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5378 out,
5379 obj_reg,
5380 offset,
5381 temp_reg,
5382 /* needs_null_check */ false,
5383 /* use_load_acquire */ false);
5384 } else {
5385 // Load with slow path based read barrier.
5386 // /* HeapReference<Object> */ out = *(obj + offset)
5387 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5388 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5389 }
5390 } else {
5391 // Plain load with no read barrier.
5392 // /* HeapReference<Object> */ out = *(obj + offset)
5393 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5394 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5395 }
5396}
5397
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005398void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5399 HInstruction* instruction,
5400 Location root,
5401 Register obj,
5402 uint32_t offset,
5403 vixl::aarch64::Label* fixup_label,
5404 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005405 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005406 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005407 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005408 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005409 if (kUseBakerReadBarrier) {
5410 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5411 // Baker's read barrier are used:
5412 //
5413 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005414 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5415 // if (temp != null) {
5416 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005417 // }
5418
5419 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005420 if (fixup_label == nullptr) {
5421 __ Ldr(root_reg, MemOperand(obj, offset));
5422 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005423 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005424 }
Roland Levillain44015862016-01-22 11:47:17 +00005425 static_assert(
5426 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5427 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5428 "have different sizes.");
5429 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5430 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5431 "have different sizes.");
5432
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005433 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005434
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005435 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5436 SlowPathCodeARM64* slow_path =
5437 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5438 root,
5439 LocationFrom(temp));
5440 codegen_->AddSlowPath(slow_path);
5441 const int32_t entry_point_offset =
5442 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5443 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5444 // Loading the entrypoint does not require a load acquire since it is only changed when
5445 // threads are suspended or running a checkpoint.
5446 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5447 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5448 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005449 __ Cbnz(temp, slow_path->GetEntryLabel());
5450 __ Bind(slow_path->GetExitLabel());
5451 } else {
5452 // GC root loaded through a slow path for read barriers other
5453 // than Baker's.
5454 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005455 if (fixup_label == nullptr) {
5456 __ Add(root_reg.X(), obj.X(), offset);
5457 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005458 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005459 }
Roland Levillain44015862016-01-22 11:47:17 +00005460 // /* mirror::Object* */ root = root->Read()
5461 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5462 }
5463 } else {
5464 // Plain GC root load with no read barrier.
5465 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005466 if (fixup_label == nullptr) {
5467 __ Ldr(root_reg, MemOperand(obj, offset));
5468 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005469 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005470 }
Roland Levillain44015862016-01-22 11:47:17 +00005471 // Note that GC roots are not affected by heap poisoning, thus we
5472 // do not have to unpoison `root_reg` here.
5473 }
5474}
5475
5476void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5477 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005478 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005479 uint32_t offset,
5480 Register temp,
5481 bool needs_null_check,
5482 bool use_load_acquire) {
5483 DCHECK(kEmitCompilerReadBarrier);
5484 DCHECK(kUseBakerReadBarrier);
5485
5486 // /* HeapReference<Object> */ ref = *(obj + offset)
5487 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005488 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005489 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5490 ref,
5491 obj,
5492 offset,
5493 no_index,
5494 no_scale_factor,
5495 temp,
5496 needs_null_check,
5497 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005498}
5499
5500void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5501 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005502 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005503 uint32_t data_offset,
5504 Location index,
5505 Register temp,
5506 bool needs_null_check) {
5507 DCHECK(kEmitCompilerReadBarrier);
5508 DCHECK(kUseBakerReadBarrier);
5509
5510 // Array cells are never volatile variables, therefore array loads
5511 // never use Load-Acquire instructions on ARM64.
5512 const bool use_load_acquire = false;
5513
Roland Levillainbfea3352016-06-23 13:48:47 +01005514 static_assert(
5515 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5516 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005517 // /* HeapReference<Object> */ ref =
5518 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005519 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5520 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5521 ref,
5522 obj,
5523 data_offset,
5524 index,
5525 scale_factor,
5526 temp,
5527 needs_null_check,
5528 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005529}
5530
5531void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5532 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005533 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005534 uint32_t offset,
5535 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005536 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005537 Register temp,
5538 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005539 bool use_load_acquire,
5540 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005541 DCHECK(kEmitCompilerReadBarrier);
5542 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005543 // If we are emitting an array load, we should not be using a
5544 // Load Acquire instruction. In other words:
5545 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5546 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005547
5548 MacroAssembler* masm = GetVIXLAssembler();
5549 UseScratchRegisterScope temps(masm);
5550
5551 // In slow path based read barriers, the read barrier call is
5552 // inserted after the original load. However, in fast path based
5553 // Baker's read barriers, we need to perform the load of
5554 // mirror::Object::monitor_ *before* the original reference load.
5555 // This load-load ordering is required by the read barrier.
5556 // The fast path/slow path (for Baker's algorithm) should look like:
5557 //
5558 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5559 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5560 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005561 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005562 // if (is_gray) {
5563 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5564 // }
5565 //
5566 // Note: the original implementation in ReadBarrier::Barrier is
5567 // slightly more complex as it performs additional checks that we do
5568 // not do here for performance reasons.
5569
5570 Primitive::Type type = Primitive::kPrimNot;
5571 Register ref_reg = RegisterFrom(ref, type);
5572 DCHECK(obj.IsW());
5573 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5574
5575 // /* int32_t */ monitor = obj->monitor_
5576 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5577 if (needs_null_check) {
5578 MaybeRecordImplicitNullCheck(instruction);
5579 }
5580 // /* LockWord */ lock_word = LockWord(monitor)
5581 static_assert(sizeof(LockWord) == sizeof(int32_t),
5582 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005583
Vladimir Marko877a0332016-07-11 19:30:56 +01005584 // Introduce a dependency on the lock_word including rb_state,
5585 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005586 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005587 // `obj` is unchanged by this operation, but its value now depends
5588 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005589 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005590
5591 // The actual reference load.
5592 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005593 // Load types involving an "index": ArrayGet,
5594 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5595 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005596 if (use_load_acquire) {
5597 // UnsafeGetObjectVolatile intrinsic case.
5598 // Register `index` is not an index in an object array, but an
5599 // offset to an object reference field within object `obj`.
5600 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5601 DCHECK(instruction->GetLocations()->Intrinsified());
5602 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5603 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005604 DCHECK_EQ(offset, 0u);
5605 DCHECK_EQ(scale_factor, 0u);
5606 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005607 // /* HeapReference<Object> */ ref = *(obj + index)
5608 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5609 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005610 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005611 // ArrayGet and UnsafeGetObject intrinsics cases.
5612 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5613 if (index.IsConstant()) {
5614 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5615 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5616 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005617 Register temp3 = temps.AcquireW();
5618 __ Add(temp3, obj, offset);
5619 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5620 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005621 }
Roland Levillain44015862016-01-22 11:47:17 +00005622 }
Roland Levillain44015862016-01-22 11:47:17 +00005623 } else {
5624 // /* HeapReference<Object> */ ref = *(obj + offset)
5625 MemOperand field = HeapOperand(obj, offset);
5626 if (use_load_acquire) {
5627 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5628 } else {
5629 Load(type, ref_reg, field);
5630 }
5631 }
5632
5633 // Object* ref = ref_addr->AsMirrorPtr()
5634 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5635
Vladimir Marko953437b2016-08-24 08:30:46 +00005636 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005637 SlowPathCodeARM64* slow_path;
5638 if (always_update_field) {
5639 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5640 // address of the form `obj + field_offset`, where `obj` is a
5641 // register and `field_offset` is a register. Thus `offset` and
5642 // `scale_factor` above are expected to be null in this code path.
5643 DCHECK_EQ(offset, 0u);
5644 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5645 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5646 instruction, ref, obj, /* field_offset */ index, temp);
5647 } else {
5648 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5649 }
Roland Levillain44015862016-01-22 11:47:17 +00005650 AddSlowPath(slow_path);
5651
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005652 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005653 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005654 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005655 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5656 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005657 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005658 __ Bind(slow_path->GetExitLabel());
5659}
5660
5661void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5662 Location out,
5663 Location ref,
5664 Location obj,
5665 uint32_t offset,
5666 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005667 DCHECK(kEmitCompilerReadBarrier);
5668
Roland Levillain44015862016-01-22 11:47:17 +00005669 // Insert a slow path based read barrier *after* the reference load.
5670 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005671 // If heap poisoning is enabled, the unpoisoning of the loaded
5672 // reference will be carried out by the runtime within the slow
5673 // path.
5674 //
5675 // Note that `ref` currently does not get unpoisoned (when heap
5676 // poisoning is enabled), which is alright as the `ref` argument is
5677 // not used by the artReadBarrierSlow entry point.
5678 //
5679 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5680 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5681 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5682 AddSlowPath(slow_path);
5683
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005684 __ B(slow_path->GetEntryLabel());
5685 __ Bind(slow_path->GetExitLabel());
5686}
5687
Roland Levillain44015862016-01-22 11:47:17 +00005688void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5689 Location out,
5690 Location ref,
5691 Location obj,
5692 uint32_t offset,
5693 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005694 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005695 // Baker's read barriers shall be handled by the fast path
5696 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5697 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005698 // If heap poisoning is enabled, unpoisoning will be taken care of
5699 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005700 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005701 } else if (kPoisonHeapReferences) {
5702 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5703 }
5704}
5705
Roland Levillain44015862016-01-22 11:47:17 +00005706void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5707 Location out,
5708 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005709 DCHECK(kEmitCompilerReadBarrier);
5710
Roland Levillain44015862016-01-22 11:47:17 +00005711 // Insert a slow path based read barrier *after* the GC root load.
5712 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005713 // Note that GC roots are not affected by heap poisoning, so we do
5714 // not need to do anything special for this here.
5715 SlowPathCodeARM64* slow_path =
5716 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5717 AddSlowPath(slow_path);
5718
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005719 __ B(slow_path->GetEntryLabel());
5720 __ Bind(slow_path->GetExitLabel());
5721}
5722
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005723void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5724 LocationSummary* locations =
5725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5726 locations->SetInAt(0, Location::RequiresRegister());
5727 locations->SetOut(Location::RequiresRegister());
5728}
5729
5730void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5731 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005732 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005733 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005734 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005735 __ Ldr(XRegisterFrom(locations->Out()),
5736 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005737 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005738 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005739 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005740 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5741 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005742 __ Ldr(XRegisterFrom(locations->Out()),
5743 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005744 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005745}
5746
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00005747
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005748
Alexandre Rames67555f72014-11-18 10:55:16 +00005749#undef __
5750#undef QUICK_ENTRY_POINT
5751
Alexandre Rames5319def2014-10-23 10:03:10 +01005752} // namespace arm64
5753} // namespace art