blob: 89e5d9eb686ebb58c9292fb27a7f724aa39c3764 [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;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex().index_);
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;
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex().index_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
356 Primitive::Type type = instruction_->GetType();
357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
358
359 RestoreLiveRegisters(codegen, locations);
360
361 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
364 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
367 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
368 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100371 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100376 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 }
378
379 __ B(GetExitLabel());
380 }
381
382 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
383
384 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100385 const Register temp_;
386 vixl::aarch64::Label* adrp_label_;
387
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
389};
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 if (instruction_->CanThrowIntoCatchBlock()) {
399 // Live registers will be restored in the catch block if caught.
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
401 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000402 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
403 instruction_,
404 instruction_->GetDexPc(),
405 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800406 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100407 }
408
Alexandre Rames8158f282015-08-07 10:26:17 +0100409 bool IsFatal() const OVERRIDE { return true; }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
412
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
415};
416
417class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100419 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000420 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 if (successor_ == nullptr) {
428 __ B(GetReturnLabel());
429 } else {
430 __ B(arm64_codegen->GetLabelOf(successor_));
431 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 }
433
Scott Wakeling97c72b72016-06-24 16:19:36 +0100434 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 DCHECK(successor_ == nullptr);
436 return &return_label_;
437 }
438
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100439 HBasicBlock* GetSuccessor() const {
440 return successor_;
441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
444
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 // If not null, the block to branch to after the suspend check.
447 HBasicBlock* const successor_;
448
449 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100450 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100451
452 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
453};
454
Alexandre Rames67555f72014-11-18 10:55:16 +0000455class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000457 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000458 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800462
Alexandre Rames3e69f162014-12-10 10:36:50 +0000463 DCHECK(instruction_->IsCheckCast()
464 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100466 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000470 if (!is_fatal_) {
471 SaveLiveRegisters(codegen, locations);
472 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473
474 // We're moving two locations to locations that could overlap, so we need a parallel
475 // move resolver.
476 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800477 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800478 LocationFrom(calling_convention.GetRegisterAt(0)),
479 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800480 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800481 LocationFrom(calling_convention.GetRegisterAt(1)),
482 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000483 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000484 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800485 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000486 Primitive::Type ret_type = instruction_->GetType();
487 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
488 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
489 } else {
490 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800491 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
492 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493 }
494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000495 if (!is_fatal_) {
496 RestoreLiveRegisters(codegen, locations);
497 __ B(GetExitLabel());
498 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 }
500
Alexandre Rames9931f312015-06-19 14:47:01 +0100501 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100502 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000506
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
508};
509
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
511 public:
Aart Bik42249c32016-01-07 15:33:50 -0800512 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000513 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800516 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700517 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000518 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000519 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700520 }
521
Alexandre Rames9931f312015-06-19 14:47:01 +0100522 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
526};
527
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100528class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
533 LocationSummary* locations = instruction_->GetLocations();
534 __ Bind(GetEntryLabel());
535 SaveLiveRegisters(codegen, locations);
536
537 InvokeRuntimeCallingConvention calling_convention;
538 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
539 parallel_move.AddMove(
540 locations->InAt(0),
541 LocationFrom(calling_convention.GetRegisterAt(0)),
542 Primitive::kPrimNot,
543 nullptr);
544 parallel_move.AddMove(
545 locations->InAt(1),
546 LocationFrom(calling_convention.GetRegisterAt(1)),
547 Primitive::kPrimInt,
548 nullptr);
549 parallel_move.AddMove(
550 locations->InAt(2),
551 LocationFrom(calling_convention.GetRegisterAt(2)),
552 Primitive::kPrimNot,
553 nullptr);
554 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
555
556 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000557 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
559 RestoreLiveRegisters(codegen, locations);
560 __ B(GetExitLabel());
561 }
562
563 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
564
565 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
567};
568
Zheng Xu3927c8b2015-11-18 17:46:25 +0800569void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
570 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000571 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800572
573 // We are about to use the assembler to place literals directly. Make sure we have enough
574 // underlying code buffer and we have generated the jump table with right size.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000575 vixl::CodeBufferCheckScope scope(codegen->GetVIXLAssembler(),
576 num_entries * sizeof(int32_t),
577 vixl::CodeBufferCheckScope::kReserveBufferSpace,
578 vixl::CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800579
580 __ Bind(&table_start_);
581 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
582 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100583 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800584 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100585 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800586 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
587 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
588 Literal<int32_t> literal(jump_offset);
589 __ place(&literal);
590 }
591}
592
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100593// Slow path marking an object reference `ref` during a read
594// barrier. The field `obj.field` in the object `obj` holding this
595// reference does not get updated by this slow path after marking (see
596// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
597//
598// This means that after the execution of this slow path, `ref` will
599// always be up-to-date, but `obj.field` may not; i.e., after the
600// flip, `ref` will be a to-space reference, but `obj.field` will
601// probably still be a from-space reference (unless it gets updated by
602// another thread, or if another thread installed another object
603// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800604// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
605// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000606class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
607 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800608 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
609 Location ref,
610 Location entrypoint = Location::NoLocation())
611 : SlowPathCodeARM64(instruction),
612 ref_(ref),
613 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000614 DCHECK(kEmitCompilerReadBarrier);
615 }
616
617 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
618
619 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
620 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000621 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100622 DCHECK(ref_.IsRegister()) << ref_;
623 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000624 DCHECK(instruction_->IsInstanceFieldGet() ||
625 instruction_->IsStaticFieldGet() ||
626 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100627 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000628 instruction_->IsLoadClass() ||
629 instruction_->IsLoadString() ||
630 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100631 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100632 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
633 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000634 << "Unexpected instruction in read barrier marking slow path: "
635 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000636 // The read barrier instrumentation of object ArrayGet
637 // instructions does not support the HIntermediateAddress
638 // instruction.
639 DCHECK(!(instruction_->IsArrayGet() &&
640 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000641
642 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100643 // No need to save live registers; it's taken care of by the
644 // entrypoint. Also, there is no need to update the stack mask,
645 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000646 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100647 DCHECK_NE(ref_.reg(), LR);
648 DCHECK_NE(ref_.reg(), WSP);
649 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100650 // IP0 is used internally by the ReadBarrierMarkRegX entry point
651 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100652 DCHECK_NE(ref_.reg(), IP0);
653 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100654 // "Compact" slow path, saving two moves.
655 //
656 // Instead of using the standard runtime calling convention (input
657 // and output in W0):
658 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100659 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100660 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100661 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100662 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100663 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100664 // of a dedicated entrypoint:
665 //
666 // rX <- ReadBarrierMarkRegX(rX)
667 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800668 if (entrypoint_.IsValid()) {
669 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
670 __ Blr(XRegisterFrom(entrypoint_));
671 } else {
672 // Entrypoint is not already loaded, load from the thread.
673 int32_t entry_point_offset =
674 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
675 // This runtime call does not require a stack map.
676 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
677 }
Roland Levillain44015862016-01-22 11:47:17 +0000678 __ B(GetExitLabel());
679 }
680
681 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100682 // The location (register) of the marked object reference.
683 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000684
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800685 // The location of the entrypoint if it is already loaded.
686 const Location entrypoint_;
687
Roland Levillain44015862016-01-22 11:47:17 +0000688 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
689};
690
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100691// Slow path marking an object reference `ref` during a read barrier,
692// and if needed, atomically updating the field `obj.field` in the
693// object `obj` holding this reference after marking (contrary to
694// ReadBarrierMarkSlowPathARM64 above, which never tries to update
695// `obj.field`).
696//
697// This means that after the execution of this slow path, both `ref`
698// and `obj.field` will be up-to-date; i.e., after the flip, both will
699// hold the same to-space reference (unless another thread installed
700// another object reference (different from `ref`) in `obj.field`).
701class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
702 public:
703 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
704 Location ref,
705 Register obj,
706 Location field_offset,
707 Register temp)
708 : SlowPathCodeARM64(instruction),
709 ref_(ref),
710 obj_(obj),
711 field_offset_(field_offset),
712 temp_(temp) {
713 DCHECK(kEmitCompilerReadBarrier);
714 }
715
716 const char* GetDescription() const OVERRIDE {
717 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
718 }
719
720 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
721 LocationSummary* locations = instruction_->GetLocations();
722 Register ref_reg = WRegisterFrom(ref_);
723 DCHECK(locations->CanCall());
724 DCHECK(ref_.IsRegister()) << ref_;
725 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
726 // This slow path is only used by the UnsafeCASObject intrinsic.
727 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
728 << "Unexpected instruction in read barrier marking and field updating slow path: "
729 << instruction_->DebugName();
730 DCHECK(instruction_->GetLocations()->Intrinsified());
731 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
732 DCHECK(field_offset_.IsRegister()) << field_offset_;
733
734 __ Bind(GetEntryLabel());
735
736 // Save the old reference.
737 // Note that we cannot use IP to save the old reference, as IP is
738 // used internally by the ReadBarrierMarkRegX entry point, and we
739 // need the old reference after the call to that entry point.
740 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
741 __ Mov(temp_.W(), ref_reg);
742
743 // No need to save live registers; it's taken care of by the
744 // entrypoint. Also, there is no need to update the stack mask,
745 // as this runtime call will not trigger a garbage collection.
746 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
747 DCHECK_NE(ref_.reg(), LR);
748 DCHECK_NE(ref_.reg(), WSP);
749 DCHECK_NE(ref_.reg(), WZR);
750 // IP0 is used internally by the ReadBarrierMarkRegX entry point
751 // as a temporary, it cannot be the entry point's input/output.
752 DCHECK_NE(ref_.reg(), IP0);
753 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
754 // "Compact" slow path, saving two moves.
755 //
756 // Instead of using the standard runtime calling convention (input
757 // and output in W0):
758 //
759 // W0 <- ref
760 // W0 <- ReadBarrierMark(W0)
761 // ref <- W0
762 //
763 // we just use rX (the register containing `ref`) as input and output
764 // of a dedicated entrypoint:
765 //
766 // rX <- ReadBarrierMarkRegX(rX)
767 //
768 int32_t entry_point_offset =
769 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
770 // This runtime call does not require a stack map.
771 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
772
773 // If the new reference is different from the old reference,
774 // update the field in the holder (`*(obj_ + field_offset_)`).
775 //
776 // Note that this field could also hold a different object, if
777 // another thread had concurrently changed it. In that case, the
778 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
779 // (CAS) operation below would abort the CAS, leaving the field
780 // as-is.
781 vixl::aarch64::Label done;
782 __ Cmp(temp_.W(), ref_reg);
783 __ B(eq, &done);
784
785 // Update the the holder's field atomically. This may fail if
786 // mutator updates before us, but it's OK. This is achieved
787 // using a strong compare-and-set (CAS) operation with relaxed
788 // memory synchronization ordering, where the expected value is
789 // the old reference and the desired value is the new reference.
790
791 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
792 UseScratchRegisterScope temps(masm);
793
794 // Convenience aliases.
795 Register base = obj_.W();
796 Register offset = XRegisterFrom(field_offset_);
797 Register expected = temp_.W();
798 Register value = ref_reg;
799 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
800 Register tmp_value = temps.AcquireW(); // Value in memory.
801
802 __ Add(tmp_ptr, base.X(), Operand(offset));
803
804 if (kPoisonHeapReferences) {
805 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
806 if (value.Is(expected)) {
807 // Do not poison `value`, as it is the same register as
808 // `expected`, which has just been poisoned.
809 } else {
810 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
811 }
812 }
813
814 // do {
815 // tmp_value = [tmp_ptr] - expected;
816 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
817
Roland Levillain24a4d112016-10-26 13:10:46 +0100818 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100819 __ Bind(&loop_head);
820 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
821 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100822 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100823 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
824 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100825 __ B(&exit_loop);
826 __ Bind(&comparison_failed);
827 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100828 __ Bind(&exit_loop);
829
830 if (kPoisonHeapReferences) {
831 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
832 if (value.Is(expected)) {
833 // Do not unpoison `value`, as it is the same register as
834 // `expected`, which has just been unpoisoned.
835 } else {
836 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
837 }
838 }
839
840 __ Bind(&done);
841 __ B(GetExitLabel());
842 }
843
844 private:
845 // The location (register) of the marked object reference.
846 const Location ref_;
847 // The register containing the object holding the marked object reference field.
848 const Register obj_;
849 // The location of the offset of the marked reference field within `obj_`.
850 Location field_offset_;
851
852 const Register temp_;
853
854 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
855};
856
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000857// Slow path generating a read barrier for a heap reference.
858class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
859 public:
860 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
861 Location out,
862 Location ref,
863 Location obj,
864 uint32_t offset,
865 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000866 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000867 out_(out),
868 ref_(ref),
869 obj_(obj),
870 offset_(offset),
871 index_(index) {
872 DCHECK(kEmitCompilerReadBarrier);
873 // If `obj` is equal to `out` or `ref`, it means the initial object
874 // has been overwritten by (or after) the heap object reference load
875 // to be instrumented, e.g.:
876 //
877 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000878 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000879 //
880 // In that case, we have lost the information about the original
881 // object, and the emitted read barrier cannot work properly.
882 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
883 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
884 }
885
886 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
887 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
888 LocationSummary* locations = instruction_->GetLocations();
889 Primitive::Type type = Primitive::kPrimNot;
890 DCHECK(locations->CanCall());
891 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100892 DCHECK(instruction_->IsInstanceFieldGet() ||
893 instruction_->IsStaticFieldGet() ||
894 instruction_->IsArrayGet() ||
895 instruction_->IsInstanceOf() ||
896 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100897 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000898 << "Unexpected instruction in read barrier for heap reference slow path: "
899 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000900 // The read barrier instrumentation of object ArrayGet
901 // instructions does not support the HIntermediateAddress
902 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000903 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100904 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000905
906 __ Bind(GetEntryLabel());
907
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000908 SaveLiveRegisters(codegen, locations);
909
910 // We may have to change the index's value, but as `index_` is a
911 // constant member (like other "inputs" of this slow path),
912 // introduce a copy of it, `index`.
913 Location index = index_;
914 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100915 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000916 if (instruction_->IsArrayGet()) {
917 // Compute the actual memory offset and store it in `index`.
918 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
919 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
920 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
921 // We are about to change the value of `index_reg` (see the
922 // calls to vixl::MacroAssembler::Lsl and
923 // vixl::MacroAssembler::Mov below), but it has
924 // not been saved by the previous call to
925 // art::SlowPathCode::SaveLiveRegisters, as it is a
926 // callee-save register --
927 // art::SlowPathCode::SaveLiveRegisters does not consider
928 // callee-save registers, as it has been designed with the
929 // assumption that callee-save registers are supposed to be
930 // handled by the called function. So, as a callee-save
931 // register, `index_reg` _would_ eventually be saved onto
932 // the stack, but it would be too late: we would have
933 // changed its value earlier. Therefore, we manually save
934 // it here into another freely available register,
935 // `free_reg`, chosen of course among the caller-save
936 // registers (as a callee-save `free_reg` register would
937 // exhibit the same problem).
938 //
939 // Note we could have requested a temporary register from
940 // the register allocator instead; but we prefer not to, as
941 // this is a slow path, and we know we can find a
942 // caller-save register that is available.
943 Register free_reg = FindAvailableCallerSaveRegister(codegen);
944 __ Mov(free_reg.W(), index_reg);
945 index_reg = free_reg;
946 index = LocationFrom(index_reg);
947 } else {
948 // The initial register stored in `index_` has already been
949 // saved in the call to art::SlowPathCode::SaveLiveRegisters
950 // (as it is not a callee-save register), so we can freely
951 // use it.
952 }
953 // Shifting the index value contained in `index_reg` by the scale
954 // factor (2) cannot overflow in practice, as the runtime is
955 // unable to allocate object arrays with a size larger than
956 // 2^26 - 1 (that is, 2^28 - 4 bytes).
957 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
958 static_assert(
959 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
960 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
961 __ Add(index_reg, index_reg, Operand(offset_));
962 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100963 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
964 // intrinsics, `index_` is not shifted by a scale factor of 2
965 // (as in the case of ArrayGet), as it is actually an offset
966 // to an object field within an object.
967 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000968 DCHECK(instruction_->GetLocations()->Intrinsified());
969 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
970 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
971 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100972 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100973 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000974 }
975 }
976
977 // We're moving two or three locations to locations that could
978 // overlap, so we need a parallel move resolver.
979 InvokeRuntimeCallingConvention calling_convention;
980 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
981 parallel_move.AddMove(ref_,
982 LocationFrom(calling_convention.GetRegisterAt(0)),
983 type,
984 nullptr);
985 parallel_move.AddMove(obj_,
986 LocationFrom(calling_convention.GetRegisterAt(1)),
987 type,
988 nullptr);
989 if (index.IsValid()) {
990 parallel_move.AddMove(index,
991 LocationFrom(calling_convention.GetRegisterAt(2)),
992 Primitive::kPrimInt,
993 nullptr);
994 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
995 } else {
996 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
997 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
998 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000999 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001000 instruction_,
1001 instruction_->GetDexPc(),
1002 this);
1003 CheckEntrypointTypes<
1004 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1005 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1006
1007 RestoreLiveRegisters(codegen, locations);
1008
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001009 __ B(GetExitLabel());
1010 }
1011
1012 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1013
1014 private:
1015 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001016 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1017 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001018 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1019 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1020 return Register(VIXLRegCodeFromART(i), kXRegSize);
1021 }
1022 }
1023 // We shall never fail to find a free caller-save register, as
1024 // there are more than two core caller-save registers on ARM64
1025 // (meaning it is possible to find one which is different from
1026 // `ref` and `obj`).
1027 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1028 LOG(FATAL) << "Could not find a free register";
1029 UNREACHABLE();
1030 }
1031
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001032 const Location out_;
1033 const Location ref_;
1034 const Location obj_;
1035 const uint32_t offset_;
1036 // An additional location containing an index to an array.
1037 // Only used for HArrayGet and the UnsafeGetObject &
1038 // UnsafeGetObjectVolatile intrinsics.
1039 const Location index_;
1040
1041 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1042};
1043
1044// Slow path generating a read barrier for a GC root.
1045class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1046 public:
1047 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001048 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001049 DCHECK(kEmitCompilerReadBarrier);
1050 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001051
1052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1053 LocationSummary* locations = instruction_->GetLocations();
1054 Primitive::Type type = Primitive::kPrimNot;
1055 DCHECK(locations->CanCall());
1056 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001057 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1058 << "Unexpected instruction in read barrier for GC root slow path: "
1059 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001060
1061 __ Bind(GetEntryLabel());
1062 SaveLiveRegisters(codegen, locations);
1063
1064 InvokeRuntimeCallingConvention calling_convention;
1065 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1066 // The argument of the ReadBarrierForRootSlow is not a managed
1067 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1068 // thus we need a 64-bit move here, and we cannot use
1069 //
1070 // arm64_codegen->MoveLocation(
1071 // LocationFrom(calling_convention.GetRegisterAt(0)),
1072 // root_,
1073 // type);
1074 //
1075 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1076 // reference type (`Primitive::kPrimNot`).
1077 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001078 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001079 instruction_,
1080 instruction_->GetDexPc(),
1081 this);
1082 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1083 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1084
1085 RestoreLiveRegisters(codegen, locations);
1086 __ B(GetExitLabel());
1087 }
1088
1089 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1090
1091 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001092 const Location out_;
1093 const Location root_;
1094
1095 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1096};
1097
Alexandre Rames5319def2014-10-23 10:03:10 +01001098#undef __
1099
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001100Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 Location next_location;
1102 if (type == Primitive::kPrimVoid) {
1103 LOG(FATAL) << "Unreachable type " << type;
1104 }
1105
Alexandre Rames542361f2015-01-29 16:57:31 +00001106 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001107 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1108 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001109 } else if (!Primitive::IsFloatingPointType(type) &&
1110 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001111 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1112 } else {
1113 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001114 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1115 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001116 }
1117
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001118 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001119 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001120 return next_location;
1121}
1122
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001123Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001124 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001125}
1126
Serban Constantinescu579885a2015-02-22 20:51:33 +00001127CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1128 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001129 const CompilerOptions& compiler_options,
1130 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001131 : CodeGenerator(graph,
1132 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001133 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001134 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001135 callee_saved_core_registers.GetList(),
1136 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001137 compiler_options,
1138 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001139 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001140 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001142 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001143 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001144 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001145 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001146 uint32_literals_(std::less<uint32_t>(),
1147 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001148 uint64_literals_(std::less<uint64_t>(),
1149 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1150 method_patches_(MethodReferenceComparator(),
1151 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1152 call_patches_(MethodReferenceComparator(),
1153 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1154 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001155 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 boot_image_string_patches_(StringReferenceValueComparator(),
1157 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1158 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001159 boot_image_type_patches_(TypeReferenceValueComparator(),
1160 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1161 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001162 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001163 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1164 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001165 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1166 jit_class_patches_(TypeReferenceValueComparator(),
1167 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001168 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001169 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001170}
Alexandre Rames5319def2014-10-23 10:03:10 +01001171
Alexandre Rames67555f72014-11-18 10:55:16 +00001172#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001173
Zheng Xu3927c8b2015-11-18 17:46:25 +08001174void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001175 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001176 jump_table->EmitTable(this);
1177 }
1178}
1179
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001180void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001181 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001182 // Ensure we emit the literal pool.
1183 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001184
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001185 CodeGenerator::Finalize(allocator);
1186}
1187
Zheng Xuad4450e2015-04-17 18:48:56 +08001188void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1189 // Note: There are 6 kinds of moves:
1190 // 1. constant -> GPR/FPR (non-cycle)
1191 // 2. constant -> stack (non-cycle)
1192 // 3. GPR/FPR -> GPR/FPR
1193 // 4. GPR/FPR -> stack
1194 // 5. stack -> GPR/FPR
1195 // 6. stack -> stack (non-cycle)
1196 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1197 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1198 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1199 // dependency.
1200 vixl_temps_.Open(GetVIXLAssembler());
1201}
1202
1203void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1204 vixl_temps_.Close();
1205}
1206
1207Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1208 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1209 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1210 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1211 Location scratch = GetScratchLocation(kind);
1212 if (!scratch.Equals(Location::NoLocation())) {
1213 return scratch;
1214 }
1215 // Allocate from VIXL temp registers.
1216 if (kind == Location::kRegister) {
1217 scratch = LocationFrom(vixl_temps_.AcquireX());
1218 } else {
1219 DCHECK(kind == Location::kFpuRegister);
1220 scratch = LocationFrom(vixl_temps_.AcquireD());
1221 }
1222 AddScratchLocation(scratch);
1223 return scratch;
1224}
1225
1226void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1227 if (loc.IsRegister()) {
1228 vixl_temps_.Release(XRegisterFrom(loc));
1229 } else {
1230 DCHECK(loc.IsFpuRegister());
1231 vixl_temps_.Release(DRegisterFrom(loc));
1232 }
1233 RemoveScratchLocation(loc);
1234}
1235
Alexandre Rames3e69f162014-12-10 10:36:50 +00001236void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001237 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001238 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001239}
1240
Alexandre Rames5319def2014-10-23 10:03:10 +01001241void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001242 MacroAssembler* masm = GetVIXLAssembler();
1243 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001244 __ Bind(&frame_entry_label_);
1245
Serban Constantinescu02164b32014-11-13 14:05:07 +00001246 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1247 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001248 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001249 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001250 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001251 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001252 __ Ldr(wzr, MemOperand(temp, 0));
1253 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001254 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001255
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001256 if (!HasEmptyFrame()) {
1257 int frame_size = GetFrameSize();
1258 // Stack layout:
1259 // sp[frame_size - 8] : lr.
1260 // ... : other preserved core registers.
1261 // ... : other preserved fp registers.
1262 // ... : reserved frame space.
1263 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001264
1265 // Save the current method if we need it. Note that we do not
1266 // do this in HCurrentMethod, as the instruction might have been removed
1267 // in the SSA graph.
1268 if (RequiresCurrentMethod()) {
1269 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001270 } else {
1271 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001272 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001273 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001274 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1275 frame_size - GetCoreSpillSize());
1276 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1277 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001278
1279 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1280 // Initialize should_deoptimize flag to 0.
1281 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1282 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1283 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001284 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001285}
1286
1287void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001288 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001289 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001290 if (!HasEmptyFrame()) {
1291 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001292 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1293 frame_size - FrameEntrySpillSize());
1294 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1295 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001296 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001297 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001298 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001299 __ Ret();
1300 GetAssembler()->cfi().RestoreState();
1301 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001302}
1303
Scott Wakeling97c72b72016-06-24 16:19:36 +01001304CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001305 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001306 return CPURegList(CPURegister::kRegister, kXRegSize,
1307 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001308}
1309
Scott Wakeling97c72b72016-06-24 16:19:36 +01001310CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001311 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1312 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001313 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1314 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001315}
1316
Alexandre Rames5319def2014-10-23 10:03:10 +01001317void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1318 __ Bind(GetLabelOf(block));
1319}
1320
Calin Juravle175dc732015-08-25 15:42:32 +01001321void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1322 DCHECK(location.IsRegister());
1323 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1324}
1325
Calin Juravlee460d1d2015-09-29 04:52:17 +01001326void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1327 if (location.IsRegister()) {
1328 locations->AddTemp(location);
1329 } else {
1330 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1331 }
1332}
1333
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001334void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001335 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001336 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001337 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001338 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001339 if (value_can_be_null) {
1340 __ Cbz(value, &done);
1341 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001342 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001343 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001344 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001345 if (value_can_be_null) {
1346 __ Bind(&done);
1347 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001348}
1349
David Brazdil58282f42016-01-14 12:45:10 +00001350void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001351 // Blocked core registers:
1352 // lr : Runtime reserved.
1353 // tr : Runtime reserved.
1354 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1355 // ip1 : VIXL core temp.
1356 // ip0 : VIXL core temp.
1357 //
1358 // Blocked fp registers:
1359 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1361 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001362 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001363 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001365
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001366 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001367 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001368 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001369 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001370
David Brazdil58282f42016-01-14 12:45:10 +00001371 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001372 // Stubs do not save callee-save floating point registers. If the graph
1373 // is debuggable, we need to deal with these registers differently. For
1374 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001375 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1376 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001377 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001378 }
1379 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001380}
1381
Alexandre Rames3e69f162014-12-10 10:36:50 +00001382size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1383 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1384 __ Str(reg, MemOperand(sp, stack_index));
1385 return kArm64WordSize;
1386}
1387
1388size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1389 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1390 __ Ldr(reg, MemOperand(sp, stack_index));
1391 return kArm64WordSize;
1392}
1393
1394size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1395 FPRegister reg = FPRegister(reg_id, kDRegSize);
1396 __ Str(reg, MemOperand(sp, stack_index));
1397 return kArm64WordSize;
1398}
1399
1400size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1401 FPRegister reg = FPRegister(reg_id, kDRegSize);
1402 __ Ldr(reg, MemOperand(sp, stack_index));
1403 return kArm64WordSize;
1404}
1405
Alexandre Rames5319def2014-10-23 10:03:10 +01001406void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001407 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001408}
1409
1410void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001411 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001412}
1413
Alexandre Rames67555f72014-11-18 10:55:16 +00001414void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001415 if (constant->IsIntConstant()) {
1416 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1417 } else if (constant->IsLongConstant()) {
1418 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1419 } else if (constant->IsNullConstant()) {
1420 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001421 } else if (constant->IsFloatConstant()) {
1422 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1423 } else {
1424 DCHECK(constant->IsDoubleConstant());
1425 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1426 }
1427}
1428
Alexandre Rames3e69f162014-12-10 10:36:50 +00001429
1430static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1431 DCHECK(constant.IsConstant());
1432 HConstant* cst = constant.GetConstant();
1433 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001434 // Null is mapped to a core W register, which we associate with kPrimInt.
1435 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001436 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1437 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1438 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1439}
1440
Calin Juravlee460d1d2015-09-29 04:52:17 +01001441void CodeGeneratorARM64::MoveLocation(Location destination,
1442 Location source,
1443 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001444 if (source.Equals(destination)) {
1445 return;
1446 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001447
1448 // A valid move can always be inferred from the destination and source
1449 // locations. When moving from and to a register, the argument type can be
1450 // used to generate 32bit instead of 64bit moves. In debug mode we also
1451 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001452 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453
1454 if (destination.IsRegister() || destination.IsFpuRegister()) {
1455 if (unspecified_type) {
1456 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1457 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001458 (src_cst != nullptr && (src_cst->IsIntConstant()
1459 || src_cst->IsFloatConstant()
1460 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001462 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001463 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001464 // If the source is a double stack slot or a 64bit constant, a 64bit
1465 // type is appropriate. Else the source is a register, and since the
1466 // type has not been specified, we chose a 64bit type to force a 64bit
1467 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001469 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001470 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001471 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1472 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1473 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001474 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1475 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1476 __ Ldr(dst, StackOperandFrom(source));
1477 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001478 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001479 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001480 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001481 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001482 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001483 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001484 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001485 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1486 ? Primitive::kPrimLong
1487 : Primitive::kPrimInt;
1488 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1489 }
1490 } else {
1491 DCHECK(source.IsFpuRegister());
1492 if (destination.IsRegister()) {
1493 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1494 ? Primitive::kPrimDouble
1495 : Primitive::kPrimFloat;
1496 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1497 } else {
1498 DCHECK(destination.IsFpuRegister());
1499 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001500 }
1501 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001502 } else { // The destination is not a register. It must be a stack slot.
1503 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1504 if (source.IsRegister() || source.IsFpuRegister()) {
1505 if (unspecified_type) {
1506 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001507 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001508 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001509 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001510 }
1511 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001512 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1513 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1514 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001515 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001516 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1517 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001518 UseScratchRegisterScope temps(GetVIXLAssembler());
1519 HConstant* src_cst = source.GetConstant();
1520 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001521 if (src_cst->IsZeroBitPattern()) {
1522 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001523 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001524 if (src_cst->IsIntConstant()) {
1525 temp = temps.AcquireW();
1526 } else if (src_cst->IsLongConstant()) {
1527 temp = temps.AcquireX();
1528 } else if (src_cst->IsFloatConstant()) {
1529 temp = temps.AcquireS();
1530 } else {
1531 DCHECK(src_cst->IsDoubleConstant());
1532 temp = temps.AcquireD();
1533 }
1534 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001535 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001536 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001537 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001538 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001540 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001541 // There is generally less pressure on FP registers.
1542 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001543 __ Ldr(temp, StackOperandFrom(source));
1544 __ Str(temp, StackOperandFrom(destination));
1545 }
1546 }
1547}
1548
1549void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001550 CPURegister dst,
1551 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001552 switch (type) {
1553 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001554 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001555 break;
1556 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001557 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001558 break;
1559 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001560 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001561 break;
1562 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001563 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001564 break;
1565 case Primitive::kPrimInt:
1566 case Primitive::kPrimNot:
1567 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 case Primitive::kPrimFloat:
1569 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001570 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001571 __ Ldr(dst, src);
1572 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001573 case Primitive::kPrimVoid:
1574 LOG(FATAL) << "Unreachable type " << type;
1575 }
1576}
1577
Calin Juravle77520bc2015-01-12 18:45:46 +00001578void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001579 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001580 const MemOperand& src,
1581 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001582 MacroAssembler* masm = GetVIXLAssembler();
1583 BlockPoolsScope block_pools(masm);
1584 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001585 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001586 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001587
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001588 DCHECK(!src.IsPreIndex());
1589 DCHECK(!src.IsPostIndex());
1590
1591 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001592 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001593 MemOperand base = MemOperand(temp_base);
1594 switch (type) {
1595 case Primitive::kPrimBoolean:
1596 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001597 if (needs_null_check) {
1598 MaybeRecordImplicitNullCheck(instruction);
1599 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001600 break;
1601 case Primitive::kPrimByte:
1602 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001603 if (needs_null_check) {
1604 MaybeRecordImplicitNullCheck(instruction);
1605 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001606 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1607 break;
1608 case Primitive::kPrimChar:
1609 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001610 if (needs_null_check) {
1611 MaybeRecordImplicitNullCheck(instruction);
1612 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001613 break;
1614 case Primitive::kPrimShort:
1615 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001616 if (needs_null_check) {
1617 MaybeRecordImplicitNullCheck(instruction);
1618 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001619 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1620 break;
1621 case Primitive::kPrimInt:
1622 case Primitive::kPrimNot:
1623 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001624 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001625 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001626 if (needs_null_check) {
1627 MaybeRecordImplicitNullCheck(instruction);
1628 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001629 break;
1630 case Primitive::kPrimFloat:
1631 case Primitive::kPrimDouble: {
1632 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001633 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001634
1635 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1636 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001637 if (needs_null_check) {
1638 MaybeRecordImplicitNullCheck(instruction);
1639 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001640 __ Fmov(FPRegister(dst), temp);
1641 break;
1642 }
1643 case Primitive::kPrimVoid:
1644 LOG(FATAL) << "Unreachable type " << type;
1645 }
1646}
1647
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001648void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001649 CPURegister src,
1650 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001651 switch (type) {
1652 case Primitive::kPrimBoolean:
1653 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001655 break;
1656 case Primitive::kPrimChar:
1657 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001658 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001659 break;
1660 case Primitive::kPrimInt:
1661 case Primitive::kPrimNot:
1662 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001663 case Primitive::kPrimFloat:
1664 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001665 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001666 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001667 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001668 case Primitive::kPrimVoid:
1669 LOG(FATAL) << "Unreachable type " << type;
1670 }
1671}
1672
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001673void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1674 CPURegister src,
1675 const MemOperand& dst) {
1676 UseScratchRegisterScope temps(GetVIXLAssembler());
1677 Register temp_base = temps.AcquireX();
1678
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679 DCHECK(!dst.IsPreIndex());
1680 DCHECK(!dst.IsPostIndex());
1681
1682 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001683 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001684 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001685 MemOperand base = MemOperand(temp_base);
1686 switch (type) {
1687 case Primitive::kPrimBoolean:
1688 case Primitive::kPrimByte:
1689 __ Stlrb(Register(src), base);
1690 break;
1691 case Primitive::kPrimChar:
1692 case Primitive::kPrimShort:
1693 __ Stlrh(Register(src), base);
1694 break;
1695 case Primitive::kPrimInt:
1696 case Primitive::kPrimNot:
1697 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001698 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001699 __ Stlr(Register(src), base);
1700 break;
1701 case Primitive::kPrimFloat:
1702 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001703 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001704 Register temp_src;
1705 if (src.IsZero()) {
1706 // The zero register is used to avoid synthesizing zero constants.
1707 temp_src = Register(src);
1708 } else {
1709 DCHECK(src.IsFPRegister());
1710 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1711 __ Fmov(temp_src, FPRegister(src));
1712 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001713
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001714 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001715 break;
1716 }
1717 case Primitive::kPrimVoid:
1718 LOG(FATAL) << "Unreachable type " << type;
1719 }
1720}
1721
Calin Juravle175dc732015-08-25 15:42:32 +01001722void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1723 HInstruction* instruction,
1724 uint32_t dex_pc,
1725 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001726 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001727 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001728 if (EntrypointRequiresStackMap(entrypoint)) {
1729 RecordPcInfo(instruction, dex_pc, slow_path);
1730 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001731}
1732
Roland Levillaindec8f632016-07-22 17:10:06 +01001733void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1734 HInstruction* instruction,
1735 SlowPathCode* slow_path) {
1736 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001737 GenerateInvokeRuntime(entry_point_offset);
1738}
1739
1740void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001741 BlockPoolsScope block_pools(GetVIXLAssembler());
1742 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1743 __ Blr(lr);
1744}
1745
Alexandre Rames67555f72014-11-18 10:55:16 +00001746void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001747 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 UseScratchRegisterScope temps(GetVIXLAssembler());
1749 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001750 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1751
Serban Constantinescu02164b32014-11-13 14:05:07 +00001752 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001753 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1754 __ Add(temp, class_reg, status_offset);
1755 __ Ldar(temp, HeapOperand(temp));
1756 __ Cmp(temp, mirror::Class::kStatusInitialized);
1757 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001758 __ Bind(slow_path->GetExitLabel());
1759}
Alexandre Rames5319def2014-10-23 10:03:10 +01001760
Roland Levillain44015862016-01-22 11:47:17 +00001761void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001762 BarrierType type = BarrierAll;
1763
1764 switch (kind) {
1765 case MemBarrierKind::kAnyAny:
1766 case MemBarrierKind::kAnyStore: {
1767 type = BarrierAll;
1768 break;
1769 }
1770 case MemBarrierKind::kLoadAny: {
1771 type = BarrierReads;
1772 break;
1773 }
1774 case MemBarrierKind::kStoreStore: {
1775 type = BarrierWrites;
1776 break;
1777 }
1778 default:
1779 LOG(FATAL) << "Unexpected memory barrier " << kind;
1780 }
1781 __ Dmb(InnerShareable, type);
1782}
1783
Serban Constantinescu02164b32014-11-13 14:05:07 +00001784void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1785 HBasicBlock* successor) {
1786 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001787 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1788 if (slow_path == nullptr) {
1789 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1790 instruction->SetSlowPath(slow_path);
1791 codegen_->AddSlowPath(slow_path);
1792 if (successor != nullptr) {
1793 DCHECK(successor->IsLoopHeader());
1794 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1795 }
1796 } else {
1797 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1798 }
1799
Serban Constantinescu02164b32014-11-13 14:05:07 +00001800 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1801 Register temp = temps.AcquireW();
1802
Andreas Gampe542451c2016-07-26 09:02:02 -07001803 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001804 if (successor == nullptr) {
1805 __ Cbnz(temp, slow_path->GetEntryLabel());
1806 __ Bind(slow_path->GetReturnLabel());
1807 } else {
1808 __ Cbz(temp, codegen_->GetLabelOf(successor));
1809 __ B(slow_path->GetEntryLabel());
1810 // slow_path will return to GetLabelOf(successor).
1811 }
1812}
1813
Alexandre Rames5319def2014-10-23 10:03:10 +01001814InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1815 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001816 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001817 assembler_(codegen->GetAssembler()),
1818 codegen_(codegen) {}
1819
1820#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001821 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001822
1823#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1824
1825enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001826 // Using a base helps identify when we hit such breakpoints.
1827 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001828#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1829 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1830#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1831};
1832
1833#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001834 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001835 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1836 } \
1837 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1838 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1839 locations->SetOut(Location::Any()); \
1840 }
1841 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1842#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1843
1844#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001845#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001846
Alexandre Rames67555f72014-11-18 10:55:16 +00001847void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001848 DCHECK_EQ(instr->InputCount(), 2U);
1849 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1850 Primitive::Type type = instr->GetResultType();
1851 switch (type) {
1852 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001853 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001854 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001855 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001856 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001857 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001858
1859 case Primitive::kPrimFloat:
1860 case Primitive::kPrimDouble:
1861 locations->SetInAt(0, Location::RequiresFpuRegister());
1862 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001863 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001864 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001865
Alexandre Rames5319def2014-10-23 10:03:10 +01001866 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001867 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001868 }
1869}
1870
Alexandre Rames09a99962015-04-15 11:47:56 +01001871void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001872 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1873
1874 bool object_field_get_with_read_barrier =
1875 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001876 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001877 new (GetGraph()->GetArena()) LocationSummary(instruction,
1878 object_field_get_with_read_barrier ?
1879 LocationSummary::kCallOnSlowPath :
1880 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001881 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001882 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001883 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001884 locations->SetInAt(0, Location::RequiresRegister());
1885 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1886 locations->SetOut(Location::RequiresFpuRegister());
1887 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001888 // The output overlaps for an object field get when read barriers
1889 // are enabled: we do not want the load to overwrite the object's
1890 // location, as we need it to emit the read barrier.
1891 locations->SetOut(
1892 Location::RequiresRegister(),
1893 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001894 }
1895}
1896
1897void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1898 const FieldInfo& field_info) {
1899 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001900 LocationSummary* locations = instruction->GetLocations();
1901 Location base_loc = locations->InAt(0);
1902 Location out = locations->Out();
1903 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001904 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001905 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001906 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001907
Roland Levillain44015862016-01-22 11:47:17 +00001908 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1909 // Object FieldGet with Baker's read barrier case.
1910 MacroAssembler* masm = GetVIXLAssembler();
1911 UseScratchRegisterScope temps(masm);
1912 // /* HeapReference<Object> */ out = *(base + offset)
1913 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1914 Register temp = temps.AcquireW();
1915 // Note that potential implicit null checks are handled in this
1916 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1917 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1918 instruction,
1919 out,
1920 base,
1921 offset,
1922 temp,
1923 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001924 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001925 } else {
1926 // General case.
1927 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001928 // Note that a potential implicit null check is handled in this
1929 // CodeGeneratorARM64::LoadAcquire call.
1930 // NB: LoadAcquire will record the pc info if needed.
1931 codegen_->LoadAcquire(
1932 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001933 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001934 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001935 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001936 }
Roland Levillain44015862016-01-22 11:47:17 +00001937 if (field_type == Primitive::kPrimNot) {
1938 // If read barriers are enabled, emit read barriers other than
1939 // Baker's using a slow path (and also unpoison the loaded
1940 // reference, if heap poisoning is enabled).
1941 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1942 }
Roland Levillain4d027112015-07-01 15:41:14 +01001943 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001944}
1945
1946void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1947 LocationSummary* locations =
1948 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1949 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001950 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1951 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1952 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001953 locations->SetInAt(1, Location::RequiresFpuRegister());
1954 } else {
1955 locations->SetInAt(1, Location::RequiresRegister());
1956 }
1957}
1958
1959void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001960 const FieldInfo& field_info,
1961 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001962 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001963 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001964
1965 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001966 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001967 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001968 Offset offset = field_info.GetFieldOffset();
1969 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001970
Roland Levillain4d027112015-07-01 15:41:14 +01001971 {
1972 // We use a block to end the scratch scope before the write barrier, thus
1973 // freeing the temporary registers so they can be used in `MarkGCCard`.
1974 UseScratchRegisterScope temps(GetVIXLAssembler());
1975
1976 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1977 DCHECK(value.IsW());
1978 Register temp = temps.AcquireW();
1979 __ Mov(temp, value.W());
1980 GetAssembler()->PoisonHeapReference(temp.W());
1981 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001982 }
Roland Levillain4d027112015-07-01 15:41:14 +01001983
1984 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001985 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1986 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001987 } else {
1988 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1989 codegen_->MaybeRecordImplicitNullCheck(instruction);
1990 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001991 }
1992
1993 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001994 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001995 }
1996}
1997
Alexandre Rames67555f72014-11-18 10:55:16 +00001998void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001999 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002000
2001 switch (type) {
2002 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002003 case Primitive::kPrimLong: {
2004 Register dst = OutputRegister(instr);
2005 Register lhs = InputRegisterAt(instr, 0);
2006 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002007 if (instr->IsAdd()) {
2008 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002009 } else if (instr->IsAnd()) {
2010 __ And(dst, lhs, rhs);
2011 } else if (instr->IsOr()) {
2012 __ Orr(dst, lhs, rhs);
2013 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002014 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002015 } else if (instr->IsRor()) {
2016 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002017 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002018 __ Ror(dst, lhs, shift);
2019 } else {
2020 // Ensure shift distance is in the same size register as the result. If
2021 // we are rotating a long and the shift comes in a w register originally,
2022 // we don't need to sxtw for use as an x since the shift distances are
2023 // all & reg_bits - 1.
2024 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2025 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002026 } else {
2027 DCHECK(instr->IsXor());
2028 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002029 }
2030 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002031 }
2032 case Primitive::kPrimFloat:
2033 case Primitive::kPrimDouble: {
2034 FPRegister dst = OutputFPRegister(instr);
2035 FPRegister lhs = InputFPRegisterAt(instr, 0);
2036 FPRegister rhs = InputFPRegisterAt(instr, 1);
2037 if (instr->IsAdd()) {
2038 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002039 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002040 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002041 } else {
2042 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002043 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002044 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002045 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002046 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002047 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002048 }
2049}
2050
Serban Constantinescu02164b32014-11-13 14:05:07 +00002051void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2052 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2053
2054 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2055 Primitive::Type type = instr->GetResultType();
2056 switch (type) {
2057 case Primitive::kPrimInt:
2058 case Primitive::kPrimLong: {
2059 locations->SetInAt(0, Location::RequiresRegister());
2060 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2061 locations->SetOut(Location::RequiresRegister());
2062 break;
2063 }
2064 default:
2065 LOG(FATAL) << "Unexpected shift type " << type;
2066 }
2067}
2068
2069void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2070 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2071
2072 Primitive::Type type = instr->GetType();
2073 switch (type) {
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimLong: {
2076 Register dst = OutputRegister(instr);
2077 Register lhs = InputRegisterAt(instr, 0);
2078 Operand rhs = InputOperandAt(instr, 1);
2079 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002080 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002081 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002082 if (instr->IsShl()) {
2083 __ Lsl(dst, lhs, shift_value);
2084 } else if (instr->IsShr()) {
2085 __ Asr(dst, lhs, shift_value);
2086 } else {
2087 __ Lsr(dst, lhs, shift_value);
2088 }
2089 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002090 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002091
2092 if (instr->IsShl()) {
2093 __ Lsl(dst, lhs, rhs_reg);
2094 } else if (instr->IsShr()) {
2095 __ Asr(dst, lhs, rhs_reg);
2096 } else {
2097 __ Lsr(dst, lhs, rhs_reg);
2098 }
2099 }
2100 break;
2101 }
2102 default:
2103 LOG(FATAL) << "Unexpected shift operation type " << type;
2104 }
2105}
2106
Alexandre Rames5319def2014-10-23 10:03:10 +01002107void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002108 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002109}
2110
2111void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002112 HandleBinaryOp(instruction);
2113}
2114
2115void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2116 HandleBinaryOp(instruction);
2117}
2118
2119void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2120 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002121}
2122
Artem Serov7fc63502016-02-09 17:15:29 +00002123void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002124 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2126 locations->SetInAt(0, Location::RequiresRegister());
2127 // There is no immediate variant of negated bitwise instructions in AArch64.
2128 locations->SetInAt(1, Location::RequiresRegister());
2129 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2130}
2131
Artem Serov7fc63502016-02-09 17:15:29 +00002132void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002133 Register dst = OutputRegister(instr);
2134 Register lhs = InputRegisterAt(instr, 0);
2135 Register rhs = InputRegisterAt(instr, 1);
2136
2137 switch (instr->GetOpKind()) {
2138 case HInstruction::kAnd:
2139 __ Bic(dst, lhs, rhs);
2140 break;
2141 case HInstruction::kOr:
2142 __ Orn(dst, lhs, rhs);
2143 break;
2144 case HInstruction::kXor:
2145 __ Eon(dst, lhs, rhs);
2146 break;
2147 default:
2148 LOG(FATAL) << "Unreachable";
2149 }
2150}
2151
Alexandre Rames8626b742015-11-25 16:28:08 +00002152void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2153 HArm64DataProcWithShifterOp* instruction) {
2154 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2155 instruction->GetType() == Primitive::kPrimLong);
2156 LocationSummary* locations =
2157 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2158 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2159 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2160 } else {
2161 locations->SetInAt(0, Location::RequiresRegister());
2162 }
2163 locations->SetInAt(1, Location::RequiresRegister());
2164 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2165}
2166
2167void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2168 HArm64DataProcWithShifterOp* instruction) {
2169 Primitive::Type type = instruction->GetType();
2170 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2171 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2172 Register out = OutputRegister(instruction);
2173 Register left;
2174 if (kind != HInstruction::kNeg) {
2175 left = InputRegisterAt(instruction, 0);
2176 }
2177 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2178 // shifter operand operation, the IR generating `right_reg` (input to the type
2179 // conversion) can have a different type from the current instruction's type,
2180 // so we manually indicate the type.
2181 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002182 int64_t shift_amount = instruction->GetShiftAmount() &
2183 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002184
2185 Operand right_operand(0);
2186
2187 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2188 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2189 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2190 } else {
2191 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2192 }
2193
2194 // Logical binary operations do not support extension operations in the
2195 // operand. Note that VIXL would still manage if it was passed by generating
2196 // the extension as a separate instruction.
2197 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2198 DCHECK(!right_operand.IsExtendedRegister() ||
2199 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2200 kind != HInstruction::kNeg));
2201 switch (kind) {
2202 case HInstruction::kAdd:
2203 __ Add(out, left, right_operand);
2204 break;
2205 case HInstruction::kAnd:
2206 __ And(out, left, right_operand);
2207 break;
2208 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002209 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002210 __ Neg(out, right_operand);
2211 break;
2212 case HInstruction::kOr:
2213 __ Orr(out, left, right_operand);
2214 break;
2215 case HInstruction::kSub:
2216 __ Sub(out, left, right_operand);
2217 break;
2218 case HInstruction::kXor:
2219 __ Eor(out, left, right_operand);
2220 break;
2221 default:
2222 LOG(FATAL) << "Unexpected operation kind: " << kind;
2223 UNREACHABLE();
2224 }
2225}
2226
Artem Serov328429f2016-07-06 16:23:04 +01002227void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002228 LocationSummary* locations =
2229 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2230 locations->SetInAt(0, Location::RequiresRegister());
2231 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2232 locations->SetOut(Location::RequiresRegister());
2233}
2234
Roland Levillain19c54192016-11-04 13:44:09 +00002235void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002236 __ Add(OutputRegister(instruction),
2237 InputRegisterAt(instruction, 0),
2238 Operand(InputOperandAt(instruction, 1)));
2239}
2240
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002241void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002242 LocationSummary* locations =
2243 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002244 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2245 if (instr->GetOpKind() == HInstruction::kSub &&
2246 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002247 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002248 // Don't allocate register for Mneg instruction.
2249 } else {
2250 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2251 Location::RequiresRegister());
2252 }
2253 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2254 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002255 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2256}
2257
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002258void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002259 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002260 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2261 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002262
2263 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2264 // This fixup should be carried out for all multiply-accumulate instructions:
2265 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2266 if (instr->GetType() == Primitive::kPrimLong &&
2267 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2268 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002269 vixl::aarch64::Instruction* prev =
2270 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002271 if (prev->IsLoadOrStore()) {
2272 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002273 vixl::CodeBufferCheckScope scope(masm,
2274 kInstructionSize,
2275 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2276 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002277 __ nop();
2278 }
2279 }
2280
2281 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002282 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002283 __ Madd(res, mul_left, mul_right, accumulator);
2284 } else {
2285 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002286 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002287 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002288 __ Mneg(res, mul_left, mul_right);
2289 } else {
2290 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2291 __ Msub(res, mul_left, mul_right, accumulator);
2292 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002293 }
2294}
2295
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002296void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002297 bool object_array_get_with_read_barrier =
2298 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002299 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002300 new (GetGraph()->GetArena()) LocationSummary(instruction,
2301 object_array_get_with_read_barrier ?
2302 LocationSummary::kCallOnSlowPath :
2303 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002304 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002305 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002306 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002307 locations->SetInAt(0, Location::RequiresRegister());
2308 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002309 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2310 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2311 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002312 // The output overlaps in the case of an object array get with
2313 // read barriers enabled: we do not want the move to overwrite the
2314 // array's location, as we need it to emit the read barrier.
2315 locations->SetOut(
2316 Location::RequiresRegister(),
2317 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002318 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002319}
2320
2321void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002322 Primitive::Type type = instruction->GetType();
2323 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002324 LocationSummary* locations = instruction->GetLocations();
2325 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002326 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002327 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002328 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2329 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002330 MacroAssembler* masm = GetVIXLAssembler();
2331 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002332 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002333 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002334
Roland Levillain19c54192016-11-04 13:44:09 +00002335 // The read barrier instrumentation of object ArrayGet instructions
2336 // does not support the HIntermediateAddress instruction.
2337 DCHECK(!((type == Primitive::kPrimNot) &&
2338 instruction->GetArray()->IsIntermediateAddress() &&
2339 kEmitCompilerReadBarrier));
2340
Roland Levillain44015862016-01-22 11:47:17 +00002341 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2342 // Object ArrayGet with Baker's read barrier case.
2343 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002344 // Note that a potential implicit null check is handled in the
2345 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2346 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2347 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002348 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002349 // General case.
2350 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002351 Register length;
2352 if (maybe_compressed_char_at) {
2353 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2354 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002355 if (instruction->GetArray()->IsIntermediateAddress()) {
2356 DCHECK_LT(count_offset, offset);
2357 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2358 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2359 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2360 } else {
2361 __ Ldr(length, HeapOperand(obj, count_offset));
2362 }
jessicahandojo05765752016-09-09 19:01:32 -07002363 codegen_->MaybeRecordImplicitNullCheck(instruction);
2364 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002365 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002366 if (maybe_compressed_char_at) {
2367 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002368 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2369 "Expecting 0=compressed, 1=uncompressed");
2370 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002371 __ Ldrb(Register(OutputCPURegister(instruction)),
2372 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2373 __ B(&done);
2374 __ Bind(&uncompressed_load);
2375 __ Ldrh(Register(OutputCPURegister(instruction)),
2376 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2377 __ Bind(&done);
2378 } else {
2379 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2380 source = HeapOperand(obj, offset);
2381 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002382 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002383 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002384 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002385 // We do not need to compute the intermediate address from the array: the
2386 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002387 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002388 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002389 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002390 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2391 }
2392 temp = obj;
2393 } else {
2394 __ Add(temp, obj, offset);
2395 }
jessicahandojo05765752016-09-09 19:01:32 -07002396 if (maybe_compressed_char_at) {
2397 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002398 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2399 "Expecting 0=compressed, 1=uncompressed");
2400 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002401 __ Ldrb(Register(OutputCPURegister(instruction)),
2402 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2403 __ B(&done);
2404 __ Bind(&uncompressed_load);
2405 __ Ldrh(Register(OutputCPURegister(instruction)),
2406 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2407 __ Bind(&done);
2408 } else {
2409 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2410 }
Roland Levillain44015862016-01-22 11:47:17 +00002411 }
jessicahandojo05765752016-09-09 19:01:32 -07002412 if (!maybe_compressed_char_at) {
2413 codegen_->Load(type, OutputCPURegister(instruction), source);
2414 codegen_->MaybeRecordImplicitNullCheck(instruction);
2415 }
Roland Levillain44015862016-01-22 11:47:17 +00002416
2417 if (type == Primitive::kPrimNot) {
2418 static_assert(
2419 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2420 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2421 Location obj_loc = locations->InAt(0);
2422 if (index.IsConstant()) {
2423 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2424 } else {
2425 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2426 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002427 }
Roland Levillain4d027112015-07-01 15:41:14 +01002428 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002429}
2430
Alexandre Rames5319def2014-10-23 10:03:10 +01002431void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2433 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002434 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002435}
2436
2437void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002438 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002439 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002440 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002441 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002442 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002443 // Mask out compression flag from String's array length.
2444 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002445 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002446 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002447}
2448
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002449void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002450 Primitive::Type value_type = instruction->GetComponentType();
2451
2452 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002453 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2454 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002455 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002456 LocationSummary::kCallOnSlowPath :
2457 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002458 locations->SetInAt(0, Location::RequiresRegister());
2459 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002460 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2461 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2462 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002463 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002464 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002465 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002466 }
2467}
2468
2469void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2470 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002471 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002472 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002473 bool needs_write_barrier =
2474 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002475
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002476 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002477 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002478 CPURegister source = value;
2479 Location index = locations->InAt(1);
2480 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2481 MemOperand destination = HeapOperand(array);
2482 MacroAssembler* masm = GetVIXLAssembler();
2483 BlockPoolsScope block_pools(masm);
2484
2485 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002486 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002487 if (index.IsConstant()) {
2488 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2489 destination = HeapOperand(array, offset);
2490 } else {
2491 UseScratchRegisterScope temps(masm);
2492 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002493 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002494 // We do not need to compute the intermediate address from the array: the
2495 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002496 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002497 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002498 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002499 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2500 }
2501 temp = array;
2502 } else {
2503 __ Add(temp, array, offset);
2504 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002505 destination = HeapOperand(temp,
2506 XRegisterFrom(index),
2507 LSL,
2508 Primitive::ComponentSizeShift(value_type));
2509 }
2510 codegen_->Store(value_type, value, destination);
2511 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002512 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002513 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002514 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002515 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002516 {
2517 // We use a block to end the scratch scope before the write barrier, thus
2518 // freeing the temporary registers so they can be used in `MarkGCCard`.
2519 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002520 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002521 if (index.IsConstant()) {
2522 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002523 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002524 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002525 destination = HeapOperand(temp,
2526 XRegisterFrom(index),
2527 LSL,
2528 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002529 }
2530
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002531 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2532 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2533 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2534
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002535 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002536 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2537 codegen_->AddSlowPath(slow_path);
2538 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002539 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002540 __ Cbnz(Register(value), &non_zero);
2541 if (!index.IsConstant()) {
2542 __ Add(temp, array, offset);
2543 }
2544 __ Str(wzr, destination);
2545 codegen_->MaybeRecordImplicitNullCheck(instruction);
2546 __ B(&done);
2547 __ Bind(&non_zero);
2548 }
2549
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002550 // Note that when Baker read barriers are enabled, the type
2551 // checks are performed without read barriers. This is fine,
2552 // even in the case where a class object is in the from-space
2553 // after the flip, as a comparison involving such a type would
2554 // not produce a false positive; it may of course produce a
2555 // false negative, in which case we would take the ArraySet
2556 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002557
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002558 Register temp2 = temps.AcquireSameSizeAs(array);
2559 // /* HeapReference<Class> */ temp = array->klass_
2560 __ Ldr(temp, HeapOperand(array, class_offset));
2561 codegen_->MaybeRecordImplicitNullCheck(instruction);
2562 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002563
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002564 // /* HeapReference<Class> */ temp = temp->component_type_
2565 __ Ldr(temp, HeapOperand(temp, component_offset));
2566 // /* HeapReference<Class> */ temp2 = value->klass_
2567 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2568 // If heap poisoning is enabled, no need to unpoison `temp`
2569 // nor `temp2`, as we are comparing two poisoned references.
2570 __ Cmp(temp, temp2);
2571 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002572
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002573 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2574 vixl::aarch64::Label do_put;
2575 __ B(eq, &do_put);
2576 // If heap poisoning is enabled, the `temp` reference has
2577 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002578 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2579
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002580 // /* HeapReference<Class> */ temp = temp->super_class_
2581 __ Ldr(temp, HeapOperand(temp, super_offset));
2582 // If heap poisoning is enabled, no need to unpoison
2583 // `temp`, as we are comparing against null below.
2584 __ Cbnz(temp, slow_path->GetEntryLabel());
2585 __ Bind(&do_put);
2586 } else {
2587 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002588 }
2589 }
2590
2591 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002592 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002593 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002594 __ Mov(temp2, value.W());
2595 GetAssembler()->PoisonHeapReference(temp2);
2596 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002597 }
2598
2599 if (!index.IsConstant()) {
2600 __ Add(temp, array, offset);
2601 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002602 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002603
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002604 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002605 codegen_->MaybeRecordImplicitNullCheck(instruction);
2606 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002607 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002608
2609 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2610
2611 if (done.IsLinked()) {
2612 __ Bind(&done);
2613 }
2614
2615 if (slow_path != nullptr) {
2616 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002617 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002618 }
2619}
2620
Alexandre Rames67555f72014-11-18 10:55:16 +00002621void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002622 RegisterSet caller_saves = RegisterSet::Empty();
2623 InvokeRuntimeCallingConvention calling_convention;
2624 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2625 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2626 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002627 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002628 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002629}
2630
2631void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002632 BoundsCheckSlowPathARM64* slow_path =
2633 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002634 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002635 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2636 __ B(slow_path->GetEntryLabel(), hs);
2637}
2638
Alexandre Rames67555f72014-11-18 10:55:16 +00002639void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2640 LocationSummary* locations =
2641 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2642 locations->SetInAt(0, Location::RequiresRegister());
2643 if (check->HasUses()) {
2644 locations->SetOut(Location::SameAsFirstInput());
2645 }
2646}
2647
2648void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2649 // We assume the class is not null.
2650 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2651 check->GetLoadClass(), check, check->GetDexPc(), true);
2652 codegen_->AddSlowPath(slow_path);
2653 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2654}
2655
Roland Levillain1a653882016-03-18 18:05:57 +00002656static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2657 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2658 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2659}
2660
2661void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2662 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2663 Location rhs_loc = instruction->GetLocations()->InAt(1);
2664 if (rhs_loc.IsConstant()) {
2665 // 0.0 is the only immediate that can be encoded directly in
2666 // an FCMP instruction.
2667 //
2668 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2669 // specify that in a floating-point comparison, positive zero
2670 // and negative zero are considered equal, so we can use the
2671 // literal 0.0 for both cases here.
2672 //
2673 // Note however that some methods (Float.equal, Float.compare,
2674 // Float.compareTo, Double.equal, Double.compare,
2675 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2676 // StrictMath.min) consider 0.0 to be (strictly) greater than
2677 // -0.0. So if we ever translate calls to these methods into a
2678 // HCompare instruction, we must handle the -0.0 case with
2679 // care here.
2680 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2681 __ Fcmp(lhs_reg, 0.0);
2682 } else {
2683 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2684 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002685}
2686
Serban Constantinescu02164b32014-11-13 14:05:07 +00002687void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002688 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002689 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2690 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002691 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002692 case Primitive::kPrimBoolean:
2693 case Primitive::kPrimByte:
2694 case Primitive::kPrimShort:
2695 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002696 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002697 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002698 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002699 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002700 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2701 break;
2702 }
2703 case Primitive::kPrimFloat:
2704 case Primitive::kPrimDouble: {
2705 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002706 locations->SetInAt(1,
2707 IsFloatingPointZeroConstant(compare->InputAt(1))
2708 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2709 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002710 locations->SetOut(Location::RequiresRegister());
2711 break;
2712 }
2713 default:
2714 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2715 }
2716}
2717
2718void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2719 Primitive::Type in_type = compare->InputAt(0)->GetType();
2720
2721 // 0 if: left == right
2722 // 1 if: left > right
2723 // -1 if: left < right
2724 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002725 case Primitive::kPrimBoolean:
2726 case Primitive::kPrimByte:
2727 case Primitive::kPrimShort:
2728 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002729 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002730 case Primitive::kPrimLong: {
2731 Register result = OutputRegister(compare);
2732 Register left = InputRegisterAt(compare, 0);
2733 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002734 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002735 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2736 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002737 break;
2738 }
2739 case Primitive::kPrimFloat:
2740 case Primitive::kPrimDouble: {
2741 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002742 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002743 __ Cset(result, ne);
2744 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002745 break;
2746 }
2747 default:
2748 LOG(FATAL) << "Unimplemented compare type " << in_type;
2749 }
2750}
2751
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002752void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002753 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002754
2755 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2756 locations->SetInAt(0, Location::RequiresFpuRegister());
2757 locations->SetInAt(1,
2758 IsFloatingPointZeroConstant(instruction->InputAt(1))
2759 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2760 : Location::RequiresFpuRegister());
2761 } else {
2762 // Integer cases.
2763 locations->SetInAt(0, Location::RequiresRegister());
2764 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2765 }
2766
David Brazdilb3e773e2016-01-26 11:28:37 +00002767 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002769 }
2770}
2771
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002772void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002773 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002774 return;
2775 }
2776
2777 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002778 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002779 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002780
Roland Levillain7f63c522015-07-13 15:54:55 +00002781 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002782 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002783 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002784 } else {
2785 // Integer cases.
2786 Register lhs = InputRegisterAt(instruction, 0);
2787 Operand rhs = InputOperandAt(instruction, 1);
2788 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002789 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002790 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002791}
2792
2793#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2794 M(Equal) \
2795 M(NotEqual) \
2796 M(LessThan) \
2797 M(LessThanOrEqual) \
2798 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002799 M(GreaterThanOrEqual) \
2800 M(Below) \
2801 M(BelowOrEqual) \
2802 M(Above) \
2803 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002804#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002805void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2806void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002807FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002808#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002809#undef FOR_EACH_CONDITION_INSTRUCTION
2810
Zheng Xuc6667102015-05-15 16:08:45 +08002811void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2812 DCHECK(instruction->IsDiv() || instruction->IsRem());
2813
2814 LocationSummary* locations = instruction->GetLocations();
2815 Location second = locations->InAt(1);
2816 DCHECK(second.IsConstant());
2817
2818 Register out = OutputRegister(instruction);
2819 Register dividend = InputRegisterAt(instruction, 0);
2820 int64_t imm = Int64FromConstant(second.GetConstant());
2821 DCHECK(imm == 1 || imm == -1);
2822
2823 if (instruction->IsRem()) {
2824 __ Mov(out, 0);
2825 } else {
2826 if (imm == 1) {
2827 __ Mov(out, dividend);
2828 } else {
2829 __ Neg(out, dividend);
2830 }
2831 }
2832}
2833
2834void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2835 DCHECK(instruction->IsDiv() || instruction->IsRem());
2836
2837 LocationSummary* locations = instruction->GetLocations();
2838 Location second = locations->InAt(1);
2839 DCHECK(second.IsConstant());
2840
2841 Register out = OutputRegister(instruction);
2842 Register dividend = InputRegisterAt(instruction, 0);
2843 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002844 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002845 int ctz_imm = CTZ(abs_imm);
2846
2847 UseScratchRegisterScope temps(GetVIXLAssembler());
2848 Register temp = temps.AcquireSameSizeAs(out);
2849
2850 if (instruction->IsDiv()) {
2851 __ Add(temp, dividend, abs_imm - 1);
2852 __ Cmp(dividend, 0);
2853 __ Csel(out, temp, dividend, lt);
2854 if (imm > 0) {
2855 __ Asr(out, out, ctz_imm);
2856 } else {
2857 __ Neg(out, Operand(out, ASR, ctz_imm));
2858 }
2859 } else {
2860 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2861 __ Asr(temp, dividend, bits - 1);
2862 __ Lsr(temp, temp, bits - ctz_imm);
2863 __ Add(out, dividend, temp);
2864 __ And(out, out, abs_imm - 1);
2865 __ Sub(out, out, temp);
2866 }
2867}
2868
2869void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2870 DCHECK(instruction->IsDiv() || instruction->IsRem());
2871
2872 LocationSummary* locations = instruction->GetLocations();
2873 Location second = locations->InAt(1);
2874 DCHECK(second.IsConstant());
2875
2876 Register out = OutputRegister(instruction);
2877 Register dividend = InputRegisterAt(instruction, 0);
2878 int64_t imm = Int64FromConstant(second.GetConstant());
2879
2880 Primitive::Type type = instruction->GetResultType();
2881 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2882
2883 int64_t magic;
2884 int shift;
2885 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2886
2887 UseScratchRegisterScope temps(GetVIXLAssembler());
2888 Register temp = temps.AcquireSameSizeAs(out);
2889
2890 // temp = get_high(dividend * magic)
2891 __ Mov(temp, magic);
2892 if (type == Primitive::kPrimLong) {
2893 __ Smulh(temp, dividend, temp);
2894 } else {
2895 __ Smull(temp.X(), dividend, temp);
2896 __ Lsr(temp.X(), temp.X(), 32);
2897 }
2898
2899 if (imm > 0 && magic < 0) {
2900 __ Add(temp, temp, dividend);
2901 } else if (imm < 0 && magic > 0) {
2902 __ Sub(temp, temp, dividend);
2903 }
2904
2905 if (shift != 0) {
2906 __ Asr(temp, temp, shift);
2907 }
2908
2909 if (instruction->IsDiv()) {
2910 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2911 } else {
2912 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2913 // TODO: Strength reduction for msub.
2914 Register temp_imm = temps.AcquireSameSizeAs(out);
2915 __ Mov(temp_imm, imm);
2916 __ Msub(out, temp, temp_imm, dividend);
2917 }
2918}
2919
2920void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2921 DCHECK(instruction->IsDiv() || instruction->IsRem());
2922 Primitive::Type type = instruction->GetResultType();
2923 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2924
2925 LocationSummary* locations = instruction->GetLocations();
2926 Register out = OutputRegister(instruction);
2927 Location second = locations->InAt(1);
2928
2929 if (second.IsConstant()) {
2930 int64_t imm = Int64FromConstant(second.GetConstant());
2931
2932 if (imm == 0) {
2933 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2934 } else if (imm == 1 || imm == -1) {
2935 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002936 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002937 DivRemByPowerOfTwo(instruction);
2938 } else {
2939 DCHECK(imm <= -2 || imm >= 2);
2940 GenerateDivRemWithAnyConstant(instruction);
2941 }
2942 } else {
2943 Register dividend = InputRegisterAt(instruction, 0);
2944 Register divisor = InputRegisterAt(instruction, 1);
2945 if (instruction->IsDiv()) {
2946 __ Sdiv(out, dividend, divisor);
2947 } else {
2948 UseScratchRegisterScope temps(GetVIXLAssembler());
2949 Register temp = temps.AcquireSameSizeAs(out);
2950 __ Sdiv(temp, dividend, divisor);
2951 __ Msub(out, temp, divisor, dividend);
2952 }
2953 }
2954}
2955
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002956void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2957 LocationSummary* locations =
2958 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2959 switch (div->GetResultType()) {
2960 case Primitive::kPrimInt:
2961 case Primitive::kPrimLong:
2962 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002963 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2965 break;
2966
2967 case Primitive::kPrimFloat:
2968 case Primitive::kPrimDouble:
2969 locations->SetInAt(0, Location::RequiresFpuRegister());
2970 locations->SetInAt(1, Location::RequiresFpuRegister());
2971 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2972 break;
2973
2974 default:
2975 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2976 }
2977}
2978
2979void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2980 Primitive::Type type = div->GetResultType();
2981 switch (type) {
2982 case Primitive::kPrimInt:
2983 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002984 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002985 break;
2986
2987 case Primitive::kPrimFloat:
2988 case Primitive::kPrimDouble:
2989 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2990 break;
2991
2992 default:
2993 LOG(FATAL) << "Unexpected div type " << type;
2994 }
2995}
2996
Alexandre Rames67555f72014-11-18 10:55:16 +00002997void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002998 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002999 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003000}
3001
3002void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3003 SlowPathCodeARM64* slow_path =
3004 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3005 codegen_->AddSlowPath(slow_path);
3006 Location value = instruction->GetLocations()->InAt(0);
3007
Alexandre Rames3e69f162014-12-10 10:36:50 +00003008 Primitive::Type type = instruction->GetType();
3009
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003010 if (!Primitive::IsIntegralType(type)) {
3011 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003012 return;
3013 }
3014
Alexandre Rames67555f72014-11-18 10:55:16 +00003015 if (value.IsConstant()) {
3016 int64_t divisor = Int64ConstantFrom(value);
3017 if (divisor == 0) {
3018 __ B(slow_path->GetEntryLabel());
3019 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003020 // A division by a non-null constant is valid. We don't need to perform
3021 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003022 }
3023 } else {
3024 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3025 }
3026}
3027
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003028void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3029 LocationSummary* locations =
3030 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3031 locations->SetOut(Location::ConstantLocation(constant));
3032}
3033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003034void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3035 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003036 // Will be generated at use site.
3037}
3038
Alexandre Rames5319def2014-10-23 10:03:10 +01003039void LocationsBuilderARM64::VisitExit(HExit* exit) {
3040 exit->SetLocations(nullptr);
3041}
3042
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003043void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003044}
3045
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003046void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3047 LocationSummary* locations =
3048 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3049 locations->SetOut(Location::ConstantLocation(constant));
3050}
3051
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003052void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003053 // Will be generated at use site.
3054}
3055
David Brazdilfc6a86a2015-06-26 10:33:45 +00003056void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003057 DCHECK(!successor->IsExitBlock());
3058 HBasicBlock* block = got->GetBlock();
3059 HInstruction* previous = got->GetPrevious();
3060 HLoopInformation* info = block->GetLoopInformation();
3061
David Brazdil46e2a392015-03-16 17:31:52 +00003062 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003063 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3064 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3065 return;
3066 }
3067 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3068 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3069 }
3070 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003071 __ B(codegen_->GetLabelOf(successor));
3072 }
3073}
3074
David Brazdilfc6a86a2015-06-26 10:33:45 +00003075void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3076 got->SetLocations(nullptr);
3077}
3078
3079void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3080 HandleGoto(got, got->GetSuccessor());
3081}
3082
3083void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3084 try_boundary->SetLocations(nullptr);
3085}
3086
3087void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3088 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3089 if (!successor->IsExitBlock()) {
3090 HandleGoto(try_boundary, successor);
3091 }
3092}
3093
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003094void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003095 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003096 vixl::aarch64::Label* true_target,
3097 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003098 // FP branching requires both targets to be explicit. If either of the targets
3099 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003100 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003101 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003102
David Brazdil0debae72015-11-12 18:37:00 +00003103 if (true_target == nullptr && false_target == nullptr) {
3104 // Nothing to do. The code always falls through.
3105 return;
3106 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003107 // Constant condition, statically compared against "true" (integer value 1).
3108 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003109 if (true_target != nullptr) {
3110 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003111 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003112 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003113 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003114 if (false_target != nullptr) {
3115 __ B(false_target);
3116 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003117 }
David Brazdil0debae72015-11-12 18:37:00 +00003118 return;
3119 }
3120
3121 // The following code generates these patterns:
3122 // (1) true_target == nullptr && false_target != nullptr
3123 // - opposite condition true => branch to false_target
3124 // (2) true_target != nullptr && false_target == nullptr
3125 // - condition true => branch to true_target
3126 // (3) true_target != nullptr && false_target != nullptr
3127 // - condition true => branch to true_target
3128 // - branch to false_target
3129 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003130 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003131 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003132 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003133 if (true_target == nullptr) {
3134 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3135 } else {
3136 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3137 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003138 } else {
3139 // The condition instruction has not been materialized, use its inputs as
3140 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003141 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003142
David Brazdil0debae72015-11-12 18:37:00 +00003143 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003144 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003145 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003146 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003147 IfCondition opposite_condition = condition->GetOppositeCondition();
3148 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003149 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003150 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003151 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003152 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003153 // Integer cases.
3154 Register lhs = InputRegisterAt(condition, 0);
3155 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003156
3157 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003158 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003159 if (true_target == nullptr) {
3160 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3161 non_fallthrough_target = false_target;
3162 } else {
3163 arm64_cond = ARM64Condition(condition->GetCondition());
3164 non_fallthrough_target = true_target;
3165 }
3166
Aart Bik086d27e2016-01-20 17:02:00 -08003167 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003168 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003169 switch (arm64_cond) {
3170 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003171 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003172 break;
3173 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003174 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003175 break;
3176 case lt:
3177 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003178 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003179 break;
3180 case ge:
3181 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003182 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003183 break;
3184 default:
3185 // Without the `static_cast` the compiler throws an error for
3186 // `-Werror=sign-promo`.
3187 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3188 }
3189 } else {
3190 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003191 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003192 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003193 }
3194 }
David Brazdil0debae72015-11-12 18:37:00 +00003195
3196 // If neither branch falls through (case 3), the conditional branch to `true_target`
3197 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3198 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003199 __ B(false_target);
3200 }
David Brazdil0debae72015-11-12 18:37:00 +00003201
3202 if (fallthrough_target.IsLinked()) {
3203 __ Bind(&fallthrough_target);
3204 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003205}
3206
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003207void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3208 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003209 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003210 locations->SetInAt(0, Location::RequiresRegister());
3211 }
3212}
3213
3214void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003215 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3216 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003217 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3218 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3219 true_target = nullptr;
3220 }
3221 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3222 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3223 false_target = nullptr;
3224 }
David Brazdil0debae72015-11-12 18:37:00 +00003225 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003226}
3227
3228void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3229 LocationSummary* locations = new (GetGraph()->GetArena())
3230 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003231 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003232 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003233 locations->SetInAt(0, Location::RequiresRegister());
3234 }
3235}
3236
3237void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003238 SlowPathCodeARM64* slow_path =
3239 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003240 GenerateTestAndBranch(deoptimize,
3241 /* condition_input_index */ 0,
3242 slow_path->GetEntryLabel(),
3243 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003244}
3245
Mingyao Yang063fc772016-08-02 11:02:54 -07003246void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3247 LocationSummary* locations = new (GetGraph()->GetArena())
3248 LocationSummary(flag, LocationSummary::kNoCall);
3249 locations->SetOut(Location::RequiresRegister());
3250}
3251
3252void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3253 __ Ldr(OutputRegister(flag),
3254 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3255}
3256
David Brazdilc0b601b2016-02-08 14:20:45 +00003257static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3258 return condition->IsCondition() &&
3259 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3260}
3261
Alexandre Rames880f1192016-06-13 16:04:50 +01003262static inline Condition GetConditionForSelect(HCondition* condition) {
3263 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003264 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3265 : ARM64Condition(cond);
3266}
3267
David Brazdil74eb1b22015-12-14 11:44:01 +00003268void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003270 if (Primitive::IsFloatingPointType(select->GetType())) {
3271 locations->SetInAt(0, Location::RequiresFpuRegister());
3272 locations->SetInAt(1, Location::RequiresFpuRegister());
3273 locations->SetOut(Location::RequiresFpuRegister());
3274 } else {
3275 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3276 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3277 bool is_true_value_constant = cst_true_value != nullptr;
3278 bool is_false_value_constant = cst_false_value != nullptr;
3279 // Ask VIXL whether we should synthesize constants in registers.
3280 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3281 Operand true_op = is_true_value_constant ?
3282 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3283 Operand false_op = is_false_value_constant ?
3284 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3285 bool true_value_in_register = false;
3286 bool false_value_in_register = false;
3287 MacroAssembler::GetCselSynthesisInformation(
3288 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3289 true_value_in_register |= !is_true_value_constant;
3290 false_value_in_register |= !is_false_value_constant;
3291
3292 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3293 : Location::ConstantLocation(cst_true_value));
3294 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3295 : Location::ConstantLocation(cst_false_value));
3296 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003297 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003298
David Brazdil74eb1b22015-12-14 11:44:01 +00003299 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3300 locations->SetInAt(2, Location::RequiresRegister());
3301 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003302}
3303
3304void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003305 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003306 Condition csel_cond;
3307
3308 if (IsBooleanValueOrMaterializedCondition(cond)) {
3309 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003310 // Use the condition flags set by the previous instruction.
3311 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003312 } else {
3313 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003314 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003315 }
3316 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003317 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003318 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003319 } else {
3320 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003321 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003322 }
3323
Alexandre Rames880f1192016-06-13 16:04:50 +01003324 if (Primitive::IsFloatingPointType(select->GetType())) {
3325 __ Fcsel(OutputFPRegister(select),
3326 InputFPRegisterAt(select, 1),
3327 InputFPRegisterAt(select, 0),
3328 csel_cond);
3329 } else {
3330 __ Csel(OutputRegister(select),
3331 InputOperandAt(select, 1),
3332 InputOperandAt(select, 0),
3333 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003334 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003335}
3336
David Srbecky0cf44932015-12-09 14:09:59 +00003337void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3338 new (GetGraph()->GetArena()) LocationSummary(info);
3339}
3340
David Srbeckyd28f4a02016-03-14 17:14:24 +00003341void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3342 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003343}
3344
3345void CodeGeneratorARM64::GenerateNop() {
3346 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003347}
3348
Alexandre Rames5319def2014-10-23 10:03:10 +01003349void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003350 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003351}
3352
3353void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003354 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003355}
3356
3357void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003358 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003359}
3360
3361void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003362 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003363}
3364
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003365// Temp is used for read barrier.
3366static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3367 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003368 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003369 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3370 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3371 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3372 return 1;
3373 }
3374 return 0;
3375}
3376
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003377// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003378// interface pointer, one for loading the current interface.
3379// The other checks have one temp for loading the object's class.
3380static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3381 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3382 return 3;
3383 }
3384 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003385}
3386
Alexandre Rames67555f72014-11-18 10:55:16 +00003387void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003388 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003390 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003392 case TypeCheckKind::kExactCheck:
3393 case TypeCheckKind::kAbstractClassCheck:
3394 case TypeCheckKind::kClassHierarchyCheck:
3395 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396 call_kind =
3397 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003398 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003399 break;
3400 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003401 case TypeCheckKind::kUnresolvedCheck:
3402 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003403 call_kind = LocationSummary::kCallOnSlowPath;
3404 break;
3405 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003406
Alexandre Rames67555f72014-11-18 10:55:16 +00003407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003408 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003409 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003410 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003411 locations->SetInAt(0, Location::RequiresRegister());
3412 locations->SetInAt(1, Location::RequiresRegister());
3413 // The "out" register is used as a temporary, so it overlaps with the inputs.
3414 // Note that TypeCheckSlowPathARM64 uses this register too.
3415 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003416 // Add temps if necessary for read barriers.
3417 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003418}
3419
3420void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003421 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003422 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003424 Register obj = InputRegisterAt(instruction, 0);
3425 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003427 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003428 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3429 DCHECK_LE(num_temps, 1u);
3430 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3432 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3433 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3434 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003435
Scott Wakeling97c72b72016-06-24 16:19:36 +01003436 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003437 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003438
3439 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003440 // Avoid null check if we know `obj` is not null.
3441 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003442 __ Cbz(obj, &zero);
3443 }
3444
Roland Levillain44015862016-01-22 11:47:17 +00003445 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003446 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003447 // /* HeapReference<Class> */ out = obj->klass_
3448 GenerateReferenceLoadTwoRegisters(instruction,
3449 out_loc,
3450 obj_loc,
3451 class_offset,
3452 maybe_temp_loc,
3453 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003454 __ Cmp(out, cls);
3455 __ Cset(out, eq);
3456 if (zero.IsLinked()) {
3457 __ B(&done);
3458 }
3459 break;
3460 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003462 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003463 // /* HeapReference<Class> */ out = obj->klass_
3464 GenerateReferenceLoadTwoRegisters(instruction,
3465 out_loc,
3466 obj_loc,
3467 class_offset,
3468 maybe_temp_loc,
3469 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003470 // If the class is abstract, we eagerly fetch the super class of the
3471 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003472 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003473 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003474 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003475 GenerateReferenceLoadOneRegister(instruction,
3476 out_loc,
3477 super_offset,
3478 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003479 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003480 // If `out` is null, we use it for the result, and jump to `done`.
3481 __ Cbz(out, &done);
3482 __ Cmp(out, cls);
3483 __ B(ne, &loop);
3484 __ Mov(out, 1);
3485 if (zero.IsLinked()) {
3486 __ B(&done);
3487 }
3488 break;
3489 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003490
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003491 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003492 // /* HeapReference<Class> */ out = obj->klass_
3493 GenerateReferenceLoadTwoRegisters(instruction,
3494 out_loc,
3495 obj_loc,
3496 class_offset,
3497 maybe_temp_loc,
3498 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003499 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003500 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003501 __ Bind(&loop);
3502 __ Cmp(out, cls);
3503 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003504 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003505 GenerateReferenceLoadOneRegister(instruction,
3506 out_loc,
3507 super_offset,
3508 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003509 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003510 __ Cbnz(out, &loop);
3511 // If `out` is null, we use it for the result, and jump to `done`.
3512 __ B(&done);
3513 __ Bind(&success);
3514 __ Mov(out, 1);
3515 if (zero.IsLinked()) {
3516 __ B(&done);
3517 }
3518 break;
3519 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003520
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003521 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003522 // /* HeapReference<Class> */ out = obj->klass_
3523 GenerateReferenceLoadTwoRegisters(instruction,
3524 out_loc,
3525 obj_loc,
3526 class_offset,
3527 maybe_temp_loc,
3528 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003529 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003530 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003531 __ Cmp(out, cls);
3532 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003533 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003534 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003535 GenerateReferenceLoadOneRegister(instruction,
3536 out_loc,
3537 component_offset,
3538 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003539 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003540 // If `out` is null, we use it for the result, and jump to `done`.
3541 __ Cbz(out, &done);
3542 __ Ldrh(out, HeapOperand(out, primitive_offset));
3543 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3544 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003545 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003546 __ Mov(out, 1);
3547 __ B(&done);
3548 break;
3549 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003550
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003551 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003552 // No read barrier since the slow path will retry upon failure.
3553 // /* HeapReference<Class> */ out = obj->klass_
3554 GenerateReferenceLoadTwoRegisters(instruction,
3555 out_loc,
3556 obj_loc,
3557 class_offset,
3558 maybe_temp_loc,
3559 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003560 __ Cmp(out, cls);
3561 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003562 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3563 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003564 codegen_->AddSlowPath(slow_path);
3565 __ B(ne, slow_path->GetEntryLabel());
3566 __ Mov(out, 1);
3567 if (zero.IsLinked()) {
3568 __ B(&done);
3569 }
3570 break;
3571 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003572
Calin Juravle98893e12015-10-02 21:05:03 +01003573 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003574 case TypeCheckKind::kInterfaceCheck: {
3575 // Note that we indeed only call on slow path, but we always go
3576 // into the slow path for the unresolved and interface check
3577 // cases.
3578 //
3579 // We cannot directly call the InstanceofNonTrivial runtime
3580 // entry point without resorting to a type checking slow path
3581 // here (i.e. by calling InvokeRuntime directly), as it would
3582 // require to assign fixed registers for the inputs of this
3583 // HInstanceOf instruction (following the runtime calling
3584 // convention), which might be cluttered by the potential first
3585 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003586 //
3587 // TODO: Introduce a new runtime entry point taking the object
3588 // to test (instead of its class) as argument, and let it deal
3589 // with the read barrier issues. This will let us refactor this
3590 // case of the `switch` code as it was previously (with a direct
3591 // call to the runtime not using a type checking slow path).
3592 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003593 DCHECK(locations->OnlyCallsOnSlowPath());
3594 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3595 /* is_fatal */ false);
3596 codegen_->AddSlowPath(slow_path);
3597 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003598 if (zero.IsLinked()) {
3599 __ B(&done);
3600 }
3601 break;
3602 }
3603 }
3604
3605 if (zero.IsLinked()) {
3606 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003607 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003608 }
3609
3610 if (done.IsLinked()) {
3611 __ Bind(&done);
3612 }
3613
3614 if (slow_path != nullptr) {
3615 __ Bind(slow_path->GetExitLabel());
3616 }
3617}
3618
3619void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3620 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3621 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3622
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003623 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3624 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003625 case TypeCheckKind::kExactCheck:
3626 case TypeCheckKind::kAbstractClassCheck:
3627 case TypeCheckKind::kClassHierarchyCheck:
3628 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003629 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3630 LocationSummary::kCallOnSlowPath :
3631 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003632 break;
3633 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003634 case TypeCheckKind::kUnresolvedCheck:
3635 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003636 call_kind = LocationSummary::kCallOnSlowPath;
3637 break;
3638 }
3639
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3641 locations->SetInAt(0, Location::RequiresRegister());
3642 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003643 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3644 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003645}
3646
3647void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003648 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003649 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003650 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003651 Register obj = InputRegisterAt(instruction, 0);
3652 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003653 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3654 DCHECK_GE(num_temps, 1u);
3655 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003656 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003657 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3658 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003659 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003660 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3661 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3662 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3663 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3664 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3665 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3666 const uint32_t object_array_data_offset =
3667 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003668
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003669 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003670 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3671 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3672 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003673 if (!kEmitCompilerReadBarrier) {
3674 is_type_check_slow_path_fatal =
3675 (type_check_kind == TypeCheckKind::kExactCheck ||
3676 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3677 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3678 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3679 !instruction->CanThrowIntoCatchBlock();
3680 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003681 SlowPathCodeARM64* type_check_slow_path =
3682 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3683 is_type_check_slow_path_fatal);
3684 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003685
Scott Wakeling97c72b72016-06-24 16:19:36 +01003686 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003687 // Avoid null check if we know obj is not null.
3688 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003689 __ Cbz(obj, &done);
3690 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003691
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003692 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003693 case TypeCheckKind::kExactCheck:
3694 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003695 // /* HeapReference<Class> */ temp = obj->klass_
3696 GenerateReferenceLoadTwoRegisters(instruction,
3697 temp_loc,
3698 obj_loc,
3699 class_offset,
3700 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003701 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003702
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003703 __ Cmp(temp, cls);
3704 // Jump to slow path for throwing the exception or doing a
3705 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003706 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003707 break;
3708 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003709
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003710 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003711 // /* HeapReference<Class> */ temp = obj->klass_
3712 GenerateReferenceLoadTwoRegisters(instruction,
3713 temp_loc,
3714 obj_loc,
3715 class_offset,
3716 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003717 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003718
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003719 // If the class is abstract, we eagerly fetch the super class of the
3720 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003721 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003722 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003723 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003724 GenerateReferenceLoadOneRegister(instruction,
3725 temp_loc,
3726 super_offset,
3727 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003728 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003729
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003730 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3731 // exception.
3732 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3733 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003734 __ Cmp(temp, cls);
3735 __ B(ne, &loop);
3736 break;
3737 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003738
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003739 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003740 // /* HeapReference<Class> */ temp = obj->klass_
3741 GenerateReferenceLoadTwoRegisters(instruction,
3742 temp_loc,
3743 obj_loc,
3744 class_offset,
3745 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003746 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003747
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003748 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003749 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003750 __ Bind(&loop);
3751 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003752 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003753
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003754 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003755 GenerateReferenceLoadOneRegister(instruction,
3756 temp_loc,
3757 super_offset,
3758 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003759 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003760
3761 // If the class reference currently in `temp` is not null, jump
3762 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003763 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003764 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003765 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003766 break;
3767 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003768
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003769 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003770 // /* HeapReference<Class> */ temp = obj->klass_
3771 GenerateReferenceLoadTwoRegisters(instruction,
3772 temp_loc,
3773 obj_loc,
3774 class_offset,
3775 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003776 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003777
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003778 // Do an exact check.
3779 __ Cmp(temp, cls);
3780 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003781
3782 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003783 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003784 GenerateReferenceLoadOneRegister(instruction,
3785 temp_loc,
3786 component_offset,
3787 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003788 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003789
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003790 // If the component type is null, jump to the slow path to throw the exception.
3791 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3792 // Otherwise, the object is indeed an array. Further check that this component type is not a
3793 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003794 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3795 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003796 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003797 break;
3798 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003799
Calin Juravle98893e12015-10-02 21:05:03 +01003800 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003801 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003802 //
3803 // We cannot directly call the CheckCast runtime entry point
3804 // without resorting to a type checking slow path here (i.e. by
3805 // calling InvokeRuntime directly), as it would require to
3806 // assign fixed registers for the inputs of this HInstanceOf
3807 // instruction (following the runtime calling convention), which
3808 // might be cluttered by the potential first read barrier
3809 // emission at the beginning of this method.
3810 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003811 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003812 case TypeCheckKind::kInterfaceCheck: {
3813 // /* HeapReference<Class> */ temp = obj->klass_
3814 GenerateReferenceLoadTwoRegisters(instruction,
3815 temp_loc,
3816 obj_loc,
3817 class_offset,
3818 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003819 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003820
3821 // /* HeapReference<Class> */ temp = temp->iftable_
3822 GenerateReferenceLoadTwoRegisters(instruction,
3823 temp_loc,
3824 temp_loc,
3825 iftable_offset,
3826 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003827 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003828 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003829 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003830 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003831 vixl::aarch64::Label start_loop;
3832 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003833 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003834 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3835 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003836 // Go to next interface.
3837 __ Add(temp, temp, 2 * kHeapReferenceSize);
3838 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003839 // Compare the classes and continue the loop if they do not match.
3840 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3841 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003842 break;
3843 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003844 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003845 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003847 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003848}
3849
Alexandre Rames5319def2014-10-23 10:03:10 +01003850void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3851 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3852 locations->SetOut(Location::ConstantLocation(constant));
3853}
3854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003855void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003856 // Will be generated at use site.
3857}
3858
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003859void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3860 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3861 locations->SetOut(Location::ConstantLocation(constant));
3862}
3863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003864void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003865 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003866}
3867
Calin Juravle175dc732015-08-25 15:42:32 +01003868void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3869 // The trampoline uses the same calling convention as dex calling conventions,
3870 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3871 // the method_idx.
3872 HandleInvoke(invoke);
3873}
3874
3875void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3876 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3877}
3878
Alexandre Rames5319def2014-10-23 10:03:10 +01003879void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003880 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003881 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003882}
3883
Alexandre Rames67555f72014-11-18 10:55:16 +00003884void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3885 HandleInvoke(invoke);
3886}
3887
3888void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3889 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003890 LocationSummary* locations = invoke->GetLocations();
3891 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003892 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003893 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003894 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003895
3896 // The register ip1 is required to be used for the hidden argument in
3897 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003898 MacroAssembler* masm = GetVIXLAssembler();
3899 UseScratchRegisterScope scratch_scope(masm);
3900 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003901 scratch_scope.Exclude(ip1);
3902 __ Mov(ip1, invoke->GetDexMethodIndex());
3903
Alexandre Rames67555f72014-11-18 10:55:16 +00003904 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003905 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003906 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003907 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003908 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003909 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003910 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003911 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003912 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003913 // Instead of simply (possibly) unpoisoning `temp` here, we should
3914 // emit a read barrier for the previous class reference load.
3915 // However this is not required in practice, as this is an
3916 // intermediate/temporary reference and because the current
3917 // concurrent copying collector keeps the from-space memory
3918 // intact/accessible until the end of the marking phase (the
3919 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003920 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003921 __ Ldr(temp,
3922 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3923 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003924 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003925 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003926 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003927 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003928 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003929 // lr();
3930 __ Blr(lr);
3931 DCHECK(!codegen_->IsLeafMethod());
3932 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3933}
3934
3935void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003936 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3937 if (intrinsic.TryDispatch(invoke)) {
3938 return;
3939 }
3940
Alexandre Rames67555f72014-11-18 10:55:16 +00003941 HandleInvoke(invoke);
3942}
3943
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003944void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003945 // Explicit clinit checks triggered by static invokes must have been pruned by
3946 // art::PrepareForRegisterAllocation.
3947 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003948
Andreas Gampe878d58c2015-01-15 23:24:00 -08003949 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3950 if (intrinsic.TryDispatch(invoke)) {
3951 return;
3952 }
3953
Alexandre Rames67555f72014-11-18 10:55:16 +00003954 HandleInvoke(invoke);
3955}
3956
Andreas Gampe878d58c2015-01-15 23:24:00 -08003957static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3958 if (invoke->GetLocations()->Intrinsified()) {
3959 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3960 intrinsic.Dispatch(invoke);
3961 return true;
3962 }
3963 return false;
3964}
3965
Vladimir Markodc151b22015-10-15 18:02:30 +01003966HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3967 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003968 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003969 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003970 return desired_dispatch_info;
3971}
3972
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003973void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003974 // For better instruction scheduling we load the direct code pointer before the method pointer.
3975 bool direct_code_loaded = false;
3976 switch (invoke->GetCodePtrLocation()) {
3977 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3978 // LR = code address from literal pool with link-time patch.
3979 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3980 direct_code_loaded = true;
3981 break;
3982 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3983 // LR = invoke->GetDirectCodePtr();
3984 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3985 direct_code_loaded = true;
3986 break;
3987 default:
3988 break;
3989 }
3990
Andreas Gampe878d58c2015-01-15 23:24:00 -08003991 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003992 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3993 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003994 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3995 uint32_t offset =
3996 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003997 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003998 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003999 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004000 }
Vladimir Marko58155012015-08-19 12:49:41 +00004001 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004002 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004003 break;
4004 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4005 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004006 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004007 break;
4008 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4009 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004010 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00004011 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4012 break;
4013 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4014 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004015 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004016 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004017 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004018 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004019 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004020 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004021 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004022 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004023 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004024 }
Vladimir Marko58155012015-08-19 12:49:41 +00004025 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004026 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004027 Register reg = XRegisterFrom(temp);
4028 Register method_reg;
4029 if (current_method.IsRegister()) {
4030 method_reg = XRegisterFrom(current_method);
4031 } else {
4032 DCHECK(invoke->GetLocations()->Intrinsified());
4033 DCHECK(!current_method.IsValid());
4034 method_reg = reg;
4035 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4036 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004037
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004038 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004039 __ Ldr(reg.X(),
4040 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004041 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004042 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004043 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4044 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004045 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4046 break;
4047 }
4048 }
4049
4050 switch (invoke->GetCodePtrLocation()) {
4051 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4052 __ Bl(&frame_entry_label_);
4053 break;
4054 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004055 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4056 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01004057 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
4058 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00004059 __ Bind(label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004060 // Branch and link to itself. This will be overriden at link time.
4061 __ bl(static_cast<int64_t>(0));
Vladimir Marko58155012015-08-19 12:49:41 +00004062 break;
4063 }
4064 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4065 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4066 // LR prepared above for better instruction scheduling.
4067 DCHECK(direct_code_loaded);
4068 // lr()
4069 __ Blr(lr);
4070 break;
4071 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4072 // LR = callee_method->entry_point_from_quick_compiled_code_;
4073 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004074 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004075 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004076 // lr()
4077 __ Blr(lr);
4078 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004079 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004080
Andreas Gampe878d58c2015-01-15 23:24:00 -08004081 DCHECK(!IsLeafMethod());
4082}
4083
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004084void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004085 // Use the calling convention instead of the location of the receiver, as
4086 // intrinsics may have put the receiver in a different register. In the intrinsics
4087 // slow path, the arguments have been moved to the right place, so here we are
4088 // guaranteed that the receiver is the first register of the calling convention.
4089 InvokeDexCallingConvention calling_convention;
4090 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004091 Register temp = XRegisterFrom(temp_in);
4092 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4093 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4094 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004095 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004096
4097 BlockPoolsScope block_pools(GetVIXLAssembler());
4098
4099 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004100 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004101 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004102 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004103 // Instead of simply (possibly) unpoisoning `temp` here, we should
4104 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105 // intermediate/temporary reference and because the current
4106 // concurrent copying collector keeps the from-space memory
4107 // intact/accessible until the end of the marking phase (the
4108 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004109 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4110 // temp = temp->GetMethodAt(method_offset);
4111 __ Ldr(temp, MemOperand(temp, method_offset));
4112 // lr = temp->GetEntryPoint();
4113 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4114 // lr();
4115 __ Blr(lr);
4116}
4117
Scott Wakeling97c72b72016-06-24 16:19:36 +01004118vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4119 const DexFile& dex_file,
4120 uint32_t string_index,
4121 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004122 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4123}
4124
Scott Wakeling97c72b72016-06-24 16:19:36 +01004125vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4126 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004127 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004128 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004129 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004130}
4131
Scott Wakeling97c72b72016-06-24 16:19:36 +01004132vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4133 const DexFile& dex_file,
4134 uint32_t element_offset,
4135 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004136 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4137}
4138
Scott Wakeling97c72b72016-06-24 16:19:36 +01004139vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4140 const DexFile& dex_file,
4141 uint32_t offset_or_index,
4142 vixl::aarch64::Label* adrp_label,
4143 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004144 // Add a patch entry and return the label.
4145 patches->emplace_back(dex_file, offset_or_index);
4146 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004147 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004148 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4149 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4150 return label;
4151}
4152
Scott Wakeling97c72b72016-06-24 16:19:36 +01004153vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004154 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004155 return boot_image_string_patches_.GetOrCreate(
4156 StringReference(&dex_file, string_index),
4157 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4158}
4159
Scott Wakeling97c72b72016-06-24 16:19:36 +01004160vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004161 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004162 return boot_image_type_patches_.GetOrCreate(
4163 TypeReference(&dex_file, type_index),
4164 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4165}
4166
Scott Wakeling97c72b72016-06-24 16:19:36 +01004167vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4168 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004169 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4170 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4171 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4172}
4173
Scott Wakeling97c72b72016-06-24 16:19:36 +01004174vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4175 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004176 return DeduplicateUint64Literal(address);
4177}
4178
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004179vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004180 const DexFile& dex_file, dex::StringIndex string_index) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004181 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index), /* placeholder */ 0u);
4182 return jit_string_patches_.GetOrCreate(
4183 StringReference(&dex_file, string_index),
4184 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4185}
4186
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004187vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
4188 const DexFile& dex_file, dex::TypeIndex type_index, uint64_t address) {
4189 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
4190 return jit_class_patches_.GetOrCreate(
4191 TypeReference(&dex_file, type_index),
4192 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4193}
4194
Vladimir Markoaad75c62016-10-03 08:46:48 +00004195void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4196 vixl::aarch64::Register reg) {
4197 DCHECK(reg.IsX());
4198 SingleEmissionCheckScope guard(GetVIXLAssembler());
4199 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004200 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004201}
4202
4203void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4204 vixl::aarch64::Register out,
4205 vixl::aarch64::Register base) {
4206 DCHECK(out.IsX());
4207 DCHECK(base.IsX());
4208 SingleEmissionCheckScope guard(GetVIXLAssembler());
4209 __ Bind(fixup_label);
4210 __ add(out, base, Operand(/* offset placeholder */ 0));
4211}
4212
4213void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4214 vixl::aarch64::Register out,
4215 vixl::aarch64::Register base) {
4216 DCHECK(base.IsX());
4217 SingleEmissionCheckScope guard(GetVIXLAssembler());
4218 __ Bind(fixup_label);
4219 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4220}
4221
4222template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4223inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4224 const ArenaDeque<PcRelativePatchInfo>& infos,
4225 ArenaVector<LinkerPatch>* linker_patches) {
4226 for (const PcRelativePatchInfo& info : infos) {
4227 linker_patches->push_back(Factory(info.label.GetLocation(),
4228 &info.target_dex_file,
4229 info.pc_insn_label->GetLocation(),
4230 info.offset_or_index));
4231 }
4232}
4233
Vladimir Marko58155012015-08-19 12:49:41 +00004234void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4235 DCHECK(linker_patches->empty());
4236 size_t size =
4237 method_patches_.size() +
4238 call_patches_.size() +
4239 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004240 pc_relative_dex_cache_patches_.size() +
4241 boot_image_string_patches_.size() +
4242 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004243 boot_image_type_patches_.size() +
4244 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004245 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004246 linker_patches->reserve(size);
4247 for (const auto& entry : method_patches_) {
4248 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004249 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4250 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004251 target_method.dex_file,
4252 target_method.dex_method_index));
4253 }
4254 for (const auto& entry : call_patches_) {
4255 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004256 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4257 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004258 target_method.dex_file,
4259 target_method.dex_method_index));
4260 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004261 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4262 linker_patches->push_back(
4263 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004264 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004265 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004266 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004267 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004268 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004269 info.offset_or_index));
4270 }
4271 for (const auto& entry : boot_image_string_patches_) {
4272 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004273 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4274 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004275 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004276 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004277 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004278 if (!GetCompilerOptions().IsBootImage()) {
4279 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4280 linker_patches);
4281 } else {
4282 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4283 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004284 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004285 for (const auto& entry : boot_image_type_patches_) {
4286 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004287 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4288 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004289 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004290 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004291 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004292 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4293 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004294 for (const auto& entry : boot_image_address_patches_) {
4295 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004296 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4297 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004298 }
4299}
4300
Scott Wakeling97c72b72016-06-24 16:19:36 +01004301vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004302 Uint32ToLiteralMap* map) {
4303 return map->GetOrCreate(
4304 value,
4305 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4306}
4307
Scott Wakeling97c72b72016-06-24 16:19:36 +01004308vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004309 return uint64_literals_.GetOrCreate(
4310 value,
4311 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004312}
4313
Scott Wakeling97c72b72016-06-24 16:19:36 +01004314vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004315 MethodReference target_method,
4316 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004317 return map->GetOrCreate(
4318 target_method,
4319 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004320}
4321
Scott Wakeling97c72b72016-06-24 16:19:36 +01004322vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004323 MethodReference target_method) {
4324 return DeduplicateMethodLiteral(target_method, &method_patches_);
4325}
4326
Scott Wakeling97c72b72016-06-24 16:19:36 +01004327vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004328 MethodReference target_method) {
4329 return DeduplicateMethodLiteral(target_method, &call_patches_);
4330}
4331
4332
Andreas Gampe878d58c2015-01-15 23:24:00 -08004333void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004334 // Explicit clinit checks triggered by static invokes must have been pruned by
4335 // art::PrepareForRegisterAllocation.
4336 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004337
Andreas Gampe878d58c2015-01-15 23:24:00 -08004338 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4339 return;
4340 }
4341
Alexandre Ramesd921d642015-04-16 15:07:16 +01004342 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004343 LocationSummary* locations = invoke->GetLocations();
4344 codegen_->GenerateStaticOrDirectCall(
4345 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004346 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004347}
4348
4349void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004350 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4351 return;
4352 }
4353
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004354 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004355 DCHECK(!codegen_->IsLeafMethod());
4356 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4357}
4358
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004359HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4360 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004361 switch (desired_class_load_kind) {
4362 case HLoadClass::LoadKind::kReferrersClass:
4363 break;
4364 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4365 DCHECK(!GetCompilerOptions().GetCompilePic());
4366 break;
4367 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4368 DCHECK(GetCompilerOptions().GetCompilePic());
4369 break;
4370 case HLoadClass::LoadKind::kBootImageAddress:
4371 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004372 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004373 DCHECK(Runtime::Current()->UseJitCompilation());
4374 break;
4375 case HLoadClass::LoadKind::kDexCachePcRelative:
4376 DCHECK(!Runtime::Current()->UseJitCompilation());
4377 break;
4378 case HLoadClass::LoadKind::kDexCacheViaMethod:
4379 break;
4380 }
4381 return desired_class_load_kind;
4382}
4383
Alexandre Rames67555f72014-11-18 10:55:16 +00004384void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004385 if (cls->NeedsAccessCheck()) {
4386 InvokeRuntimeCallingConvention calling_convention;
4387 CodeGenerator::CreateLoadClassLocationSummary(
4388 cls,
4389 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004390 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004391 /* code_generator_supports_read_barrier */ true);
4392 return;
4393 }
4394
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004395 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4396 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004397 ? LocationSummary::kCallOnSlowPath
4398 : LocationSummary::kNoCall;
4399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004400 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004401 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004402 }
4403
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004404 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4405 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4406 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4407 locations->SetInAt(0, Location::RequiresRegister());
4408 }
4409 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004410}
4411
4412void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004413 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004414 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004415 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004416 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004417 return;
4418 }
4419
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004420 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004421 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004422
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004423 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4424 ? kWithoutReadBarrier
4425 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004426 bool generate_null_check = false;
4427 switch (cls->GetLoadKind()) {
4428 case HLoadClass::LoadKind::kReferrersClass: {
4429 DCHECK(!cls->CanCallRuntime());
4430 DCHECK(!cls->MustGenerateClinitCheck());
4431 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4432 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004433 GenerateGcRootFieldLoad(cls,
4434 out_loc,
4435 current_method,
4436 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004437 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004438 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004439 break;
4440 }
4441 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004442 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004443 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4444 cls->GetTypeIndex()));
4445 break;
4446 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004447 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004448 // Add ADRP with its PC-relative type patch.
4449 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004450 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004451 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004452 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004453 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004454 vixl::aarch64::Label* add_label =
4455 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004456 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004457 break;
4458 }
4459 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004460 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004461 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4462 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4463 break;
4464 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004465 case HLoadClass::LoadKind::kJitTableAddress: {
4466 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4467 cls->GetTypeIndex(),
4468 cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004469 GenerateGcRootFieldLoad(cls,
4470 out_loc,
4471 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004472 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004473 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004474 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004475 break;
4476 }
4477 case HLoadClass::LoadKind::kDexCachePcRelative: {
4478 // Add ADRP with its PC-relative DexCache access patch.
4479 const DexFile& dex_file = cls->GetDexFile();
4480 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004481 vixl::aarch64::Label* adrp_label =
4482 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004483 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004484 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004485 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004486 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4487 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004488 GenerateGcRootFieldLoad(cls,
4489 out_loc,
4490 out.X(),
4491 /* offset placeholder */ 0,
4492 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004493 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004494 generate_null_check = !cls->IsInDexCache();
4495 break;
4496 }
4497 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4498 MemberOffset resolved_types_offset =
4499 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4500 // /* GcRoot<mirror::Class>[] */ out =
4501 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4502 Register current_method = InputRegisterAt(cls, 0);
4503 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4504 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004505 GenerateGcRootFieldLoad(cls,
4506 out_loc,
4507 out.X(),
Andreas Gampea5b09a62016-11-17 15:21:22 -08004508 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_),
Roland Levillain00468f32016-10-27 18:02:48 +01004509 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004510 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004511 generate_null_check = !cls->IsInDexCache();
4512 break;
4513 }
4514 }
4515
4516 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4517 DCHECK(cls->CanCallRuntime());
4518 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4519 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4520 codegen_->AddSlowPath(slow_path);
4521 if (generate_null_check) {
4522 __ Cbz(out, slow_path->GetEntryLabel());
4523 }
4524 if (cls->MustGenerateClinitCheck()) {
4525 GenerateClassInitializationCheck(slow_path, out);
4526 } else {
4527 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004528 }
4529 }
4530}
4531
David Brazdilcb1c0552015-08-04 16:22:25 +01004532static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004533 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004534}
4535
Alexandre Rames67555f72014-11-18 10:55:16 +00004536void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4537 LocationSummary* locations =
4538 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4539 locations->SetOut(Location::RequiresRegister());
4540}
4541
4542void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004543 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4544}
4545
4546void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4547 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4548}
4549
4550void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4551 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004552}
4553
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004554HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4555 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004556 switch (desired_string_load_kind) {
4557 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4558 DCHECK(!GetCompilerOptions().GetCompilePic());
4559 break;
4560 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4561 DCHECK(GetCompilerOptions().GetCompilePic());
4562 break;
4563 case HLoadString::LoadKind::kBootImageAddress:
4564 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004565 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004566 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004567 break;
4568 case HLoadString::LoadKind::kDexCacheViaMethod:
4569 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004570 case HLoadString::LoadKind::kJitTableAddress:
4571 DCHECK(Runtime::Current()->UseJitCompilation());
4572 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004573 }
4574 return desired_string_load_kind;
4575}
4576
Alexandre Rames67555f72014-11-18 10:55:16 +00004577void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004578 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004580 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004581 InvokeRuntimeCallingConvention calling_convention;
4582 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4583 } else {
4584 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004585 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4586 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4587 // Rely on the pResolveString and/or marking to save everything, including temps.
4588 RegisterSet caller_saves = RegisterSet::Empty();
4589 InvokeRuntimeCallingConvention calling_convention;
4590 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4591 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4592 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4593 Primitive::kPrimNot).GetCode());
4594 locations->SetCustomSlowPathCallerSaves(caller_saves);
4595 } else {
4596 // For non-Baker read barrier we have a temp-clobbering call.
4597 }
4598 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004599 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004600}
4601
4602void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004603 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004604 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004605
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004606 switch (load->GetLoadKind()) {
4607 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004608 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4609 load->GetStringIndex()));
4610 return; // No dex cache slow path.
4611 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004612 // Add ADRP with its PC-relative String patch.
4613 const DexFile& dex_file = load->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004614 uint32_t string_index = load->GetStringIndex().index_;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004615 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004616 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004617 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004618 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004619 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004620 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004621 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004622 return; // No dex cache slow path.
4623 }
4624 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004625 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4626 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4627 return; // No dex cache slow path.
4628 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004629 case HLoadString::LoadKind::kBssEntry: {
4630 // Add ADRP with its PC-relative String .bss entry patch.
4631 const DexFile& dex_file = load->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004632 uint32_t string_index = load->GetStringIndex().index_;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004633 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004634 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4635 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004636 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004637 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004638 // Add LDR with its PC-relative String patch.
4639 vixl::aarch64::Label* ldr_label =
4640 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004641 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004642 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004643 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004644 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004645 /* offset placeholder */ 0u,
4646 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004647 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004648 SlowPathCodeARM64* slow_path =
4649 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004650 codegen_->AddSlowPath(slow_path);
4651 __ Cbz(out.X(), slow_path->GetEntryLabel());
4652 __ Bind(slow_path->GetExitLabel());
4653 return;
4654 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004655 case HLoadString::LoadKind::kJitTableAddress: {
4656 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
4657 load->GetStringIndex()));
4658 GenerateGcRootFieldLoad(load,
4659 out_loc,
4660 out.X(),
4661 /* offset */ 0,
4662 /* fixup_label */ nullptr,
4663 kCompilerReadBarrierOption);
4664 return;
4665 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004666 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004667 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004668 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004669
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004670 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004671 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004672 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004673 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004674 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4675 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004676}
4677
Alexandre Rames5319def2014-10-23 10:03:10 +01004678void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4680 locations->SetOut(Location::ConstantLocation(constant));
4681}
4682
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004683void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004684 // Will be generated at use site.
4685}
4686
Alexandre Rames67555f72014-11-18 10:55:16 +00004687void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4688 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004689 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004690 InvokeRuntimeCallingConvention calling_convention;
4691 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4692}
4693
4694void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004695 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4696 instruction,
4697 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004698 if (instruction->IsEnter()) {
4699 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4700 } else {
4701 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4702 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004703}
4704
Alexandre Rames42d641b2014-10-27 14:00:51 +00004705void LocationsBuilderARM64::VisitMul(HMul* mul) {
4706 LocationSummary* locations =
4707 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4708 switch (mul->GetResultType()) {
4709 case Primitive::kPrimInt:
4710 case Primitive::kPrimLong:
4711 locations->SetInAt(0, Location::RequiresRegister());
4712 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004713 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004714 break;
4715
4716 case Primitive::kPrimFloat:
4717 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004718 locations->SetInAt(0, Location::RequiresFpuRegister());
4719 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004720 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004721 break;
4722
4723 default:
4724 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4725 }
4726}
4727
4728void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4729 switch (mul->GetResultType()) {
4730 case Primitive::kPrimInt:
4731 case Primitive::kPrimLong:
4732 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4733 break;
4734
4735 case Primitive::kPrimFloat:
4736 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004737 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004738 break;
4739
4740 default:
4741 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4742 }
4743}
4744
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004745void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4746 LocationSummary* locations =
4747 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4748 switch (neg->GetResultType()) {
4749 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004750 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004751 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004753 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004754
4755 case Primitive::kPrimFloat:
4756 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004757 locations->SetInAt(0, Location::RequiresFpuRegister());
4758 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004759 break;
4760
4761 default:
4762 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4763 }
4764}
4765
4766void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4767 switch (neg->GetResultType()) {
4768 case Primitive::kPrimInt:
4769 case Primitive::kPrimLong:
4770 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4771 break;
4772
4773 case Primitive::kPrimFloat:
4774 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004775 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004776 break;
4777
4778 default:
4779 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4780 }
4781}
4782
4783void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4784 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004785 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004786 InvokeRuntimeCallingConvention calling_convention;
4787 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004788 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004789 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004790 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004791}
4792
4793void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4794 LocationSummary* locations = instruction->GetLocations();
4795 InvokeRuntimeCallingConvention calling_convention;
4796 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4797 DCHECK(type_index.Is(w0));
Andreas Gampea5b09a62016-11-17 15:21:22 -08004798 __ Mov(type_index, instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01004799 // Note: if heap poisoning is enabled, the entry point takes cares
4800 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004801 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004802 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004803}
4804
Alexandre Rames5319def2014-10-23 10:03:10 +01004805void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4806 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004807 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004808 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004809 if (instruction->IsStringAlloc()) {
4810 locations->AddTemp(LocationFrom(kArtMethodRegister));
4811 } else {
4812 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4813 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4814 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004815 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4816}
4817
4818void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004819 // Note: if heap poisoning is enabled, the entry point takes cares
4820 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004821 if (instruction->IsStringAlloc()) {
4822 // String is allocated through StringFactory. Call NewEmptyString entry point.
4823 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004824 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004825 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4826 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4827 __ Blr(lr);
4828 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4829 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004830 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004831 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4832 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004833}
4834
4835void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004837 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004839}
4840
4841void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004842 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004843 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004844 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004845 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004846 break;
4847
4848 default:
4849 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4850 }
4851}
4852
David Brazdil66d126e2015-04-03 16:02:44 +01004853void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4854 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4855 locations->SetInAt(0, Location::RequiresRegister());
4856 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4857}
4858
4859void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004860 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004861}
4862
Alexandre Rames5319def2014-10-23 10:03:10 +01004863void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004864 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4865 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004866}
4867
Calin Juravle2ae48182016-03-16 14:05:09 +00004868void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4869 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004870 return;
4871 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004872
Alexandre Ramesd921d642015-04-16 15:07:16 +01004873 BlockPoolsScope block_pools(GetVIXLAssembler());
4874 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004875 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004876 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004877}
4878
Calin Juravle2ae48182016-03-16 14:05:09 +00004879void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004880 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004881 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004882
4883 LocationSummary* locations = instruction->GetLocations();
4884 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004885
4886 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004887}
4888
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004889void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004890 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004891}
4892
Alexandre Rames67555f72014-11-18 10:55:16 +00004893void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4894 HandleBinaryOp(instruction);
4895}
4896
4897void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4898 HandleBinaryOp(instruction);
4899}
4900
Alexandre Rames3e69f162014-12-10 10:36:50 +00004901void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4902 LOG(FATAL) << "Unreachable";
4903}
4904
4905void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4906 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4907}
4908
Alexandre Rames5319def2014-10-23 10:03:10 +01004909void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4911 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4912 if (location.IsStackSlot()) {
4913 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4914 } else if (location.IsDoubleStackSlot()) {
4915 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4916 }
4917 locations->SetOut(location);
4918}
4919
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004920void InstructionCodeGeneratorARM64::VisitParameterValue(
4921 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004922 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004923}
4924
4925void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4926 LocationSummary* locations =
4927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004928 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004929}
4930
4931void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4932 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4933 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004934}
4935
4936void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004938 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004939 locations->SetInAt(i, Location::Any());
4940 }
4941 locations->SetOut(Location::Any());
4942}
4943
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004944void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004945 LOG(FATAL) << "Unreachable";
4946}
4947
Serban Constantinescu02164b32014-11-13 14:05:07 +00004948void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004949 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004950 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004951 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4952 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004953 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4954
4955 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004956 case Primitive::kPrimInt:
4957 case Primitive::kPrimLong:
4958 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004959 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4961 break;
4962
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004963 case Primitive::kPrimFloat:
4964 case Primitive::kPrimDouble: {
4965 InvokeRuntimeCallingConvention calling_convention;
4966 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4967 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4968 locations->SetOut(calling_convention.GetReturnLocation(type));
4969
4970 break;
4971 }
4972
Serban Constantinescu02164b32014-11-13 14:05:07 +00004973 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004974 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004975 }
4976}
4977
4978void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4979 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004980
Serban Constantinescu02164b32014-11-13 14:05:07 +00004981 switch (type) {
4982 case Primitive::kPrimInt:
4983 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004984 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004985 break;
4986 }
4987
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004988 case Primitive::kPrimFloat:
4989 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004990 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4991 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004992 if (type == Primitive::kPrimFloat) {
4993 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4994 } else {
4995 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4996 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004997 break;
4998 }
4999
Serban Constantinescu02164b32014-11-13 14:05:07 +00005000 default:
5001 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005002 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005003 }
5004}
5005
Calin Juravle27df7582015-04-17 19:12:31 +01005006void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5007 memory_barrier->SetLocations(nullptr);
5008}
5009
5010void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005011 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005012}
5013
Alexandre Rames5319def2014-10-23 10:03:10 +01005014void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5016 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005017 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005018}
5019
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005020void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005021 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005022}
5023
5024void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5025 instruction->SetLocations(nullptr);
5026}
5027
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005028void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005029 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005030}
5031
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005032void LocationsBuilderARM64::VisitRor(HRor* ror) {
5033 HandleBinaryOp(ror);
5034}
5035
5036void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5037 HandleBinaryOp(ror);
5038}
5039
Serban Constantinescu02164b32014-11-13 14:05:07 +00005040void LocationsBuilderARM64::VisitShl(HShl* shl) {
5041 HandleShift(shl);
5042}
5043
5044void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5045 HandleShift(shl);
5046}
5047
5048void LocationsBuilderARM64::VisitShr(HShr* shr) {
5049 HandleShift(shr);
5050}
5051
5052void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5053 HandleShift(shr);
5054}
5055
Alexandre Rames5319def2014-10-23 10:03:10 +01005056void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005057 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005058}
5059
5060void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005061 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005062}
5063
Alexandre Rames67555f72014-11-18 10:55:16 +00005064void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005065 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005066}
5067
5068void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005069 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005070}
5071
5072void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005073 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005074}
5075
Alexandre Rames67555f72014-11-18 10:55:16 +00005076void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005077 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005078}
5079
Calin Juravlee460d1d2015-09-29 04:52:17 +01005080void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5081 HUnresolvedInstanceFieldGet* instruction) {
5082 FieldAccessCallingConventionARM64 calling_convention;
5083 codegen_->CreateUnresolvedFieldLocationSummary(
5084 instruction, instruction->GetFieldType(), calling_convention);
5085}
5086
5087void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5088 HUnresolvedInstanceFieldGet* instruction) {
5089 FieldAccessCallingConventionARM64 calling_convention;
5090 codegen_->GenerateUnresolvedFieldAccess(instruction,
5091 instruction->GetFieldType(),
5092 instruction->GetFieldIndex(),
5093 instruction->GetDexPc(),
5094 calling_convention);
5095}
5096
5097void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5098 HUnresolvedInstanceFieldSet* instruction) {
5099 FieldAccessCallingConventionARM64 calling_convention;
5100 codegen_->CreateUnresolvedFieldLocationSummary(
5101 instruction, instruction->GetFieldType(), calling_convention);
5102}
5103
5104void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5105 HUnresolvedInstanceFieldSet* instruction) {
5106 FieldAccessCallingConventionARM64 calling_convention;
5107 codegen_->GenerateUnresolvedFieldAccess(instruction,
5108 instruction->GetFieldType(),
5109 instruction->GetFieldIndex(),
5110 instruction->GetDexPc(),
5111 calling_convention);
5112}
5113
5114void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5115 HUnresolvedStaticFieldGet* instruction) {
5116 FieldAccessCallingConventionARM64 calling_convention;
5117 codegen_->CreateUnresolvedFieldLocationSummary(
5118 instruction, instruction->GetFieldType(), calling_convention);
5119}
5120
5121void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5122 HUnresolvedStaticFieldGet* instruction) {
5123 FieldAccessCallingConventionARM64 calling_convention;
5124 codegen_->GenerateUnresolvedFieldAccess(instruction,
5125 instruction->GetFieldType(),
5126 instruction->GetFieldIndex(),
5127 instruction->GetDexPc(),
5128 calling_convention);
5129}
5130
5131void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5132 HUnresolvedStaticFieldSet* instruction) {
5133 FieldAccessCallingConventionARM64 calling_convention;
5134 codegen_->CreateUnresolvedFieldLocationSummary(
5135 instruction, instruction->GetFieldType(), calling_convention);
5136}
5137
5138void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5139 HUnresolvedStaticFieldSet* instruction) {
5140 FieldAccessCallingConventionARM64 calling_convention;
5141 codegen_->GenerateUnresolvedFieldAccess(instruction,
5142 instruction->GetFieldType(),
5143 instruction->GetFieldIndex(),
5144 instruction->GetDexPc(),
5145 calling_convention);
5146}
5147
Alexandre Rames5319def2014-10-23 10:03:10 +01005148void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005149 LocationSummary* locations =
5150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005151 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005152}
5153
5154void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005155 HBasicBlock* block = instruction->GetBlock();
5156 if (block->GetLoopInformation() != nullptr) {
5157 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5158 // The back edge will generate the suspend check.
5159 return;
5160 }
5161 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5162 // The goto will generate the suspend check.
5163 return;
5164 }
5165 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005166}
5167
Alexandre Rames67555f72014-11-18 10:55:16 +00005168void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5169 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005170 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005171 InvokeRuntimeCallingConvention calling_convention;
5172 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5173}
5174
5175void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005176 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005177 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005178}
5179
5180void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5181 LocationSummary* locations =
5182 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5183 Primitive::Type input_type = conversion->GetInputType();
5184 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005185 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005186 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5187 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5188 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5189 }
5190
Alexandre Rames542361f2015-01-29 16:57:31 +00005191 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005192 locations->SetInAt(0, Location::RequiresFpuRegister());
5193 } else {
5194 locations->SetInAt(0, Location::RequiresRegister());
5195 }
5196
Alexandre Rames542361f2015-01-29 16:57:31 +00005197 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005198 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5199 } else {
5200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5201 }
5202}
5203
5204void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5205 Primitive::Type result_type = conversion->GetResultType();
5206 Primitive::Type input_type = conversion->GetInputType();
5207
5208 DCHECK_NE(input_type, result_type);
5209
Alexandre Rames542361f2015-01-29 16:57:31 +00005210 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005211 int result_size = Primitive::ComponentSize(result_type);
5212 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005213 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005214 Register output = OutputRegister(conversion);
5215 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005216 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005217 // 'int' values are used directly as W registers, discarding the top
5218 // bits, so we don't need to sign-extend and can just perform a move.
5219 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5220 // top 32 bits of the target register. We theoretically could leave those
5221 // bits unchanged, but we would have to make sure that no code uses a
5222 // 32bit input value as a 64bit value assuming that the top 32 bits are
5223 // zero.
5224 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005225 } else if (result_type == Primitive::kPrimChar ||
5226 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5227 __ Ubfx(output,
5228 output.IsX() ? source.X() : source.W(),
5229 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005230 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005231 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005232 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005233 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005234 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005235 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005236 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5237 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005238 } else if (Primitive::IsFloatingPointType(result_type) &&
5239 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005240 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5241 } else {
5242 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5243 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005244 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005245}
Alexandre Rames67555f72014-11-18 10:55:16 +00005246
Serban Constantinescu02164b32014-11-13 14:05:07 +00005247void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5248 HandleShift(ushr);
5249}
5250
5251void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5252 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005253}
5254
5255void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5256 HandleBinaryOp(instruction);
5257}
5258
5259void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5260 HandleBinaryOp(instruction);
5261}
5262
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005263void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005264 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005265 LOG(FATAL) << "Unreachable";
5266}
5267
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005268void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005269 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005270 LOG(FATAL) << "Unreachable";
5271}
5272
Mark Mendellfe57faa2015-09-18 09:26:15 -04005273// Simple implementation of packed switch - generate cascaded compare/jumps.
5274void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5275 LocationSummary* locations =
5276 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5277 locations->SetInAt(0, Location::RequiresRegister());
5278}
5279
5280void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5281 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005282 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005283 Register value_reg = InputRegisterAt(switch_instr, 0);
5284 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5285
Zheng Xu3927c8b2015-11-18 17:46:25 +08005286 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005287 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005288 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5289 // make sure we don't emit it if the target may run out of range.
5290 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5291 // ranges and emit the tables only as required.
5292 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005293
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005294 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005295 // Current instruction id is an upper bound of the number of HIRs in the graph.
5296 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5297 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005298 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5299 Register temp = temps.AcquireW();
5300 __ Subs(temp, value_reg, Operand(lower_bound));
5301
Zheng Xu3927c8b2015-11-18 17:46:25 +08005302 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005303 // Jump to successors[0] if value == lower_bound.
5304 __ B(eq, codegen_->GetLabelOf(successors[0]));
5305 int32_t last_index = 0;
5306 for (; num_entries - last_index > 2; last_index += 2) {
5307 __ Subs(temp, temp, Operand(2));
5308 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5309 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5310 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5311 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5312 }
5313 if (num_entries - last_index == 2) {
5314 // The last missing case_value.
5315 __ Cmp(temp, Operand(1));
5316 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005317 }
5318
5319 // And the default for any other value.
5320 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5321 __ B(codegen_->GetLabelOf(default_block));
5322 }
5323 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005324 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005325
5326 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5327
5328 // Below instructions should use at most one blocked register. Since there are two blocked
5329 // registers, we are free to block one.
5330 Register temp_w = temps.AcquireW();
5331 Register index;
5332 // Remove the bias.
5333 if (lower_bound != 0) {
5334 index = temp_w;
5335 __ Sub(index, value_reg, Operand(lower_bound));
5336 } else {
5337 index = value_reg;
5338 }
5339
5340 // Jump to default block if index is out of the range.
5341 __ Cmp(index, Operand(num_entries));
5342 __ B(hs, codegen_->GetLabelOf(default_block));
5343
5344 // In current VIXL implementation, it won't require any blocked registers to encode the
5345 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5346 // register pressure.
5347 Register table_base = temps.AcquireX();
5348 // Load jump offset from the table.
5349 __ Adr(table_base, jump_table->GetTableStartLabel());
5350 Register jump_offset = temp_w;
5351 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5352
5353 // Jump to target block by branching to table_base(pc related) + offset.
5354 Register target_address = table_base;
5355 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5356 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005357 }
5358}
5359
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005360void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5361 HInstruction* instruction,
5362 Location out,
5363 uint32_t offset,
5364 Location maybe_temp,
5365 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005366 Primitive::Type type = Primitive::kPrimNot;
5367 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005368 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005369 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005370 Register temp_reg = RegisterFrom(maybe_temp, type);
5371 if (kUseBakerReadBarrier) {
5372 // Load with fast path based Baker's read barrier.
5373 // /* HeapReference<Object> */ out = *(out + offset)
5374 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5375 out,
5376 out_reg,
5377 offset,
5378 temp_reg,
5379 /* needs_null_check */ false,
5380 /* use_load_acquire */ false);
5381 } else {
5382 // Load with slow path based read barrier.
5383 // Save the value of `out` into `maybe_temp` before overwriting it
5384 // in the following move operation, as we will need it for the
5385 // read barrier below.
5386 __ Mov(temp_reg, out_reg);
5387 // /* HeapReference<Object> */ out = *(out + offset)
5388 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5389 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5390 }
5391 } else {
5392 // Plain load with no read barrier.
5393 // /* HeapReference<Object> */ out = *(out + offset)
5394 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5395 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5396 }
5397}
5398
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005399void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5400 HInstruction* instruction,
5401 Location out,
5402 Location obj,
5403 uint32_t offset,
5404 Location maybe_temp,
5405 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005406 Primitive::Type type = Primitive::kPrimNot;
5407 Register out_reg = RegisterFrom(out, type);
5408 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005409 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005410 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005411 if (kUseBakerReadBarrier) {
5412 // Load with fast path based Baker's read barrier.
5413 Register temp_reg = RegisterFrom(maybe_temp, type);
5414 // /* HeapReference<Object> */ out = *(obj + offset)
5415 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5416 out,
5417 obj_reg,
5418 offset,
5419 temp_reg,
5420 /* needs_null_check */ false,
5421 /* use_load_acquire */ false);
5422 } else {
5423 // Load with slow path based read barrier.
5424 // /* HeapReference<Object> */ out = *(obj + offset)
5425 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5426 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5427 }
5428 } else {
5429 // Plain load with no read barrier.
5430 // /* HeapReference<Object> */ out = *(obj + offset)
5431 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5432 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5433 }
5434}
5435
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005436void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5437 HInstruction* instruction,
5438 Location root,
5439 Register obj,
5440 uint32_t offset,
5441 vixl::aarch64::Label* fixup_label,
5442 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005443 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005444 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005445 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005446 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005447 if (kUseBakerReadBarrier) {
5448 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5449 // Baker's read barrier are used:
5450 //
5451 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005452 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5453 // if (temp != null) {
5454 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005455 // }
5456
5457 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005458 if (fixup_label == nullptr) {
5459 __ Ldr(root_reg, MemOperand(obj, offset));
5460 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005461 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005462 }
Roland Levillain44015862016-01-22 11:47:17 +00005463 static_assert(
5464 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5465 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5466 "have different sizes.");
5467 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5468 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5469 "have different sizes.");
5470
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005471 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005472
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005473 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5474 SlowPathCodeARM64* slow_path =
5475 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5476 root,
5477 LocationFrom(temp));
5478 codegen_->AddSlowPath(slow_path);
5479 const int32_t entry_point_offset =
5480 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5481 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5482 // Loading the entrypoint does not require a load acquire since it is only changed when
5483 // threads are suspended or running a checkpoint.
5484 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5485 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5486 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005487 __ Cbnz(temp, slow_path->GetEntryLabel());
5488 __ Bind(slow_path->GetExitLabel());
5489 } else {
5490 // GC root loaded through a slow path for read barriers other
5491 // than Baker's.
5492 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005493 if (fixup_label == nullptr) {
5494 __ Add(root_reg.X(), obj.X(), offset);
5495 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005496 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005497 }
Roland Levillain44015862016-01-22 11:47:17 +00005498 // /* mirror::Object* */ root = root->Read()
5499 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5500 }
5501 } else {
5502 // Plain GC root load with no read barrier.
5503 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005504 if (fixup_label == nullptr) {
5505 __ Ldr(root_reg, MemOperand(obj, offset));
5506 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005507 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005508 }
Roland Levillain44015862016-01-22 11:47:17 +00005509 // Note that GC roots are not affected by heap poisoning, thus we
5510 // do not have to unpoison `root_reg` here.
5511 }
5512}
5513
5514void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5515 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005516 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005517 uint32_t offset,
5518 Register temp,
5519 bool needs_null_check,
5520 bool use_load_acquire) {
5521 DCHECK(kEmitCompilerReadBarrier);
5522 DCHECK(kUseBakerReadBarrier);
5523
5524 // /* HeapReference<Object> */ ref = *(obj + offset)
5525 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005526 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005527 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5528 ref,
5529 obj,
5530 offset,
5531 no_index,
5532 no_scale_factor,
5533 temp,
5534 needs_null_check,
5535 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005536}
5537
5538void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5539 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005540 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005541 uint32_t data_offset,
5542 Location index,
5543 Register temp,
5544 bool needs_null_check) {
5545 DCHECK(kEmitCompilerReadBarrier);
5546 DCHECK(kUseBakerReadBarrier);
5547
5548 // Array cells are never volatile variables, therefore array loads
5549 // never use Load-Acquire instructions on ARM64.
5550 const bool use_load_acquire = false;
5551
Roland Levillainbfea3352016-06-23 13:48:47 +01005552 static_assert(
5553 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5554 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005555 // /* HeapReference<Object> */ ref =
5556 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005557 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5558 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5559 ref,
5560 obj,
5561 data_offset,
5562 index,
5563 scale_factor,
5564 temp,
5565 needs_null_check,
5566 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005567}
5568
5569void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5570 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005571 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005572 uint32_t offset,
5573 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005574 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005575 Register temp,
5576 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005577 bool use_load_acquire,
5578 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005579 DCHECK(kEmitCompilerReadBarrier);
5580 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005581 // If we are emitting an array load, we should not be using a
5582 // Load Acquire instruction. In other words:
5583 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5584 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005585
5586 MacroAssembler* masm = GetVIXLAssembler();
5587 UseScratchRegisterScope temps(masm);
5588
5589 // In slow path based read barriers, the read barrier call is
5590 // inserted after the original load. However, in fast path based
5591 // Baker's read barriers, we need to perform the load of
5592 // mirror::Object::monitor_ *before* the original reference load.
5593 // This load-load ordering is required by the read barrier.
5594 // The fast path/slow path (for Baker's algorithm) should look like:
5595 //
5596 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5597 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5598 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005599 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005600 // if (is_gray) {
5601 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5602 // }
5603 //
5604 // Note: the original implementation in ReadBarrier::Barrier is
5605 // slightly more complex as it performs additional checks that we do
5606 // not do here for performance reasons.
5607
5608 Primitive::Type type = Primitive::kPrimNot;
5609 Register ref_reg = RegisterFrom(ref, type);
5610 DCHECK(obj.IsW());
5611 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5612
5613 // /* int32_t */ monitor = obj->monitor_
5614 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5615 if (needs_null_check) {
5616 MaybeRecordImplicitNullCheck(instruction);
5617 }
5618 // /* LockWord */ lock_word = LockWord(monitor)
5619 static_assert(sizeof(LockWord) == sizeof(int32_t),
5620 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005621
Vladimir Marko877a0332016-07-11 19:30:56 +01005622 // Introduce a dependency on the lock_word including rb_state,
5623 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005624 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005625 // `obj` is unchanged by this operation, but its value now depends
5626 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005627 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005628
5629 // The actual reference load.
5630 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005631 // Load types involving an "index": ArrayGet,
5632 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5633 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005634 if (use_load_acquire) {
5635 // UnsafeGetObjectVolatile intrinsic case.
5636 // Register `index` is not an index in an object array, but an
5637 // offset to an object reference field within object `obj`.
5638 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5639 DCHECK(instruction->GetLocations()->Intrinsified());
5640 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5641 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005642 DCHECK_EQ(offset, 0u);
5643 DCHECK_EQ(scale_factor, 0u);
5644 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005645 // /* HeapReference<Object> */ ref = *(obj + index)
5646 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5647 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005648 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005649 // ArrayGet and UnsafeGetObject intrinsics cases.
5650 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5651 if (index.IsConstant()) {
5652 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5653 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5654 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005655 Register temp3 = temps.AcquireW();
5656 __ Add(temp3, obj, offset);
5657 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5658 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005659 }
Roland Levillain44015862016-01-22 11:47:17 +00005660 }
Roland Levillain44015862016-01-22 11:47:17 +00005661 } else {
5662 // /* HeapReference<Object> */ ref = *(obj + offset)
5663 MemOperand field = HeapOperand(obj, offset);
5664 if (use_load_acquire) {
5665 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5666 } else {
5667 Load(type, ref_reg, field);
5668 }
5669 }
5670
5671 // Object* ref = ref_addr->AsMirrorPtr()
5672 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5673
Vladimir Marko953437b2016-08-24 08:30:46 +00005674 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005675 SlowPathCodeARM64* slow_path;
5676 if (always_update_field) {
5677 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5678 // address of the form `obj + field_offset`, where `obj` is a
5679 // register and `field_offset` is a register. Thus `offset` and
5680 // `scale_factor` above are expected to be null in this code path.
5681 DCHECK_EQ(offset, 0u);
5682 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5683 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5684 instruction, ref, obj, /* field_offset */ index, temp);
5685 } else {
5686 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5687 }
Roland Levillain44015862016-01-22 11:47:17 +00005688 AddSlowPath(slow_path);
5689
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005690 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005691 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005692 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005693 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5694 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005695 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005696 __ Bind(slow_path->GetExitLabel());
5697}
5698
5699void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5700 Location out,
5701 Location ref,
5702 Location obj,
5703 uint32_t offset,
5704 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005705 DCHECK(kEmitCompilerReadBarrier);
5706
Roland Levillain44015862016-01-22 11:47:17 +00005707 // Insert a slow path based read barrier *after* the reference load.
5708 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005709 // If heap poisoning is enabled, the unpoisoning of the loaded
5710 // reference will be carried out by the runtime within the slow
5711 // path.
5712 //
5713 // Note that `ref` currently does not get unpoisoned (when heap
5714 // poisoning is enabled), which is alright as the `ref` argument is
5715 // not used by the artReadBarrierSlow entry point.
5716 //
5717 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5718 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5719 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5720 AddSlowPath(slow_path);
5721
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005722 __ B(slow_path->GetEntryLabel());
5723 __ Bind(slow_path->GetExitLabel());
5724}
5725
Roland Levillain44015862016-01-22 11:47:17 +00005726void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5727 Location out,
5728 Location ref,
5729 Location obj,
5730 uint32_t offset,
5731 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005732 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005733 // Baker's read barriers shall be handled by the fast path
5734 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5735 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005736 // If heap poisoning is enabled, unpoisoning will be taken care of
5737 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005738 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005739 } else if (kPoisonHeapReferences) {
5740 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5741 }
5742}
5743
Roland Levillain44015862016-01-22 11:47:17 +00005744void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5745 Location out,
5746 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005747 DCHECK(kEmitCompilerReadBarrier);
5748
Roland Levillain44015862016-01-22 11:47:17 +00005749 // Insert a slow path based read barrier *after* the GC root load.
5750 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005751 // Note that GC roots are not affected by heap poisoning, so we do
5752 // not need to do anything special for this here.
5753 SlowPathCodeARM64* slow_path =
5754 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5755 AddSlowPath(slow_path);
5756
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005757 __ B(slow_path->GetEntryLabel());
5758 __ Bind(slow_path->GetExitLabel());
5759}
5760
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005761void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5762 LocationSummary* locations =
5763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5764 locations->SetInAt(0, Location::RequiresRegister());
5765 locations->SetOut(Location::RequiresRegister());
5766}
5767
5768void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5769 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005770 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005771 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005772 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005773 __ Ldr(XRegisterFrom(locations->Out()),
5774 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005775 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005776 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005777 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005778 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5779 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005780 __ Ldr(XRegisterFrom(locations->Out()),
5781 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005782 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005783}
5784
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005785static void PatchJitRootUse(uint8_t* code,
5786 const uint8_t* roots_data,
5787 vixl::aarch64::Literal<uint32_t>* literal,
5788 uint64_t index_in_table) {
5789 uint32_t literal_offset = literal->GetOffset();
5790 uintptr_t address =
5791 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5792 uint8_t* data = code + literal_offset;
5793 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5794}
5795
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005796void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5797 for (const auto& entry : jit_string_patches_) {
5798 const auto& it = jit_string_roots_.find(entry.first);
5799 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005800 PatchJitRootUse(code, roots_data, entry.second, it->second);
5801 }
5802 for (const auto& entry : jit_class_patches_) {
5803 const auto& it = jit_class_roots_.find(entry.first);
5804 DCHECK(it != jit_class_roots_.end());
5805 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005806 }
5807}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005808
Alexandre Rames67555f72014-11-18 10:55:16 +00005809#undef __
5810#undef QUICK_ENTRY_POINT
5811
Alexandre Rames5319def2014-10-23 10:03:10 +01005812} // namespace arm64
5813} // namespace art