blob: 37322c68f1bc9c48b60f7bf16f42e5fe1ab594de [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();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100462 Location class_to_check = locations->InAt(1);
463 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
464 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 DCHECK(instruction_->IsCheckCast()
466 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100468 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000469
Alexandre Rames67555f72014-11-18 10:55:16 +0000470 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000472 if (!is_fatal_) {
473 SaveLiveRegisters(codegen, locations);
474 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475
476 // We're moving two locations to locations that could overlap, so we need a parallel
477 // move resolver.
478 InvokeRuntimeCallingConvention calling_convention;
479 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100480 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
481 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000482
483 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000484 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700485 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000486 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487 Primitive::Type ret_type = instruction_->GetType();
488 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
489 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
490 } else {
491 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000492 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800493 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000494 }
495
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000496 if (!is_fatal_) {
497 RestoreLiveRegisters(codegen, locations);
498 __ B(GetExitLabel());
499 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000500 }
501
Alexandre Rames9931f312015-06-19 14:47:01 +0100502 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100503 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100504
Alexandre Rames67555f72014-11-18 10:55:16 +0000505 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507
Alexandre Rames67555f72014-11-18 10:55:16 +0000508 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
509};
510
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700511class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
512 public:
Aart Bik42249c32016-01-07 15:33:50 -0800513 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000514 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700515
516 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800517 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700518 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000519 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000520 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700521 }
522
Alexandre Rames9931f312015-06-19 14:47:01 +0100523 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
524
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700526 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
527};
528
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100529class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
530 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
534 LocationSummary* locations = instruction_->GetLocations();
535 __ Bind(GetEntryLabel());
536 SaveLiveRegisters(codegen, locations);
537
538 InvokeRuntimeCallingConvention calling_convention;
539 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
540 parallel_move.AddMove(
541 locations->InAt(0),
542 LocationFrom(calling_convention.GetRegisterAt(0)),
543 Primitive::kPrimNot,
544 nullptr);
545 parallel_move.AddMove(
546 locations->InAt(1),
547 LocationFrom(calling_convention.GetRegisterAt(1)),
548 Primitive::kPrimInt,
549 nullptr);
550 parallel_move.AddMove(
551 locations->InAt(2),
552 LocationFrom(calling_convention.GetRegisterAt(2)),
553 Primitive::kPrimNot,
554 nullptr);
555 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
556
557 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000558 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100559 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
560 RestoreLiveRegisters(codegen, locations);
561 __ B(GetExitLabel());
562 }
563
564 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
565
566 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100567 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
568};
569
Zheng Xu3927c8b2015-11-18 17:46:25 +0800570void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
571 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000572 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800573
574 // We are about to use the assembler to place literals directly. Make sure we have enough
575 // underlying code buffer and we have generated the jump table with right size.
576 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
577 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
578
579 __ Bind(&table_start_);
580 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
581 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100582 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800583 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100584 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800585 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
586 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
587 Literal<int32_t> literal(jump_offset);
588 __ place(&literal);
589 }
590}
591
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100592// Slow path marking an object reference `ref` during a read
593// barrier. The field `obj.field` in the object `obj` holding this
594// reference does not get updated by this slow path after marking (see
595// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
596//
597// This means that after the execution of this slow path, `ref` will
598// always be up-to-date, but `obj.field` may not; i.e., after the
599// flip, `ref` will be a to-space reference, but `obj.field` will
600// probably still be a from-space reference (unless it gets updated by
601// another thread, or if another thread installed another object
602// reference (different from `ref`) in `obj.field`).
Roland Levillain44015862016-01-22 11:47:17 +0000603class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
604 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100605 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location ref)
606 : SlowPathCodeARM64(instruction), ref_(ref) {
Roland Levillain44015862016-01-22 11:47:17 +0000607 DCHECK(kEmitCompilerReadBarrier);
608 }
609
610 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
611
612 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
613 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000614 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100615 DCHECK(ref_.IsRegister()) << ref_;
616 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000617 DCHECK(instruction_->IsInstanceFieldGet() ||
618 instruction_->IsStaticFieldGet() ||
619 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100620 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000621 instruction_->IsLoadClass() ||
622 instruction_->IsLoadString() ||
623 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100624 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100625 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
626 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000627 << "Unexpected instruction in read barrier marking slow path: "
628 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000629 // The read barrier instrumentation of object ArrayGet
630 // instructions does not support the HIntermediateAddress
631 // instruction.
632 DCHECK(!(instruction_->IsArrayGet() &&
633 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000634
635 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100636 // No need to save live registers; it's taken care of by the
637 // entrypoint. Also, there is no need to update the stack mask,
638 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000639 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100640 DCHECK_NE(ref_.reg(), LR);
641 DCHECK_NE(ref_.reg(), WSP);
642 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100643 // IP0 is used internally by the ReadBarrierMarkRegX entry point
644 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100645 DCHECK_NE(ref_.reg(), IP0);
646 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100647 // "Compact" slow path, saving two moves.
648 //
649 // Instead of using the standard runtime calling convention (input
650 // and output in W0):
651 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100652 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100653 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100654 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100655 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100656 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100657 // of a dedicated entrypoint:
658 //
659 // rX <- ReadBarrierMarkRegX(rX)
660 //
661 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100662 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100663 // This runtime call does not require a stack map.
664 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000665 __ B(GetExitLabel());
666 }
667
668 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100669 // The location (register) of the marked object reference.
670 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000671
672 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
673};
674
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100675// Slow path marking an object reference `ref` during a read barrier,
676// and if needed, atomically updating the field `obj.field` in the
677// object `obj` holding this reference after marking (contrary to
678// ReadBarrierMarkSlowPathARM64 above, which never tries to update
679// `obj.field`).
680//
681// This means that after the execution of this slow path, both `ref`
682// and `obj.field` will be up-to-date; i.e., after the flip, both will
683// hold the same to-space reference (unless another thread installed
684// another object reference (different from `ref`) in `obj.field`).
685class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
686 public:
687 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
688 Location ref,
689 Register obj,
690 Location field_offset,
691 Register temp)
692 : SlowPathCodeARM64(instruction),
693 ref_(ref),
694 obj_(obj),
695 field_offset_(field_offset),
696 temp_(temp) {
697 DCHECK(kEmitCompilerReadBarrier);
698 }
699
700 const char* GetDescription() const OVERRIDE {
701 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
702 }
703
704 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
705 LocationSummary* locations = instruction_->GetLocations();
706 Register ref_reg = WRegisterFrom(ref_);
707 DCHECK(locations->CanCall());
708 DCHECK(ref_.IsRegister()) << ref_;
709 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
710 // This slow path is only used by the UnsafeCASObject intrinsic.
711 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
712 << "Unexpected instruction in read barrier marking and field updating slow path: "
713 << instruction_->DebugName();
714 DCHECK(instruction_->GetLocations()->Intrinsified());
715 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
716 DCHECK(field_offset_.IsRegister()) << field_offset_;
717
718 __ Bind(GetEntryLabel());
719
720 // Save the old reference.
721 // Note that we cannot use IP to save the old reference, as IP is
722 // used internally by the ReadBarrierMarkRegX entry point, and we
723 // need the old reference after the call to that entry point.
724 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
725 __ Mov(temp_.W(), ref_reg);
726
727 // No need to save live registers; it's taken care of by the
728 // entrypoint. Also, there is no need to update the stack mask,
729 // as this runtime call will not trigger a garbage collection.
730 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
731 DCHECK_NE(ref_.reg(), LR);
732 DCHECK_NE(ref_.reg(), WSP);
733 DCHECK_NE(ref_.reg(), WZR);
734 // IP0 is used internally by the ReadBarrierMarkRegX entry point
735 // as a temporary, it cannot be the entry point's input/output.
736 DCHECK_NE(ref_.reg(), IP0);
737 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
738 // "Compact" slow path, saving two moves.
739 //
740 // Instead of using the standard runtime calling convention (input
741 // and output in W0):
742 //
743 // W0 <- ref
744 // W0 <- ReadBarrierMark(W0)
745 // ref <- W0
746 //
747 // we just use rX (the register containing `ref`) as input and output
748 // of a dedicated entrypoint:
749 //
750 // rX <- ReadBarrierMarkRegX(rX)
751 //
752 int32_t entry_point_offset =
753 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
754 // This runtime call does not require a stack map.
755 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
756
757 // If the new reference is different from the old reference,
758 // update the field in the holder (`*(obj_ + field_offset_)`).
759 //
760 // Note that this field could also hold a different object, if
761 // another thread had concurrently changed it. In that case, the
762 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
763 // (CAS) operation below would abort the CAS, leaving the field
764 // as-is.
765 vixl::aarch64::Label done;
766 __ Cmp(temp_.W(), ref_reg);
767 __ B(eq, &done);
768
769 // Update the the holder's field atomically. This may fail if
770 // mutator updates before us, but it's OK. This is achieved
771 // using a strong compare-and-set (CAS) operation with relaxed
772 // memory synchronization ordering, where the expected value is
773 // the old reference and the desired value is the new reference.
774
775 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
776 UseScratchRegisterScope temps(masm);
777
778 // Convenience aliases.
779 Register base = obj_.W();
780 Register offset = XRegisterFrom(field_offset_);
781 Register expected = temp_.W();
782 Register value = ref_reg;
783 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
784 Register tmp_value = temps.AcquireW(); // Value in memory.
785
786 __ Add(tmp_ptr, base.X(), Operand(offset));
787
788 if (kPoisonHeapReferences) {
789 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
790 if (value.Is(expected)) {
791 // Do not poison `value`, as it is the same register as
792 // `expected`, which has just been poisoned.
793 } else {
794 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
795 }
796 }
797
798 // do {
799 // tmp_value = [tmp_ptr] - expected;
800 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
801
Roland Levillain24a4d112016-10-26 13:10:46 +0100802 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100803 __ Bind(&loop_head);
804 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
805 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100806 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100807 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
808 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100809 __ B(&exit_loop);
810 __ Bind(&comparison_failed);
811 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100812 __ Bind(&exit_loop);
813
814 if (kPoisonHeapReferences) {
815 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
816 if (value.Is(expected)) {
817 // Do not unpoison `value`, as it is the same register as
818 // `expected`, which has just been unpoisoned.
819 } else {
820 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
821 }
822 }
823
824 __ Bind(&done);
825 __ B(GetExitLabel());
826 }
827
828 private:
829 // The location (register) of the marked object reference.
830 const Location ref_;
831 // The register containing the object holding the marked object reference field.
832 const Register obj_;
833 // The location of the offset of the marked reference field within `obj_`.
834 Location field_offset_;
835
836 const Register temp_;
837
838 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
839};
840
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000841// Slow path generating a read barrier for a heap reference.
842class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
843 public:
844 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
845 Location out,
846 Location ref,
847 Location obj,
848 uint32_t offset,
849 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000850 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000851 out_(out),
852 ref_(ref),
853 obj_(obj),
854 offset_(offset),
855 index_(index) {
856 DCHECK(kEmitCompilerReadBarrier);
857 // If `obj` is equal to `out` or `ref`, it means the initial object
858 // has been overwritten by (or after) the heap object reference load
859 // to be instrumented, e.g.:
860 //
861 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000862 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000863 //
864 // In that case, we have lost the information about the original
865 // object, and the emitted read barrier cannot work properly.
866 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
867 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
868 }
869
870 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
871 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
872 LocationSummary* locations = instruction_->GetLocations();
873 Primitive::Type type = Primitive::kPrimNot;
874 DCHECK(locations->CanCall());
875 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100876 DCHECK(instruction_->IsInstanceFieldGet() ||
877 instruction_->IsStaticFieldGet() ||
878 instruction_->IsArrayGet() ||
879 instruction_->IsInstanceOf() ||
880 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100881 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000882 << "Unexpected instruction in read barrier for heap reference slow path: "
883 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000884 // The read barrier instrumentation of object ArrayGet
885 // instructions does not support the HIntermediateAddress
886 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000887 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100888 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000889
890 __ Bind(GetEntryLabel());
891
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000892 SaveLiveRegisters(codegen, locations);
893
894 // We may have to change the index's value, but as `index_` is a
895 // constant member (like other "inputs" of this slow path),
896 // introduce a copy of it, `index`.
897 Location index = index_;
898 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100899 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000900 if (instruction_->IsArrayGet()) {
901 // Compute the actual memory offset and store it in `index`.
902 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
903 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
904 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
905 // We are about to change the value of `index_reg` (see the
906 // calls to vixl::MacroAssembler::Lsl and
907 // vixl::MacroAssembler::Mov below), but it has
908 // not been saved by the previous call to
909 // art::SlowPathCode::SaveLiveRegisters, as it is a
910 // callee-save register --
911 // art::SlowPathCode::SaveLiveRegisters does not consider
912 // callee-save registers, as it has been designed with the
913 // assumption that callee-save registers are supposed to be
914 // handled by the called function. So, as a callee-save
915 // register, `index_reg` _would_ eventually be saved onto
916 // the stack, but it would be too late: we would have
917 // changed its value earlier. Therefore, we manually save
918 // it here into another freely available register,
919 // `free_reg`, chosen of course among the caller-save
920 // registers (as a callee-save `free_reg` register would
921 // exhibit the same problem).
922 //
923 // Note we could have requested a temporary register from
924 // the register allocator instead; but we prefer not to, as
925 // this is a slow path, and we know we can find a
926 // caller-save register that is available.
927 Register free_reg = FindAvailableCallerSaveRegister(codegen);
928 __ Mov(free_reg.W(), index_reg);
929 index_reg = free_reg;
930 index = LocationFrom(index_reg);
931 } else {
932 // The initial register stored in `index_` has already been
933 // saved in the call to art::SlowPathCode::SaveLiveRegisters
934 // (as it is not a callee-save register), so we can freely
935 // use it.
936 }
937 // Shifting the index value contained in `index_reg` by the scale
938 // factor (2) cannot overflow in practice, as the runtime is
939 // unable to allocate object arrays with a size larger than
940 // 2^26 - 1 (that is, 2^28 - 4 bytes).
941 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
942 static_assert(
943 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
944 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
945 __ Add(index_reg, index_reg, Operand(offset_));
946 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100947 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
948 // intrinsics, `index_` is not shifted by a scale factor of 2
949 // (as in the case of ArrayGet), as it is actually an offset
950 // to an object field within an object.
951 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000952 DCHECK(instruction_->GetLocations()->Intrinsified());
953 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
954 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
955 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100956 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100957 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000958 }
959 }
960
961 // We're moving two or three locations to locations that could
962 // overlap, so we need a parallel move resolver.
963 InvokeRuntimeCallingConvention calling_convention;
964 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
965 parallel_move.AddMove(ref_,
966 LocationFrom(calling_convention.GetRegisterAt(0)),
967 type,
968 nullptr);
969 parallel_move.AddMove(obj_,
970 LocationFrom(calling_convention.GetRegisterAt(1)),
971 type,
972 nullptr);
973 if (index.IsValid()) {
974 parallel_move.AddMove(index,
975 LocationFrom(calling_convention.GetRegisterAt(2)),
976 Primitive::kPrimInt,
977 nullptr);
978 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
979 } else {
980 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
981 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
982 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000983 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000984 instruction_,
985 instruction_->GetDexPc(),
986 this);
987 CheckEntrypointTypes<
988 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
989 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
990
991 RestoreLiveRegisters(codegen, locations);
992
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000993 __ B(GetExitLabel());
994 }
995
996 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
997
998 private:
999 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001000 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1001 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001002 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1003 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1004 return Register(VIXLRegCodeFromART(i), kXRegSize);
1005 }
1006 }
1007 // We shall never fail to find a free caller-save register, as
1008 // there are more than two core caller-save registers on ARM64
1009 // (meaning it is possible to find one which is different from
1010 // `ref` and `obj`).
1011 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1012 LOG(FATAL) << "Could not find a free register";
1013 UNREACHABLE();
1014 }
1015
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001016 const Location out_;
1017 const Location ref_;
1018 const Location obj_;
1019 const uint32_t offset_;
1020 // An additional location containing an index to an array.
1021 // Only used for HArrayGet and the UnsafeGetObject &
1022 // UnsafeGetObjectVolatile intrinsics.
1023 const Location index_;
1024
1025 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1026};
1027
1028// Slow path generating a read barrier for a GC root.
1029class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1030 public:
1031 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001032 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001033 DCHECK(kEmitCompilerReadBarrier);
1034 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001035
1036 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1037 LocationSummary* locations = instruction_->GetLocations();
1038 Primitive::Type type = Primitive::kPrimNot;
1039 DCHECK(locations->CanCall());
1040 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001041 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1042 << "Unexpected instruction in read barrier for GC root slow path: "
1043 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001044
1045 __ Bind(GetEntryLabel());
1046 SaveLiveRegisters(codegen, locations);
1047
1048 InvokeRuntimeCallingConvention calling_convention;
1049 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1050 // The argument of the ReadBarrierForRootSlow is not a managed
1051 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1052 // thus we need a 64-bit move here, and we cannot use
1053 //
1054 // arm64_codegen->MoveLocation(
1055 // LocationFrom(calling_convention.GetRegisterAt(0)),
1056 // root_,
1057 // type);
1058 //
1059 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1060 // reference type (`Primitive::kPrimNot`).
1061 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001062 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001063 instruction_,
1064 instruction_->GetDexPc(),
1065 this);
1066 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1067 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1068
1069 RestoreLiveRegisters(codegen, locations);
1070 __ B(GetExitLabel());
1071 }
1072
1073 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1074
1075 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001076 const Location out_;
1077 const Location root_;
1078
1079 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1080};
1081
Alexandre Rames5319def2014-10-23 10:03:10 +01001082#undef __
1083
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001084Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001085 Location next_location;
1086 if (type == Primitive::kPrimVoid) {
1087 LOG(FATAL) << "Unreachable type " << type;
1088 }
1089
Alexandre Rames542361f2015-01-29 16:57:31 +00001090 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001091 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1092 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001093 } else if (!Primitive::IsFloatingPointType(type) &&
1094 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001095 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1096 } else {
1097 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001098 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1099 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001100 }
1101
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001102 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001103 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001104 return next_location;
1105}
1106
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001107Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001108 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001109}
1110
Serban Constantinescu579885a2015-02-22 20:51:33 +00001111CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1112 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001113 const CompilerOptions& compiler_options,
1114 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001115 : CodeGenerator(graph,
1116 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001117 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001118 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001119 callee_saved_core_registers.GetList(),
1120 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001121 compiler_options,
1122 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001123 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001124 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001125 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001126 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001127 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001128 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001129 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001130 uint32_literals_(std::less<uint32_t>(),
1131 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001132 uint64_literals_(std::less<uint64_t>(),
1133 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1134 method_patches_(MethodReferenceComparator(),
1135 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1136 call_patches_(MethodReferenceComparator(),
1137 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1138 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001139 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1140 boot_image_string_patches_(StringReferenceValueComparator(),
1141 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1142 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001143 boot_image_type_patches_(TypeReferenceValueComparator(),
1144 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1145 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001146 boot_image_address_patches_(std::less<uint32_t>(),
1147 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001148 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001149 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001150}
Alexandre Rames5319def2014-10-23 10:03:10 +01001151
Alexandre Rames67555f72014-11-18 10:55:16 +00001152#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001153
Zheng Xu3927c8b2015-11-18 17:46:25 +08001154void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001155 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001156 jump_table->EmitTable(this);
1157 }
1158}
1159
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001160void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001161 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001162 // Ensure we emit the literal pool.
1163 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001164
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001165 CodeGenerator::Finalize(allocator);
1166}
1167
Zheng Xuad4450e2015-04-17 18:48:56 +08001168void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1169 // Note: There are 6 kinds of moves:
1170 // 1. constant -> GPR/FPR (non-cycle)
1171 // 2. constant -> stack (non-cycle)
1172 // 3. GPR/FPR -> GPR/FPR
1173 // 4. GPR/FPR -> stack
1174 // 5. stack -> GPR/FPR
1175 // 6. stack -> stack (non-cycle)
1176 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1177 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1178 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1179 // dependency.
1180 vixl_temps_.Open(GetVIXLAssembler());
1181}
1182
1183void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1184 vixl_temps_.Close();
1185}
1186
1187Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1188 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1189 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1190 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1191 Location scratch = GetScratchLocation(kind);
1192 if (!scratch.Equals(Location::NoLocation())) {
1193 return scratch;
1194 }
1195 // Allocate from VIXL temp registers.
1196 if (kind == Location::kRegister) {
1197 scratch = LocationFrom(vixl_temps_.AcquireX());
1198 } else {
1199 DCHECK(kind == Location::kFpuRegister);
1200 scratch = LocationFrom(vixl_temps_.AcquireD());
1201 }
1202 AddScratchLocation(scratch);
1203 return scratch;
1204}
1205
1206void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1207 if (loc.IsRegister()) {
1208 vixl_temps_.Release(XRegisterFrom(loc));
1209 } else {
1210 DCHECK(loc.IsFpuRegister());
1211 vixl_temps_.Release(DRegisterFrom(loc));
1212 }
1213 RemoveScratchLocation(loc);
1214}
1215
Alexandre Rames3e69f162014-12-10 10:36:50 +00001216void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001217 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001219}
1220
Alexandre Rames5319def2014-10-23 10:03:10 +01001221void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001222 MacroAssembler* masm = GetVIXLAssembler();
1223 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001224 __ Bind(&frame_entry_label_);
1225
Serban Constantinescu02164b32014-11-13 14:05:07 +00001226 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1227 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001228 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001229 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001230 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001231 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001232 __ Ldr(wzr, MemOperand(temp, 0));
1233 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001234 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001235
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001236 if (!HasEmptyFrame()) {
1237 int frame_size = GetFrameSize();
1238 // Stack layout:
1239 // sp[frame_size - 8] : lr.
1240 // ... : other preserved core registers.
1241 // ... : other preserved fp registers.
1242 // ... : reserved frame space.
1243 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001244
1245 // Save the current method if we need it. Note that we do not
1246 // do this in HCurrentMethod, as the instruction might have been removed
1247 // in the SSA graph.
1248 if (RequiresCurrentMethod()) {
1249 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001250 } else {
1251 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001252 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001253 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001254 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1255 frame_size - GetCoreSpillSize());
1256 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1257 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001258 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001259}
1260
1261void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001262 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001263 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001264 if (!HasEmptyFrame()) {
1265 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001266 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1267 frame_size - FrameEntrySpillSize());
1268 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1269 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001270 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001271 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001272 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001273 __ Ret();
1274 GetAssembler()->cfi().RestoreState();
1275 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001276}
1277
Scott Wakeling97c72b72016-06-24 16:19:36 +01001278CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001279 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001280 return CPURegList(CPURegister::kRegister, kXRegSize,
1281 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001282}
1283
Scott Wakeling97c72b72016-06-24 16:19:36 +01001284CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001285 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1286 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001287 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1288 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001289}
1290
Alexandre Rames5319def2014-10-23 10:03:10 +01001291void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1292 __ Bind(GetLabelOf(block));
1293}
1294
Calin Juravle175dc732015-08-25 15:42:32 +01001295void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1296 DCHECK(location.IsRegister());
1297 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1298}
1299
Calin Juravlee460d1d2015-09-29 04:52:17 +01001300void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1301 if (location.IsRegister()) {
1302 locations->AddTemp(location);
1303 } else {
1304 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1305 }
1306}
1307
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001308void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001309 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001310 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001311 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001312 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001313 if (value_can_be_null) {
1314 __ Cbz(value, &done);
1315 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001316 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001317 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001318 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001319 if (value_can_be_null) {
1320 __ Bind(&done);
1321 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001322}
1323
David Brazdil58282f42016-01-14 12:45:10 +00001324void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001325 // Blocked core registers:
1326 // lr : Runtime reserved.
1327 // tr : Runtime reserved.
1328 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1329 // ip1 : VIXL core temp.
1330 // ip0 : VIXL core temp.
1331 //
1332 // Blocked fp registers:
1333 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001334 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1335 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001336 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001337 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001338 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001339
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001340 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001341 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001342 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001343 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001344
David Brazdil58282f42016-01-14 12:45:10 +00001345 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001346 // Stubs do not save callee-save floating point registers. If the graph
1347 // is debuggable, we need to deal with these registers differently. For
1348 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001349 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1350 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001352 }
1353 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001354}
1355
Alexandre Rames3e69f162014-12-10 10:36:50 +00001356size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1357 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1358 __ Str(reg, MemOperand(sp, stack_index));
1359 return kArm64WordSize;
1360}
1361
1362size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1363 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1364 __ Ldr(reg, MemOperand(sp, stack_index));
1365 return kArm64WordSize;
1366}
1367
1368size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1369 FPRegister reg = FPRegister(reg_id, kDRegSize);
1370 __ Str(reg, MemOperand(sp, stack_index));
1371 return kArm64WordSize;
1372}
1373
1374size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1375 FPRegister reg = FPRegister(reg_id, kDRegSize);
1376 __ Ldr(reg, MemOperand(sp, stack_index));
1377 return kArm64WordSize;
1378}
1379
Alexandre Rames5319def2014-10-23 10:03:10 +01001380void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001381 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001382}
1383
1384void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001385 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001386}
1387
Alexandre Rames67555f72014-11-18 10:55:16 +00001388void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001389 if (constant->IsIntConstant()) {
1390 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1391 } else if (constant->IsLongConstant()) {
1392 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1393 } else if (constant->IsNullConstant()) {
1394 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001395 } else if (constant->IsFloatConstant()) {
1396 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1397 } else {
1398 DCHECK(constant->IsDoubleConstant());
1399 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1400 }
1401}
1402
Alexandre Rames3e69f162014-12-10 10:36:50 +00001403
1404static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1405 DCHECK(constant.IsConstant());
1406 HConstant* cst = constant.GetConstant();
1407 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001408 // Null is mapped to a core W register, which we associate with kPrimInt.
1409 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001410 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1411 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1412 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1413}
1414
Calin Juravlee460d1d2015-09-29 04:52:17 +01001415void CodeGeneratorARM64::MoveLocation(Location destination,
1416 Location source,
1417 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001418 if (source.Equals(destination)) {
1419 return;
1420 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001421
1422 // A valid move can always be inferred from the destination and source
1423 // locations. When moving from and to a register, the argument type can be
1424 // used to generate 32bit instead of 64bit moves. In debug mode we also
1425 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001426 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001427
1428 if (destination.IsRegister() || destination.IsFpuRegister()) {
1429 if (unspecified_type) {
1430 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1431 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001432 (src_cst != nullptr && (src_cst->IsIntConstant()
1433 || src_cst->IsFloatConstant()
1434 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001435 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001436 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001437 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001438 // If the source is a double stack slot or a 64bit constant, a 64bit
1439 // type is appropriate. Else the source is a register, and since the
1440 // type has not been specified, we chose a 64bit type to force a 64bit
1441 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001442 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001443 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001444 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001445 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1446 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1447 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001448 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1449 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1450 __ Ldr(dst, StackOperandFrom(source));
1451 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001452 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001454 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001455 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001456 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001457 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001458 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001459 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1460 ? Primitive::kPrimLong
1461 : Primitive::kPrimInt;
1462 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1463 }
1464 } else {
1465 DCHECK(source.IsFpuRegister());
1466 if (destination.IsRegister()) {
1467 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1468 ? Primitive::kPrimDouble
1469 : Primitive::kPrimFloat;
1470 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1471 } else {
1472 DCHECK(destination.IsFpuRegister());
1473 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001474 }
1475 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001476 } else { // The destination is not a register. It must be a stack slot.
1477 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1478 if (source.IsRegister() || source.IsFpuRegister()) {
1479 if (unspecified_type) {
1480 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001481 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001482 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001483 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001484 }
1485 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001486 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1487 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1488 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001489 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001490 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1491 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001492 UseScratchRegisterScope temps(GetVIXLAssembler());
1493 HConstant* src_cst = source.GetConstant();
1494 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001495 if (src_cst->IsZeroBitPattern()) {
1496 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001497 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001498 if (src_cst->IsIntConstant()) {
1499 temp = temps.AcquireW();
1500 } else if (src_cst->IsLongConstant()) {
1501 temp = temps.AcquireX();
1502 } else if (src_cst->IsFloatConstant()) {
1503 temp = temps.AcquireS();
1504 } else {
1505 DCHECK(src_cst->IsDoubleConstant());
1506 temp = temps.AcquireD();
1507 }
1508 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001509 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001510 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001511 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001512 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001514 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001515 // There is generally less pressure on FP registers.
1516 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001517 __ Ldr(temp, StackOperandFrom(source));
1518 __ Str(temp, StackOperandFrom(destination));
1519 }
1520 }
1521}
1522
1523void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001524 CPURegister dst,
1525 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001526 switch (type) {
1527 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001528 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001529 break;
1530 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001531 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001532 break;
1533 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001534 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001535 break;
1536 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001537 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001538 break;
1539 case Primitive::kPrimInt:
1540 case Primitive::kPrimNot:
1541 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001542 case Primitive::kPrimFloat:
1543 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001544 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001545 __ Ldr(dst, src);
1546 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001547 case Primitive::kPrimVoid:
1548 LOG(FATAL) << "Unreachable type " << type;
1549 }
1550}
1551
Calin Juravle77520bc2015-01-12 18:45:46 +00001552void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001553 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001554 const MemOperand& src,
1555 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001556 MacroAssembler* masm = GetVIXLAssembler();
1557 BlockPoolsScope block_pools(masm);
1558 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001559 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001560 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001561
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001562 DCHECK(!src.IsPreIndex());
1563 DCHECK(!src.IsPostIndex());
1564
1565 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001566 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001567 MemOperand base = MemOperand(temp_base);
1568 switch (type) {
1569 case Primitive::kPrimBoolean:
1570 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001571 if (needs_null_check) {
1572 MaybeRecordImplicitNullCheck(instruction);
1573 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001574 break;
1575 case Primitive::kPrimByte:
1576 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001577 if (needs_null_check) {
1578 MaybeRecordImplicitNullCheck(instruction);
1579 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001580 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1581 break;
1582 case Primitive::kPrimChar:
1583 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001584 if (needs_null_check) {
1585 MaybeRecordImplicitNullCheck(instruction);
1586 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001587 break;
1588 case Primitive::kPrimShort:
1589 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001590 if (needs_null_check) {
1591 MaybeRecordImplicitNullCheck(instruction);
1592 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001593 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1594 break;
1595 case Primitive::kPrimInt:
1596 case Primitive::kPrimNot:
1597 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001598 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001599 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001600 if (needs_null_check) {
1601 MaybeRecordImplicitNullCheck(instruction);
1602 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001603 break;
1604 case Primitive::kPrimFloat:
1605 case Primitive::kPrimDouble: {
1606 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001607 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001608
1609 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1610 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001611 if (needs_null_check) {
1612 MaybeRecordImplicitNullCheck(instruction);
1613 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001614 __ Fmov(FPRegister(dst), temp);
1615 break;
1616 }
1617 case Primitive::kPrimVoid:
1618 LOG(FATAL) << "Unreachable type " << type;
1619 }
1620}
1621
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001622void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001623 CPURegister src,
1624 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001625 switch (type) {
1626 case Primitive::kPrimBoolean:
1627 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001628 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001629 break;
1630 case Primitive::kPrimChar:
1631 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001632 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001633 break;
1634 case Primitive::kPrimInt:
1635 case Primitive::kPrimNot:
1636 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001637 case Primitive::kPrimFloat:
1638 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001639 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001640 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001641 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001642 case Primitive::kPrimVoid:
1643 LOG(FATAL) << "Unreachable type " << type;
1644 }
1645}
1646
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001647void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1648 CPURegister src,
1649 const MemOperand& dst) {
1650 UseScratchRegisterScope temps(GetVIXLAssembler());
1651 Register temp_base = temps.AcquireX();
1652
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001653 DCHECK(!dst.IsPreIndex());
1654 DCHECK(!dst.IsPostIndex());
1655
1656 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001657 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001658 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001659 MemOperand base = MemOperand(temp_base);
1660 switch (type) {
1661 case Primitive::kPrimBoolean:
1662 case Primitive::kPrimByte:
1663 __ Stlrb(Register(src), base);
1664 break;
1665 case Primitive::kPrimChar:
1666 case Primitive::kPrimShort:
1667 __ Stlrh(Register(src), base);
1668 break;
1669 case Primitive::kPrimInt:
1670 case Primitive::kPrimNot:
1671 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001672 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001673 __ Stlr(Register(src), base);
1674 break;
1675 case Primitive::kPrimFloat:
1676 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001677 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001678 Register temp_src;
1679 if (src.IsZero()) {
1680 // The zero register is used to avoid synthesizing zero constants.
1681 temp_src = Register(src);
1682 } else {
1683 DCHECK(src.IsFPRegister());
1684 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1685 __ Fmov(temp_src, FPRegister(src));
1686 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001687
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001688 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001689 break;
1690 }
1691 case Primitive::kPrimVoid:
1692 LOG(FATAL) << "Unreachable type " << type;
1693 }
1694}
1695
Calin Juravle175dc732015-08-25 15:42:32 +01001696void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1697 HInstruction* instruction,
1698 uint32_t dex_pc,
1699 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001700 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001701 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001702 if (EntrypointRequiresStackMap(entrypoint)) {
1703 RecordPcInfo(instruction, dex_pc, slow_path);
1704 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001705}
1706
Roland Levillaindec8f632016-07-22 17:10:06 +01001707void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1708 HInstruction* instruction,
1709 SlowPathCode* slow_path) {
1710 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001711 GenerateInvokeRuntime(entry_point_offset);
1712}
1713
1714void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001715 BlockPoolsScope block_pools(GetVIXLAssembler());
1716 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1717 __ Blr(lr);
1718}
1719
Alexandre Rames67555f72014-11-18 10:55:16 +00001720void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001721 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001722 UseScratchRegisterScope temps(GetVIXLAssembler());
1723 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001724 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1725
Serban Constantinescu02164b32014-11-13 14:05:07 +00001726 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001727 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1728 __ Add(temp, class_reg, status_offset);
1729 __ Ldar(temp, HeapOperand(temp));
1730 __ Cmp(temp, mirror::Class::kStatusInitialized);
1731 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001732 __ Bind(slow_path->GetExitLabel());
1733}
Alexandre Rames5319def2014-10-23 10:03:10 +01001734
Roland Levillain44015862016-01-22 11:47:17 +00001735void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001736 BarrierType type = BarrierAll;
1737
1738 switch (kind) {
1739 case MemBarrierKind::kAnyAny:
1740 case MemBarrierKind::kAnyStore: {
1741 type = BarrierAll;
1742 break;
1743 }
1744 case MemBarrierKind::kLoadAny: {
1745 type = BarrierReads;
1746 break;
1747 }
1748 case MemBarrierKind::kStoreStore: {
1749 type = BarrierWrites;
1750 break;
1751 }
1752 default:
1753 LOG(FATAL) << "Unexpected memory barrier " << kind;
1754 }
1755 __ Dmb(InnerShareable, type);
1756}
1757
Serban Constantinescu02164b32014-11-13 14:05:07 +00001758void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1759 HBasicBlock* successor) {
1760 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001761 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1762 if (slow_path == nullptr) {
1763 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1764 instruction->SetSlowPath(slow_path);
1765 codegen_->AddSlowPath(slow_path);
1766 if (successor != nullptr) {
1767 DCHECK(successor->IsLoopHeader());
1768 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1769 }
1770 } else {
1771 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1772 }
1773
Serban Constantinescu02164b32014-11-13 14:05:07 +00001774 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1775 Register temp = temps.AcquireW();
1776
Andreas Gampe542451c2016-07-26 09:02:02 -07001777 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001778 if (successor == nullptr) {
1779 __ Cbnz(temp, slow_path->GetEntryLabel());
1780 __ Bind(slow_path->GetReturnLabel());
1781 } else {
1782 __ Cbz(temp, codegen_->GetLabelOf(successor));
1783 __ B(slow_path->GetEntryLabel());
1784 // slow_path will return to GetLabelOf(successor).
1785 }
1786}
1787
Alexandre Rames5319def2014-10-23 10:03:10 +01001788InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1789 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001790 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001791 assembler_(codegen->GetAssembler()),
1792 codegen_(codegen) {}
1793
1794#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001795 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001796
1797#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1798
1799enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001800 // Using a base helps identify when we hit such breakpoints.
1801 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001802#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1803 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1804#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1805};
1806
1807#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001808 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001809 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1810 } \
1811 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1812 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1813 locations->SetOut(Location::Any()); \
1814 }
1815 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1816#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1817
1818#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001819#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001820
Alexandre Rames67555f72014-11-18 10:55:16 +00001821void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001822 DCHECK_EQ(instr->InputCount(), 2U);
1823 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1824 Primitive::Type type = instr->GetResultType();
1825 switch (type) {
1826 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001827 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001828 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001829 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001831 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001832
1833 case Primitive::kPrimFloat:
1834 case Primitive::kPrimDouble:
1835 locations->SetInAt(0, Location::RequiresFpuRegister());
1836 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001837 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001838 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001839
Alexandre Rames5319def2014-10-23 10:03:10 +01001840 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001841 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001842 }
1843}
1844
Alexandre Rames09a99962015-04-15 11:47:56 +01001845void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001846 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1847
1848 bool object_field_get_with_read_barrier =
1849 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001850 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001851 new (GetGraph()->GetArena()) LocationSummary(instruction,
1852 object_field_get_with_read_barrier ?
1853 LocationSummary::kCallOnSlowPath :
1854 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001855 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001856 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001857 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001858 locations->SetInAt(0, Location::RequiresRegister());
1859 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1860 locations->SetOut(Location::RequiresFpuRegister());
1861 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001862 // The output overlaps for an object field get when read barriers
1863 // are enabled: we do not want the load to overwrite the object's
1864 // location, as we need it to emit the read barrier.
1865 locations->SetOut(
1866 Location::RequiresRegister(),
1867 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001868 }
1869}
1870
1871void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1872 const FieldInfo& field_info) {
1873 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001874 LocationSummary* locations = instruction->GetLocations();
1875 Location base_loc = locations->InAt(0);
1876 Location out = locations->Out();
1877 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001878 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001879 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001880 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001881
Roland Levillain44015862016-01-22 11:47:17 +00001882 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1883 // Object FieldGet with Baker's read barrier case.
1884 MacroAssembler* masm = GetVIXLAssembler();
1885 UseScratchRegisterScope temps(masm);
1886 // /* HeapReference<Object> */ out = *(base + offset)
1887 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1888 Register temp = temps.AcquireW();
1889 // Note that potential implicit null checks are handled in this
1890 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1891 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1892 instruction,
1893 out,
1894 base,
1895 offset,
1896 temp,
1897 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001898 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001899 } else {
1900 // General case.
1901 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001902 // Note that a potential implicit null check is handled in this
1903 // CodeGeneratorARM64::LoadAcquire call.
1904 // NB: LoadAcquire will record the pc info if needed.
1905 codegen_->LoadAcquire(
1906 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001907 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001908 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001909 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001910 }
Roland Levillain44015862016-01-22 11:47:17 +00001911 if (field_type == Primitive::kPrimNot) {
1912 // If read barriers are enabled, emit read barriers other than
1913 // Baker's using a slow path (and also unpoison the loaded
1914 // reference, if heap poisoning is enabled).
1915 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1916 }
Roland Levillain4d027112015-07-01 15:41:14 +01001917 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001918}
1919
1920void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1921 LocationSummary* locations =
1922 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1923 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001924 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1925 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1926 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001927 locations->SetInAt(1, Location::RequiresFpuRegister());
1928 } else {
1929 locations->SetInAt(1, Location::RequiresRegister());
1930 }
1931}
1932
1933void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001934 const FieldInfo& field_info,
1935 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001936 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001937 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001938
1939 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001940 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001941 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001942 Offset offset = field_info.GetFieldOffset();
1943 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001944
Roland Levillain4d027112015-07-01 15:41:14 +01001945 {
1946 // We use a block to end the scratch scope before the write barrier, thus
1947 // freeing the temporary registers so they can be used in `MarkGCCard`.
1948 UseScratchRegisterScope temps(GetVIXLAssembler());
1949
1950 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1951 DCHECK(value.IsW());
1952 Register temp = temps.AcquireW();
1953 __ Mov(temp, value.W());
1954 GetAssembler()->PoisonHeapReference(temp.W());
1955 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001956 }
Roland Levillain4d027112015-07-01 15:41:14 +01001957
1958 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001959 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1960 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001961 } else {
1962 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1963 codegen_->MaybeRecordImplicitNullCheck(instruction);
1964 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001965 }
1966
1967 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001968 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001969 }
1970}
1971
Alexandre Rames67555f72014-11-18 10:55:16 +00001972void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001973 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001974
1975 switch (type) {
1976 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001977 case Primitive::kPrimLong: {
1978 Register dst = OutputRegister(instr);
1979 Register lhs = InputRegisterAt(instr, 0);
1980 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001981 if (instr->IsAdd()) {
1982 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001983 } else if (instr->IsAnd()) {
1984 __ And(dst, lhs, rhs);
1985 } else if (instr->IsOr()) {
1986 __ Orr(dst, lhs, rhs);
1987 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001988 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001989 } else if (instr->IsRor()) {
1990 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001991 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001992 __ Ror(dst, lhs, shift);
1993 } else {
1994 // Ensure shift distance is in the same size register as the result. If
1995 // we are rotating a long and the shift comes in a w register originally,
1996 // we don't need to sxtw for use as an x since the shift distances are
1997 // all & reg_bits - 1.
1998 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1999 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002000 } else {
2001 DCHECK(instr->IsXor());
2002 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002003 }
2004 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002005 }
2006 case Primitive::kPrimFloat:
2007 case Primitive::kPrimDouble: {
2008 FPRegister dst = OutputFPRegister(instr);
2009 FPRegister lhs = InputFPRegisterAt(instr, 0);
2010 FPRegister rhs = InputFPRegisterAt(instr, 1);
2011 if (instr->IsAdd()) {
2012 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002013 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002014 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002015 } else {
2016 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002017 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002018 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002019 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002020 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002021 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002022 }
2023}
2024
Serban Constantinescu02164b32014-11-13 14:05:07 +00002025void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2026 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2027
2028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2029 Primitive::Type type = instr->GetResultType();
2030 switch (type) {
2031 case Primitive::kPrimInt:
2032 case Primitive::kPrimLong: {
2033 locations->SetInAt(0, Location::RequiresRegister());
2034 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2035 locations->SetOut(Location::RequiresRegister());
2036 break;
2037 }
2038 default:
2039 LOG(FATAL) << "Unexpected shift type " << type;
2040 }
2041}
2042
2043void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2044 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2045
2046 Primitive::Type type = instr->GetType();
2047 switch (type) {
2048 case Primitive::kPrimInt:
2049 case Primitive::kPrimLong: {
2050 Register dst = OutputRegister(instr);
2051 Register lhs = InputRegisterAt(instr, 0);
2052 Operand rhs = InputOperandAt(instr, 1);
2053 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002054 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002055 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002056 if (instr->IsShl()) {
2057 __ Lsl(dst, lhs, shift_value);
2058 } else if (instr->IsShr()) {
2059 __ Asr(dst, lhs, shift_value);
2060 } else {
2061 __ Lsr(dst, lhs, shift_value);
2062 }
2063 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002064 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002065
2066 if (instr->IsShl()) {
2067 __ Lsl(dst, lhs, rhs_reg);
2068 } else if (instr->IsShr()) {
2069 __ Asr(dst, lhs, rhs_reg);
2070 } else {
2071 __ Lsr(dst, lhs, rhs_reg);
2072 }
2073 }
2074 break;
2075 }
2076 default:
2077 LOG(FATAL) << "Unexpected shift operation type " << type;
2078 }
2079}
2080
Alexandre Rames5319def2014-10-23 10:03:10 +01002081void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002082 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002083}
2084
2085void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002086 HandleBinaryOp(instruction);
2087}
2088
2089void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2090 HandleBinaryOp(instruction);
2091}
2092
2093void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2094 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002095}
2096
Artem Serov7fc63502016-02-09 17:15:29 +00002097void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002098 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2100 locations->SetInAt(0, Location::RequiresRegister());
2101 // There is no immediate variant of negated bitwise instructions in AArch64.
2102 locations->SetInAt(1, Location::RequiresRegister());
2103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2104}
2105
Artem Serov7fc63502016-02-09 17:15:29 +00002106void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002107 Register dst = OutputRegister(instr);
2108 Register lhs = InputRegisterAt(instr, 0);
2109 Register rhs = InputRegisterAt(instr, 1);
2110
2111 switch (instr->GetOpKind()) {
2112 case HInstruction::kAnd:
2113 __ Bic(dst, lhs, rhs);
2114 break;
2115 case HInstruction::kOr:
2116 __ Orn(dst, lhs, rhs);
2117 break;
2118 case HInstruction::kXor:
2119 __ Eon(dst, lhs, rhs);
2120 break;
2121 default:
2122 LOG(FATAL) << "Unreachable";
2123 }
2124}
2125
Alexandre Rames8626b742015-11-25 16:28:08 +00002126void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2127 HArm64DataProcWithShifterOp* instruction) {
2128 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2129 instruction->GetType() == Primitive::kPrimLong);
2130 LocationSummary* locations =
2131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2132 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2133 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2134 } else {
2135 locations->SetInAt(0, Location::RequiresRegister());
2136 }
2137 locations->SetInAt(1, Location::RequiresRegister());
2138 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2139}
2140
2141void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2142 HArm64DataProcWithShifterOp* instruction) {
2143 Primitive::Type type = instruction->GetType();
2144 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2145 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2146 Register out = OutputRegister(instruction);
2147 Register left;
2148 if (kind != HInstruction::kNeg) {
2149 left = InputRegisterAt(instruction, 0);
2150 }
2151 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2152 // shifter operand operation, the IR generating `right_reg` (input to the type
2153 // conversion) can have a different type from the current instruction's type,
2154 // so we manually indicate the type.
2155 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002156 int64_t shift_amount = instruction->GetShiftAmount() &
2157 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002158
2159 Operand right_operand(0);
2160
2161 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2162 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2163 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2164 } else {
2165 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2166 }
2167
2168 // Logical binary operations do not support extension operations in the
2169 // operand. Note that VIXL would still manage if it was passed by generating
2170 // the extension as a separate instruction.
2171 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2172 DCHECK(!right_operand.IsExtendedRegister() ||
2173 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2174 kind != HInstruction::kNeg));
2175 switch (kind) {
2176 case HInstruction::kAdd:
2177 __ Add(out, left, right_operand);
2178 break;
2179 case HInstruction::kAnd:
2180 __ And(out, left, right_operand);
2181 break;
2182 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002183 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002184 __ Neg(out, right_operand);
2185 break;
2186 case HInstruction::kOr:
2187 __ Orr(out, left, right_operand);
2188 break;
2189 case HInstruction::kSub:
2190 __ Sub(out, left, right_operand);
2191 break;
2192 case HInstruction::kXor:
2193 __ Eor(out, left, right_operand);
2194 break;
2195 default:
2196 LOG(FATAL) << "Unexpected operation kind: " << kind;
2197 UNREACHABLE();
2198 }
2199}
2200
Artem Serov328429f2016-07-06 16:23:04 +01002201void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002202 LocationSummary* locations =
2203 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2204 locations->SetInAt(0, Location::RequiresRegister());
2205 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2206 locations->SetOut(Location::RequiresRegister());
2207}
2208
Roland Levillain19c54192016-11-04 13:44:09 +00002209void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002210 __ Add(OutputRegister(instruction),
2211 InputRegisterAt(instruction, 0),
2212 Operand(InputOperandAt(instruction, 1)));
2213}
2214
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002215void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002216 LocationSummary* locations =
2217 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002218 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2219 if (instr->GetOpKind() == HInstruction::kSub &&
2220 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002221 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002222 // Don't allocate register for Mneg instruction.
2223 } else {
2224 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2225 Location::RequiresRegister());
2226 }
2227 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2228 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2230}
2231
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002232void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002233 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002234 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2235 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002236
2237 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2238 // This fixup should be carried out for all multiply-accumulate instructions:
2239 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2240 if (instr->GetType() == Primitive::kPrimLong &&
2241 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2242 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002243 vixl::aarch64::Instruction* prev =
2244 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002245 if (prev->IsLoadOrStore()) {
2246 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002247 vixl::aarch64::CodeBufferCheckScope scope(masm,
2248 kInstructionSize,
2249 vixl::aarch64::CodeBufferCheckScope::kCheck,
2250 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002251 __ nop();
2252 }
2253 }
2254
2255 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002256 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002257 __ Madd(res, mul_left, mul_right, accumulator);
2258 } else {
2259 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002260 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002261 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002262 __ Mneg(res, mul_left, mul_right);
2263 } else {
2264 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2265 __ Msub(res, mul_left, mul_right, accumulator);
2266 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002267 }
2268}
2269
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002270void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002271 bool object_array_get_with_read_barrier =
2272 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002273 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002274 new (GetGraph()->GetArena()) LocationSummary(instruction,
2275 object_array_get_with_read_barrier ?
2276 LocationSummary::kCallOnSlowPath :
2277 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002278 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002279 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002280 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002281 locations->SetInAt(0, Location::RequiresRegister());
2282 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002283 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2285 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002286 // The output overlaps in the case of an object array get with
2287 // read barriers enabled: we do not want the move to overwrite the
2288 // array's location, as we need it to emit the read barrier.
2289 locations->SetOut(
2290 Location::RequiresRegister(),
2291 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002292 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002293}
2294
2295void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002296 Primitive::Type type = instruction->GetType();
2297 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002298 LocationSummary* locations = instruction->GetLocations();
2299 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002300 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002301 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002302 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2303 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002304 MacroAssembler* masm = GetVIXLAssembler();
2305 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002306 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002307 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002308
Roland Levillain19c54192016-11-04 13:44:09 +00002309 // The read barrier instrumentation of object ArrayGet instructions
2310 // does not support the HIntermediateAddress instruction.
2311 DCHECK(!((type == Primitive::kPrimNot) &&
2312 instruction->GetArray()->IsIntermediateAddress() &&
2313 kEmitCompilerReadBarrier));
2314
Roland Levillain44015862016-01-22 11:47:17 +00002315 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2316 // Object ArrayGet with Baker's read barrier case.
2317 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002318 // Note that a potential implicit null check is handled in the
2319 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2320 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2321 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002322 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002323 // General case.
2324 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002325 Register length;
2326 if (maybe_compressed_char_at) {
2327 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2328 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002329 if (instruction->GetArray()->IsIntermediateAddress()) {
2330 DCHECK_LT(count_offset, offset);
2331 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2332 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2333 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2334 } else {
2335 __ Ldr(length, HeapOperand(obj, count_offset));
2336 }
jessicahandojo05765752016-09-09 19:01:32 -07002337 codegen_->MaybeRecordImplicitNullCheck(instruction);
2338 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002339 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002340 if (maybe_compressed_char_at) {
2341 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002342 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2343 "Expecting 0=compressed, 1=uncompressed");
2344 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002345 __ Ldrb(Register(OutputCPURegister(instruction)),
2346 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2347 __ B(&done);
2348 __ Bind(&uncompressed_load);
2349 __ Ldrh(Register(OutputCPURegister(instruction)),
2350 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2351 __ Bind(&done);
2352 } else {
2353 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2354 source = HeapOperand(obj, offset);
2355 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002356 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002357 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002358 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002359 // We do not need to compute the intermediate address from the array: the
2360 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002361 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002362 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002363 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002364 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2365 }
2366 temp = obj;
2367 } else {
2368 __ Add(temp, obj, offset);
2369 }
jessicahandojo05765752016-09-09 19:01:32 -07002370 if (maybe_compressed_char_at) {
2371 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002372 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2373 "Expecting 0=compressed, 1=uncompressed");
2374 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002375 __ Ldrb(Register(OutputCPURegister(instruction)),
2376 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2377 __ B(&done);
2378 __ Bind(&uncompressed_load);
2379 __ Ldrh(Register(OutputCPURegister(instruction)),
2380 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2381 __ Bind(&done);
2382 } else {
2383 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2384 }
Roland Levillain44015862016-01-22 11:47:17 +00002385 }
jessicahandojo05765752016-09-09 19:01:32 -07002386 if (!maybe_compressed_char_at) {
2387 codegen_->Load(type, OutputCPURegister(instruction), source);
2388 codegen_->MaybeRecordImplicitNullCheck(instruction);
2389 }
Roland Levillain44015862016-01-22 11:47:17 +00002390
2391 if (type == Primitive::kPrimNot) {
2392 static_assert(
2393 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2394 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2395 Location obj_loc = locations->InAt(0);
2396 if (index.IsConstant()) {
2397 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2398 } else {
2399 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2400 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002401 }
Roland Levillain4d027112015-07-01 15:41:14 +01002402 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002403}
2404
Alexandre Rames5319def2014-10-23 10:03:10 +01002405void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2407 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002409}
2410
2411void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002412 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002413 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002414 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002415 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002416 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002417 // Mask out compression flag from String's array length.
2418 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002419 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002420 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002421}
2422
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002423void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002424 Primitive::Type value_type = instruction->GetComponentType();
2425
2426 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2428 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002429 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002430 LocationSummary::kCallOnSlowPath :
2431 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002432 locations->SetInAt(0, Location::RequiresRegister());
2433 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002434 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2435 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2436 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002437 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002438 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002439 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002440 }
2441}
2442
2443void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2444 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002445 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002446 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002447 bool needs_write_barrier =
2448 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002449
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002450 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002451 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002452 CPURegister source = value;
2453 Location index = locations->InAt(1);
2454 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2455 MemOperand destination = HeapOperand(array);
2456 MacroAssembler* masm = GetVIXLAssembler();
2457 BlockPoolsScope block_pools(masm);
2458
2459 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002460 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002461 if (index.IsConstant()) {
2462 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2463 destination = HeapOperand(array, offset);
2464 } else {
2465 UseScratchRegisterScope temps(masm);
2466 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002467 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002468 // We do not need to compute the intermediate address from the array: the
2469 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002470 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002471 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002472 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002473 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2474 }
2475 temp = array;
2476 } else {
2477 __ Add(temp, array, offset);
2478 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002479 destination = HeapOperand(temp,
2480 XRegisterFrom(index),
2481 LSL,
2482 Primitive::ComponentSizeShift(value_type));
2483 }
2484 codegen_->Store(value_type, value, destination);
2485 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002486 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002487 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002488 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002489 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002490 {
2491 // We use a block to end the scratch scope before the write barrier, thus
2492 // freeing the temporary registers so they can be used in `MarkGCCard`.
2493 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002494 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002495 if (index.IsConstant()) {
2496 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002497 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002498 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002499 destination = HeapOperand(temp,
2500 XRegisterFrom(index),
2501 LSL,
2502 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002503 }
2504
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002505 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2506 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2507 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2508
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002509 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002510 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2511 codegen_->AddSlowPath(slow_path);
2512 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002513 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002514 __ Cbnz(Register(value), &non_zero);
2515 if (!index.IsConstant()) {
2516 __ Add(temp, array, offset);
2517 }
2518 __ Str(wzr, destination);
2519 codegen_->MaybeRecordImplicitNullCheck(instruction);
2520 __ B(&done);
2521 __ Bind(&non_zero);
2522 }
2523
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002524 // Note that when Baker read barriers are enabled, the type
2525 // checks are performed without read barriers. This is fine,
2526 // even in the case where a class object is in the from-space
2527 // after the flip, as a comparison involving such a type would
2528 // not produce a false positive; it may of course produce a
2529 // false negative, in which case we would take the ArraySet
2530 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002531
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002532 Register temp2 = temps.AcquireSameSizeAs(array);
2533 // /* HeapReference<Class> */ temp = array->klass_
2534 __ Ldr(temp, HeapOperand(array, class_offset));
2535 codegen_->MaybeRecordImplicitNullCheck(instruction);
2536 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002537
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002538 // /* HeapReference<Class> */ temp = temp->component_type_
2539 __ Ldr(temp, HeapOperand(temp, component_offset));
2540 // /* HeapReference<Class> */ temp2 = value->klass_
2541 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2542 // If heap poisoning is enabled, no need to unpoison `temp`
2543 // nor `temp2`, as we are comparing two poisoned references.
2544 __ Cmp(temp, temp2);
2545 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002546
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002547 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2548 vixl::aarch64::Label do_put;
2549 __ B(eq, &do_put);
2550 // If heap poisoning is enabled, the `temp` reference has
2551 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002552 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2553
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002554 // /* HeapReference<Class> */ temp = temp->super_class_
2555 __ Ldr(temp, HeapOperand(temp, super_offset));
2556 // If heap poisoning is enabled, no need to unpoison
2557 // `temp`, as we are comparing against null below.
2558 __ Cbnz(temp, slow_path->GetEntryLabel());
2559 __ Bind(&do_put);
2560 } else {
2561 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002562 }
2563 }
2564
2565 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002566 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002567 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002568 __ Mov(temp2, value.W());
2569 GetAssembler()->PoisonHeapReference(temp2);
2570 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002571 }
2572
2573 if (!index.IsConstant()) {
2574 __ Add(temp, array, offset);
2575 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002576 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002577
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002578 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002579 codegen_->MaybeRecordImplicitNullCheck(instruction);
2580 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002581 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002582
2583 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2584
2585 if (done.IsLinked()) {
2586 __ Bind(&done);
2587 }
2588
2589 if (slow_path != nullptr) {
2590 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002591 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002592 }
2593}
2594
Alexandre Rames67555f72014-11-18 10:55:16 +00002595void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002596 RegisterSet caller_saves = RegisterSet::Empty();
2597 InvokeRuntimeCallingConvention calling_convention;
2598 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2599 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2600 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002601 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002602 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002603}
2604
2605void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002606 BoundsCheckSlowPathARM64* slow_path =
2607 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002608 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002609 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2610 __ B(slow_path->GetEntryLabel(), hs);
2611}
2612
Alexandre Rames67555f72014-11-18 10:55:16 +00002613void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2614 LocationSummary* locations =
2615 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2616 locations->SetInAt(0, Location::RequiresRegister());
2617 if (check->HasUses()) {
2618 locations->SetOut(Location::SameAsFirstInput());
2619 }
2620}
2621
2622void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2623 // We assume the class is not null.
2624 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2625 check->GetLoadClass(), check, check->GetDexPc(), true);
2626 codegen_->AddSlowPath(slow_path);
2627 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2628}
2629
Roland Levillain1a653882016-03-18 18:05:57 +00002630static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2631 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2632 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2633}
2634
2635void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2636 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2637 Location rhs_loc = instruction->GetLocations()->InAt(1);
2638 if (rhs_loc.IsConstant()) {
2639 // 0.0 is the only immediate that can be encoded directly in
2640 // an FCMP instruction.
2641 //
2642 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2643 // specify that in a floating-point comparison, positive zero
2644 // and negative zero are considered equal, so we can use the
2645 // literal 0.0 for both cases here.
2646 //
2647 // Note however that some methods (Float.equal, Float.compare,
2648 // Float.compareTo, Double.equal, Double.compare,
2649 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2650 // StrictMath.min) consider 0.0 to be (strictly) greater than
2651 // -0.0. So if we ever translate calls to these methods into a
2652 // HCompare instruction, we must handle the -0.0 case with
2653 // care here.
2654 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2655 __ Fcmp(lhs_reg, 0.0);
2656 } else {
2657 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2658 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002659}
2660
Serban Constantinescu02164b32014-11-13 14:05:07 +00002661void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002662 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002663 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2664 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002665 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002666 case Primitive::kPrimBoolean:
2667 case Primitive::kPrimByte:
2668 case Primitive::kPrimShort:
2669 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002670 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002671 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002672 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002673 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2675 break;
2676 }
2677 case Primitive::kPrimFloat:
2678 case Primitive::kPrimDouble: {
2679 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002680 locations->SetInAt(1,
2681 IsFloatingPointZeroConstant(compare->InputAt(1))
2682 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2683 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002684 locations->SetOut(Location::RequiresRegister());
2685 break;
2686 }
2687 default:
2688 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2689 }
2690}
2691
2692void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2693 Primitive::Type in_type = compare->InputAt(0)->GetType();
2694
2695 // 0 if: left == right
2696 // 1 if: left > right
2697 // -1 if: left < right
2698 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002699 case Primitive::kPrimBoolean:
2700 case Primitive::kPrimByte:
2701 case Primitive::kPrimShort:
2702 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002703 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002704 case Primitive::kPrimLong: {
2705 Register result = OutputRegister(compare);
2706 Register left = InputRegisterAt(compare, 0);
2707 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002708 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002709 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2710 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002711 break;
2712 }
2713 case Primitive::kPrimFloat:
2714 case Primitive::kPrimDouble: {
2715 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002716 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002717 __ Cset(result, ne);
2718 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002719 break;
2720 }
2721 default:
2722 LOG(FATAL) << "Unimplemented compare type " << in_type;
2723 }
2724}
2725
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002726void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002727 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002728
2729 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2730 locations->SetInAt(0, Location::RequiresFpuRegister());
2731 locations->SetInAt(1,
2732 IsFloatingPointZeroConstant(instruction->InputAt(1))
2733 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2734 : Location::RequiresFpuRegister());
2735 } else {
2736 // Integer cases.
2737 locations->SetInAt(0, Location::RequiresRegister());
2738 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2739 }
2740
David Brazdilb3e773e2016-01-26 11:28:37 +00002741 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002742 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002743 }
2744}
2745
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002746void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002747 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002748 return;
2749 }
2750
2751 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002752 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002753 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002754
Roland Levillain7f63c522015-07-13 15:54:55 +00002755 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002756 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002757 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002758 } else {
2759 // Integer cases.
2760 Register lhs = InputRegisterAt(instruction, 0);
2761 Operand rhs = InputOperandAt(instruction, 1);
2762 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002763 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002764 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002765}
2766
2767#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2768 M(Equal) \
2769 M(NotEqual) \
2770 M(LessThan) \
2771 M(LessThanOrEqual) \
2772 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002773 M(GreaterThanOrEqual) \
2774 M(Below) \
2775 M(BelowOrEqual) \
2776 M(Above) \
2777 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002778#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002779void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2780void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002781FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002782#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002783#undef FOR_EACH_CONDITION_INSTRUCTION
2784
Zheng Xuc6667102015-05-15 16:08:45 +08002785void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2786 DCHECK(instruction->IsDiv() || instruction->IsRem());
2787
2788 LocationSummary* locations = instruction->GetLocations();
2789 Location second = locations->InAt(1);
2790 DCHECK(second.IsConstant());
2791
2792 Register out = OutputRegister(instruction);
2793 Register dividend = InputRegisterAt(instruction, 0);
2794 int64_t imm = Int64FromConstant(second.GetConstant());
2795 DCHECK(imm == 1 || imm == -1);
2796
2797 if (instruction->IsRem()) {
2798 __ Mov(out, 0);
2799 } else {
2800 if (imm == 1) {
2801 __ Mov(out, dividend);
2802 } else {
2803 __ Neg(out, dividend);
2804 }
2805 }
2806}
2807
2808void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2809 DCHECK(instruction->IsDiv() || instruction->IsRem());
2810
2811 LocationSummary* locations = instruction->GetLocations();
2812 Location second = locations->InAt(1);
2813 DCHECK(second.IsConstant());
2814
2815 Register out = OutputRegister(instruction);
2816 Register dividend = InputRegisterAt(instruction, 0);
2817 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002818 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002819 int ctz_imm = CTZ(abs_imm);
2820
2821 UseScratchRegisterScope temps(GetVIXLAssembler());
2822 Register temp = temps.AcquireSameSizeAs(out);
2823
2824 if (instruction->IsDiv()) {
2825 __ Add(temp, dividend, abs_imm - 1);
2826 __ Cmp(dividend, 0);
2827 __ Csel(out, temp, dividend, lt);
2828 if (imm > 0) {
2829 __ Asr(out, out, ctz_imm);
2830 } else {
2831 __ Neg(out, Operand(out, ASR, ctz_imm));
2832 }
2833 } else {
2834 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2835 __ Asr(temp, dividend, bits - 1);
2836 __ Lsr(temp, temp, bits - ctz_imm);
2837 __ Add(out, dividend, temp);
2838 __ And(out, out, abs_imm - 1);
2839 __ Sub(out, out, temp);
2840 }
2841}
2842
2843void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2844 DCHECK(instruction->IsDiv() || instruction->IsRem());
2845
2846 LocationSummary* locations = instruction->GetLocations();
2847 Location second = locations->InAt(1);
2848 DCHECK(second.IsConstant());
2849
2850 Register out = OutputRegister(instruction);
2851 Register dividend = InputRegisterAt(instruction, 0);
2852 int64_t imm = Int64FromConstant(second.GetConstant());
2853
2854 Primitive::Type type = instruction->GetResultType();
2855 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2856
2857 int64_t magic;
2858 int shift;
2859 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2860
2861 UseScratchRegisterScope temps(GetVIXLAssembler());
2862 Register temp = temps.AcquireSameSizeAs(out);
2863
2864 // temp = get_high(dividend * magic)
2865 __ Mov(temp, magic);
2866 if (type == Primitive::kPrimLong) {
2867 __ Smulh(temp, dividend, temp);
2868 } else {
2869 __ Smull(temp.X(), dividend, temp);
2870 __ Lsr(temp.X(), temp.X(), 32);
2871 }
2872
2873 if (imm > 0 && magic < 0) {
2874 __ Add(temp, temp, dividend);
2875 } else if (imm < 0 && magic > 0) {
2876 __ Sub(temp, temp, dividend);
2877 }
2878
2879 if (shift != 0) {
2880 __ Asr(temp, temp, shift);
2881 }
2882
2883 if (instruction->IsDiv()) {
2884 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2885 } else {
2886 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2887 // TODO: Strength reduction for msub.
2888 Register temp_imm = temps.AcquireSameSizeAs(out);
2889 __ Mov(temp_imm, imm);
2890 __ Msub(out, temp, temp_imm, dividend);
2891 }
2892}
2893
2894void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2895 DCHECK(instruction->IsDiv() || instruction->IsRem());
2896 Primitive::Type type = instruction->GetResultType();
2897 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2898
2899 LocationSummary* locations = instruction->GetLocations();
2900 Register out = OutputRegister(instruction);
2901 Location second = locations->InAt(1);
2902
2903 if (second.IsConstant()) {
2904 int64_t imm = Int64FromConstant(second.GetConstant());
2905
2906 if (imm == 0) {
2907 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2908 } else if (imm == 1 || imm == -1) {
2909 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002910 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002911 DivRemByPowerOfTwo(instruction);
2912 } else {
2913 DCHECK(imm <= -2 || imm >= 2);
2914 GenerateDivRemWithAnyConstant(instruction);
2915 }
2916 } else {
2917 Register dividend = InputRegisterAt(instruction, 0);
2918 Register divisor = InputRegisterAt(instruction, 1);
2919 if (instruction->IsDiv()) {
2920 __ Sdiv(out, dividend, divisor);
2921 } else {
2922 UseScratchRegisterScope temps(GetVIXLAssembler());
2923 Register temp = temps.AcquireSameSizeAs(out);
2924 __ Sdiv(temp, dividend, divisor);
2925 __ Msub(out, temp, divisor, dividend);
2926 }
2927 }
2928}
2929
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002930void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2931 LocationSummary* locations =
2932 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2933 switch (div->GetResultType()) {
2934 case Primitive::kPrimInt:
2935 case Primitive::kPrimLong:
2936 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002937 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2939 break;
2940
2941 case Primitive::kPrimFloat:
2942 case Primitive::kPrimDouble:
2943 locations->SetInAt(0, Location::RequiresFpuRegister());
2944 locations->SetInAt(1, Location::RequiresFpuRegister());
2945 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2946 break;
2947
2948 default:
2949 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2950 }
2951}
2952
2953void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2954 Primitive::Type type = div->GetResultType();
2955 switch (type) {
2956 case Primitive::kPrimInt:
2957 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002958 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002959 break;
2960
2961 case Primitive::kPrimFloat:
2962 case Primitive::kPrimDouble:
2963 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2964 break;
2965
2966 default:
2967 LOG(FATAL) << "Unexpected div type " << type;
2968 }
2969}
2970
Alexandre Rames67555f72014-11-18 10:55:16 +00002971void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002972 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002973 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002974}
2975
2976void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2977 SlowPathCodeARM64* slow_path =
2978 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2979 codegen_->AddSlowPath(slow_path);
2980 Location value = instruction->GetLocations()->InAt(0);
2981
Alexandre Rames3e69f162014-12-10 10:36:50 +00002982 Primitive::Type type = instruction->GetType();
2983
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002984 if (!Primitive::IsIntegralType(type)) {
2985 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002986 return;
2987 }
2988
Alexandre Rames67555f72014-11-18 10:55:16 +00002989 if (value.IsConstant()) {
2990 int64_t divisor = Int64ConstantFrom(value);
2991 if (divisor == 0) {
2992 __ B(slow_path->GetEntryLabel());
2993 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002994 // A division by a non-null constant is valid. We don't need to perform
2995 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002996 }
2997 } else {
2998 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2999 }
3000}
3001
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003002void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3003 LocationSummary* locations =
3004 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3005 locations->SetOut(Location::ConstantLocation(constant));
3006}
3007
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003008void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3009 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003010 // Will be generated at use site.
3011}
3012
Alexandre Rames5319def2014-10-23 10:03:10 +01003013void LocationsBuilderARM64::VisitExit(HExit* exit) {
3014 exit->SetLocations(nullptr);
3015}
3016
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003017void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003018}
3019
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003020void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3021 LocationSummary* locations =
3022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3023 locations->SetOut(Location::ConstantLocation(constant));
3024}
3025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003026void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003027 // Will be generated at use site.
3028}
3029
David Brazdilfc6a86a2015-06-26 10:33:45 +00003030void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003031 DCHECK(!successor->IsExitBlock());
3032 HBasicBlock* block = got->GetBlock();
3033 HInstruction* previous = got->GetPrevious();
3034 HLoopInformation* info = block->GetLoopInformation();
3035
David Brazdil46e2a392015-03-16 17:31:52 +00003036 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003037 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3038 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3039 return;
3040 }
3041 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3042 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3043 }
3044 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003045 __ B(codegen_->GetLabelOf(successor));
3046 }
3047}
3048
David Brazdilfc6a86a2015-06-26 10:33:45 +00003049void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3050 got->SetLocations(nullptr);
3051}
3052
3053void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3054 HandleGoto(got, got->GetSuccessor());
3055}
3056
3057void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3058 try_boundary->SetLocations(nullptr);
3059}
3060
3061void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3062 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3063 if (!successor->IsExitBlock()) {
3064 HandleGoto(try_boundary, successor);
3065 }
3066}
3067
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003068void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003069 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003070 vixl::aarch64::Label* true_target,
3071 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003072 // FP branching requires both targets to be explicit. If either of the targets
3073 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003074 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003075 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003076
David Brazdil0debae72015-11-12 18:37:00 +00003077 if (true_target == nullptr && false_target == nullptr) {
3078 // Nothing to do. The code always falls through.
3079 return;
3080 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003081 // Constant condition, statically compared against "true" (integer value 1).
3082 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003083 if (true_target != nullptr) {
3084 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003085 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003086 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003087 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003088 if (false_target != nullptr) {
3089 __ B(false_target);
3090 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003091 }
David Brazdil0debae72015-11-12 18:37:00 +00003092 return;
3093 }
3094
3095 // The following code generates these patterns:
3096 // (1) true_target == nullptr && false_target != nullptr
3097 // - opposite condition true => branch to false_target
3098 // (2) true_target != nullptr && false_target == nullptr
3099 // - condition true => branch to true_target
3100 // (3) true_target != nullptr && false_target != nullptr
3101 // - condition true => branch to true_target
3102 // - branch to false_target
3103 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003104 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003105 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003106 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003107 if (true_target == nullptr) {
3108 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3109 } else {
3110 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3111 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003112 } else {
3113 // The condition instruction has not been materialized, use its inputs as
3114 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003115 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003116
David Brazdil0debae72015-11-12 18:37:00 +00003117 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003118 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003119 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003120 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003121 IfCondition opposite_condition = condition->GetOppositeCondition();
3122 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003123 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003124 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003125 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003126 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003127 // Integer cases.
3128 Register lhs = InputRegisterAt(condition, 0);
3129 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003130
3131 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003132 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003133 if (true_target == nullptr) {
3134 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3135 non_fallthrough_target = false_target;
3136 } else {
3137 arm64_cond = ARM64Condition(condition->GetCondition());
3138 non_fallthrough_target = true_target;
3139 }
3140
Aart Bik086d27e2016-01-20 17:02:00 -08003141 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003142 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003143 switch (arm64_cond) {
3144 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003145 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003146 break;
3147 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003148 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003149 break;
3150 case lt:
3151 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003152 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003153 break;
3154 case ge:
3155 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003156 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003157 break;
3158 default:
3159 // Without the `static_cast` the compiler throws an error for
3160 // `-Werror=sign-promo`.
3161 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3162 }
3163 } else {
3164 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003165 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003166 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003167 }
3168 }
David Brazdil0debae72015-11-12 18:37:00 +00003169
3170 // If neither branch falls through (case 3), the conditional branch to `true_target`
3171 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3172 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003173 __ B(false_target);
3174 }
David Brazdil0debae72015-11-12 18:37:00 +00003175
3176 if (fallthrough_target.IsLinked()) {
3177 __ Bind(&fallthrough_target);
3178 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003179}
3180
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003181void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3182 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003183 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003184 locations->SetInAt(0, Location::RequiresRegister());
3185 }
3186}
3187
3188void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003189 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3190 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003191 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3192 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3193 true_target = nullptr;
3194 }
3195 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3196 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3197 false_target = nullptr;
3198 }
David Brazdil0debae72015-11-12 18:37:00 +00003199 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003200}
3201
3202void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3203 LocationSummary* locations = new (GetGraph()->GetArena())
3204 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003205 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003206 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003207 locations->SetInAt(0, Location::RequiresRegister());
3208 }
3209}
3210
3211void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003212 SlowPathCodeARM64* slow_path =
3213 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003214 GenerateTestAndBranch(deoptimize,
3215 /* condition_input_index */ 0,
3216 slow_path->GetEntryLabel(),
3217 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003218}
3219
David Brazdilc0b601b2016-02-08 14:20:45 +00003220static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3221 return condition->IsCondition() &&
3222 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3223}
3224
Alexandre Rames880f1192016-06-13 16:04:50 +01003225static inline Condition GetConditionForSelect(HCondition* condition) {
3226 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003227 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3228 : ARM64Condition(cond);
3229}
3230
David Brazdil74eb1b22015-12-14 11:44:01 +00003231void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003233 if (Primitive::IsFloatingPointType(select->GetType())) {
3234 locations->SetInAt(0, Location::RequiresFpuRegister());
3235 locations->SetInAt(1, Location::RequiresFpuRegister());
3236 locations->SetOut(Location::RequiresFpuRegister());
3237 } else {
3238 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3239 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3240 bool is_true_value_constant = cst_true_value != nullptr;
3241 bool is_false_value_constant = cst_false_value != nullptr;
3242 // Ask VIXL whether we should synthesize constants in registers.
3243 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3244 Operand true_op = is_true_value_constant ?
3245 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3246 Operand false_op = is_false_value_constant ?
3247 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3248 bool true_value_in_register = false;
3249 bool false_value_in_register = false;
3250 MacroAssembler::GetCselSynthesisInformation(
3251 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3252 true_value_in_register |= !is_true_value_constant;
3253 false_value_in_register |= !is_false_value_constant;
3254
3255 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3256 : Location::ConstantLocation(cst_true_value));
3257 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3258 : Location::ConstantLocation(cst_false_value));
3259 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003260 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003261
David Brazdil74eb1b22015-12-14 11:44:01 +00003262 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3263 locations->SetInAt(2, Location::RequiresRegister());
3264 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003265}
3266
3267void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003268 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003269 Condition csel_cond;
3270
3271 if (IsBooleanValueOrMaterializedCondition(cond)) {
3272 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003273 // Use the condition flags set by the previous instruction.
3274 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003275 } else {
3276 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003277 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003278 }
3279 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003280 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003281 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003282 } else {
3283 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003284 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003285 }
3286
Alexandre Rames880f1192016-06-13 16:04:50 +01003287 if (Primitive::IsFloatingPointType(select->GetType())) {
3288 __ Fcsel(OutputFPRegister(select),
3289 InputFPRegisterAt(select, 1),
3290 InputFPRegisterAt(select, 0),
3291 csel_cond);
3292 } else {
3293 __ Csel(OutputRegister(select),
3294 InputOperandAt(select, 1),
3295 InputOperandAt(select, 0),
3296 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003297 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003298}
3299
David Srbecky0cf44932015-12-09 14:09:59 +00003300void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3301 new (GetGraph()->GetArena()) LocationSummary(info);
3302}
3303
David Srbeckyd28f4a02016-03-14 17:14:24 +00003304void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3305 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003306}
3307
3308void CodeGeneratorARM64::GenerateNop() {
3309 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003310}
3311
Alexandre Rames5319def2014-10-23 10:03:10 +01003312void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003313 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003314}
3315
3316void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003317 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003318}
3319
3320void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003321 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003322}
3323
3324void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003325 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003326}
3327
Roland Levillain44015862016-01-22 11:47:17 +00003328static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3329 return kEmitCompilerReadBarrier &&
3330 (kUseBakerReadBarrier ||
3331 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3332 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3333 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3334}
3335
Alexandre Rames67555f72014-11-18 10:55:16 +00003336void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003337 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003338 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003339 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003340 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003341 case TypeCheckKind::kExactCheck:
3342 case TypeCheckKind::kAbstractClassCheck:
3343 case TypeCheckKind::kClassHierarchyCheck:
3344 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003345 call_kind =
3346 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003347 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003348 break;
3349 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003350 case TypeCheckKind::kUnresolvedCheck:
3351 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003352 call_kind = LocationSummary::kCallOnSlowPath;
3353 break;
3354 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003355
Alexandre Rames67555f72014-11-18 10:55:16 +00003356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003357 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003358 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003359 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003360 locations->SetInAt(0, Location::RequiresRegister());
3361 locations->SetInAt(1, Location::RequiresRegister());
3362 // The "out" register is used as a temporary, so it overlaps with the inputs.
3363 // Note that TypeCheckSlowPathARM64 uses this register too.
3364 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3365 // When read barriers are enabled, we need a temporary register for
3366 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003367 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003370}
3371
3372void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003373 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003374 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003375 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003376 Register obj = InputRegisterAt(instruction, 0);
3377 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003379 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003380 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3381 locations->GetTemp(0) :
3382 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003383 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3384 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3385 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3386 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003387
Scott Wakeling97c72b72016-06-24 16:19:36 +01003388 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003389 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003390
3391 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003392 // Avoid null check if we know `obj` is not null.
3393 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003394 __ Cbz(obj, &zero);
3395 }
3396
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003397 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003398 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003399
Roland Levillain44015862016-01-22 11:47:17 +00003400 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003401 case TypeCheckKind::kExactCheck: {
3402 __ Cmp(out, cls);
3403 __ Cset(out, eq);
3404 if (zero.IsLinked()) {
3405 __ B(&done);
3406 }
3407 break;
3408 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003409
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003410 case TypeCheckKind::kAbstractClassCheck: {
3411 // If the class is abstract, we eagerly fetch the super class of the
3412 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003413 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003416 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003417 // If `out` is null, we use it for the result, and jump to `done`.
3418 __ Cbz(out, &done);
3419 __ Cmp(out, cls);
3420 __ B(ne, &loop);
3421 __ Mov(out, 1);
3422 if (zero.IsLinked()) {
3423 __ B(&done);
3424 }
3425 break;
3426 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003427
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003428 case TypeCheckKind::kClassHierarchyCheck: {
3429 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003430 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 __ Bind(&loop);
3432 __ Cmp(out, cls);
3433 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003434 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003435 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003436 __ Cbnz(out, &loop);
3437 // If `out` is null, we use it for the result, and jump to `done`.
3438 __ B(&done);
3439 __ Bind(&success);
3440 __ Mov(out, 1);
3441 if (zero.IsLinked()) {
3442 __ B(&done);
3443 }
3444 break;
3445 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003446
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003448 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003449 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003450 __ Cmp(out, cls);
3451 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003452 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003454 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003455 // If `out` is null, we use it for the result, and jump to `done`.
3456 __ Cbz(out, &done);
3457 __ Ldrh(out, HeapOperand(out, primitive_offset));
3458 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3459 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003460 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003461 __ Mov(out, 1);
3462 __ B(&done);
3463 break;
3464 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003465
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003466 case TypeCheckKind::kArrayCheck: {
3467 __ Cmp(out, cls);
3468 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003469 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3470 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003471 codegen_->AddSlowPath(slow_path);
3472 __ B(ne, slow_path->GetEntryLabel());
3473 __ Mov(out, 1);
3474 if (zero.IsLinked()) {
3475 __ B(&done);
3476 }
3477 break;
3478 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003479
Calin Juravle98893e12015-10-02 21:05:03 +01003480 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003481 case TypeCheckKind::kInterfaceCheck: {
3482 // Note that we indeed only call on slow path, but we always go
3483 // into the slow path for the unresolved and interface check
3484 // cases.
3485 //
3486 // We cannot directly call the InstanceofNonTrivial runtime
3487 // entry point without resorting to a type checking slow path
3488 // here (i.e. by calling InvokeRuntime directly), as it would
3489 // require to assign fixed registers for the inputs of this
3490 // HInstanceOf instruction (following the runtime calling
3491 // convention), which might be cluttered by the potential first
3492 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003493 //
3494 // TODO: Introduce a new runtime entry point taking the object
3495 // to test (instead of its class) as argument, and let it deal
3496 // with the read barrier issues. This will let us refactor this
3497 // case of the `switch` code as it was previously (with a direct
3498 // call to the runtime not using a type checking slow path).
3499 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003500 DCHECK(locations->OnlyCallsOnSlowPath());
3501 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3502 /* is_fatal */ false);
3503 codegen_->AddSlowPath(slow_path);
3504 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003505 if (zero.IsLinked()) {
3506 __ B(&done);
3507 }
3508 break;
3509 }
3510 }
3511
3512 if (zero.IsLinked()) {
3513 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003514 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003515 }
3516
3517 if (done.IsLinked()) {
3518 __ Bind(&done);
3519 }
3520
3521 if (slow_path != nullptr) {
3522 __ Bind(slow_path->GetExitLabel());
3523 }
3524}
3525
3526void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3527 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3528 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3529
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003530 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3531 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003532 case TypeCheckKind::kExactCheck:
3533 case TypeCheckKind::kAbstractClassCheck:
3534 case TypeCheckKind::kClassHierarchyCheck:
3535 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003536 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3537 LocationSummary::kCallOnSlowPath :
3538 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003539 break;
3540 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003541 case TypeCheckKind::kUnresolvedCheck:
3542 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003543 call_kind = LocationSummary::kCallOnSlowPath;
3544 break;
3545 }
3546
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3548 locations->SetInAt(0, Location::RequiresRegister());
3549 locations->SetInAt(1, Location::RequiresRegister());
3550 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3551 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003552 // When read barriers are enabled, we need an additional temporary
3553 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003554 if (TypeCheckNeedsATemporary(type_check_kind)) {
3555 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003556 }
3557}
3558
3559void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003560 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003561 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003562 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003563 Register obj = InputRegisterAt(instruction, 0);
3564 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003565 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003566 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3567 locations->GetTemp(1) :
3568 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003569 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003570 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3571 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3572 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3573 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003574
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003575 bool is_type_check_slow_path_fatal =
3576 (type_check_kind == TypeCheckKind::kExactCheck ||
3577 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3578 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3579 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3580 !instruction->CanThrowIntoCatchBlock();
3581 SlowPathCodeARM64* type_check_slow_path =
3582 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3583 is_type_check_slow_path_fatal);
3584 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003585
Scott Wakeling97c72b72016-06-24 16:19:36 +01003586 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003587 // Avoid null check if we know obj is not null.
3588 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003589 __ Cbz(obj, &done);
3590 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003591
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003592 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003593 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003594
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003595 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003596 case TypeCheckKind::kExactCheck:
3597 case TypeCheckKind::kArrayCheck: {
3598 __ Cmp(temp, cls);
3599 // Jump to slow path for throwing the exception or doing a
3600 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003601 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003602 break;
3603 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003604
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003605 case TypeCheckKind::kAbstractClassCheck: {
3606 // If the class is abstract, we eagerly fetch the super class of the
3607 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003608 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003609 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003610 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003611 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003612
3613 // If the class reference currently in `temp` is not null, jump
3614 // to the `compare_classes` label to compare it with the checked
3615 // class.
3616 __ Cbnz(temp, &compare_classes);
3617 // Otherwise, jump to the slow path to throw the exception.
3618 //
3619 // But before, move back the object's class into `temp` before
3620 // going into the slow path, as it has been overwritten in the
3621 // meantime.
3622 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003623 GenerateReferenceLoadTwoRegisters(
3624 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003625 __ B(type_check_slow_path->GetEntryLabel());
3626
3627 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003628 __ Cmp(temp, cls);
3629 __ B(ne, &loop);
3630 break;
3631 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003632
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003633 case TypeCheckKind::kClassHierarchyCheck: {
3634 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003635 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003636 __ Bind(&loop);
3637 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003638 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003639
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003641 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003642
3643 // If the class reference currently in `temp` is not null, jump
3644 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003645 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003646 // Otherwise, jump to the slow path to throw the exception.
3647 //
3648 // But before, move back the object's class into `temp` before
3649 // going into the slow path, as it has been overwritten in the
3650 // meantime.
3651 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003652 GenerateReferenceLoadTwoRegisters(
3653 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003654 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003655 break;
3656 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003657
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003658 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003659 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003660 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003661 __ Cmp(temp, cls);
3662 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003663
3664 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003665 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003666 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003667
3668 // If the component type is not null (i.e. the object is indeed
3669 // an array), jump to label `check_non_primitive_component_type`
3670 // to further check that this component type is not a primitive
3671 // type.
3672 __ Cbnz(temp, &check_non_primitive_component_type);
3673 // Otherwise, jump to the slow path to throw the exception.
3674 //
3675 // But before, move back the object's class into `temp` before
3676 // going into the slow path, as it has been overwritten in the
3677 // meantime.
3678 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003679 GenerateReferenceLoadTwoRegisters(
3680 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003681 __ B(type_check_slow_path->GetEntryLabel());
3682
3683 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003684 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3685 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003686 __ Cbz(temp, &done);
3687 // Same comment as above regarding `temp` and the slow path.
3688 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003689 GenerateReferenceLoadTwoRegisters(
3690 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003691 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003692 break;
3693 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003694
Calin Juravle98893e12015-10-02 21:05:03 +01003695 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003696 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003697 // We always go into the type check slow path for the unresolved
3698 // and interface check cases.
3699 //
3700 // We cannot directly call the CheckCast runtime entry point
3701 // without resorting to a type checking slow path here (i.e. by
3702 // calling InvokeRuntime directly), as it would require to
3703 // assign fixed registers for the inputs of this HInstanceOf
3704 // instruction (following the runtime calling convention), which
3705 // might be cluttered by the potential first read barrier
3706 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003707 //
3708 // TODO: Introduce a new runtime entry point taking the object
3709 // to test (instead of its class) as argument, and let it deal
3710 // with the read barrier issues. This will let us refactor this
3711 // case of the `switch` code as it was previously (with a direct
3712 // call to the runtime not using a type checking slow path).
3713 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003714 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003715 break;
3716 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003717 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003718
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003719 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003720}
3721
Alexandre Rames5319def2014-10-23 10:03:10 +01003722void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3724 locations->SetOut(Location::ConstantLocation(constant));
3725}
3726
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003727void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003728 // Will be generated at use site.
3729}
3730
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003731void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3732 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3733 locations->SetOut(Location::ConstantLocation(constant));
3734}
3735
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003736void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003737 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003738}
3739
Calin Juravle175dc732015-08-25 15:42:32 +01003740void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3741 // The trampoline uses the same calling convention as dex calling conventions,
3742 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3743 // the method_idx.
3744 HandleInvoke(invoke);
3745}
3746
3747void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3748 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3749}
3750
Alexandre Rames5319def2014-10-23 10:03:10 +01003751void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003752 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003753 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003754}
3755
Alexandre Rames67555f72014-11-18 10:55:16 +00003756void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3757 HandleInvoke(invoke);
3758}
3759
3760void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3761 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003762 LocationSummary* locations = invoke->GetLocations();
3763 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003764 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003765 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003766 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003767
3768 // The register ip1 is required to be used for the hidden argument in
3769 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003770 MacroAssembler* masm = GetVIXLAssembler();
3771 UseScratchRegisterScope scratch_scope(masm);
3772 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003773 scratch_scope.Exclude(ip1);
3774 __ Mov(ip1, invoke->GetDexMethodIndex());
3775
Alexandre Rames67555f72014-11-18 10:55:16 +00003776 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003777 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003778 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003779 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003780 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003781 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003782 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003783 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003784 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003785 // Instead of simply (possibly) unpoisoning `temp` here, we should
3786 // emit a read barrier for the previous class reference load.
3787 // However this is not required in practice, as this is an
3788 // intermediate/temporary reference and because the current
3789 // concurrent copying collector keeps the from-space memory
3790 // intact/accessible until the end of the marking phase (the
3791 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003792 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003793 __ Ldr(temp,
3794 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3795 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003796 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003797 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003798 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003799 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003800 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003801 // lr();
3802 __ Blr(lr);
3803 DCHECK(!codegen_->IsLeafMethod());
3804 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3805}
3806
3807void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003808 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3809 if (intrinsic.TryDispatch(invoke)) {
3810 return;
3811 }
3812
Alexandre Rames67555f72014-11-18 10:55:16 +00003813 HandleInvoke(invoke);
3814}
3815
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003816void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003817 // Explicit clinit checks triggered by static invokes must have been pruned by
3818 // art::PrepareForRegisterAllocation.
3819 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003820
Andreas Gampe878d58c2015-01-15 23:24:00 -08003821 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3822 if (intrinsic.TryDispatch(invoke)) {
3823 return;
3824 }
3825
Alexandre Rames67555f72014-11-18 10:55:16 +00003826 HandleInvoke(invoke);
3827}
3828
Andreas Gampe878d58c2015-01-15 23:24:00 -08003829static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3830 if (invoke->GetLocations()->Intrinsified()) {
3831 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3832 intrinsic.Dispatch(invoke);
3833 return true;
3834 }
3835 return false;
3836}
3837
Vladimir Markodc151b22015-10-15 18:02:30 +01003838HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3839 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003840 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003841 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003842 return desired_dispatch_info;
3843}
3844
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003845void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003846 // For better instruction scheduling we load the direct code pointer before the method pointer.
3847 bool direct_code_loaded = false;
3848 switch (invoke->GetCodePtrLocation()) {
3849 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3850 // LR = code address from literal pool with link-time patch.
3851 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3852 direct_code_loaded = true;
3853 break;
3854 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3855 // LR = invoke->GetDirectCodePtr();
3856 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3857 direct_code_loaded = true;
3858 break;
3859 default:
3860 break;
3861 }
3862
Andreas Gampe878d58c2015-01-15 23:24:00 -08003863 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003864 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3865 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003866 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3867 uint32_t offset =
3868 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003869 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003870 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003871 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003872 }
Vladimir Marko58155012015-08-19 12:49:41 +00003873 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003874 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003875 break;
3876 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3877 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003878 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003879 break;
3880 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3881 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003882 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003883 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3884 break;
3885 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3886 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003887 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003888 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003889 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003890 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003891 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003892 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003893 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003894 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003895 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003896 }
Vladimir Marko58155012015-08-19 12:49:41 +00003897 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003898 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003899 Register reg = XRegisterFrom(temp);
3900 Register method_reg;
3901 if (current_method.IsRegister()) {
3902 method_reg = XRegisterFrom(current_method);
3903 } else {
3904 DCHECK(invoke->GetLocations()->Intrinsified());
3905 DCHECK(!current_method.IsValid());
3906 method_reg = reg;
3907 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3908 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003909
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003910 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003911 __ Ldr(reg.X(),
3912 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003913 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003914 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003915 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3916 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003917 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3918 break;
3919 }
3920 }
3921
3922 switch (invoke->GetCodePtrLocation()) {
3923 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3924 __ Bl(&frame_entry_label_);
3925 break;
3926 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00003927 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3928 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01003929 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3930 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003931 __ Bind(label);
3932 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003933 break;
3934 }
3935 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3936 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3937 // LR prepared above for better instruction scheduling.
3938 DCHECK(direct_code_loaded);
3939 // lr()
3940 __ Blr(lr);
3941 break;
3942 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3943 // LR = callee_method->entry_point_from_quick_compiled_code_;
3944 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003945 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003946 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003947 // lr()
3948 __ Blr(lr);
3949 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003950 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003951
Andreas Gampe878d58c2015-01-15 23:24:00 -08003952 DCHECK(!IsLeafMethod());
3953}
3954
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003955void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003956 // Use the calling convention instead of the location of the receiver, as
3957 // intrinsics may have put the receiver in a different register. In the intrinsics
3958 // slow path, the arguments have been moved to the right place, so here we are
3959 // guaranteed that the receiver is the first register of the calling convention.
3960 InvokeDexCallingConvention calling_convention;
3961 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003962 Register temp = XRegisterFrom(temp_in);
3963 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3964 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3965 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003966 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003967
3968 BlockPoolsScope block_pools(GetVIXLAssembler());
3969
3970 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003971 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003972 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003973 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003974 // Instead of simply (possibly) unpoisoning `temp` here, we should
3975 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003976 // intermediate/temporary reference and because the current
3977 // concurrent copying collector keeps the from-space memory
3978 // intact/accessible until the end of the marking phase (the
3979 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003980 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3981 // temp = temp->GetMethodAt(method_offset);
3982 __ Ldr(temp, MemOperand(temp, method_offset));
3983 // lr = temp->GetEntryPoint();
3984 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3985 // lr();
3986 __ Blr(lr);
3987}
3988
Scott Wakeling97c72b72016-06-24 16:19:36 +01003989vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3990 const DexFile& dex_file,
3991 uint32_t string_index,
3992 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003993 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3994}
3995
Scott Wakeling97c72b72016-06-24 16:19:36 +01003996vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3997 const DexFile& dex_file,
3998 uint32_t type_index,
3999 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004000 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
4001}
4002
Scott Wakeling97c72b72016-06-24 16:19:36 +01004003vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4004 const DexFile& dex_file,
4005 uint32_t element_offset,
4006 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004007 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4008}
4009
Scott Wakeling97c72b72016-06-24 16:19:36 +01004010vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4011 const DexFile& dex_file,
4012 uint32_t offset_or_index,
4013 vixl::aarch64::Label* adrp_label,
4014 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004015 // Add a patch entry and return the label.
4016 patches->emplace_back(dex_file, offset_or_index);
4017 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004018 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004019 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4020 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4021 return label;
4022}
4023
Scott Wakeling97c72b72016-06-24 16:19:36 +01004024vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004025 const DexFile& dex_file, uint32_t string_index) {
4026 return boot_image_string_patches_.GetOrCreate(
4027 StringReference(&dex_file, string_index),
4028 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4029}
4030
Scott Wakeling97c72b72016-06-24 16:19:36 +01004031vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004032 const DexFile& dex_file, uint32_t type_index) {
4033 return boot_image_type_patches_.GetOrCreate(
4034 TypeReference(&dex_file, type_index),
4035 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4036}
4037
Scott Wakeling97c72b72016-06-24 16:19:36 +01004038vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4039 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004040 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4041 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4042 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4043}
4044
Scott Wakeling97c72b72016-06-24 16:19:36 +01004045vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4046 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004047 return DeduplicateUint64Literal(address);
4048}
4049
Vladimir Markoaad75c62016-10-03 08:46:48 +00004050void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4051 vixl::aarch64::Register reg) {
4052 DCHECK(reg.IsX());
4053 SingleEmissionCheckScope guard(GetVIXLAssembler());
4054 __ Bind(fixup_label);
4055 __ adrp(reg, /* offset placeholder */ 0);
4056}
4057
4058void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4059 vixl::aarch64::Register out,
4060 vixl::aarch64::Register base) {
4061 DCHECK(out.IsX());
4062 DCHECK(base.IsX());
4063 SingleEmissionCheckScope guard(GetVIXLAssembler());
4064 __ Bind(fixup_label);
4065 __ add(out, base, Operand(/* offset placeholder */ 0));
4066}
4067
4068void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4069 vixl::aarch64::Register out,
4070 vixl::aarch64::Register base) {
4071 DCHECK(base.IsX());
4072 SingleEmissionCheckScope guard(GetVIXLAssembler());
4073 __ Bind(fixup_label);
4074 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4075}
4076
4077template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4078inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4079 const ArenaDeque<PcRelativePatchInfo>& infos,
4080 ArenaVector<LinkerPatch>* linker_patches) {
4081 for (const PcRelativePatchInfo& info : infos) {
4082 linker_patches->push_back(Factory(info.label.GetLocation(),
4083 &info.target_dex_file,
4084 info.pc_insn_label->GetLocation(),
4085 info.offset_or_index));
4086 }
4087}
4088
Vladimir Marko58155012015-08-19 12:49:41 +00004089void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4090 DCHECK(linker_patches->empty());
4091 size_t size =
4092 method_patches_.size() +
4093 call_patches_.size() +
4094 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004095 pc_relative_dex_cache_patches_.size() +
4096 boot_image_string_patches_.size() +
4097 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004098 boot_image_type_patches_.size() +
4099 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004100 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004101 linker_patches->reserve(size);
4102 for (const auto& entry : method_patches_) {
4103 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004104 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4105 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004106 target_method.dex_file,
4107 target_method.dex_method_index));
4108 }
4109 for (const auto& entry : call_patches_) {
4110 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004111 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4112 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004113 target_method.dex_file,
4114 target_method.dex_method_index));
4115 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004116 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4117 linker_patches->push_back(
4118 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004119 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004120 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004121 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004122 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004123 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004124 info.offset_or_index));
4125 }
4126 for (const auto& entry : boot_image_string_patches_) {
4127 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004128 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4129 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004130 target_string.dex_file,
4131 target_string.string_index));
4132 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004133 if (!GetCompilerOptions().IsBootImage()) {
4134 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4135 linker_patches);
4136 } else {
4137 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4138 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004139 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004140 for (const auto& entry : boot_image_type_patches_) {
4141 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004142 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4143 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004144 target_type.dex_file,
4145 target_type.type_index));
4146 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004147 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4148 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004149 for (const auto& entry : boot_image_address_patches_) {
4150 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004151 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4152 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004153 }
4154}
4155
Scott Wakeling97c72b72016-06-24 16:19:36 +01004156vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004157 Uint32ToLiteralMap* map) {
4158 return map->GetOrCreate(
4159 value,
4160 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4161}
4162
Scott Wakeling97c72b72016-06-24 16:19:36 +01004163vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004164 return uint64_literals_.GetOrCreate(
4165 value,
4166 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004167}
4168
Scott Wakeling97c72b72016-06-24 16:19:36 +01004169vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004170 MethodReference target_method,
4171 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004172 return map->GetOrCreate(
4173 target_method,
4174 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004175}
4176
Scott Wakeling97c72b72016-06-24 16:19:36 +01004177vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004178 MethodReference target_method) {
4179 return DeduplicateMethodLiteral(target_method, &method_patches_);
4180}
4181
Scott Wakeling97c72b72016-06-24 16:19:36 +01004182vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004183 MethodReference target_method) {
4184 return DeduplicateMethodLiteral(target_method, &call_patches_);
4185}
4186
4187
Andreas Gampe878d58c2015-01-15 23:24:00 -08004188void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004189 // Explicit clinit checks triggered by static invokes must have been pruned by
4190 // art::PrepareForRegisterAllocation.
4191 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004192
Andreas Gampe878d58c2015-01-15 23:24:00 -08004193 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4194 return;
4195 }
4196
Alexandre Ramesd921d642015-04-16 15:07:16 +01004197 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004198 LocationSummary* locations = invoke->GetLocations();
4199 codegen_->GenerateStaticOrDirectCall(
4200 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004201 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004202}
4203
4204void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004205 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4206 return;
4207 }
4208
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004209 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004210 DCHECK(!codegen_->IsLeafMethod());
4211 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4212}
4213
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004214HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4215 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004216 switch (desired_class_load_kind) {
4217 case HLoadClass::LoadKind::kReferrersClass:
4218 break;
4219 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4220 DCHECK(!GetCompilerOptions().GetCompilePic());
4221 break;
4222 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4223 DCHECK(GetCompilerOptions().GetCompilePic());
4224 break;
4225 case HLoadClass::LoadKind::kBootImageAddress:
4226 break;
4227 case HLoadClass::LoadKind::kDexCacheAddress:
4228 DCHECK(Runtime::Current()->UseJitCompilation());
4229 break;
4230 case HLoadClass::LoadKind::kDexCachePcRelative:
4231 DCHECK(!Runtime::Current()->UseJitCompilation());
4232 break;
4233 case HLoadClass::LoadKind::kDexCacheViaMethod:
4234 break;
4235 }
4236 return desired_class_load_kind;
4237}
4238
Alexandre Rames67555f72014-11-18 10:55:16 +00004239void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004240 if (cls->NeedsAccessCheck()) {
4241 InvokeRuntimeCallingConvention calling_convention;
4242 CodeGenerator::CreateLoadClassLocationSummary(
4243 cls,
4244 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004245 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004246 /* code_generator_supports_read_barrier */ true);
4247 return;
4248 }
4249
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004250 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4251 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004252 ? LocationSummary::kCallOnSlowPath
4253 : LocationSummary::kNoCall;
4254 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004255 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004256 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004257 }
4258
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004259 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4260 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4261 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4262 locations->SetInAt(0, Location::RequiresRegister());
4263 }
4264 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004265}
4266
4267void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004268 if (cls->NeedsAccessCheck()) {
4269 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004270 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004271 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004272 return;
4273 }
4274
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004275 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004276 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004277
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004278 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004279 bool generate_null_check = false;
4280 switch (cls->GetLoadKind()) {
4281 case HLoadClass::LoadKind::kReferrersClass: {
4282 DCHECK(!cls->CanCallRuntime());
4283 DCHECK(!cls->MustGenerateClinitCheck());
4284 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4285 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004286 GenerateGcRootFieldLoad(cls,
4287 out_loc,
4288 current_method,
4289 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004290 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004291 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004292 break;
4293 }
4294 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004295 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004296 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4297 cls->GetTypeIndex()));
4298 break;
4299 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004300 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004301 // Add ADRP with its PC-relative type patch.
4302 const DexFile& dex_file = cls->GetDexFile();
4303 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004304 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004305 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004306 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004307 vixl::aarch64::Label* add_label =
4308 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004309 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004310 break;
4311 }
4312 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004313 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004314 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4315 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4316 break;
4317 }
4318 case HLoadClass::LoadKind::kDexCacheAddress: {
4319 DCHECK_NE(cls->GetAddress(), 0u);
4320 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4321 // that gives a 16KiB range. To try and reduce the number of literals if we load
4322 // multiple types, simply split the dex cache address to a 16KiB aligned base
4323 // loaded from a literal and the remaining offset embedded in the load.
4324 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4325 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4326 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4327 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4328 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4329 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4330 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004331 GenerateGcRootFieldLoad(cls,
4332 out_loc,
4333 out.X(),
4334 offset,
Roland Levillain00468f32016-10-27 18:02:48 +01004335 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004336 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004337 generate_null_check = !cls->IsInDexCache();
4338 break;
4339 }
4340 case HLoadClass::LoadKind::kDexCachePcRelative: {
4341 // Add ADRP with its PC-relative DexCache access patch.
4342 const DexFile& dex_file = cls->GetDexFile();
4343 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004344 vixl::aarch64::Label* adrp_label =
4345 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004346 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004347 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004348 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004349 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4350 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004351 GenerateGcRootFieldLoad(cls,
4352 out_loc,
4353 out.X(),
4354 /* offset placeholder */ 0,
4355 ldr_label,
4356 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004357 generate_null_check = !cls->IsInDexCache();
4358 break;
4359 }
4360 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4361 MemberOffset resolved_types_offset =
4362 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4363 // /* GcRoot<mirror::Class>[] */ out =
4364 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4365 Register current_method = InputRegisterAt(cls, 0);
4366 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4367 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004368 GenerateGcRootFieldLoad(cls,
4369 out_loc,
4370 out.X(),
4371 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
Roland Levillain00468f32016-10-27 18:02:48 +01004372 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004373 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004374 generate_null_check = !cls->IsInDexCache();
4375 break;
4376 }
4377 }
4378
4379 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4380 DCHECK(cls->CanCallRuntime());
4381 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4382 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4383 codegen_->AddSlowPath(slow_path);
4384 if (generate_null_check) {
4385 __ Cbz(out, slow_path->GetEntryLabel());
4386 }
4387 if (cls->MustGenerateClinitCheck()) {
4388 GenerateClassInitializationCheck(slow_path, out);
4389 } else {
4390 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004391 }
4392 }
4393}
4394
David Brazdilcb1c0552015-08-04 16:22:25 +01004395static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004396 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004397}
4398
Alexandre Rames67555f72014-11-18 10:55:16 +00004399void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4400 LocationSummary* locations =
4401 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4402 locations->SetOut(Location::RequiresRegister());
4403}
4404
4405void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004406 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4407}
4408
4409void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4410 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4411}
4412
4413void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4414 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004415}
4416
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004417HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4418 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004419 switch (desired_string_load_kind) {
4420 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4421 DCHECK(!GetCompilerOptions().GetCompilePic());
4422 break;
4423 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4424 DCHECK(GetCompilerOptions().GetCompilePic());
4425 break;
4426 case HLoadString::LoadKind::kBootImageAddress:
4427 break;
4428 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004429 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004430 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004431 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004432 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004433 break;
4434 case HLoadString::LoadKind::kDexCacheViaMethod:
4435 break;
4436 }
4437 return desired_string_load_kind;
4438}
4439
Alexandre Rames67555f72014-11-18 10:55:16 +00004440void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004441 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00004442 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4443 ? LocationSummary::kCallOnMainOnly
4444 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004445 : LocationSummary::kNoCall;
4446 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004447 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004448 InvokeRuntimeCallingConvention calling_convention;
4449 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4450 } else {
4451 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004452 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4453 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4454 // Rely on the pResolveString and/or marking to save everything, including temps.
4455 RegisterSet caller_saves = RegisterSet::Empty();
4456 InvokeRuntimeCallingConvention calling_convention;
4457 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4458 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4459 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4460 Primitive::kPrimNot).GetCode());
4461 locations->SetCustomSlowPathCallerSaves(caller_saves);
4462 } else {
4463 // For non-Baker read barrier we have a temp-clobbering call.
4464 }
4465 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004466 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004467}
4468
4469void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004470 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004471
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004472 switch (load->GetLoadKind()) {
4473 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004474 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4475 load->GetStringIndex()));
4476 return; // No dex cache slow path.
4477 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004478 // Add ADRP with its PC-relative String patch.
4479 const DexFile& dex_file = load->GetDexFile();
4480 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004481 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004482 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004483 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004484 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004485 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004486 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004487 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004488 return; // No dex cache slow path.
4489 }
4490 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004491 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4492 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4493 return; // No dex cache slow path.
4494 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004495 case HLoadString::LoadKind::kBssEntry: {
4496 // Add ADRP with its PC-relative String .bss entry patch.
4497 const DexFile& dex_file = load->GetDexFile();
4498 uint32_t string_index = load->GetStringIndex();
4499 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004500 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4501 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004502 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004503 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004504 // Add LDR with its PC-relative String patch.
4505 vixl::aarch64::Label* ldr_label =
4506 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4507 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4508 GenerateGcRootFieldLoad(load,
4509 load->GetLocations()->Out(),
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004510 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004511 /* offset placeholder */ 0u,
4512 ldr_label,
4513 kEmitCompilerReadBarrier);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004514 SlowPathCodeARM64* slow_path =
4515 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004516 codegen_->AddSlowPath(slow_path);
4517 __ Cbz(out.X(), slow_path->GetEntryLabel());
4518 __ Bind(slow_path->GetExitLabel());
4519 return;
4520 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004521 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004522 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004523 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004524
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004525 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004526 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004527 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004528 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4529 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4530 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004531}
4532
Alexandre Rames5319def2014-10-23 10:03:10 +01004533void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4535 locations->SetOut(Location::ConstantLocation(constant));
4536}
4537
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004538void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004539 // Will be generated at use site.
4540}
4541
Alexandre Rames67555f72014-11-18 10:55:16 +00004542void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4543 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004544 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004545 InvokeRuntimeCallingConvention calling_convention;
4546 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4547}
4548
4549void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004550 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4551 instruction,
4552 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004553 if (instruction->IsEnter()) {
4554 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4555 } else {
4556 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4557 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004558}
4559
Alexandre Rames42d641b2014-10-27 14:00:51 +00004560void LocationsBuilderARM64::VisitMul(HMul* mul) {
4561 LocationSummary* locations =
4562 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4563 switch (mul->GetResultType()) {
4564 case Primitive::kPrimInt:
4565 case Primitive::kPrimLong:
4566 locations->SetInAt(0, Location::RequiresRegister());
4567 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004569 break;
4570
4571 case Primitive::kPrimFloat:
4572 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004573 locations->SetInAt(0, Location::RequiresFpuRegister());
4574 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004575 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004576 break;
4577
4578 default:
4579 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4580 }
4581}
4582
4583void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4584 switch (mul->GetResultType()) {
4585 case Primitive::kPrimInt:
4586 case Primitive::kPrimLong:
4587 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4588 break;
4589
4590 case Primitive::kPrimFloat:
4591 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004592 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004593 break;
4594
4595 default:
4596 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4597 }
4598}
4599
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004600void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4601 LocationSummary* locations =
4602 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4603 switch (neg->GetResultType()) {
4604 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004605 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004606 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004608 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004609
4610 case Primitive::kPrimFloat:
4611 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004612 locations->SetInAt(0, Location::RequiresFpuRegister());
4613 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004614 break;
4615
4616 default:
4617 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4618 }
4619}
4620
4621void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4622 switch (neg->GetResultType()) {
4623 case Primitive::kPrimInt:
4624 case Primitive::kPrimLong:
4625 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4626 break;
4627
4628 case Primitive::kPrimFloat:
4629 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004630 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004631 break;
4632
4633 default:
4634 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4635 }
4636}
4637
4638void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4639 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004641 InvokeRuntimeCallingConvention calling_convention;
4642 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004643 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004644 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004645 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004646}
4647
4648void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4649 LocationSummary* locations = instruction->GetLocations();
4650 InvokeRuntimeCallingConvention calling_convention;
4651 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4652 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004653 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004654 // Note: if heap poisoning is enabled, the entry point takes cares
4655 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004656 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004657 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004658}
4659
Alexandre Rames5319def2014-10-23 10:03:10 +01004660void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4661 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004663 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004664 if (instruction->IsStringAlloc()) {
4665 locations->AddTemp(LocationFrom(kArtMethodRegister));
4666 } else {
4667 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4668 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4669 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004670 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4671}
4672
4673void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004674 // Note: if heap poisoning is enabled, the entry point takes cares
4675 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004676 if (instruction->IsStringAlloc()) {
4677 // String is allocated through StringFactory. Call NewEmptyString entry point.
4678 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004679 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004680 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4681 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4682 __ Blr(lr);
4683 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4684 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004685 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004686 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4687 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004688}
4689
4690void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004692 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004694}
4695
4696void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004697 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004698 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004699 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004700 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004701 break;
4702
4703 default:
4704 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4705 }
4706}
4707
David Brazdil66d126e2015-04-03 16:02:44 +01004708void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4710 locations->SetInAt(0, Location::RequiresRegister());
4711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4712}
4713
4714void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004715 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004716}
4717
Alexandre Rames5319def2014-10-23 10:03:10 +01004718void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004719 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4720 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004721}
4722
Calin Juravle2ae48182016-03-16 14:05:09 +00004723void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4724 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004725 return;
4726 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004727
Alexandre Ramesd921d642015-04-16 15:07:16 +01004728 BlockPoolsScope block_pools(GetVIXLAssembler());
4729 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004730 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004731 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004732}
4733
Calin Juravle2ae48182016-03-16 14:05:09 +00004734void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004735 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004736 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004737
4738 LocationSummary* locations = instruction->GetLocations();
4739 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004740
4741 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004742}
4743
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004744void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004745 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004746}
4747
Alexandre Rames67555f72014-11-18 10:55:16 +00004748void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4749 HandleBinaryOp(instruction);
4750}
4751
4752void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4753 HandleBinaryOp(instruction);
4754}
4755
Alexandre Rames3e69f162014-12-10 10:36:50 +00004756void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4757 LOG(FATAL) << "Unreachable";
4758}
4759
4760void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4761 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4762}
4763
Alexandre Rames5319def2014-10-23 10:03:10 +01004764void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4765 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4766 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4767 if (location.IsStackSlot()) {
4768 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4769 } else if (location.IsDoubleStackSlot()) {
4770 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4771 }
4772 locations->SetOut(location);
4773}
4774
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004775void InstructionCodeGeneratorARM64::VisitParameterValue(
4776 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004777 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004778}
4779
4780void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4781 LocationSummary* locations =
4782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004783 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004784}
4785
4786void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4787 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4788 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004789}
4790
4791void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4792 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004793 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004794 locations->SetInAt(i, Location::Any());
4795 }
4796 locations->SetOut(Location::Any());
4797}
4798
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004799void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004800 LOG(FATAL) << "Unreachable";
4801}
4802
Serban Constantinescu02164b32014-11-13 14:05:07 +00004803void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004804 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004805 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004806 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4807 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4809
4810 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004811 case Primitive::kPrimInt:
4812 case Primitive::kPrimLong:
4813 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004814 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4816 break;
4817
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004818 case Primitive::kPrimFloat:
4819 case Primitive::kPrimDouble: {
4820 InvokeRuntimeCallingConvention calling_convention;
4821 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4822 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4823 locations->SetOut(calling_convention.GetReturnLocation(type));
4824
4825 break;
4826 }
4827
Serban Constantinescu02164b32014-11-13 14:05:07 +00004828 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004829 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004830 }
4831}
4832
4833void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4834 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004835
Serban Constantinescu02164b32014-11-13 14:05:07 +00004836 switch (type) {
4837 case Primitive::kPrimInt:
4838 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004839 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004840 break;
4841 }
4842
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004843 case Primitive::kPrimFloat:
4844 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004845 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4846 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004847 if (type == Primitive::kPrimFloat) {
4848 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4849 } else {
4850 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4851 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004852 break;
4853 }
4854
Serban Constantinescu02164b32014-11-13 14:05:07 +00004855 default:
4856 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004857 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004858 }
4859}
4860
Calin Juravle27df7582015-04-17 19:12:31 +01004861void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4862 memory_barrier->SetLocations(nullptr);
4863}
4864
4865void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004866 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004867}
4868
Alexandre Rames5319def2014-10-23 10:03:10 +01004869void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4870 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4871 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004872 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004873}
4874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004875void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004876 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004877}
4878
4879void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4880 instruction->SetLocations(nullptr);
4881}
4882
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004883void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004884 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004885}
4886
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004887void LocationsBuilderARM64::VisitRor(HRor* ror) {
4888 HandleBinaryOp(ror);
4889}
4890
4891void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4892 HandleBinaryOp(ror);
4893}
4894
Serban Constantinescu02164b32014-11-13 14:05:07 +00004895void LocationsBuilderARM64::VisitShl(HShl* shl) {
4896 HandleShift(shl);
4897}
4898
4899void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4900 HandleShift(shl);
4901}
4902
4903void LocationsBuilderARM64::VisitShr(HShr* shr) {
4904 HandleShift(shr);
4905}
4906
4907void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4908 HandleShift(shr);
4909}
4910
Alexandre Rames5319def2014-10-23 10:03:10 +01004911void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004912 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004913}
4914
4915void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004916 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004917}
4918
Alexandre Rames67555f72014-11-18 10:55:16 +00004919void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004920 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004921}
4922
4923void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004924 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004925}
4926
4927void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004928 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004929}
4930
Alexandre Rames67555f72014-11-18 10:55:16 +00004931void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004932 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004933}
4934
Calin Juravlee460d1d2015-09-29 04:52:17 +01004935void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4936 HUnresolvedInstanceFieldGet* instruction) {
4937 FieldAccessCallingConventionARM64 calling_convention;
4938 codegen_->CreateUnresolvedFieldLocationSummary(
4939 instruction, instruction->GetFieldType(), calling_convention);
4940}
4941
4942void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4943 HUnresolvedInstanceFieldGet* instruction) {
4944 FieldAccessCallingConventionARM64 calling_convention;
4945 codegen_->GenerateUnresolvedFieldAccess(instruction,
4946 instruction->GetFieldType(),
4947 instruction->GetFieldIndex(),
4948 instruction->GetDexPc(),
4949 calling_convention);
4950}
4951
4952void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4953 HUnresolvedInstanceFieldSet* instruction) {
4954 FieldAccessCallingConventionARM64 calling_convention;
4955 codegen_->CreateUnresolvedFieldLocationSummary(
4956 instruction, instruction->GetFieldType(), calling_convention);
4957}
4958
4959void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4960 HUnresolvedInstanceFieldSet* instruction) {
4961 FieldAccessCallingConventionARM64 calling_convention;
4962 codegen_->GenerateUnresolvedFieldAccess(instruction,
4963 instruction->GetFieldType(),
4964 instruction->GetFieldIndex(),
4965 instruction->GetDexPc(),
4966 calling_convention);
4967}
4968
4969void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4970 HUnresolvedStaticFieldGet* instruction) {
4971 FieldAccessCallingConventionARM64 calling_convention;
4972 codegen_->CreateUnresolvedFieldLocationSummary(
4973 instruction, instruction->GetFieldType(), calling_convention);
4974}
4975
4976void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4977 HUnresolvedStaticFieldGet* instruction) {
4978 FieldAccessCallingConventionARM64 calling_convention;
4979 codegen_->GenerateUnresolvedFieldAccess(instruction,
4980 instruction->GetFieldType(),
4981 instruction->GetFieldIndex(),
4982 instruction->GetDexPc(),
4983 calling_convention);
4984}
4985
4986void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4987 HUnresolvedStaticFieldSet* instruction) {
4988 FieldAccessCallingConventionARM64 calling_convention;
4989 codegen_->CreateUnresolvedFieldLocationSummary(
4990 instruction, instruction->GetFieldType(), calling_convention);
4991}
4992
4993void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4994 HUnresolvedStaticFieldSet* instruction) {
4995 FieldAccessCallingConventionARM64 calling_convention;
4996 codegen_->GenerateUnresolvedFieldAccess(instruction,
4997 instruction->GetFieldType(),
4998 instruction->GetFieldIndex(),
4999 instruction->GetDexPc(),
5000 calling_convention);
5001}
5002
Alexandre Rames5319def2014-10-23 10:03:10 +01005003void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005004 LocationSummary* locations =
5005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005006 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005007}
5008
5009void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005010 HBasicBlock* block = instruction->GetBlock();
5011 if (block->GetLoopInformation() != nullptr) {
5012 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5013 // The back edge will generate the suspend check.
5014 return;
5015 }
5016 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5017 // The goto will generate the suspend check.
5018 return;
5019 }
5020 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005021}
5022
Alexandre Rames67555f72014-11-18 10:55:16 +00005023void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5024 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005025 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005026 InvokeRuntimeCallingConvention calling_convention;
5027 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5028}
5029
5030void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005031 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005032 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005033}
5034
5035void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5036 LocationSummary* locations =
5037 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5038 Primitive::Type input_type = conversion->GetInputType();
5039 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005040 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005041 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5042 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5043 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5044 }
5045
Alexandre Rames542361f2015-01-29 16:57:31 +00005046 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005047 locations->SetInAt(0, Location::RequiresFpuRegister());
5048 } else {
5049 locations->SetInAt(0, Location::RequiresRegister());
5050 }
5051
Alexandre Rames542361f2015-01-29 16:57:31 +00005052 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5054 } else {
5055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5056 }
5057}
5058
5059void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5060 Primitive::Type result_type = conversion->GetResultType();
5061 Primitive::Type input_type = conversion->GetInputType();
5062
5063 DCHECK_NE(input_type, result_type);
5064
Alexandre Rames542361f2015-01-29 16:57:31 +00005065 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005066 int result_size = Primitive::ComponentSize(result_type);
5067 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005068 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005069 Register output = OutputRegister(conversion);
5070 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005071 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005072 // 'int' values are used directly as W registers, discarding the top
5073 // bits, so we don't need to sign-extend and can just perform a move.
5074 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5075 // top 32 bits of the target register. We theoretically could leave those
5076 // bits unchanged, but we would have to make sure that no code uses a
5077 // 32bit input value as a 64bit value assuming that the top 32 bits are
5078 // zero.
5079 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005080 } else if (result_type == Primitive::kPrimChar ||
5081 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5082 __ Ubfx(output,
5083 output.IsX() ? source.X() : source.W(),
5084 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005085 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005086 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005087 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005088 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005089 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005090 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005091 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5092 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005093 } else if (Primitive::IsFloatingPointType(result_type) &&
5094 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005095 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5096 } else {
5097 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5098 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005099 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005100}
Alexandre Rames67555f72014-11-18 10:55:16 +00005101
Serban Constantinescu02164b32014-11-13 14:05:07 +00005102void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5103 HandleShift(ushr);
5104}
5105
5106void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5107 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005108}
5109
5110void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5111 HandleBinaryOp(instruction);
5112}
5113
5114void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5115 HandleBinaryOp(instruction);
5116}
5117
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005118void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005119 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005120 LOG(FATAL) << "Unreachable";
5121}
5122
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005123void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005124 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005125 LOG(FATAL) << "Unreachable";
5126}
5127
Mark Mendellfe57faa2015-09-18 09:26:15 -04005128// Simple implementation of packed switch - generate cascaded compare/jumps.
5129void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5130 LocationSummary* locations =
5131 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5132 locations->SetInAt(0, Location::RequiresRegister());
5133}
5134
5135void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5136 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005137 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005138 Register value_reg = InputRegisterAt(switch_instr, 0);
5139 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5140
Zheng Xu3927c8b2015-11-18 17:46:25 +08005141 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005142 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005143 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5144 // make sure we don't emit it if the target may run out of range.
5145 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5146 // ranges and emit the tables only as required.
5147 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005148
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005149 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005150 // Current instruction id is an upper bound of the number of HIRs in the graph.
5151 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5152 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005153 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5154 Register temp = temps.AcquireW();
5155 __ Subs(temp, value_reg, Operand(lower_bound));
5156
Zheng Xu3927c8b2015-11-18 17:46:25 +08005157 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005158 // Jump to successors[0] if value == lower_bound.
5159 __ B(eq, codegen_->GetLabelOf(successors[0]));
5160 int32_t last_index = 0;
5161 for (; num_entries - last_index > 2; last_index += 2) {
5162 __ Subs(temp, temp, Operand(2));
5163 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5164 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5165 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5166 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5167 }
5168 if (num_entries - last_index == 2) {
5169 // The last missing case_value.
5170 __ Cmp(temp, Operand(1));
5171 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005172 }
5173
5174 // And the default for any other value.
5175 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5176 __ B(codegen_->GetLabelOf(default_block));
5177 }
5178 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005179 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005180
5181 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5182
5183 // Below instructions should use at most one blocked register. Since there are two blocked
5184 // registers, we are free to block one.
5185 Register temp_w = temps.AcquireW();
5186 Register index;
5187 // Remove the bias.
5188 if (lower_bound != 0) {
5189 index = temp_w;
5190 __ Sub(index, value_reg, Operand(lower_bound));
5191 } else {
5192 index = value_reg;
5193 }
5194
5195 // Jump to default block if index is out of the range.
5196 __ Cmp(index, Operand(num_entries));
5197 __ B(hs, codegen_->GetLabelOf(default_block));
5198
5199 // In current VIXL implementation, it won't require any blocked registers to encode the
5200 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5201 // register pressure.
5202 Register table_base = temps.AcquireX();
5203 // Load jump offset from the table.
5204 __ Adr(table_base, jump_table->GetTableStartLabel());
5205 Register jump_offset = temp_w;
5206 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5207
5208 // Jump to target block by branching to table_base(pc related) + offset.
5209 Register target_address = table_base;
5210 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5211 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005212 }
5213}
5214
Roland Levillain44015862016-01-22 11:47:17 +00005215void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5216 Location out,
5217 uint32_t offset,
5218 Location maybe_temp) {
5219 Primitive::Type type = Primitive::kPrimNot;
5220 Register out_reg = RegisterFrom(out, type);
5221 if (kEmitCompilerReadBarrier) {
5222 Register temp_reg = RegisterFrom(maybe_temp, type);
5223 if (kUseBakerReadBarrier) {
5224 // Load with fast path based Baker's read barrier.
5225 // /* HeapReference<Object> */ out = *(out + offset)
5226 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5227 out,
5228 out_reg,
5229 offset,
5230 temp_reg,
5231 /* needs_null_check */ false,
5232 /* use_load_acquire */ false);
5233 } else {
5234 // Load with slow path based read barrier.
5235 // Save the value of `out` into `maybe_temp` before overwriting it
5236 // in the following move operation, as we will need it for the
5237 // read barrier below.
5238 __ Mov(temp_reg, out_reg);
5239 // /* HeapReference<Object> */ out = *(out + offset)
5240 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5241 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5242 }
5243 } else {
5244 // Plain load with no read barrier.
5245 // /* HeapReference<Object> */ out = *(out + offset)
5246 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5247 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5248 }
5249}
5250
5251void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5252 Location out,
5253 Location obj,
5254 uint32_t offset,
5255 Location maybe_temp) {
5256 Primitive::Type type = Primitive::kPrimNot;
5257 Register out_reg = RegisterFrom(out, type);
5258 Register obj_reg = RegisterFrom(obj, type);
5259 if (kEmitCompilerReadBarrier) {
5260 if (kUseBakerReadBarrier) {
5261 // Load with fast path based Baker's read barrier.
5262 Register temp_reg = RegisterFrom(maybe_temp, type);
5263 // /* HeapReference<Object> */ out = *(obj + offset)
5264 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5265 out,
5266 obj_reg,
5267 offset,
5268 temp_reg,
5269 /* needs_null_check */ false,
5270 /* use_load_acquire */ false);
5271 } else {
5272 // Load with slow path based read barrier.
5273 // /* HeapReference<Object> */ out = *(obj + offset)
5274 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5275 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5276 }
5277 } else {
5278 // Plain load with no read barrier.
5279 // /* HeapReference<Object> */ out = *(obj + offset)
5280 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5281 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5282 }
5283}
5284
5285void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5286 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005287 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005288 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005289 vixl::aarch64::Label* fixup_label,
5290 bool requires_read_barrier) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005291 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005292 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005293 if (requires_read_barrier) {
5294 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005295 if (kUseBakerReadBarrier) {
5296 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5297 // Baker's read barrier are used:
5298 //
5299 // root = obj.field;
5300 // if (Thread::Current()->GetIsGcMarking()) {
5301 // root = ReadBarrier::Mark(root)
5302 // }
5303
5304 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005305 if (fixup_label == nullptr) {
5306 __ Ldr(root_reg, MemOperand(obj, offset));
5307 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005308 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005309 }
Roland Levillain44015862016-01-22 11:47:17 +00005310 static_assert(
5311 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5312 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5313 "have different sizes.");
5314 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5315 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5316 "have different sizes.");
5317
Vladimir Marko953437b2016-08-24 08:30:46 +00005318 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005319 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005320 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005321 codegen_->AddSlowPath(slow_path);
5322
5323 MacroAssembler* masm = GetVIXLAssembler();
5324 UseScratchRegisterScope temps(masm);
5325 Register temp = temps.AcquireW();
5326 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005327 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005328 __ Cbnz(temp, slow_path->GetEntryLabel());
5329 __ Bind(slow_path->GetExitLabel());
5330 } else {
5331 // GC root loaded through a slow path for read barriers other
5332 // than Baker's.
5333 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005334 if (fixup_label == nullptr) {
5335 __ Add(root_reg.X(), obj.X(), offset);
5336 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005337 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005338 }
Roland Levillain44015862016-01-22 11:47:17 +00005339 // /* mirror::Object* */ root = root->Read()
5340 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5341 }
5342 } else {
5343 // Plain GC root load with no read barrier.
5344 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005345 if (fixup_label == nullptr) {
5346 __ Ldr(root_reg, MemOperand(obj, offset));
5347 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005348 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005349 }
Roland Levillain44015862016-01-22 11:47:17 +00005350 // Note that GC roots are not affected by heap poisoning, thus we
5351 // do not have to unpoison `root_reg` here.
5352 }
5353}
5354
5355void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5356 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005357 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005358 uint32_t offset,
5359 Register temp,
5360 bool needs_null_check,
5361 bool use_load_acquire) {
5362 DCHECK(kEmitCompilerReadBarrier);
5363 DCHECK(kUseBakerReadBarrier);
5364
5365 // /* HeapReference<Object> */ ref = *(obj + offset)
5366 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005367 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005368 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5369 ref,
5370 obj,
5371 offset,
5372 no_index,
5373 no_scale_factor,
5374 temp,
5375 needs_null_check,
5376 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005377}
5378
5379void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5380 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005381 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005382 uint32_t data_offset,
5383 Location index,
5384 Register temp,
5385 bool needs_null_check) {
5386 DCHECK(kEmitCompilerReadBarrier);
5387 DCHECK(kUseBakerReadBarrier);
5388
5389 // Array cells are never volatile variables, therefore array loads
5390 // never use Load-Acquire instructions on ARM64.
5391 const bool use_load_acquire = false;
5392
Roland Levillainbfea3352016-06-23 13:48:47 +01005393 static_assert(
5394 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5395 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005396 // /* HeapReference<Object> */ ref =
5397 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005398 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5399 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5400 ref,
5401 obj,
5402 data_offset,
5403 index,
5404 scale_factor,
5405 temp,
5406 needs_null_check,
5407 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005408}
5409
5410void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5411 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005412 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005413 uint32_t offset,
5414 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005415 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005416 Register temp,
5417 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005418 bool use_load_acquire,
5419 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005420 DCHECK(kEmitCompilerReadBarrier);
5421 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005422 // If we are emitting an array load, we should not be using a
5423 // Load Acquire instruction. In other words:
5424 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5425 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005426
5427 MacroAssembler* masm = GetVIXLAssembler();
5428 UseScratchRegisterScope temps(masm);
5429
5430 // In slow path based read barriers, the read barrier call is
5431 // inserted after the original load. However, in fast path based
5432 // Baker's read barriers, we need to perform the load of
5433 // mirror::Object::monitor_ *before* the original reference load.
5434 // This load-load ordering is required by the read barrier.
5435 // The fast path/slow path (for Baker's algorithm) should look like:
5436 //
5437 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5438 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5439 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005440 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005441 // if (is_gray) {
5442 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5443 // }
5444 //
5445 // Note: the original implementation in ReadBarrier::Barrier is
5446 // slightly more complex as it performs additional checks that we do
5447 // not do here for performance reasons.
5448
5449 Primitive::Type type = Primitive::kPrimNot;
5450 Register ref_reg = RegisterFrom(ref, type);
5451 DCHECK(obj.IsW());
5452 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5453
5454 // /* int32_t */ monitor = obj->monitor_
5455 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5456 if (needs_null_check) {
5457 MaybeRecordImplicitNullCheck(instruction);
5458 }
5459 // /* LockWord */ lock_word = LockWord(monitor)
5460 static_assert(sizeof(LockWord) == sizeof(int32_t),
5461 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005462
Vladimir Marko877a0332016-07-11 19:30:56 +01005463 // Introduce a dependency on the lock_word including rb_state,
5464 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005465 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005466 // `obj` is unchanged by this operation, but its value now depends
5467 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005468 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005469
5470 // The actual reference load.
5471 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005472 // Load types involving an "index": ArrayGet,
5473 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5474 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005475 if (use_load_acquire) {
5476 // UnsafeGetObjectVolatile intrinsic case.
5477 // Register `index` is not an index in an object array, but an
5478 // offset to an object reference field within object `obj`.
5479 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5480 DCHECK(instruction->GetLocations()->Intrinsified());
5481 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5482 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005483 DCHECK_EQ(offset, 0u);
5484 DCHECK_EQ(scale_factor, 0u);
5485 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005486 // /* HeapReference<Object> */ ref = *(obj + index)
5487 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5488 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005489 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005490 // ArrayGet and UnsafeGetObject intrinsics cases.
5491 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5492 if (index.IsConstant()) {
5493 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5494 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5495 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005496 Register temp3 = temps.AcquireW();
5497 __ Add(temp3, obj, offset);
5498 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5499 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005500 }
Roland Levillain44015862016-01-22 11:47:17 +00005501 }
Roland Levillain44015862016-01-22 11:47:17 +00005502 } else {
5503 // /* HeapReference<Object> */ ref = *(obj + offset)
5504 MemOperand field = HeapOperand(obj, offset);
5505 if (use_load_acquire) {
5506 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5507 } else {
5508 Load(type, ref_reg, field);
5509 }
5510 }
5511
5512 // Object* ref = ref_addr->AsMirrorPtr()
5513 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5514
Vladimir Marko953437b2016-08-24 08:30:46 +00005515 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005516 SlowPathCodeARM64* slow_path;
5517 if (always_update_field) {
5518 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5519 // address of the form `obj + field_offset`, where `obj` is a
5520 // register and `field_offset` is a register. Thus `offset` and
5521 // `scale_factor` above are expected to be null in this code path.
5522 DCHECK_EQ(offset, 0u);
5523 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5524 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5525 instruction, ref, obj, /* field_offset */ index, temp);
5526 } else {
5527 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5528 }
Roland Levillain44015862016-01-22 11:47:17 +00005529 AddSlowPath(slow_path);
5530
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005531 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005532 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005533 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005534 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5535 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005536 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005537 __ Bind(slow_path->GetExitLabel());
5538}
5539
5540void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5541 Location out,
5542 Location ref,
5543 Location obj,
5544 uint32_t offset,
5545 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005546 DCHECK(kEmitCompilerReadBarrier);
5547
Roland Levillain44015862016-01-22 11:47:17 +00005548 // Insert a slow path based read barrier *after* the reference load.
5549 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005550 // If heap poisoning is enabled, the unpoisoning of the loaded
5551 // reference will be carried out by the runtime within the slow
5552 // path.
5553 //
5554 // Note that `ref` currently does not get unpoisoned (when heap
5555 // poisoning is enabled), which is alright as the `ref` argument is
5556 // not used by the artReadBarrierSlow entry point.
5557 //
5558 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5559 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5560 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5561 AddSlowPath(slow_path);
5562
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005563 __ B(slow_path->GetEntryLabel());
5564 __ Bind(slow_path->GetExitLabel());
5565}
5566
Roland Levillain44015862016-01-22 11:47:17 +00005567void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5568 Location out,
5569 Location ref,
5570 Location obj,
5571 uint32_t offset,
5572 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005573 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005574 // Baker's read barriers shall be handled by the fast path
5575 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5576 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005577 // If heap poisoning is enabled, unpoisoning will be taken care of
5578 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005579 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005580 } else if (kPoisonHeapReferences) {
5581 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5582 }
5583}
5584
Roland Levillain44015862016-01-22 11:47:17 +00005585void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5586 Location out,
5587 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005588 DCHECK(kEmitCompilerReadBarrier);
5589
Roland Levillain44015862016-01-22 11:47:17 +00005590 // Insert a slow path based read barrier *after* the GC root load.
5591 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005592 // Note that GC roots are not affected by heap poisoning, so we do
5593 // not need to do anything special for this here.
5594 SlowPathCodeARM64* slow_path =
5595 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5596 AddSlowPath(slow_path);
5597
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005598 __ B(slow_path->GetEntryLabel());
5599 __ Bind(slow_path->GetExitLabel());
5600}
5601
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005602void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5603 LocationSummary* locations =
5604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5605 locations->SetInAt(0, Location::RequiresRegister());
5606 locations->SetOut(Location::RequiresRegister());
5607}
5608
5609void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5610 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005611 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005612 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005613 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005614 __ Ldr(XRegisterFrom(locations->Out()),
5615 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005616 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005617 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005618 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005619 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5620 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005621 __ Ldr(XRegisterFrom(locations->Out()),
5622 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005623 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005624}
5625
5626
5627
Alexandre Rames67555f72014-11-18 10:55:16 +00005628#undef __
5629#undef QUICK_ENTRY_POINT
5630
Alexandre Rames5319def2014-10-23 10:03:10 +01005631} // namespace arm64
5632} // namespace art