blob: 9aaeadb44a03b835dfc2a65ea4a7bc964c2c7fa1 [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)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001150 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1151 boot_image_string_patches_(StringReferenceValueComparator(),
1152 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1153 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001154 boot_image_type_patches_(TypeReferenceValueComparator(),
1155 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001157 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001158 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1159 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001160 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1161 jit_class_patches_(TypeReferenceValueComparator(),
1162 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001163 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001164 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001165}
Alexandre Rames5319def2014-10-23 10:03:10 +01001166
Alexandre Rames67555f72014-11-18 10:55:16 +00001167#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001168
Zheng Xu3927c8b2015-11-18 17:46:25 +08001169void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001170 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001171 jump_table->EmitTable(this);
1172 }
1173}
1174
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001175void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001176 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001177 // Ensure we emit the literal pool.
1178 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001179
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001180 CodeGenerator::Finalize(allocator);
1181}
1182
Zheng Xuad4450e2015-04-17 18:48:56 +08001183void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1184 // Note: There are 6 kinds of moves:
1185 // 1. constant -> GPR/FPR (non-cycle)
1186 // 2. constant -> stack (non-cycle)
1187 // 3. GPR/FPR -> GPR/FPR
1188 // 4. GPR/FPR -> stack
1189 // 5. stack -> GPR/FPR
1190 // 6. stack -> stack (non-cycle)
1191 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1192 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1193 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1194 // dependency.
1195 vixl_temps_.Open(GetVIXLAssembler());
1196}
1197
1198void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1199 vixl_temps_.Close();
1200}
1201
1202Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1203 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1204 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1205 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1206 Location scratch = GetScratchLocation(kind);
1207 if (!scratch.Equals(Location::NoLocation())) {
1208 return scratch;
1209 }
1210 // Allocate from VIXL temp registers.
1211 if (kind == Location::kRegister) {
1212 scratch = LocationFrom(vixl_temps_.AcquireX());
1213 } else {
1214 DCHECK(kind == Location::kFpuRegister);
1215 scratch = LocationFrom(vixl_temps_.AcquireD());
1216 }
1217 AddScratchLocation(scratch);
1218 return scratch;
1219}
1220
1221void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1222 if (loc.IsRegister()) {
1223 vixl_temps_.Release(XRegisterFrom(loc));
1224 } else {
1225 DCHECK(loc.IsFpuRegister());
1226 vixl_temps_.Release(DRegisterFrom(loc));
1227 }
1228 RemoveScratchLocation(loc);
1229}
1230
Alexandre Rames3e69f162014-12-10 10:36:50 +00001231void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001232 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001234}
1235
Alexandre Rames5319def2014-10-23 10:03:10 +01001236void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001237 MacroAssembler* masm = GetVIXLAssembler();
1238 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001239 __ Bind(&frame_entry_label_);
1240
Serban Constantinescu02164b32014-11-13 14:05:07 +00001241 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1242 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001243 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001244 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001245 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001246 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001247 __ Ldr(wzr, MemOperand(temp, 0));
1248 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001249 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001250
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001251 if (!HasEmptyFrame()) {
1252 int frame_size = GetFrameSize();
1253 // Stack layout:
1254 // sp[frame_size - 8] : lr.
1255 // ... : other preserved core registers.
1256 // ... : other preserved fp registers.
1257 // ... : reserved frame space.
1258 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001259
1260 // Save the current method if we need it. Note that we do not
1261 // do this in HCurrentMethod, as the instruction might have been removed
1262 // in the SSA graph.
1263 if (RequiresCurrentMethod()) {
1264 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001265 } else {
1266 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001267 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001268 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001269 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1270 frame_size - GetCoreSpillSize());
1271 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1272 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001273
1274 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1275 // Initialize should_deoptimize flag to 0.
1276 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1277 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1278 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001279 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001280}
1281
1282void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001283 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001284 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001285 if (!HasEmptyFrame()) {
1286 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001287 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1288 frame_size - FrameEntrySpillSize());
1289 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1290 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001291 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001292 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001293 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001294 __ Ret();
1295 GetAssembler()->cfi().RestoreState();
1296 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001297}
1298
Scott Wakeling97c72b72016-06-24 16:19:36 +01001299CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001300 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001301 return CPURegList(CPURegister::kRegister, kXRegSize,
1302 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001303}
1304
Scott Wakeling97c72b72016-06-24 16:19:36 +01001305CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001306 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1307 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001308 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1309 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001310}
1311
Alexandre Rames5319def2014-10-23 10:03:10 +01001312void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1313 __ Bind(GetLabelOf(block));
1314}
1315
Calin Juravle175dc732015-08-25 15:42:32 +01001316void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1317 DCHECK(location.IsRegister());
1318 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1319}
1320
Calin Juravlee460d1d2015-09-29 04:52:17 +01001321void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1322 if (location.IsRegister()) {
1323 locations->AddTemp(location);
1324 } else {
1325 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1326 }
1327}
1328
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001329void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001330 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001331 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001332 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001333 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001334 if (value_can_be_null) {
1335 __ Cbz(value, &done);
1336 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001337 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001338 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001339 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001340 if (value_can_be_null) {
1341 __ Bind(&done);
1342 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001343}
1344
David Brazdil58282f42016-01-14 12:45:10 +00001345void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001346 // Blocked core registers:
1347 // lr : Runtime reserved.
1348 // tr : Runtime reserved.
1349 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1350 // ip1 : VIXL core temp.
1351 // ip0 : VIXL core temp.
1352 //
1353 // Blocked fp registers:
1354 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001355 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1356 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001357 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001358 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001359 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001360
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001361 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001362 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001363 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001364 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001365
David Brazdil58282f42016-01-14 12:45:10 +00001366 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001367 // Stubs do not save callee-save floating point registers. If the graph
1368 // is debuggable, we need to deal with these registers differently. For
1369 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001370 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1371 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001372 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001373 }
1374 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001375}
1376
Alexandre Rames3e69f162014-12-10 10:36:50 +00001377size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1378 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1379 __ Str(reg, MemOperand(sp, stack_index));
1380 return kArm64WordSize;
1381}
1382
1383size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1384 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1385 __ Ldr(reg, MemOperand(sp, stack_index));
1386 return kArm64WordSize;
1387}
1388
1389size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1390 FPRegister reg = FPRegister(reg_id, kDRegSize);
1391 __ Str(reg, MemOperand(sp, stack_index));
1392 return kArm64WordSize;
1393}
1394
1395size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1396 FPRegister reg = FPRegister(reg_id, kDRegSize);
1397 __ Ldr(reg, MemOperand(sp, stack_index));
1398 return kArm64WordSize;
1399}
1400
Alexandre Rames5319def2014-10-23 10:03:10 +01001401void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001402 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001403}
1404
1405void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001406 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001407}
1408
Alexandre Rames67555f72014-11-18 10:55:16 +00001409void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001410 if (constant->IsIntConstant()) {
1411 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1412 } else if (constant->IsLongConstant()) {
1413 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1414 } else if (constant->IsNullConstant()) {
1415 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001416 } else if (constant->IsFloatConstant()) {
1417 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1418 } else {
1419 DCHECK(constant->IsDoubleConstant());
1420 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1421 }
1422}
1423
Alexandre Rames3e69f162014-12-10 10:36:50 +00001424
1425static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1426 DCHECK(constant.IsConstant());
1427 HConstant* cst = constant.GetConstant();
1428 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001429 // Null is mapped to a core W register, which we associate with kPrimInt.
1430 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001431 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1432 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1433 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1434}
1435
Calin Juravlee460d1d2015-09-29 04:52:17 +01001436void CodeGeneratorARM64::MoveLocation(Location destination,
1437 Location source,
1438 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001439 if (source.Equals(destination)) {
1440 return;
1441 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001442
1443 // A valid move can always be inferred from the destination and source
1444 // locations. When moving from and to a register, the argument type can be
1445 // used to generate 32bit instead of 64bit moves. In debug mode we also
1446 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001447 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001448
1449 if (destination.IsRegister() || destination.IsFpuRegister()) {
1450 if (unspecified_type) {
1451 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1452 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001453 (src_cst != nullptr && (src_cst->IsIntConstant()
1454 || src_cst->IsFloatConstant()
1455 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001456 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001457 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001458 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001459 // If the source is a double stack slot or a 64bit constant, a 64bit
1460 // type is appropriate. Else the source is a register, and since the
1461 // type has not been specified, we chose a 64bit type to force a 64bit
1462 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001463 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001464 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001465 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001466 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1467 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1468 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1470 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1471 __ Ldr(dst, StackOperandFrom(source));
1472 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001473 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001474 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001475 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001476 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001477 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001478 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001479 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001480 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1481 ? Primitive::kPrimLong
1482 : Primitive::kPrimInt;
1483 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1484 }
1485 } else {
1486 DCHECK(source.IsFpuRegister());
1487 if (destination.IsRegister()) {
1488 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1489 ? Primitive::kPrimDouble
1490 : Primitive::kPrimFloat;
1491 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1492 } else {
1493 DCHECK(destination.IsFpuRegister());
1494 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001495 }
1496 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001497 } else { // The destination is not a register. It must be a stack slot.
1498 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1499 if (source.IsRegister() || source.IsFpuRegister()) {
1500 if (unspecified_type) {
1501 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001502 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001504 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001505 }
1506 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001507 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1508 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1509 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001510 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001511 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1512 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513 UseScratchRegisterScope temps(GetVIXLAssembler());
1514 HConstant* src_cst = source.GetConstant();
1515 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001516 if (src_cst->IsZeroBitPattern()) {
1517 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001518 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001519 if (src_cst->IsIntConstant()) {
1520 temp = temps.AcquireW();
1521 } else if (src_cst->IsLongConstant()) {
1522 temp = temps.AcquireX();
1523 } else if (src_cst->IsFloatConstant()) {
1524 temp = temps.AcquireS();
1525 } else {
1526 DCHECK(src_cst->IsDoubleConstant());
1527 temp = temps.AcquireD();
1528 }
1529 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001530 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001531 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001532 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001533 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001534 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001535 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001536 // Use any scratch register (a core or a floating-point one)
1537 // from VIXL scratch register pools as a temporary.
1538 //
1539 // We used to only use the FP scratch register pool, but in some
1540 // rare cases the only register from this pool (D31) would
1541 // already be used (e.g. within a ParallelMove instruction, when
1542 // a move is blocked by a another move requiring a scratch FP
1543 // register, which would reserve D31). To prevent this issue, we
1544 // ask for a scratch register of any type (core or FP).
1545 CPURegister temp =
1546 temps.AcquireCPURegisterOfSize(destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001547 __ Ldr(temp, StackOperandFrom(source));
1548 __ Str(temp, StackOperandFrom(destination));
1549 }
1550 }
1551}
1552
1553void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001554 CPURegister dst,
1555 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556 switch (type) {
1557 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001558 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001559 break;
1560 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001561 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001562 break;
1563 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001564 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001565 break;
1566 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001567 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 break;
1569 case Primitive::kPrimInt:
1570 case Primitive::kPrimNot:
1571 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001572 case Primitive::kPrimFloat:
1573 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001574 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001575 __ Ldr(dst, src);
1576 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577 case Primitive::kPrimVoid:
1578 LOG(FATAL) << "Unreachable type " << type;
1579 }
1580}
1581
Calin Juravle77520bc2015-01-12 18:45:46 +00001582void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001583 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001584 const MemOperand& src,
1585 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001586 MacroAssembler* masm = GetVIXLAssembler();
1587 BlockPoolsScope block_pools(masm);
1588 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001589 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001590 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001591
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001592 DCHECK(!src.IsPreIndex());
1593 DCHECK(!src.IsPostIndex());
1594
1595 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001596 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001597 MemOperand base = MemOperand(temp_base);
1598 switch (type) {
1599 case Primitive::kPrimBoolean:
1600 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001601 if (needs_null_check) {
1602 MaybeRecordImplicitNullCheck(instruction);
1603 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001604 break;
1605 case Primitive::kPrimByte:
1606 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001607 if (needs_null_check) {
1608 MaybeRecordImplicitNullCheck(instruction);
1609 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001610 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1611 break;
1612 case Primitive::kPrimChar:
1613 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001614 if (needs_null_check) {
1615 MaybeRecordImplicitNullCheck(instruction);
1616 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001617 break;
1618 case Primitive::kPrimShort:
1619 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001620 if (needs_null_check) {
1621 MaybeRecordImplicitNullCheck(instruction);
1622 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001623 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1624 break;
1625 case Primitive::kPrimInt:
1626 case Primitive::kPrimNot:
1627 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001628 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001629 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001630 if (needs_null_check) {
1631 MaybeRecordImplicitNullCheck(instruction);
1632 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001633 break;
1634 case Primitive::kPrimFloat:
1635 case Primitive::kPrimDouble: {
1636 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001637 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001638
1639 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1640 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001641 if (needs_null_check) {
1642 MaybeRecordImplicitNullCheck(instruction);
1643 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001644 __ Fmov(FPRegister(dst), temp);
1645 break;
1646 }
1647 case Primitive::kPrimVoid:
1648 LOG(FATAL) << "Unreachable type " << type;
1649 }
1650}
1651
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001652void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001653 CPURegister src,
1654 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001655 switch (type) {
1656 case Primitive::kPrimBoolean:
1657 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001658 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001659 break;
1660 case Primitive::kPrimChar:
1661 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001662 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001663 break;
1664 case Primitive::kPrimInt:
1665 case Primitive::kPrimNot:
1666 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001667 case Primitive::kPrimFloat:
1668 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001669 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001670 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001671 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001672 case Primitive::kPrimVoid:
1673 LOG(FATAL) << "Unreachable type " << type;
1674 }
1675}
1676
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001677void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1678 CPURegister src,
1679 const MemOperand& dst) {
1680 UseScratchRegisterScope temps(GetVIXLAssembler());
1681 Register temp_base = temps.AcquireX();
1682
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001683 DCHECK(!dst.IsPreIndex());
1684 DCHECK(!dst.IsPostIndex());
1685
1686 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001687 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001688 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001689 MemOperand base = MemOperand(temp_base);
1690 switch (type) {
1691 case Primitive::kPrimBoolean:
1692 case Primitive::kPrimByte:
1693 __ Stlrb(Register(src), base);
1694 break;
1695 case Primitive::kPrimChar:
1696 case Primitive::kPrimShort:
1697 __ Stlrh(Register(src), base);
1698 break;
1699 case Primitive::kPrimInt:
1700 case Primitive::kPrimNot:
1701 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001702 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001703 __ Stlr(Register(src), base);
1704 break;
1705 case Primitive::kPrimFloat:
1706 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001707 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001708 Register temp_src;
1709 if (src.IsZero()) {
1710 // The zero register is used to avoid synthesizing zero constants.
1711 temp_src = Register(src);
1712 } else {
1713 DCHECK(src.IsFPRegister());
1714 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1715 __ Fmov(temp_src, FPRegister(src));
1716 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001717
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001718 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001719 break;
1720 }
1721 case Primitive::kPrimVoid:
1722 LOG(FATAL) << "Unreachable type " << type;
1723 }
1724}
1725
Calin Juravle175dc732015-08-25 15:42:32 +01001726void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1727 HInstruction* instruction,
1728 uint32_t dex_pc,
1729 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001730 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001731 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001732 if (EntrypointRequiresStackMap(entrypoint)) {
1733 RecordPcInfo(instruction, dex_pc, slow_path);
1734 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001735}
1736
Roland Levillaindec8f632016-07-22 17:10:06 +01001737void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1738 HInstruction* instruction,
1739 SlowPathCode* slow_path) {
1740 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001741 GenerateInvokeRuntime(entry_point_offset);
1742}
1743
1744void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001745 BlockPoolsScope block_pools(GetVIXLAssembler());
1746 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1747 __ Blr(lr);
1748}
1749
Alexandre Rames67555f72014-11-18 10:55:16 +00001750void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001751 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001752 UseScratchRegisterScope temps(GetVIXLAssembler());
1753 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001754 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1755
Serban Constantinescu02164b32014-11-13 14:05:07 +00001756 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001757 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1758 __ Add(temp, class_reg, status_offset);
1759 __ Ldar(temp, HeapOperand(temp));
1760 __ Cmp(temp, mirror::Class::kStatusInitialized);
1761 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001762 __ Bind(slow_path->GetExitLabel());
1763}
Alexandre Rames5319def2014-10-23 10:03:10 +01001764
Roland Levillain44015862016-01-22 11:47:17 +00001765void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001766 BarrierType type = BarrierAll;
1767
1768 switch (kind) {
1769 case MemBarrierKind::kAnyAny:
1770 case MemBarrierKind::kAnyStore: {
1771 type = BarrierAll;
1772 break;
1773 }
1774 case MemBarrierKind::kLoadAny: {
1775 type = BarrierReads;
1776 break;
1777 }
1778 case MemBarrierKind::kStoreStore: {
1779 type = BarrierWrites;
1780 break;
1781 }
1782 default:
1783 LOG(FATAL) << "Unexpected memory barrier " << kind;
1784 }
1785 __ Dmb(InnerShareable, type);
1786}
1787
Serban Constantinescu02164b32014-11-13 14:05:07 +00001788void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1789 HBasicBlock* successor) {
1790 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001791 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1792 if (slow_path == nullptr) {
1793 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1794 instruction->SetSlowPath(slow_path);
1795 codegen_->AddSlowPath(slow_path);
1796 if (successor != nullptr) {
1797 DCHECK(successor->IsLoopHeader());
1798 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1799 }
1800 } else {
1801 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1802 }
1803
Serban Constantinescu02164b32014-11-13 14:05:07 +00001804 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1805 Register temp = temps.AcquireW();
1806
Andreas Gampe542451c2016-07-26 09:02:02 -07001807 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001808 if (successor == nullptr) {
1809 __ Cbnz(temp, slow_path->GetEntryLabel());
1810 __ Bind(slow_path->GetReturnLabel());
1811 } else {
1812 __ Cbz(temp, codegen_->GetLabelOf(successor));
1813 __ B(slow_path->GetEntryLabel());
1814 // slow_path will return to GetLabelOf(successor).
1815 }
1816}
1817
Alexandre Rames5319def2014-10-23 10:03:10 +01001818InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1819 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001820 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001821 assembler_(codegen->GetAssembler()),
1822 codegen_(codegen) {}
1823
1824#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001825 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001826
1827#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1828
1829enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001830 // Using a base helps identify when we hit such breakpoints.
1831 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001832#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1833 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1834#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1835};
1836
1837#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001838 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001839 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1840 } \
1841 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1842 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1843 locations->SetOut(Location::Any()); \
1844 }
1845 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1846#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1847
1848#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001849#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001850
Alexandre Rames67555f72014-11-18 10:55:16 +00001851void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001852 DCHECK_EQ(instr->InputCount(), 2U);
1853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1854 Primitive::Type type = instr->GetResultType();
1855 switch (type) {
1856 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001857 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001858 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001859 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001861 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001862
1863 case Primitive::kPrimFloat:
1864 case Primitive::kPrimDouble:
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
1866 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001867 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001868 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001869
Alexandre Rames5319def2014-10-23 10:03:10 +01001870 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001871 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001872 }
1873}
1874
Alexandre Rames09a99962015-04-15 11:47:56 +01001875void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001876 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1877
1878 bool object_field_get_with_read_barrier =
1879 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001880 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001881 new (GetGraph()->GetArena()) LocationSummary(instruction,
1882 object_field_get_with_read_barrier ?
1883 LocationSummary::kCallOnSlowPath :
1884 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001885 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001886 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001887 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001888 locations->SetInAt(0, Location::RequiresRegister());
1889 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1890 locations->SetOut(Location::RequiresFpuRegister());
1891 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001892 // The output overlaps for an object field get when read barriers
1893 // are enabled: we do not want the load to overwrite the object's
1894 // location, as we need it to emit the read barrier.
1895 locations->SetOut(
1896 Location::RequiresRegister(),
1897 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001898 }
1899}
1900
1901void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1902 const FieldInfo& field_info) {
1903 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001904 LocationSummary* locations = instruction->GetLocations();
1905 Location base_loc = locations->InAt(0);
1906 Location out = locations->Out();
1907 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001908 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001909 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001910 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001911
Roland Levillain44015862016-01-22 11:47:17 +00001912 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1913 // Object FieldGet with Baker's read barrier case.
1914 MacroAssembler* masm = GetVIXLAssembler();
1915 UseScratchRegisterScope temps(masm);
1916 // /* HeapReference<Object> */ out = *(base + offset)
1917 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1918 Register temp = temps.AcquireW();
1919 // Note that potential implicit null checks are handled in this
1920 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1921 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1922 instruction,
1923 out,
1924 base,
1925 offset,
1926 temp,
1927 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001928 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001929 } else {
1930 // General case.
1931 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001932 // Note that a potential implicit null check is handled in this
1933 // CodeGeneratorARM64::LoadAcquire call.
1934 // NB: LoadAcquire will record the pc info if needed.
1935 codegen_->LoadAcquire(
1936 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001937 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001938 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001939 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001940 }
Roland Levillain44015862016-01-22 11:47:17 +00001941 if (field_type == Primitive::kPrimNot) {
1942 // If read barriers are enabled, emit read barriers other than
1943 // Baker's using a slow path (and also unpoison the loaded
1944 // reference, if heap poisoning is enabled).
1945 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1946 }
Roland Levillain4d027112015-07-01 15:41:14 +01001947 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001948}
1949
1950void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1951 LocationSummary* locations =
1952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1953 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001954 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1955 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1956 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001957 locations->SetInAt(1, Location::RequiresFpuRegister());
1958 } else {
1959 locations->SetInAt(1, Location::RequiresRegister());
1960 }
1961}
1962
1963void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001964 const FieldInfo& field_info,
1965 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001966 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001967 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001968
1969 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001970 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001971 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001972 Offset offset = field_info.GetFieldOffset();
1973 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001974
Roland Levillain4d027112015-07-01 15:41:14 +01001975 {
1976 // We use a block to end the scratch scope before the write barrier, thus
1977 // freeing the temporary registers so they can be used in `MarkGCCard`.
1978 UseScratchRegisterScope temps(GetVIXLAssembler());
1979
1980 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1981 DCHECK(value.IsW());
1982 Register temp = temps.AcquireW();
1983 __ Mov(temp, value.W());
1984 GetAssembler()->PoisonHeapReference(temp.W());
1985 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001986 }
Roland Levillain4d027112015-07-01 15:41:14 +01001987
1988 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001989 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1990 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001991 } else {
1992 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1993 codegen_->MaybeRecordImplicitNullCheck(instruction);
1994 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001995 }
1996
1997 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001998 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001999 }
2000}
2001
Alexandre Rames67555f72014-11-18 10:55:16 +00002002void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002003 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002004
2005 switch (type) {
2006 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002007 case Primitive::kPrimLong: {
2008 Register dst = OutputRegister(instr);
2009 Register lhs = InputRegisterAt(instr, 0);
2010 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002011 if (instr->IsAdd()) {
2012 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002013 } else if (instr->IsAnd()) {
2014 __ And(dst, lhs, rhs);
2015 } else if (instr->IsOr()) {
2016 __ Orr(dst, lhs, rhs);
2017 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002018 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002019 } else if (instr->IsRor()) {
2020 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002021 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002022 __ Ror(dst, lhs, shift);
2023 } else {
2024 // Ensure shift distance is in the same size register as the result. If
2025 // we are rotating a long and the shift comes in a w register originally,
2026 // we don't need to sxtw for use as an x since the shift distances are
2027 // all & reg_bits - 1.
2028 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2029 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002030 } else {
2031 DCHECK(instr->IsXor());
2032 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002033 }
2034 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002035 }
2036 case Primitive::kPrimFloat:
2037 case Primitive::kPrimDouble: {
2038 FPRegister dst = OutputFPRegister(instr);
2039 FPRegister lhs = InputFPRegisterAt(instr, 0);
2040 FPRegister rhs = InputFPRegisterAt(instr, 1);
2041 if (instr->IsAdd()) {
2042 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002043 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002044 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002045 } else {
2046 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002047 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002048 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002049 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002050 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002051 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002052 }
2053}
2054
Serban Constantinescu02164b32014-11-13 14:05:07 +00002055void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2056 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2057
2058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2059 Primitive::Type type = instr->GetResultType();
2060 switch (type) {
2061 case Primitive::kPrimInt:
2062 case Primitive::kPrimLong: {
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2065 locations->SetOut(Location::RequiresRegister());
2066 break;
2067 }
2068 default:
2069 LOG(FATAL) << "Unexpected shift type " << type;
2070 }
2071}
2072
2073void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2074 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2075
2076 Primitive::Type type = instr->GetType();
2077 switch (type) {
2078 case Primitive::kPrimInt:
2079 case Primitive::kPrimLong: {
2080 Register dst = OutputRegister(instr);
2081 Register lhs = InputRegisterAt(instr, 0);
2082 Operand rhs = InputOperandAt(instr, 1);
2083 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002084 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002085 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002086 if (instr->IsShl()) {
2087 __ Lsl(dst, lhs, shift_value);
2088 } else if (instr->IsShr()) {
2089 __ Asr(dst, lhs, shift_value);
2090 } else {
2091 __ Lsr(dst, lhs, shift_value);
2092 }
2093 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002094 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002095
2096 if (instr->IsShl()) {
2097 __ Lsl(dst, lhs, rhs_reg);
2098 } else if (instr->IsShr()) {
2099 __ Asr(dst, lhs, rhs_reg);
2100 } else {
2101 __ Lsr(dst, lhs, rhs_reg);
2102 }
2103 }
2104 break;
2105 }
2106 default:
2107 LOG(FATAL) << "Unexpected shift operation type " << type;
2108 }
2109}
2110
Alexandre Rames5319def2014-10-23 10:03:10 +01002111void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002112 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002113}
2114
2115void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002116 HandleBinaryOp(instruction);
2117}
2118
2119void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2120 HandleBinaryOp(instruction);
2121}
2122
2123void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2124 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002125}
2126
Artem Serov7fc63502016-02-09 17:15:29 +00002127void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002128 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2129 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2130 locations->SetInAt(0, Location::RequiresRegister());
2131 // There is no immediate variant of negated bitwise instructions in AArch64.
2132 locations->SetInAt(1, Location::RequiresRegister());
2133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2134}
2135
Artem Serov7fc63502016-02-09 17:15:29 +00002136void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002137 Register dst = OutputRegister(instr);
2138 Register lhs = InputRegisterAt(instr, 0);
2139 Register rhs = InputRegisterAt(instr, 1);
2140
2141 switch (instr->GetOpKind()) {
2142 case HInstruction::kAnd:
2143 __ Bic(dst, lhs, rhs);
2144 break;
2145 case HInstruction::kOr:
2146 __ Orn(dst, lhs, rhs);
2147 break;
2148 case HInstruction::kXor:
2149 __ Eon(dst, lhs, rhs);
2150 break;
2151 default:
2152 LOG(FATAL) << "Unreachable";
2153 }
2154}
2155
Alexandre Rames8626b742015-11-25 16:28:08 +00002156void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2157 HArm64DataProcWithShifterOp* instruction) {
2158 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2159 instruction->GetType() == Primitive::kPrimLong);
2160 LocationSummary* locations =
2161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2162 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2163 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2164 } else {
2165 locations->SetInAt(0, Location::RequiresRegister());
2166 }
2167 locations->SetInAt(1, Location::RequiresRegister());
2168 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2169}
2170
2171void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2172 HArm64DataProcWithShifterOp* instruction) {
2173 Primitive::Type type = instruction->GetType();
2174 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2175 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2176 Register out = OutputRegister(instruction);
2177 Register left;
2178 if (kind != HInstruction::kNeg) {
2179 left = InputRegisterAt(instruction, 0);
2180 }
2181 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2182 // shifter operand operation, the IR generating `right_reg` (input to the type
2183 // conversion) can have a different type from the current instruction's type,
2184 // so we manually indicate the type.
2185 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002186 int64_t shift_amount = instruction->GetShiftAmount() &
2187 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002188
2189 Operand right_operand(0);
2190
2191 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2192 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2193 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2194 } else {
2195 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2196 }
2197
2198 // Logical binary operations do not support extension operations in the
2199 // operand. Note that VIXL would still manage if it was passed by generating
2200 // the extension as a separate instruction.
2201 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2202 DCHECK(!right_operand.IsExtendedRegister() ||
2203 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2204 kind != HInstruction::kNeg));
2205 switch (kind) {
2206 case HInstruction::kAdd:
2207 __ Add(out, left, right_operand);
2208 break;
2209 case HInstruction::kAnd:
2210 __ And(out, left, right_operand);
2211 break;
2212 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002213 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002214 __ Neg(out, right_operand);
2215 break;
2216 case HInstruction::kOr:
2217 __ Orr(out, left, right_operand);
2218 break;
2219 case HInstruction::kSub:
2220 __ Sub(out, left, right_operand);
2221 break;
2222 case HInstruction::kXor:
2223 __ Eor(out, left, right_operand);
2224 break;
2225 default:
2226 LOG(FATAL) << "Unexpected operation kind: " << kind;
2227 UNREACHABLE();
2228 }
2229}
2230
Artem Serov328429f2016-07-06 16:23:04 +01002231void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002232 LocationSummary* locations =
2233 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2234 locations->SetInAt(0, Location::RequiresRegister());
2235 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2236 locations->SetOut(Location::RequiresRegister());
2237}
2238
Roland Levillain19c54192016-11-04 13:44:09 +00002239void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002240 __ Add(OutputRegister(instruction),
2241 InputRegisterAt(instruction, 0),
2242 Operand(InputOperandAt(instruction, 1)));
2243}
2244
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002245void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002248 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2249 if (instr->GetOpKind() == HInstruction::kSub &&
2250 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002251 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002252 // Don't allocate register for Mneg instruction.
2253 } else {
2254 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2255 Location::RequiresRegister());
2256 }
2257 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2258 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2260}
2261
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002262void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002263 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002264 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2265 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002266
2267 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2268 // This fixup should be carried out for all multiply-accumulate instructions:
2269 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2270 if (instr->GetType() == Primitive::kPrimLong &&
2271 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2272 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002273 vixl::aarch64::Instruction* prev =
2274 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002275 if (prev->IsLoadOrStore()) {
2276 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002277 vixl::CodeBufferCheckScope scope(masm,
2278 kInstructionSize,
2279 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2280 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002281 __ nop();
2282 }
2283 }
2284
2285 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002286 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002287 __ Madd(res, mul_left, mul_right, accumulator);
2288 } else {
2289 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002290 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002291 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002292 __ Mneg(res, mul_left, mul_right);
2293 } else {
2294 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2295 __ Msub(res, mul_left, mul_right, accumulator);
2296 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002297 }
2298}
2299
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002300void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002301 bool object_array_get_with_read_barrier =
2302 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002303 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002304 new (GetGraph()->GetArena()) LocationSummary(instruction,
2305 object_array_get_with_read_barrier ?
2306 LocationSummary::kCallOnSlowPath :
2307 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002308 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002309 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002310 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002311 locations->SetInAt(0, Location::RequiresRegister());
2312 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002313 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2314 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2315 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002316 // The output overlaps in the case of an object array get with
2317 // read barriers enabled: we do not want the move to overwrite the
2318 // array's location, as we need it to emit the read barrier.
2319 locations->SetOut(
2320 Location::RequiresRegister(),
2321 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002322 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002323}
2324
2325void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002326 Primitive::Type type = instruction->GetType();
2327 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002328 LocationSummary* locations = instruction->GetLocations();
2329 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002330 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002331 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002332 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2333 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002334 MacroAssembler* masm = GetVIXLAssembler();
2335 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002336 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002337 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002338
Roland Levillain19c54192016-11-04 13:44:09 +00002339 // The read barrier instrumentation of object ArrayGet instructions
2340 // does not support the HIntermediateAddress instruction.
2341 DCHECK(!((type == Primitive::kPrimNot) &&
2342 instruction->GetArray()->IsIntermediateAddress() &&
2343 kEmitCompilerReadBarrier));
2344
Roland Levillain44015862016-01-22 11:47:17 +00002345 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2346 // Object ArrayGet with Baker's read barrier case.
2347 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002348 // Note that a potential implicit null check is handled in the
2349 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2350 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2351 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002352 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002353 // General case.
2354 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002355 Register length;
2356 if (maybe_compressed_char_at) {
2357 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2358 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002359 if (instruction->GetArray()->IsIntermediateAddress()) {
2360 DCHECK_LT(count_offset, offset);
2361 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2362 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2363 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2364 } else {
2365 __ Ldr(length, HeapOperand(obj, count_offset));
2366 }
jessicahandojo05765752016-09-09 19:01:32 -07002367 codegen_->MaybeRecordImplicitNullCheck(instruction);
2368 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002369 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002370 if (maybe_compressed_char_at) {
2371 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002372 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2373 "Expecting 0=compressed, 1=uncompressed");
2374 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002375 __ Ldrb(Register(OutputCPURegister(instruction)),
2376 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2377 __ B(&done);
2378 __ Bind(&uncompressed_load);
2379 __ Ldrh(Register(OutputCPURegister(instruction)),
2380 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2381 __ Bind(&done);
2382 } else {
2383 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2384 source = HeapOperand(obj, offset);
2385 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002386 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002387 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002388 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002389 // We do not need to compute the intermediate address from the array: the
2390 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002391 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002392 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002393 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002394 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2395 }
2396 temp = obj;
2397 } else {
2398 __ Add(temp, obj, offset);
2399 }
jessicahandojo05765752016-09-09 19:01:32 -07002400 if (maybe_compressed_char_at) {
2401 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002402 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2403 "Expecting 0=compressed, 1=uncompressed");
2404 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002405 __ Ldrb(Register(OutputCPURegister(instruction)),
2406 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2407 __ B(&done);
2408 __ Bind(&uncompressed_load);
2409 __ Ldrh(Register(OutputCPURegister(instruction)),
2410 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2411 __ Bind(&done);
2412 } else {
2413 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2414 }
Roland Levillain44015862016-01-22 11:47:17 +00002415 }
jessicahandojo05765752016-09-09 19:01:32 -07002416 if (!maybe_compressed_char_at) {
2417 codegen_->Load(type, OutputCPURegister(instruction), source);
2418 codegen_->MaybeRecordImplicitNullCheck(instruction);
2419 }
Roland Levillain44015862016-01-22 11:47:17 +00002420
2421 if (type == Primitive::kPrimNot) {
2422 static_assert(
2423 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2424 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2425 Location obj_loc = locations->InAt(0);
2426 if (index.IsConstant()) {
2427 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2428 } else {
2429 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2430 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002431 }
Roland Levillain4d027112015-07-01 15:41:14 +01002432 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002433}
2434
Alexandre Rames5319def2014-10-23 10:03:10 +01002435void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2437 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002439}
2440
2441void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002442 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002443 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002444 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002445 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002446 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002447 // Mask out compression flag from String's array length.
2448 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002449 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002450 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002451}
2452
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002453void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002454 Primitive::Type value_type = instruction->GetComponentType();
2455
2456 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2458 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002459 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002460 LocationSummary::kCallOnSlowPath :
2461 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002462 locations->SetInAt(0, Location::RequiresRegister());
2463 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002464 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2465 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2466 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002467 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002468 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002469 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002470 }
2471}
2472
2473void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2474 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002475 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002476 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002477 bool needs_write_barrier =
2478 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002479
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002480 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002481 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002482 CPURegister source = value;
2483 Location index = locations->InAt(1);
2484 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2485 MemOperand destination = HeapOperand(array);
2486 MacroAssembler* masm = GetVIXLAssembler();
2487 BlockPoolsScope block_pools(masm);
2488
2489 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002490 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002491 if (index.IsConstant()) {
2492 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2493 destination = HeapOperand(array, offset);
2494 } else {
2495 UseScratchRegisterScope temps(masm);
2496 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002497 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002498 // We do not need to compute the intermediate address from the array: the
2499 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002500 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002501 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002502 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002503 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2504 }
2505 temp = array;
2506 } else {
2507 __ Add(temp, array, offset);
2508 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002509 destination = HeapOperand(temp,
2510 XRegisterFrom(index),
2511 LSL,
2512 Primitive::ComponentSizeShift(value_type));
2513 }
2514 codegen_->Store(value_type, value, destination);
2515 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002516 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002517 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002518 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002519 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002520 {
2521 // We use a block to end the scratch scope before the write barrier, thus
2522 // freeing the temporary registers so they can be used in `MarkGCCard`.
2523 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002524 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002525 if (index.IsConstant()) {
2526 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002527 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002528 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002529 destination = HeapOperand(temp,
2530 XRegisterFrom(index),
2531 LSL,
2532 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002533 }
2534
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002535 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2536 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2537 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2538
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002539 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002540 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2541 codegen_->AddSlowPath(slow_path);
2542 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002543 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002544 __ Cbnz(Register(value), &non_zero);
2545 if (!index.IsConstant()) {
2546 __ Add(temp, array, offset);
2547 }
2548 __ Str(wzr, destination);
2549 codegen_->MaybeRecordImplicitNullCheck(instruction);
2550 __ B(&done);
2551 __ Bind(&non_zero);
2552 }
2553
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002554 // Note that when Baker read barriers are enabled, the type
2555 // checks are performed without read barriers. This is fine,
2556 // even in the case where a class object is in the from-space
2557 // after the flip, as a comparison involving such a type would
2558 // not produce a false positive; it may of course produce a
2559 // false negative, in which case we would take the ArraySet
2560 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002561
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002562 Register temp2 = temps.AcquireSameSizeAs(array);
2563 // /* HeapReference<Class> */ temp = array->klass_
2564 __ Ldr(temp, HeapOperand(array, class_offset));
2565 codegen_->MaybeRecordImplicitNullCheck(instruction);
2566 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002567
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002568 // /* HeapReference<Class> */ temp = temp->component_type_
2569 __ Ldr(temp, HeapOperand(temp, component_offset));
2570 // /* HeapReference<Class> */ temp2 = value->klass_
2571 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2572 // If heap poisoning is enabled, no need to unpoison `temp`
2573 // nor `temp2`, as we are comparing two poisoned references.
2574 __ Cmp(temp, temp2);
2575 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002576
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002577 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2578 vixl::aarch64::Label do_put;
2579 __ B(eq, &do_put);
2580 // If heap poisoning is enabled, the `temp` reference has
2581 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002582 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2583
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002584 // /* HeapReference<Class> */ temp = temp->super_class_
2585 __ Ldr(temp, HeapOperand(temp, super_offset));
2586 // If heap poisoning is enabled, no need to unpoison
2587 // `temp`, as we are comparing against null below.
2588 __ Cbnz(temp, slow_path->GetEntryLabel());
2589 __ Bind(&do_put);
2590 } else {
2591 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002592 }
2593 }
2594
2595 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002596 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002597 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002598 __ Mov(temp2, value.W());
2599 GetAssembler()->PoisonHeapReference(temp2);
2600 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002601 }
2602
2603 if (!index.IsConstant()) {
2604 __ Add(temp, array, offset);
2605 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002606 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002607
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002608 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002609 codegen_->MaybeRecordImplicitNullCheck(instruction);
2610 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002611 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002612
2613 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2614
2615 if (done.IsLinked()) {
2616 __ Bind(&done);
2617 }
2618
2619 if (slow_path != nullptr) {
2620 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002621 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002622 }
2623}
2624
Alexandre Rames67555f72014-11-18 10:55:16 +00002625void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002626 RegisterSet caller_saves = RegisterSet::Empty();
2627 InvokeRuntimeCallingConvention calling_convention;
2628 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2629 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2630 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002631 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002632 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002633}
2634
2635void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002636 BoundsCheckSlowPathARM64* slow_path =
2637 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002638 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002639 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2640 __ B(slow_path->GetEntryLabel(), hs);
2641}
2642
Alexandre Rames67555f72014-11-18 10:55:16 +00002643void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2644 LocationSummary* locations =
2645 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2646 locations->SetInAt(0, Location::RequiresRegister());
2647 if (check->HasUses()) {
2648 locations->SetOut(Location::SameAsFirstInput());
2649 }
2650}
2651
2652void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2653 // We assume the class is not null.
2654 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2655 check->GetLoadClass(), check, check->GetDexPc(), true);
2656 codegen_->AddSlowPath(slow_path);
2657 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2658}
2659
Roland Levillain1a653882016-03-18 18:05:57 +00002660static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2661 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2662 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2663}
2664
2665void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2666 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2667 Location rhs_loc = instruction->GetLocations()->InAt(1);
2668 if (rhs_loc.IsConstant()) {
2669 // 0.0 is the only immediate that can be encoded directly in
2670 // an FCMP instruction.
2671 //
2672 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2673 // specify that in a floating-point comparison, positive zero
2674 // and negative zero are considered equal, so we can use the
2675 // literal 0.0 for both cases here.
2676 //
2677 // Note however that some methods (Float.equal, Float.compare,
2678 // Float.compareTo, Double.equal, Double.compare,
2679 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2680 // StrictMath.min) consider 0.0 to be (strictly) greater than
2681 // -0.0. So if we ever translate calls to these methods into a
2682 // HCompare instruction, we must handle the -0.0 case with
2683 // care here.
2684 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2685 __ Fcmp(lhs_reg, 0.0);
2686 } else {
2687 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2688 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002689}
2690
Serban Constantinescu02164b32014-11-13 14:05:07 +00002691void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002692 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002693 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2694 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002695 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002696 case Primitive::kPrimBoolean:
2697 case Primitive::kPrimByte:
2698 case Primitive::kPrimShort:
2699 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002700 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002701 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002702 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002703 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2705 break;
2706 }
2707 case Primitive::kPrimFloat:
2708 case Primitive::kPrimDouble: {
2709 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002710 locations->SetInAt(1,
2711 IsFloatingPointZeroConstant(compare->InputAt(1))
2712 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2713 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002714 locations->SetOut(Location::RequiresRegister());
2715 break;
2716 }
2717 default:
2718 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2719 }
2720}
2721
2722void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2723 Primitive::Type in_type = compare->InputAt(0)->GetType();
2724
2725 // 0 if: left == right
2726 // 1 if: left > right
2727 // -1 if: left < right
2728 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002729 case Primitive::kPrimBoolean:
2730 case Primitive::kPrimByte:
2731 case Primitive::kPrimShort:
2732 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002733 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002734 case Primitive::kPrimLong: {
2735 Register result = OutputRegister(compare);
2736 Register left = InputRegisterAt(compare, 0);
2737 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002738 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002739 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2740 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002741 break;
2742 }
2743 case Primitive::kPrimFloat:
2744 case Primitive::kPrimDouble: {
2745 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002746 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002747 __ Cset(result, ne);
2748 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002749 break;
2750 }
2751 default:
2752 LOG(FATAL) << "Unimplemented compare type " << in_type;
2753 }
2754}
2755
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002756void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002758
2759 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2760 locations->SetInAt(0, Location::RequiresFpuRegister());
2761 locations->SetInAt(1,
2762 IsFloatingPointZeroConstant(instruction->InputAt(1))
2763 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2764 : Location::RequiresFpuRegister());
2765 } else {
2766 // Integer cases.
2767 locations->SetInAt(0, Location::RequiresRegister());
2768 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2769 }
2770
David Brazdilb3e773e2016-01-26 11:28:37 +00002771 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002773 }
2774}
2775
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002776void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002777 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002778 return;
2779 }
2780
2781 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002782 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002783 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002784
Roland Levillain7f63c522015-07-13 15:54:55 +00002785 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002786 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002787 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002788 } else {
2789 // Integer cases.
2790 Register lhs = InputRegisterAt(instruction, 0);
2791 Operand rhs = InputOperandAt(instruction, 1);
2792 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002793 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002794 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002795}
2796
2797#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2798 M(Equal) \
2799 M(NotEqual) \
2800 M(LessThan) \
2801 M(LessThanOrEqual) \
2802 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002803 M(GreaterThanOrEqual) \
2804 M(Below) \
2805 M(BelowOrEqual) \
2806 M(Above) \
2807 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002808#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002809void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2810void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002811FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002812#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002813#undef FOR_EACH_CONDITION_INSTRUCTION
2814
Zheng Xuc6667102015-05-15 16:08:45 +08002815void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2816 DCHECK(instruction->IsDiv() || instruction->IsRem());
2817
2818 LocationSummary* locations = instruction->GetLocations();
2819 Location second = locations->InAt(1);
2820 DCHECK(second.IsConstant());
2821
2822 Register out = OutputRegister(instruction);
2823 Register dividend = InputRegisterAt(instruction, 0);
2824 int64_t imm = Int64FromConstant(second.GetConstant());
2825 DCHECK(imm == 1 || imm == -1);
2826
2827 if (instruction->IsRem()) {
2828 __ Mov(out, 0);
2829 } else {
2830 if (imm == 1) {
2831 __ Mov(out, dividend);
2832 } else {
2833 __ Neg(out, dividend);
2834 }
2835 }
2836}
2837
2838void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2839 DCHECK(instruction->IsDiv() || instruction->IsRem());
2840
2841 LocationSummary* locations = instruction->GetLocations();
2842 Location second = locations->InAt(1);
2843 DCHECK(second.IsConstant());
2844
2845 Register out = OutputRegister(instruction);
2846 Register dividend = InputRegisterAt(instruction, 0);
2847 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002848 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002849 int ctz_imm = CTZ(abs_imm);
2850
2851 UseScratchRegisterScope temps(GetVIXLAssembler());
2852 Register temp = temps.AcquireSameSizeAs(out);
2853
2854 if (instruction->IsDiv()) {
2855 __ Add(temp, dividend, abs_imm - 1);
2856 __ Cmp(dividend, 0);
2857 __ Csel(out, temp, dividend, lt);
2858 if (imm > 0) {
2859 __ Asr(out, out, ctz_imm);
2860 } else {
2861 __ Neg(out, Operand(out, ASR, ctz_imm));
2862 }
2863 } else {
2864 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2865 __ Asr(temp, dividend, bits - 1);
2866 __ Lsr(temp, temp, bits - ctz_imm);
2867 __ Add(out, dividend, temp);
2868 __ And(out, out, abs_imm - 1);
2869 __ Sub(out, out, temp);
2870 }
2871}
2872
2873void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2874 DCHECK(instruction->IsDiv() || instruction->IsRem());
2875
2876 LocationSummary* locations = instruction->GetLocations();
2877 Location second = locations->InAt(1);
2878 DCHECK(second.IsConstant());
2879
2880 Register out = OutputRegister(instruction);
2881 Register dividend = InputRegisterAt(instruction, 0);
2882 int64_t imm = Int64FromConstant(second.GetConstant());
2883
2884 Primitive::Type type = instruction->GetResultType();
2885 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2886
2887 int64_t magic;
2888 int shift;
2889 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2890
2891 UseScratchRegisterScope temps(GetVIXLAssembler());
2892 Register temp = temps.AcquireSameSizeAs(out);
2893
2894 // temp = get_high(dividend * magic)
2895 __ Mov(temp, magic);
2896 if (type == Primitive::kPrimLong) {
2897 __ Smulh(temp, dividend, temp);
2898 } else {
2899 __ Smull(temp.X(), dividend, temp);
2900 __ Lsr(temp.X(), temp.X(), 32);
2901 }
2902
2903 if (imm > 0 && magic < 0) {
2904 __ Add(temp, temp, dividend);
2905 } else if (imm < 0 && magic > 0) {
2906 __ Sub(temp, temp, dividend);
2907 }
2908
2909 if (shift != 0) {
2910 __ Asr(temp, temp, shift);
2911 }
2912
2913 if (instruction->IsDiv()) {
2914 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2915 } else {
2916 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2917 // TODO: Strength reduction for msub.
2918 Register temp_imm = temps.AcquireSameSizeAs(out);
2919 __ Mov(temp_imm, imm);
2920 __ Msub(out, temp, temp_imm, dividend);
2921 }
2922}
2923
2924void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2925 DCHECK(instruction->IsDiv() || instruction->IsRem());
2926 Primitive::Type type = instruction->GetResultType();
2927 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2928
2929 LocationSummary* locations = instruction->GetLocations();
2930 Register out = OutputRegister(instruction);
2931 Location second = locations->InAt(1);
2932
2933 if (second.IsConstant()) {
2934 int64_t imm = Int64FromConstant(second.GetConstant());
2935
2936 if (imm == 0) {
2937 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2938 } else if (imm == 1 || imm == -1) {
2939 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002940 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002941 DivRemByPowerOfTwo(instruction);
2942 } else {
2943 DCHECK(imm <= -2 || imm >= 2);
2944 GenerateDivRemWithAnyConstant(instruction);
2945 }
2946 } else {
2947 Register dividend = InputRegisterAt(instruction, 0);
2948 Register divisor = InputRegisterAt(instruction, 1);
2949 if (instruction->IsDiv()) {
2950 __ Sdiv(out, dividend, divisor);
2951 } else {
2952 UseScratchRegisterScope temps(GetVIXLAssembler());
2953 Register temp = temps.AcquireSameSizeAs(out);
2954 __ Sdiv(temp, dividend, divisor);
2955 __ Msub(out, temp, divisor, dividend);
2956 }
2957 }
2958}
2959
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002960void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2961 LocationSummary* locations =
2962 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2963 switch (div->GetResultType()) {
2964 case Primitive::kPrimInt:
2965 case Primitive::kPrimLong:
2966 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002967 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2969 break;
2970
2971 case Primitive::kPrimFloat:
2972 case Primitive::kPrimDouble:
2973 locations->SetInAt(0, Location::RequiresFpuRegister());
2974 locations->SetInAt(1, Location::RequiresFpuRegister());
2975 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2976 break;
2977
2978 default:
2979 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2980 }
2981}
2982
2983void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2984 Primitive::Type type = div->GetResultType();
2985 switch (type) {
2986 case Primitive::kPrimInt:
2987 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002988 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002989 break;
2990
2991 case Primitive::kPrimFloat:
2992 case Primitive::kPrimDouble:
2993 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2994 break;
2995
2996 default:
2997 LOG(FATAL) << "Unexpected div type " << type;
2998 }
2999}
3000
Alexandre Rames67555f72014-11-18 10:55:16 +00003001void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003002 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003003 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003004}
3005
3006void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3007 SlowPathCodeARM64* slow_path =
3008 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3009 codegen_->AddSlowPath(slow_path);
3010 Location value = instruction->GetLocations()->InAt(0);
3011
Alexandre Rames3e69f162014-12-10 10:36:50 +00003012 Primitive::Type type = instruction->GetType();
3013
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003014 if (!Primitive::IsIntegralType(type)) {
3015 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003016 return;
3017 }
3018
Alexandre Rames67555f72014-11-18 10:55:16 +00003019 if (value.IsConstant()) {
3020 int64_t divisor = Int64ConstantFrom(value);
3021 if (divisor == 0) {
3022 __ B(slow_path->GetEntryLabel());
3023 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003024 // A division by a non-null constant is valid. We don't need to perform
3025 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003026 }
3027 } else {
3028 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3029 }
3030}
3031
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003032void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3033 LocationSummary* locations =
3034 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3035 locations->SetOut(Location::ConstantLocation(constant));
3036}
3037
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003038void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3039 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003040 // Will be generated at use site.
3041}
3042
Alexandre Rames5319def2014-10-23 10:03:10 +01003043void LocationsBuilderARM64::VisitExit(HExit* exit) {
3044 exit->SetLocations(nullptr);
3045}
3046
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003047void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003048}
3049
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003050void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3053 locations->SetOut(Location::ConstantLocation(constant));
3054}
3055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003056void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003057 // Will be generated at use site.
3058}
3059
David Brazdilfc6a86a2015-06-26 10:33:45 +00003060void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003061 DCHECK(!successor->IsExitBlock());
3062 HBasicBlock* block = got->GetBlock();
3063 HInstruction* previous = got->GetPrevious();
3064 HLoopInformation* info = block->GetLoopInformation();
3065
David Brazdil46e2a392015-03-16 17:31:52 +00003066 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003067 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3068 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3069 return;
3070 }
3071 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3072 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3073 }
3074 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003075 __ B(codegen_->GetLabelOf(successor));
3076 }
3077}
3078
David Brazdilfc6a86a2015-06-26 10:33:45 +00003079void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3080 got->SetLocations(nullptr);
3081}
3082
3083void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3084 HandleGoto(got, got->GetSuccessor());
3085}
3086
3087void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3088 try_boundary->SetLocations(nullptr);
3089}
3090
3091void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3092 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3093 if (!successor->IsExitBlock()) {
3094 HandleGoto(try_boundary, successor);
3095 }
3096}
3097
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003098void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003099 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003100 vixl::aarch64::Label* true_target,
3101 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003102 // FP branching requires both targets to be explicit. If either of the targets
3103 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003104 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003105 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003106
David Brazdil0debae72015-11-12 18:37:00 +00003107 if (true_target == nullptr && false_target == nullptr) {
3108 // Nothing to do. The code always falls through.
3109 return;
3110 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003111 // Constant condition, statically compared against "true" (integer value 1).
3112 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003113 if (true_target != nullptr) {
3114 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003115 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003116 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003117 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003118 if (false_target != nullptr) {
3119 __ B(false_target);
3120 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003121 }
David Brazdil0debae72015-11-12 18:37:00 +00003122 return;
3123 }
3124
3125 // The following code generates these patterns:
3126 // (1) true_target == nullptr && false_target != nullptr
3127 // - opposite condition true => branch to false_target
3128 // (2) true_target != nullptr && false_target == nullptr
3129 // - condition true => branch to true_target
3130 // (3) true_target != nullptr && false_target != nullptr
3131 // - condition true => branch to true_target
3132 // - branch to false_target
3133 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003134 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003135 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003136 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003137 if (true_target == nullptr) {
3138 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3139 } else {
3140 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3141 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003142 } else {
3143 // The condition instruction has not been materialized, use its inputs as
3144 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003145 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003146
David Brazdil0debae72015-11-12 18:37:00 +00003147 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003148 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003149 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003150 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003151 IfCondition opposite_condition = condition->GetOppositeCondition();
3152 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003153 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003154 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003155 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003156 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003157 // Integer cases.
3158 Register lhs = InputRegisterAt(condition, 0);
3159 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003160
3161 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003162 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003163 if (true_target == nullptr) {
3164 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3165 non_fallthrough_target = false_target;
3166 } else {
3167 arm64_cond = ARM64Condition(condition->GetCondition());
3168 non_fallthrough_target = true_target;
3169 }
3170
Aart Bik086d27e2016-01-20 17:02:00 -08003171 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003172 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003173 switch (arm64_cond) {
3174 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003175 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003176 break;
3177 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003178 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003179 break;
3180 case lt:
3181 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003182 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003183 break;
3184 case ge:
3185 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003186 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003187 break;
3188 default:
3189 // Without the `static_cast` the compiler throws an error for
3190 // `-Werror=sign-promo`.
3191 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3192 }
3193 } else {
3194 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003195 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003196 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003197 }
3198 }
David Brazdil0debae72015-11-12 18:37:00 +00003199
3200 // If neither branch falls through (case 3), the conditional branch to `true_target`
3201 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3202 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003203 __ B(false_target);
3204 }
David Brazdil0debae72015-11-12 18:37:00 +00003205
3206 if (fallthrough_target.IsLinked()) {
3207 __ Bind(&fallthrough_target);
3208 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003209}
3210
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003211void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003213 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003214 locations->SetInAt(0, Location::RequiresRegister());
3215 }
3216}
3217
3218void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003219 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3220 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003221 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3222 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3223 true_target = nullptr;
3224 }
3225 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3226 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3227 false_target = nullptr;
3228 }
David Brazdil0debae72015-11-12 18:37:00 +00003229 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003230}
3231
3232void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3233 LocationSummary* locations = new (GetGraph()->GetArena())
3234 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003235 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003236 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237 locations->SetInAt(0, Location::RequiresRegister());
3238 }
3239}
3240
3241void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003242 SlowPathCodeARM64* slow_path =
3243 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003244 GenerateTestAndBranch(deoptimize,
3245 /* condition_input_index */ 0,
3246 slow_path->GetEntryLabel(),
3247 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003248}
3249
Mingyao Yang063fc772016-08-02 11:02:54 -07003250void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3251 LocationSummary* locations = new (GetGraph()->GetArena())
3252 LocationSummary(flag, LocationSummary::kNoCall);
3253 locations->SetOut(Location::RequiresRegister());
3254}
3255
3256void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3257 __ Ldr(OutputRegister(flag),
3258 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3259}
3260
David Brazdilc0b601b2016-02-08 14:20:45 +00003261static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3262 return condition->IsCondition() &&
3263 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3264}
3265
Alexandre Rames880f1192016-06-13 16:04:50 +01003266static inline Condition GetConditionForSelect(HCondition* condition) {
3267 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003268 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3269 : ARM64Condition(cond);
3270}
3271
David Brazdil74eb1b22015-12-14 11:44:01 +00003272void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3273 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003274 if (Primitive::IsFloatingPointType(select->GetType())) {
3275 locations->SetInAt(0, Location::RequiresFpuRegister());
3276 locations->SetInAt(1, Location::RequiresFpuRegister());
3277 locations->SetOut(Location::RequiresFpuRegister());
3278 } else {
3279 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3280 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3281 bool is_true_value_constant = cst_true_value != nullptr;
3282 bool is_false_value_constant = cst_false_value != nullptr;
3283 // Ask VIXL whether we should synthesize constants in registers.
3284 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3285 Operand true_op = is_true_value_constant ?
3286 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3287 Operand false_op = is_false_value_constant ?
3288 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3289 bool true_value_in_register = false;
3290 bool false_value_in_register = false;
3291 MacroAssembler::GetCselSynthesisInformation(
3292 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3293 true_value_in_register |= !is_true_value_constant;
3294 false_value_in_register |= !is_false_value_constant;
3295
3296 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3297 : Location::ConstantLocation(cst_true_value));
3298 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3299 : Location::ConstantLocation(cst_false_value));
3300 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003301 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003302
David Brazdil74eb1b22015-12-14 11:44:01 +00003303 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3304 locations->SetInAt(2, Location::RequiresRegister());
3305 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003306}
3307
3308void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003309 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003310 Condition csel_cond;
3311
3312 if (IsBooleanValueOrMaterializedCondition(cond)) {
3313 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003314 // Use the condition flags set by the previous instruction.
3315 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003316 } else {
3317 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003318 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003319 }
3320 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003321 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003322 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003323 } else {
3324 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003325 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003326 }
3327
Alexandre Rames880f1192016-06-13 16:04:50 +01003328 if (Primitive::IsFloatingPointType(select->GetType())) {
3329 __ Fcsel(OutputFPRegister(select),
3330 InputFPRegisterAt(select, 1),
3331 InputFPRegisterAt(select, 0),
3332 csel_cond);
3333 } else {
3334 __ Csel(OutputRegister(select),
3335 InputOperandAt(select, 1),
3336 InputOperandAt(select, 0),
3337 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003338 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003339}
3340
David Srbecky0cf44932015-12-09 14:09:59 +00003341void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3342 new (GetGraph()->GetArena()) LocationSummary(info);
3343}
3344
David Srbeckyd28f4a02016-03-14 17:14:24 +00003345void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3346 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003347}
3348
3349void CodeGeneratorARM64::GenerateNop() {
3350 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003351}
3352
Alexandre Rames5319def2014-10-23 10:03:10 +01003353void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003354 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003355}
3356
3357void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003358 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003359}
3360
3361void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003362 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003363}
3364
3365void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003366 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003367}
3368
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003369// Temp is used for read barrier.
3370static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3371 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003372 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003373 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3374 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3375 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3376 return 1;
3377 }
3378 return 0;
3379}
3380
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003381// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003382// interface pointer, one for loading the current interface.
3383// The other checks have one temp for loading the object's class.
3384static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3385 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3386 return 3;
3387 }
3388 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003389}
3390
Alexandre Rames67555f72014-11-18 10:55:16 +00003391void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003392 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003393 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003394 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003395 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003396 case TypeCheckKind::kExactCheck:
3397 case TypeCheckKind::kAbstractClassCheck:
3398 case TypeCheckKind::kClassHierarchyCheck:
3399 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003400 call_kind =
3401 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003402 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003403 break;
3404 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003405 case TypeCheckKind::kUnresolvedCheck:
3406 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003407 call_kind = LocationSummary::kCallOnSlowPath;
3408 break;
3409 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410
Alexandre Rames67555f72014-11-18 10:55:16 +00003411 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003412 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003413 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003414 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 locations->SetInAt(0, Location::RequiresRegister());
3416 locations->SetInAt(1, Location::RequiresRegister());
3417 // The "out" register is used as a temporary, so it overlaps with the inputs.
3418 // Note that TypeCheckSlowPathARM64 uses this register too.
3419 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003420 // Add temps if necessary for read barriers.
3421 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003422}
3423
3424void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003425 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003426 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003427 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003428 Register obj = InputRegisterAt(instruction, 0);
3429 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003431 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003432 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3433 DCHECK_LE(num_temps, 1u);
3434 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003435 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3436 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3437 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3438 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003439
Scott Wakeling97c72b72016-06-24 16:19:36 +01003440 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003441 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003442
3443 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003444 // Avoid null check if we know `obj` is not null.
3445 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003446 __ Cbz(obj, &zero);
3447 }
3448
Roland Levillain44015862016-01-22 11:47:17 +00003449 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003451 // /* HeapReference<Class> */ out = obj->klass_
3452 GenerateReferenceLoadTwoRegisters(instruction,
3453 out_loc,
3454 obj_loc,
3455 class_offset,
3456 maybe_temp_loc,
3457 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003458 __ Cmp(out, cls);
3459 __ Cset(out, eq);
3460 if (zero.IsLinked()) {
3461 __ B(&done);
3462 }
3463 break;
3464 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003465
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003466 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003467 // /* HeapReference<Class> */ out = obj->klass_
3468 GenerateReferenceLoadTwoRegisters(instruction,
3469 out_loc,
3470 obj_loc,
3471 class_offset,
3472 maybe_temp_loc,
3473 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003474 // If the class is abstract, we eagerly fetch the super class of the
3475 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003476 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003477 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003478 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003479 GenerateReferenceLoadOneRegister(instruction,
3480 out_loc,
3481 super_offset,
3482 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003483 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003484 // If `out` is null, we use it for the result, and jump to `done`.
3485 __ Cbz(out, &done);
3486 __ Cmp(out, cls);
3487 __ B(ne, &loop);
3488 __ Mov(out, 1);
3489 if (zero.IsLinked()) {
3490 __ B(&done);
3491 }
3492 break;
3493 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003496 // /* HeapReference<Class> */ out = obj->klass_
3497 GenerateReferenceLoadTwoRegisters(instruction,
3498 out_loc,
3499 obj_loc,
3500 class_offset,
3501 maybe_temp_loc,
3502 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003503 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003504 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003505 __ Bind(&loop);
3506 __ Cmp(out, cls);
3507 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003508 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003509 GenerateReferenceLoadOneRegister(instruction,
3510 out_loc,
3511 super_offset,
3512 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003513 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003514 __ Cbnz(out, &loop);
3515 // If `out` is null, we use it for the result, and jump to `done`.
3516 __ B(&done);
3517 __ Bind(&success);
3518 __ Mov(out, 1);
3519 if (zero.IsLinked()) {
3520 __ B(&done);
3521 }
3522 break;
3523 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003525 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003526 // /* HeapReference<Class> */ out = obj->klass_
3527 GenerateReferenceLoadTwoRegisters(instruction,
3528 out_loc,
3529 obj_loc,
3530 class_offset,
3531 maybe_temp_loc,
3532 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003533 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003534 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003535 __ Cmp(out, cls);
3536 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003537 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003538 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003539 GenerateReferenceLoadOneRegister(instruction,
3540 out_loc,
3541 component_offset,
3542 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003543 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003544 // If `out` is null, we use it for the result, and jump to `done`.
3545 __ Cbz(out, &done);
3546 __ Ldrh(out, HeapOperand(out, primitive_offset));
3547 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3548 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003549 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003550 __ Mov(out, 1);
3551 __ B(&done);
3552 break;
3553 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003554
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003555 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003556 // No read barrier since the slow path will retry upon failure.
3557 // /* HeapReference<Class> */ out = obj->klass_
3558 GenerateReferenceLoadTwoRegisters(instruction,
3559 out_loc,
3560 obj_loc,
3561 class_offset,
3562 maybe_temp_loc,
3563 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003564 __ Cmp(out, cls);
3565 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003566 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3567 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003568 codegen_->AddSlowPath(slow_path);
3569 __ B(ne, slow_path->GetEntryLabel());
3570 __ Mov(out, 1);
3571 if (zero.IsLinked()) {
3572 __ B(&done);
3573 }
3574 break;
3575 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003576
Calin Juravle98893e12015-10-02 21:05:03 +01003577 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003578 case TypeCheckKind::kInterfaceCheck: {
3579 // Note that we indeed only call on slow path, but we always go
3580 // into the slow path for the unresolved and interface check
3581 // cases.
3582 //
3583 // We cannot directly call the InstanceofNonTrivial runtime
3584 // entry point without resorting to a type checking slow path
3585 // here (i.e. by calling InvokeRuntime directly), as it would
3586 // require to assign fixed registers for the inputs of this
3587 // HInstanceOf instruction (following the runtime calling
3588 // convention), which might be cluttered by the potential first
3589 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003590 //
3591 // TODO: Introduce a new runtime entry point taking the object
3592 // to test (instead of its class) as argument, and let it deal
3593 // with the read barrier issues. This will let us refactor this
3594 // case of the `switch` code as it was previously (with a direct
3595 // call to the runtime not using a type checking slow path).
3596 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003597 DCHECK(locations->OnlyCallsOnSlowPath());
3598 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3599 /* is_fatal */ false);
3600 codegen_->AddSlowPath(slow_path);
3601 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003602 if (zero.IsLinked()) {
3603 __ B(&done);
3604 }
3605 break;
3606 }
3607 }
3608
3609 if (zero.IsLinked()) {
3610 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003611 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003612 }
3613
3614 if (done.IsLinked()) {
3615 __ Bind(&done);
3616 }
3617
3618 if (slow_path != nullptr) {
3619 __ Bind(slow_path->GetExitLabel());
3620 }
3621}
3622
3623void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3624 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3625 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3626
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003627 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3628 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003629 case TypeCheckKind::kExactCheck:
3630 case TypeCheckKind::kAbstractClassCheck:
3631 case TypeCheckKind::kClassHierarchyCheck:
3632 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003633 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3634 LocationSummary::kCallOnSlowPath :
3635 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003636 break;
3637 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003638 case TypeCheckKind::kUnresolvedCheck:
3639 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003640 call_kind = LocationSummary::kCallOnSlowPath;
3641 break;
3642 }
3643
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3645 locations->SetInAt(0, Location::RequiresRegister());
3646 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003647 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3648 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003649}
3650
3651void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003652 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003653 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003654 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003655 Register obj = InputRegisterAt(instruction, 0);
3656 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003657 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3658 DCHECK_GE(num_temps, 1u);
3659 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003660 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003661 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3662 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003663 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003664 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3665 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3666 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3667 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3668 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3669 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3670 const uint32_t object_array_data_offset =
3671 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003672
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003673 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003674 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3675 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3676 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003677 if (!kEmitCompilerReadBarrier) {
3678 is_type_check_slow_path_fatal =
3679 (type_check_kind == TypeCheckKind::kExactCheck ||
3680 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3681 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3682 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3683 !instruction->CanThrowIntoCatchBlock();
3684 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003685 SlowPathCodeARM64* type_check_slow_path =
3686 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3687 is_type_check_slow_path_fatal);
3688 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003689
Scott Wakeling97c72b72016-06-24 16:19:36 +01003690 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003691 // Avoid null check if we know obj is not null.
3692 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003693 __ Cbz(obj, &done);
3694 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003695
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003696 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003697 case TypeCheckKind::kExactCheck:
3698 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003699 // /* HeapReference<Class> */ temp = obj->klass_
3700 GenerateReferenceLoadTwoRegisters(instruction,
3701 temp_loc,
3702 obj_loc,
3703 class_offset,
3704 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003705 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003706
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003707 __ Cmp(temp, cls);
3708 // Jump to slow path for throwing the exception or doing a
3709 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003710 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003711 break;
3712 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003713
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003714 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003715 // /* HeapReference<Class> */ temp = obj->klass_
3716 GenerateReferenceLoadTwoRegisters(instruction,
3717 temp_loc,
3718 obj_loc,
3719 class_offset,
3720 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003721 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003722
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003723 // If the class is abstract, we eagerly fetch the super class of the
3724 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003725 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003726 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003727 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003728 GenerateReferenceLoadOneRegister(instruction,
3729 temp_loc,
3730 super_offset,
3731 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003732 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003733
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003734 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3735 // exception.
3736 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3737 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003738 __ Cmp(temp, cls);
3739 __ B(ne, &loop);
3740 break;
3741 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003742
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003743 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003744 // /* HeapReference<Class> */ temp = obj->klass_
3745 GenerateReferenceLoadTwoRegisters(instruction,
3746 temp_loc,
3747 obj_loc,
3748 class_offset,
3749 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003750 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003751
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003752 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003753 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003754 __ Bind(&loop);
3755 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003756 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003757
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003758 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003759 GenerateReferenceLoadOneRegister(instruction,
3760 temp_loc,
3761 super_offset,
3762 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003763 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003764
3765 // If the class reference currently in `temp` is not null, jump
3766 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003767 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003768 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003769 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003770 break;
3771 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003773 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003774 // /* HeapReference<Class> */ temp = obj->klass_
3775 GenerateReferenceLoadTwoRegisters(instruction,
3776 temp_loc,
3777 obj_loc,
3778 class_offset,
3779 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003780 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003781
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003782 // Do an exact check.
3783 __ Cmp(temp, cls);
3784 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003785
3786 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003787 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003788 GenerateReferenceLoadOneRegister(instruction,
3789 temp_loc,
3790 component_offset,
3791 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003792 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003793
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003794 // If the component type is null, jump to the slow path to throw the exception.
3795 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3796 // Otherwise, the object is indeed an array. Further check that this component type is not a
3797 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003798 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3799 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003800 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003801 break;
3802 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003803
Calin Juravle98893e12015-10-02 21:05:03 +01003804 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003805 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003806 //
3807 // We cannot directly call the CheckCast runtime entry point
3808 // without resorting to a type checking slow path here (i.e. by
3809 // calling InvokeRuntime directly), as it would require to
3810 // assign fixed registers for the inputs of this HInstanceOf
3811 // instruction (following the runtime calling convention), which
3812 // might be cluttered by the potential first read barrier
3813 // emission at the beginning of this method.
3814 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003815 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003816 case TypeCheckKind::kInterfaceCheck: {
3817 // /* HeapReference<Class> */ temp = obj->klass_
3818 GenerateReferenceLoadTwoRegisters(instruction,
3819 temp_loc,
3820 obj_loc,
3821 class_offset,
3822 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003823 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003824
3825 // /* HeapReference<Class> */ temp = temp->iftable_
3826 GenerateReferenceLoadTwoRegisters(instruction,
3827 temp_loc,
3828 temp_loc,
3829 iftable_offset,
3830 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003831 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003832 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003833 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003834 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003835 vixl::aarch64::Label start_loop;
3836 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003837 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003838 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3839 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003840 // Go to next interface.
3841 __ Add(temp, temp, 2 * kHeapReferenceSize);
3842 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003843 // Compare the classes and continue the loop if they do not match.
3844 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3845 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003846 break;
3847 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003848 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003849 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003850
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003851 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003852}
3853
Alexandre Rames5319def2014-10-23 10:03:10 +01003854void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3856 locations->SetOut(Location::ConstantLocation(constant));
3857}
3858
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003859void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003860 // Will be generated at use site.
3861}
3862
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003863void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3865 locations->SetOut(Location::ConstantLocation(constant));
3866}
3867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003868void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003869 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003870}
3871
Calin Juravle175dc732015-08-25 15:42:32 +01003872void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3873 // The trampoline uses the same calling convention as dex calling conventions,
3874 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3875 // the method_idx.
3876 HandleInvoke(invoke);
3877}
3878
3879void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3880 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3881}
3882
Alexandre Rames5319def2014-10-23 10:03:10 +01003883void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003884 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003885 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003886}
3887
Alexandre Rames67555f72014-11-18 10:55:16 +00003888void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3889 HandleInvoke(invoke);
3890}
3891
3892void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3893 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894 LocationSummary* locations = invoke->GetLocations();
3895 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003897 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003898 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003899
3900 // The register ip1 is required to be used for the hidden argument in
3901 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003902 MacroAssembler* masm = GetVIXLAssembler();
3903 UseScratchRegisterScope scratch_scope(masm);
3904 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003905 scratch_scope.Exclude(ip1);
3906 __ Mov(ip1, invoke->GetDexMethodIndex());
3907
Alexandre Rames67555f72014-11-18 10:55:16 +00003908 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003909 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003910 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003911 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003912 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003913 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003914 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003915 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003916 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003917 // Instead of simply (possibly) unpoisoning `temp` here, we should
3918 // emit a read barrier for the previous class reference load.
3919 // However this is not required in practice, as this is an
3920 // intermediate/temporary reference and because the current
3921 // concurrent copying collector keeps the from-space memory
3922 // intact/accessible until the end of the marking phase (the
3923 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003924 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003925 __ Ldr(temp,
3926 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3927 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003928 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003929 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003930 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003931 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003932 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003933 // lr();
3934 __ Blr(lr);
3935 DCHECK(!codegen_->IsLeafMethod());
3936 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3937}
3938
3939void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003940 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3941 if (intrinsic.TryDispatch(invoke)) {
3942 return;
3943 }
3944
Alexandre Rames67555f72014-11-18 10:55:16 +00003945 HandleInvoke(invoke);
3946}
3947
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003948void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003949 // Explicit clinit checks triggered by static invokes must have been pruned by
3950 // art::PrepareForRegisterAllocation.
3951 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003952
Andreas Gampe878d58c2015-01-15 23:24:00 -08003953 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3954 if (intrinsic.TryDispatch(invoke)) {
3955 return;
3956 }
3957
Alexandre Rames67555f72014-11-18 10:55:16 +00003958 HandleInvoke(invoke);
3959}
3960
Andreas Gampe878d58c2015-01-15 23:24:00 -08003961static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3962 if (invoke->GetLocations()->Intrinsified()) {
3963 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3964 intrinsic.Dispatch(invoke);
3965 return true;
3966 }
3967 return false;
3968}
3969
Vladimir Markodc151b22015-10-15 18:02:30 +01003970HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3971 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003972 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003973 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003974 return desired_dispatch_info;
3975}
3976
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003977void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003978 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003979 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3980 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003981 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3982 uint32_t offset =
3983 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003984 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003985 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003986 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003987 }
Vladimir Marko58155012015-08-19 12:49:41 +00003988 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003989 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003990 break;
3991 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3992 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003993 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003994 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003995 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3996 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003997 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003998 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003999 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004000 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004001 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004002 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004003 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004004 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004005 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004006 }
Vladimir Marko58155012015-08-19 12:49:41 +00004007 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004008 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004009 Register reg = XRegisterFrom(temp);
4010 Register method_reg;
4011 if (current_method.IsRegister()) {
4012 method_reg = XRegisterFrom(current_method);
4013 } else {
4014 DCHECK(invoke->GetLocations()->Intrinsified());
4015 DCHECK(!current_method.IsValid());
4016 method_reg = reg;
4017 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4018 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004019
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004020 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004021 __ Ldr(reg.X(),
4022 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004023 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004024 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004025 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4026 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004027 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4028 break;
4029 }
4030 }
4031
4032 switch (invoke->GetCodePtrLocation()) {
4033 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4034 __ Bl(&frame_entry_label_);
4035 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004036 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4037 // LR = callee_method->entry_point_from_quick_compiled_code_;
4038 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004039 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004040 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004041 // lr()
4042 __ Blr(lr);
4043 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004044 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004045
Andreas Gampe878d58c2015-01-15 23:24:00 -08004046 DCHECK(!IsLeafMethod());
4047}
4048
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004049void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004050 // Use the calling convention instead of the location of the receiver, as
4051 // intrinsics may have put the receiver in a different register. In the intrinsics
4052 // slow path, the arguments have been moved to the right place, so here we are
4053 // guaranteed that the receiver is the first register of the calling convention.
4054 InvokeDexCallingConvention calling_convention;
4055 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004056 Register temp = XRegisterFrom(temp_in);
4057 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4058 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4059 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004060 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004061
4062 BlockPoolsScope block_pools(GetVIXLAssembler());
4063
4064 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004065 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004066 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004067 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004068 // Instead of simply (possibly) unpoisoning `temp` here, we should
4069 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070 // intermediate/temporary reference and because the current
4071 // concurrent copying collector keeps the from-space memory
4072 // intact/accessible until the end of the marking phase (the
4073 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004074 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4075 // temp = temp->GetMethodAt(method_offset);
4076 __ Ldr(temp, MemOperand(temp, method_offset));
4077 // lr = temp->GetEntryPoint();
4078 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4079 // lr();
4080 __ Blr(lr);
4081}
4082
Scott Wakeling97c72b72016-06-24 16:19:36 +01004083vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4084 const DexFile& dex_file,
4085 uint32_t string_index,
4086 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004087 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4088}
4089
Scott Wakeling97c72b72016-06-24 16:19:36 +01004090vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4091 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004092 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004093 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004094 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004095}
4096
Scott Wakeling97c72b72016-06-24 16:19:36 +01004097vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4098 const DexFile& dex_file,
4099 uint32_t element_offset,
4100 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004101 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4102}
4103
Scott Wakeling97c72b72016-06-24 16:19:36 +01004104vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4105 const DexFile& dex_file,
4106 uint32_t offset_or_index,
4107 vixl::aarch64::Label* adrp_label,
4108 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004109 // Add a patch entry and return the label.
4110 patches->emplace_back(dex_file, offset_or_index);
4111 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004112 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004113 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4114 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4115 return label;
4116}
4117
Scott Wakeling97c72b72016-06-24 16:19:36 +01004118vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004119 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004120 return boot_image_string_patches_.GetOrCreate(
4121 StringReference(&dex_file, string_index),
4122 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4123}
4124
Scott Wakeling97c72b72016-06-24 16:19:36 +01004125vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004126 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004127 return boot_image_type_patches_.GetOrCreate(
4128 TypeReference(&dex_file, type_index),
4129 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4130}
4131
Scott Wakeling97c72b72016-06-24 16:19:36 +01004132vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4133 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004134 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4135 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4136 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4137}
4138
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004139vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004140 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4141 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4142 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004143 return jit_string_patches_.GetOrCreate(
4144 StringReference(&dex_file, string_index),
4145 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4146}
4147
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004148vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
4149 const DexFile& dex_file, dex::TypeIndex type_index, uint64_t address) {
4150 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
4151 return jit_class_patches_.GetOrCreate(
4152 TypeReference(&dex_file, type_index),
4153 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4154}
4155
Vladimir Markoaad75c62016-10-03 08:46:48 +00004156void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4157 vixl::aarch64::Register reg) {
4158 DCHECK(reg.IsX());
4159 SingleEmissionCheckScope guard(GetVIXLAssembler());
4160 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004161 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004162}
4163
4164void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4165 vixl::aarch64::Register out,
4166 vixl::aarch64::Register base) {
4167 DCHECK(out.IsX());
4168 DCHECK(base.IsX());
4169 SingleEmissionCheckScope guard(GetVIXLAssembler());
4170 __ Bind(fixup_label);
4171 __ add(out, base, Operand(/* offset placeholder */ 0));
4172}
4173
4174void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4175 vixl::aarch64::Register out,
4176 vixl::aarch64::Register base) {
4177 DCHECK(base.IsX());
4178 SingleEmissionCheckScope guard(GetVIXLAssembler());
4179 __ Bind(fixup_label);
4180 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4181}
4182
4183template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4184inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4185 const ArenaDeque<PcRelativePatchInfo>& infos,
4186 ArenaVector<LinkerPatch>* linker_patches) {
4187 for (const PcRelativePatchInfo& info : infos) {
4188 linker_patches->push_back(Factory(info.label.GetLocation(),
4189 &info.target_dex_file,
4190 info.pc_insn_label->GetLocation(),
4191 info.offset_or_index));
4192 }
4193}
4194
Vladimir Marko58155012015-08-19 12:49:41 +00004195void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4196 DCHECK(linker_patches->empty());
4197 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004198 pc_relative_dex_cache_patches_.size() +
4199 boot_image_string_patches_.size() +
4200 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004201 boot_image_type_patches_.size() +
4202 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004203 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004204 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004205 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004206 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004207 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004208 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004209 info.offset_or_index));
4210 }
4211 for (const auto& entry : boot_image_string_patches_) {
4212 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004213 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4214 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004215 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004216 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004217 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004218 if (!GetCompilerOptions().IsBootImage()) {
4219 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4220 linker_patches);
4221 } else {
4222 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4223 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004224 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004225 for (const auto& entry : boot_image_type_patches_) {
4226 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004227 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4228 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004229 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004230 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004231 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004232 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4233 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004234 for (const auto& entry : boot_image_address_patches_) {
4235 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004236 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4237 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004238 }
4239}
4240
Scott Wakeling97c72b72016-06-24 16:19:36 +01004241vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004242 Uint32ToLiteralMap* map) {
4243 return map->GetOrCreate(
4244 value,
4245 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4246}
4247
Scott Wakeling97c72b72016-06-24 16:19:36 +01004248vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004249 return uint64_literals_.GetOrCreate(
4250 value,
4251 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004252}
4253
Scott Wakeling97c72b72016-06-24 16:19:36 +01004254vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004255 MethodReference target_method,
4256 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004257 return map->GetOrCreate(
4258 target_method,
4259 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004260}
4261
Andreas Gampe878d58c2015-01-15 23:24:00 -08004262void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004263 // Explicit clinit checks triggered by static invokes must have been pruned by
4264 // art::PrepareForRegisterAllocation.
4265 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004266
Andreas Gampe878d58c2015-01-15 23:24:00 -08004267 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4268 return;
4269 }
4270
Alexandre Ramesd921d642015-04-16 15:07:16 +01004271 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004272 LocationSummary* locations = invoke->GetLocations();
4273 codegen_->GenerateStaticOrDirectCall(
4274 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004275 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004276}
4277
4278void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004279 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4280 return;
4281 }
4282
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004283 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004284 DCHECK(!codegen_->IsLeafMethod());
4285 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4286}
4287
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004288HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4289 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004290 switch (desired_class_load_kind) {
4291 case HLoadClass::LoadKind::kReferrersClass:
4292 break;
4293 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4294 DCHECK(!GetCompilerOptions().GetCompilePic());
4295 break;
4296 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4297 DCHECK(GetCompilerOptions().GetCompilePic());
4298 break;
4299 case HLoadClass::LoadKind::kBootImageAddress:
4300 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004301 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004302 DCHECK(Runtime::Current()->UseJitCompilation());
4303 break;
4304 case HLoadClass::LoadKind::kDexCachePcRelative:
4305 DCHECK(!Runtime::Current()->UseJitCompilation());
4306 break;
4307 case HLoadClass::LoadKind::kDexCacheViaMethod:
4308 break;
4309 }
4310 return desired_class_load_kind;
4311}
4312
Alexandre Rames67555f72014-11-18 10:55:16 +00004313void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004314 if (cls->NeedsAccessCheck()) {
4315 InvokeRuntimeCallingConvention calling_convention;
4316 CodeGenerator::CreateLoadClassLocationSummary(
4317 cls,
4318 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004319 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004320 /* code_generator_supports_read_barrier */ true);
4321 return;
4322 }
4323
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004324 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4325 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004326 ? LocationSummary::kCallOnSlowPath
4327 : LocationSummary::kNoCall;
4328 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004329 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004330 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004331 }
4332
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004333 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4334 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4335 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4336 locations->SetInAt(0, Location::RequiresRegister());
4337 }
4338 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004339}
4340
4341void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004342 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004343 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004344 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004345 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004346 return;
4347 }
4348
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004349 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004350 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004351
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004352 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4353 ? kWithoutReadBarrier
4354 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004355 bool generate_null_check = false;
4356 switch (cls->GetLoadKind()) {
4357 case HLoadClass::LoadKind::kReferrersClass: {
4358 DCHECK(!cls->CanCallRuntime());
4359 DCHECK(!cls->MustGenerateClinitCheck());
4360 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4361 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004362 GenerateGcRootFieldLoad(cls,
4363 out_loc,
4364 current_method,
4365 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004366 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004367 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004368 break;
4369 }
4370 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004371 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004372 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4373 cls->GetTypeIndex()));
4374 break;
4375 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004376 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004377 // Add ADRP with its PC-relative type patch.
4378 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004379 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004380 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004381 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004382 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004383 vixl::aarch64::Label* add_label =
4384 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004385 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004386 break;
4387 }
4388 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004389 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004390 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4391 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4392 break;
4393 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004394 case HLoadClass::LoadKind::kJitTableAddress: {
4395 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4396 cls->GetTypeIndex(),
4397 cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004398 GenerateGcRootFieldLoad(cls,
4399 out_loc,
4400 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004401 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004402 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004403 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004404 break;
4405 }
4406 case HLoadClass::LoadKind::kDexCachePcRelative: {
4407 // Add ADRP with its PC-relative DexCache access patch.
4408 const DexFile& dex_file = cls->GetDexFile();
4409 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004410 vixl::aarch64::Label* adrp_label =
4411 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004412 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004413 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004414 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004415 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4416 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004417 GenerateGcRootFieldLoad(cls,
4418 out_loc,
4419 out.X(),
4420 /* offset placeholder */ 0,
4421 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004422 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004423 generate_null_check = !cls->IsInDexCache();
4424 break;
4425 }
4426 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4427 MemberOffset resolved_types_offset =
4428 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4429 // /* GcRoot<mirror::Class>[] */ out =
4430 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4431 Register current_method = InputRegisterAt(cls, 0);
4432 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4433 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004434 GenerateGcRootFieldLoad(cls,
4435 out_loc,
4436 out.X(),
Andreas Gampea5b09a62016-11-17 15:21:22 -08004437 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_),
Roland Levillain00468f32016-10-27 18:02:48 +01004438 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004439 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004440 generate_null_check = !cls->IsInDexCache();
4441 break;
4442 }
4443 }
4444
4445 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4446 DCHECK(cls->CanCallRuntime());
4447 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4448 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4449 codegen_->AddSlowPath(slow_path);
4450 if (generate_null_check) {
4451 __ Cbz(out, slow_path->GetEntryLabel());
4452 }
4453 if (cls->MustGenerateClinitCheck()) {
4454 GenerateClassInitializationCheck(slow_path, out);
4455 } else {
4456 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004457 }
4458 }
4459}
4460
David Brazdilcb1c0552015-08-04 16:22:25 +01004461static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004462 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004463}
4464
Alexandre Rames67555f72014-11-18 10:55:16 +00004465void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4466 LocationSummary* locations =
4467 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4468 locations->SetOut(Location::RequiresRegister());
4469}
4470
4471void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004472 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4473}
4474
4475void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4476 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4477}
4478
4479void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4480 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004481}
4482
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004483HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4484 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004485 switch (desired_string_load_kind) {
4486 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4487 DCHECK(!GetCompilerOptions().GetCompilePic());
4488 break;
4489 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4490 DCHECK(GetCompilerOptions().GetCompilePic());
4491 break;
4492 case HLoadString::LoadKind::kBootImageAddress:
4493 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004494 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004495 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004496 break;
4497 case HLoadString::LoadKind::kDexCacheViaMethod:
4498 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004499 case HLoadString::LoadKind::kJitTableAddress:
4500 DCHECK(Runtime::Current()->UseJitCompilation());
4501 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004502 }
4503 return desired_string_load_kind;
4504}
4505
Alexandre Rames67555f72014-11-18 10:55:16 +00004506void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004507 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004510 InvokeRuntimeCallingConvention calling_convention;
4511 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4512 } else {
4513 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004514 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4515 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4516 // Rely on the pResolveString and/or marking to save everything, including temps.
4517 RegisterSet caller_saves = RegisterSet::Empty();
4518 InvokeRuntimeCallingConvention calling_convention;
4519 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4520 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4521 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4522 Primitive::kPrimNot).GetCode());
4523 locations->SetCustomSlowPathCallerSaves(caller_saves);
4524 } else {
4525 // For non-Baker read barrier we have a temp-clobbering call.
4526 }
4527 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004528 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004529}
4530
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004531// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4532// move.
4533void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004534 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004535 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004536
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004537 switch (load->GetLoadKind()) {
4538 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004539 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4540 load->GetStringIndex()));
4541 return; // No dex cache slow path.
4542 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004543 // Add ADRP with its PC-relative String patch.
4544 const DexFile& dex_file = load->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004545 uint32_t string_index = load->GetStringIndex().index_;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004546 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004547 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004548 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004549 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004550 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004551 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004552 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004553 return; // No dex cache slow path.
4554 }
4555 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004556 uint32_t address = dchecked_integral_cast<uint32_t>(
4557 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4558 DCHECK_NE(address, 0u);
4559 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004560 return; // No dex cache slow path.
4561 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004562 case HLoadString::LoadKind::kBssEntry: {
4563 // Add ADRP with its PC-relative String .bss entry patch.
4564 const DexFile& dex_file = load->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004565 uint32_t string_index = load->GetStringIndex().index_;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004566 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004567 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4568 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004569 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004570 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004571 // Add LDR with its PC-relative String patch.
4572 vixl::aarch64::Label* ldr_label =
4573 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004574 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004575 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004576 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004577 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004578 /* offset placeholder */ 0u,
4579 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004580 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004581 SlowPathCodeARM64* slow_path =
4582 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004583 codegen_->AddSlowPath(slow_path);
4584 __ Cbz(out.X(), slow_path->GetEntryLabel());
4585 __ Bind(slow_path->GetExitLabel());
4586 return;
4587 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004588 case HLoadString::LoadKind::kJitTableAddress: {
4589 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004590 load->GetStringIndex(),
4591 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004592 GenerateGcRootFieldLoad(load,
4593 out_loc,
4594 out.X(),
4595 /* offset */ 0,
4596 /* fixup_label */ nullptr,
4597 kCompilerReadBarrierOption);
4598 return;
4599 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004600 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004601 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004602 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004603
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004604 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004605 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004606 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004607 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004608 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4609 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004610}
4611
Alexandre Rames5319def2014-10-23 10:03:10 +01004612void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4614 locations->SetOut(Location::ConstantLocation(constant));
4615}
4616
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004617void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004618 // Will be generated at use site.
4619}
4620
Alexandre Rames67555f72014-11-18 10:55:16 +00004621void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4622 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004623 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004624 InvokeRuntimeCallingConvention calling_convention;
4625 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4626}
4627
4628void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004629 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4630 instruction,
4631 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004632 if (instruction->IsEnter()) {
4633 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4634 } else {
4635 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4636 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004637}
4638
Alexandre Rames42d641b2014-10-27 14:00:51 +00004639void LocationsBuilderARM64::VisitMul(HMul* mul) {
4640 LocationSummary* locations =
4641 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4642 switch (mul->GetResultType()) {
4643 case Primitive::kPrimInt:
4644 case Primitive::kPrimLong:
4645 locations->SetInAt(0, Location::RequiresRegister());
4646 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004648 break;
4649
4650 case Primitive::kPrimFloat:
4651 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004652 locations->SetInAt(0, Location::RequiresFpuRegister());
4653 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004654 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004655 break;
4656
4657 default:
4658 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4659 }
4660}
4661
4662void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4663 switch (mul->GetResultType()) {
4664 case Primitive::kPrimInt:
4665 case Primitive::kPrimLong:
4666 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4667 break;
4668
4669 case Primitive::kPrimFloat:
4670 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004671 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004672 break;
4673
4674 default:
4675 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4676 }
4677}
4678
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004679void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4680 LocationSummary* locations =
4681 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4682 switch (neg->GetResultType()) {
4683 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004684 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004685 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004687 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004688
4689 case Primitive::kPrimFloat:
4690 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004691 locations->SetInAt(0, Location::RequiresFpuRegister());
4692 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004693 break;
4694
4695 default:
4696 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4697 }
4698}
4699
4700void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4701 switch (neg->GetResultType()) {
4702 case Primitive::kPrimInt:
4703 case Primitive::kPrimLong:
4704 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4705 break;
4706
4707 case Primitive::kPrimFloat:
4708 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004709 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004710 break;
4711
4712 default:
4713 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4714 }
4715}
4716
4717void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4718 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004719 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004720 InvokeRuntimeCallingConvention calling_convention;
4721 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004722 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004723 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004724 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004725}
4726
4727void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4728 LocationSummary* locations = instruction->GetLocations();
4729 InvokeRuntimeCallingConvention calling_convention;
4730 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4731 DCHECK(type_index.Is(w0));
Andreas Gampea5b09a62016-11-17 15:21:22 -08004732 __ Mov(type_index, instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01004733 // Note: if heap poisoning is enabled, the entry point takes cares
4734 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004735 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004736 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004737}
4738
Alexandre Rames5319def2014-10-23 10:03:10 +01004739void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4740 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004741 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004742 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004743 if (instruction->IsStringAlloc()) {
4744 locations->AddTemp(LocationFrom(kArtMethodRegister));
4745 } else {
4746 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004747 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004748 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4749}
4750
4751void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004752 // Note: if heap poisoning is enabled, the entry point takes cares
4753 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004754 if (instruction->IsStringAlloc()) {
4755 // String is allocated through StringFactory. Call NewEmptyString entry point.
4756 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004757 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004758 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4759 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4760 __ Blr(lr);
4761 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4762 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004763 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004764 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004765 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004766}
4767
4768void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4769 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004770 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004772}
4773
4774void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004775 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004776 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004777 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004778 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004779 break;
4780
4781 default:
4782 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4783 }
4784}
4785
David Brazdil66d126e2015-04-03 16:02:44 +01004786void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4787 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4788 locations->SetInAt(0, Location::RequiresRegister());
4789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4790}
4791
4792void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004793 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004794}
4795
Alexandre Rames5319def2014-10-23 10:03:10 +01004796void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004797 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4798 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004799}
4800
Calin Juravle2ae48182016-03-16 14:05:09 +00004801void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4802 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 return;
4804 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004805
Alexandre Ramesd921d642015-04-16 15:07:16 +01004806 BlockPoolsScope block_pools(GetVIXLAssembler());
4807 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004808 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004809 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004810}
4811
Calin Juravle2ae48182016-03-16 14:05:09 +00004812void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004813 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004814 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004815
4816 LocationSummary* locations = instruction->GetLocations();
4817 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004818
4819 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004820}
4821
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004822void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004823 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004824}
4825
Alexandre Rames67555f72014-11-18 10:55:16 +00004826void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4827 HandleBinaryOp(instruction);
4828}
4829
4830void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4831 HandleBinaryOp(instruction);
4832}
4833
Alexandre Rames3e69f162014-12-10 10:36:50 +00004834void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4835 LOG(FATAL) << "Unreachable";
4836}
4837
4838void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4839 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4840}
4841
Alexandre Rames5319def2014-10-23 10:03:10 +01004842void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4843 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4844 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4845 if (location.IsStackSlot()) {
4846 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4847 } else if (location.IsDoubleStackSlot()) {
4848 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4849 }
4850 locations->SetOut(location);
4851}
4852
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004853void InstructionCodeGeneratorARM64::VisitParameterValue(
4854 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004855 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004856}
4857
4858void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4859 LocationSummary* locations =
4860 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004861 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004862}
4863
4864void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4865 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4866 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004867}
4868
4869void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4870 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004871 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004872 locations->SetInAt(i, Location::Any());
4873 }
4874 locations->SetOut(Location::Any());
4875}
4876
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004877void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004878 LOG(FATAL) << "Unreachable";
4879}
4880
Serban Constantinescu02164b32014-11-13 14:05:07 +00004881void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004882 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004883 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004884 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4885 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4887
4888 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004889 case Primitive::kPrimInt:
4890 case Primitive::kPrimLong:
4891 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004892 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4894 break;
4895
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004896 case Primitive::kPrimFloat:
4897 case Primitive::kPrimDouble: {
4898 InvokeRuntimeCallingConvention calling_convention;
4899 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4900 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4901 locations->SetOut(calling_convention.GetReturnLocation(type));
4902
4903 break;
4904 }
4905
Serban Constantinescu02164b32014-11-13 14:05:07 +00004906 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004907 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004908 }
4909}
4910
4911void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4912 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004913
Serban Constantinescu02164b32014-11-13 14:05:07 +00004914 switch (type) {
4915 case Primitive::kPrimInt:
4916 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004917 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004918 break;
4919 }
4920
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004921 case Primitive::kPrimFloat:
4922 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004923 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4924 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004925 if (type == Primitive::kPrimFloat) {
4926 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4927 } else {
4928 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4929 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004930 break;
4931 }
4932
Serban Constantinescu02164b32014-11-13 14:05:07 +00004933 default:
4934 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004935 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004936 }
4937}
4938
Calin Juravle27df7582015-04-17 19:12:31 +01004939void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4940 memory_barrier->SetLocations(nullptr);
4941}
4942
4943void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004944 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004945}
4946
Alexandre Rames5319def2014-10-23 10:03:10 +01004947void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4948 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4949 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004950 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004951}
4952
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004953void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004954 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004955}
4956
4957void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4958 instruction->SetLocations(nullptr);
4959}
4960
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004961void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004962 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004963}
4964
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004965void LocationsBuilderARM64::VisitRor(HRor* ror) {
4966 HandleBinaryOp(ror);
4967}
4968
4969void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4970 HandleBinaryOp(ror);
4971}
4972
Serban Constantinescu02164b32014-11-13 14:05:07 +00004973void LocationsBuilderARM64::VisitShl(HShl* shl) {
4974 HandleShift(shl);
4975}
4976
4977void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4978 HandleShift(shl);
4979}
4980
4981void LocationsBuilderARM64::VisitShr(HShr* shr) {
4982 HandleShift(shr);
4983}
4984
4985void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4986 HandleShift(shr);
4987}
4988
Alexandre Rames5319def2014-10-23 10:03:10 +01004989void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004990 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004991}
4992
4993void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004994 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004995}
4996
Alexandre Rames67555f72014-11-18 10:55:16 +00004997void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004998 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004999}
5000
5001void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005002 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005003}
5004
5005void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005006 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005007}
5008
Alexandre Rames67555f72014-11-18 10:55:16 +00005009void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005010 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005011}
5012
Calin Juravlee460d1d2015-09-29 04:52:17 +01005013void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5014 HUnresolvedInstanceFieldGet* instruction) {
5015 FieldAccessCallingConventionARM64 calling_convention;
5016 codegen_->CreateUnresolvedFieldLocationSummary(
5017 instruction, instruction->GetFieldType(), calling_convention);
5018}
5019
5020void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5021 HUnresolvedInstanceFieldGet* instruction) {
5022 FieldAccessCallingConventionARM64 calling_convention;
5023 codegen_->GenerateUnresolvedFieldAccess(instruction,
5024 instruction->GetFieldType(),
5025 instruction->GetFieldIndex(),
5026 instruction->GetDexPc(),
5027 calling_convention);
5028}
5029
5030void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5031 HUnresolvedInstanceFieldSet* instruction) {
5032 FieldAccessCallingConventionARM64 calling_convention;
5033 codegen_->CreateUnresolvedFieldLocationSummary(
5034 instruction, instruction->GetFieldType(), calling_convention);
5035}
5036
5037void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5038 HUnresolvedInstanceFieldSet* instruction) {
5039 FieldAccessCallingConventionARM64 calling_convention;
5040 codegen_->GenerateUnresolvedFieldAccess(instruction,
5041 instruction->GetFieldType(),
5042 instruction->GetFieldIndex(),
5043 instruction->GetDexPc(),
5044 calling_convention);
5045}
5046
5047void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5048 HUnresolvedStaticFieldGet* instruction) {
5049 FieldAccessCallingConventionARM64 calling_convention;
5050 codegen_->CreateUnresolvedFieldLocationSummary(
5051 instruction, instruction->GetFieldType(), calling_convention);
5052}
5053
5054void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5055 HUnresolvedStaticFieldGet* instruction) {
5056 FieldAccessCallingConventionARM64 calling_convention;
5057 codegen_->GenerateUnresolvedFieldAccess(instruction,
5058 instruction->GetFieldType(),
5059 instruction->GetFieldIndex(),
5060 instruction->GetDexPc(),
5061 calling_convention);
5062}
5063
5064void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5065 HUnresolvedStaticFieldSet* instruction) {
5066 FieldAccessCallingConventionARM64 calling_convention;
5067 codegen_->CreateUnresolvedFieldLocationSummary(
5068 instruction, instruction->GetFieldType(), calling_convention);
5069}
5070
5071void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5072 HUnresolvedStaticFieldSet* instruction) {
5073 FieldAccessCallingConventionARM64 calling_convention;
5074 codegen_->GenerateUnresolvedFieldAccess(instruction,
5075 instruction->GetFieldType(),
5076 instruction->GetFieldIndex(),
5077 instruction->GetDexPc(),
5078 calling_convention);
5079}
5080
Alexandre Rames5319def2014-10-23 10:03:10 +01005081void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005082 LocationSummary* locations =
5083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005084 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005085}
5086
5087void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005088 HBasicBlock* block = instruction->GetBlock();
5089 if (block->GetLoopInformation() != nullptr) {
5090 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5091 // The back edge will generate the suspend check.
5092 return;
5093 }
5094 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5095 // The goto will generate the suspend check.
5096 return;
5097 }
5098 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005099}
5100
Alexandre Rames67555f72014-11-18 10:55:16 +00005101void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5102 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005103 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005104 InvokeRuntimeCallingConvention calling_convention;
5105 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5106}
5107
5108void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005109 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005110 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005111}
5112
5113void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5114 LocationSummary* locations =
5115 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5116 Primitive::Type input_type = conversion->GetInputType();
5117 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005118 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005119 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5120 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5121 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5122 }
5123
Alexandre Rames542361f2015-01-29 16:57:31 +00005124 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005125 locations->SetInAt(0, Location::RequiresFpuRegister());
5126 } else {
5127 locations->SetInAt(0, Location::RequiresRegister());
5128 }
5129
Alexandre Rames542361f2015-01-29 16:57:31 +00005130 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005131 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5132 } else {
5133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5134 }
5135}
5136
5137void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5138 Primitive::Type result_type = conversion->GetResultType();
5139 Primitive::Type input_type = conversion->GetInputType();
5140
5141 DCHECK_NE(input_type, result_type);
5142
Alexandre Rames542361f2015-01-29 16:57:31 +00005143 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005144 int result_size = Primitive::ComponentSize(result_type);
5145 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005146 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005147 Register output = OutputRegister(conversion);
5148 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005149 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005150 // 'int' values are used directly as W registers, discarding the top
5151 // bits, so we don't need to sign-extend and can just perform a move.
5152 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5153 // top 32 bits of the target register. We theoretically could leave those
5154 // bits unchanged, but we would have to make sure that no code uses a
5155 // 32bit input value as a 64bit value assuming that the top 32 bits are
5156 // zero.
5157 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005158 } else if (result_type == Primitive::kPrimChar ||
5159 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5160 __ Ubfx(output,
5161 output.IsX() ? source.X() : source.W(),
5162 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005163 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005164 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005165 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005166 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005167 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005168 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005169 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5170 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005171 } else if (Primitive::IsFloatingPointType(result_type) &&
5172 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005173 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5174 } else {
5175 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5176 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005177 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005178}
Alexandre Rames67555f72014-11-18 10:55:16 +00005179
Serban Constantinescu02164b32014-11-13 14:05:07 +00005180void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5181 HandleShift(ushr);
5182}
5183
5184void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5185 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005186}
5187
5188void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5189 HandleBinaryOp(instruction);
5190}
5191
5192void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5193 HandleBinaryOp(instruction);
5194}
5195
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005196void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005197 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005198 LOG(FATAL) << "Unreachable";
5199}
5200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005201void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005202 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005203 LOG(FATAL) << "Unreachable";
5204}
5205
Mark Mendellfe57faa2015-09-18 09:26:15 -04005206// Simple implementation of packed switch - generate cascaded compare/jumps.
5207void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5208 LocationSummary* locations =
5209 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5210 locations->SetInAt(0, Location::RequiresRegister());
5211}
5212
5213void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5214 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005215 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005216 Register value_reg = InputRegisterAt(switch_instr, 0);
5217 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5218
Zheng Xu3927c8b2015-11-18 17:46:25 +08005219 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005220 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005221 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5222 // make sure we don't emit it if the target may run out of range.
5223 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5224 // ranges and emit the tables only as required.
5225 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005226
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005227 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005228 // Current instruction id is an upper bound of the number of HIRs in the graph.
5229 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5230 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005231 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5232 Register temp = temps.AcquireW();
5233 __ Subs(temp, value_reg, Operand(lower_bound));
5234
Zheng Xu3927c8b2015-11-18 17:46:25 +08005235 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005236 // Jump to successors[0] if value == lower_bound.
5237 __ B(eq, codegen_->GetLabelOf(successors[0]));
5238 int32_t last_index = 0;
5239 for (; num_entries - last_index > 2; last_index += 2) {
5240 __ Subs(temp, temp, Operand(2));
5241 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5242 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5243 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5244 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5245 }
5246 if (num_entries - last_index == 2) {
5247 // The last missing case_value.
5248 __ Cmp(temp, Operand(1));
5249 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005250 }
5251
5252 // And the default for any other value.
5253 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5254 __ B(codegen_->GetLabelOf(default_block));
5255 }
5256 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005257 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005258
5259 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5260
5261 // Below instructions should use at most one blocked register. Since there are two blocked
5262 // registers, we are free to block one.
5263 Register temp_w = temps.AcquireW();
5264 Register index;
5265 // Remove the bias.
5266 if (lower_bound != 0) {
5267 index = temp_w;
5268 __ Sub(index, value_reg, Operand(lower_bound));
5269 } else {
5270 index = value_reg;
5271 }
5272
5273 // Jump to default block if index is out of the range.
5274 __ Cmp(index, Operand(num_entries));
5275 __ B(hs, codegen_->GetLabelOf(default_block));
5276
5277 // In current VIXL implementation, it won't require any blocked registers to encode the
5278 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5279 // register pressure.
5280 Register table_base = temps.AcquireX();
5281 // Load jump offset from the table.
5282 __ Adr(table_base, jump_table->GetTableStartLabel());
5283 Register jump_offset = temp_w;
5284 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5285
5286 // Jump to target block by branching to table_base(pc related) + offset.
5287 Register target_address = table_base;
5288 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5289 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005290 }
5291}
5292
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005293void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5294 HInstruction* instruction,
5295 Location out,
5296 uint32_t offset,
5297 Location maybe_temp,
5298 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005299 Primitive::Type type = Primitive::kPrimNot;
5300 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005301 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005302 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005303 Register temp_reg = RegisterFrom(maybe_temp, type);
5304 if (kUseBakerReadBarrier) {
5305 // Load with fast path based Baker's read barrier.
5306 // /* HeapReference<Object> */ out = *(out + offset)
5307 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5308 out,
5309 out_reg,
5310 offset,
5311 temp_reg,
5312 /* needs_null_check */ false,
5313 /* use_load_acquire */ false);
5314 } else {
5315 // Load with slow path based read barrier.
5316 // Save the value of `out` into `maybe_temp` before overwriting it
5317 // in the following move operation, as we will need it for the
5318 // read barrier below.
5319 __ Mov(temp_reg, out_reg);
5320 // /* HeapReference<Object> */ out = *(out + offset)
5321 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5322 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5323 }
5324 } else {
5325 // Plain load with no read barrier.
5326 // /* HeapReference<Object> */ out = *(out + offset)
5327 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5328 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5329 }
5330}
5331
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005332void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5333 HInstruction* instruction,
5334 Location out,
5335 Location obj,
5336 uint32_t offset,
5337 Location maybe_temp,
5338 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005339 Primitive::Type type = Primitive::kPrimNot;
5340 Register out_reg = RegisterFrom(out, type);
5341 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005342 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005343 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005344 if (kUseBakerReadBarrier) {
5345 // Load with fast path based Baker's read barrier.
5346 Register temp_reg = RegisterFrom(maybe_temp, type);
5347 // /* HeapReference<Object> */ out = *(obj + offset)
5348 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5349 out,
5350 obj_reg,
5351 offset,
5352 temp_reg,
5353 /* needs_null_check */ false,
5354 /* use_load_acquire */ false);
5355 } else {
5356 // Load with slow path based read barrier.
5357 // /* HeapReference<Object> */ out = *(obj + offset)
5358 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5359 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5360 }
5361 } else {
5362 // Plain load with no read barrier.
5363 // /* HeapReference<Object> */ out = *(obj + offset)
5364 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5365 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5366 }
5367}
5368
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005369void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5370 HInstruction* instruction,
5371 Location root,
5372 Register obj,
5373 uint32_t offset,
5374 vixl::aarch64::Label* fixup_label,
5375 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005376 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005377 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005378 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005379 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005380 if (kUseBakerReadBarrier) {
5381 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5382 // Baker's read barrier are used:
5383 //
5384 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005385 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5386 // if (temp != null) {
5387 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005388 // }
5389
5390 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005391 if (fixup_label == nullptr) {
5392 __ Ldr(root_reg, MemOperand(obj, offset));
5393 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005394 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005395 }
Roland Levillain44015862016-01-22 11:47:17 +00005396 static_assert(
5397 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5398 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5399 "have different sizes.");
5400 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5401 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5402 "have different sizes.");
5403
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005404 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005405
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005406 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5407 SlowPathCodeARM64* slow_path =
5408 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5409 root,
5410 LocationFrom(temp));
5411 codegen_->AddSlowPath(slow_path);
5412 const int32_t entry_point_offset =
5413 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5414 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5415 // Loading the entrypoint does not require a load acquire since it is only changed when
5416 // threads are suspended or running a checkpoint.
5417 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5418 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5419 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005420 __ Cbnz(temp, slow_path->GetEntryLabel());
5421 __ Bind(slow_path->GetExitLabel());
5422 } else {
5423 // GC root loaded through a slow path for read barriers other
5424 // than Baker's.
5425 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005426 if (fixup_label == nullptr) {
5427 __ Add(root_reg.X(), obj.X(), offset);
5428 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005429 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005430 }
Roland Levillain44015862016-01-22 11:47:17 +00005431 // /* mirror::Object* */ root = root->Read()
5432 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5433 }
5434 } else {
5435 // Plain GC root load with no read barrier.
5436 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005437 if (fixup_label == nullptr) {
5438 __ Ldr(root_reg, MemOperand(obj, offset));
5439 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005440 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005441 }
Roland Levillain44015862016-01-22 11:47:17 +00005442 // Note that GC roots are not affected by heap poisoning, thus we
5443 // do not have to unpoison `root_reg` here.
5444 }
5445}
5446
5447void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5448 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005449 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005450 uint32_t offset,
5451 Register temp,
5452 bool needs_null_check,
5453 bool use_load_acquire) {
5454 DCHECK(kEmitCompilerReadBarrier);
5455 DCHECK(kUseBakerReadBarrier);
5456
5457 // /* HeapReference<Object> */ ref = *(obj + offset)
5458 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005459 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005460 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5461 ref,
5462 obj,
5463 offset,
5464 no_index,
5465 no_scale_factor,
5466 temp,
5467 needs_null_check,
5468 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005469}
5470
5471void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5472 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005473 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005474 uint32_t data_offset,
5475 Location index,
5476 Register temp,
5477 bool needs_null_check) {
5478 DCHECK(kEmitCompilerReadBarrier);
5479 DCHECK(kUseBakerReadBarrier);
5480
5481 // Array cells are never volatile variables, therefore array loads
5482 // never use Load-Acquire instructions on ARM64.
5483 const bool use_load_acquire = false;
5484
Roland Levillainbfea3352016-06-23 13:48:47 +01005485 static_assert(
5486 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5487 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005488 // /* HeapReference<Object> */ ref =
5489 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005490 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5491 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5492 ref,
5493 obj,
5494 data_offset,
5495 index,
5496 scale_factor,
5497 temp,
5498 needs_null_check,
5499 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005500}
5501
5502void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5503 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005504 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005505 uint32_t offset,
5506 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005507 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005508 Register temp,
5509 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005510 bool use_load_acquire,
5511 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005512 DCHECK(kEmitCompilerReadBarrier);
5513 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005514 // If we are emitting an array load, we should not be using a
5515 // Load Acquire instruction. In other words:
5516 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5517 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005518
5519 MacroAssembler* masm = GetVIXLAssembler();
5520 UseScratchRegisterScope temps(masm);
5521
5522 // In slow path based read barriers, the read barrier call is
5523 // inserted after the original load. However, in fast path based
5524 // Baker's read barriers, we need to perform the load of
5525 // mirror::Object::monitor_ *before* the original reference load.
5526 // This load-load ordering is required by the read barrier.
5527 // The fast path/slow path (for Baker's algorithm) should look like:
5528 //
5529 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5530 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5531 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005532 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005533 // if (is_gray) {
5534 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5535 // }
5536 //
5537 // Note: the original implementation in ReadBarrier::Barrier is
5538 // slightly more complex as it performs additional checks that we do
5539 // not do here for performance reasons.
5540
5541 Primitive::Type type = Primitive::kPrimNot;
5542 Register ref_reg = RegisterFrom(ref, type);
5543 DCHECK(obj.IsW());
5544 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5545
5546 // /* int32_t */ monitor = obj->monitor_
5547 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5548 if (needs_null_check) {
5549 MaybeRecordImplicitNullCheck(instruction);
5550 }
5551 // /* LockWord */ lock_word = LockWord(monitor)
5552 static_assert(sizeof(LockWord) == sizeof(int32_t),
5553 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005554
Vladimir Marko877a0332016-07-11 19:30:56 +01005555 // Introduce a dependency on the lock_word including rb_state,
5556 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005557 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005558 // `obj` is unchanged by this operation, but its value now depends
5559 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005560 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005561
5562 // The actual reference load.
5563 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005564 // Load types involving an "index": ArrayGet,
5565 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5566 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005567 if (use_load_acquire) {
5568 // UnsafeGetObjectVolatile intrinsic case.
5569 // Register `index` is not an index in an object array, but an
5570 // offset to an object reference field within object `obj`.
5571 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5572 DCHECK(instruction->GetLocations()->Intrinsified());
5573 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5574 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005575 DCHECK_EQ(offset, 0u);
5576 DCHECK_EQ(scale_factor, 0u);
5577 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005578 // /* HeapReference<Object> */ ref = *(obj + index)
5579 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5580 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005581 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005582 // ArrayGet and UnsafeGetObject intrinsics cases.
5583 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5584 if (index.IsConstant()) {
5585 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5586 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5587 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005588 Register temp3 = temps.AcquireW();
5589 __ Add(temp3, obj, offset);
5590 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5591 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005592 }
Roland Levillain44015862016-01-22 11:47:17 +00005593 }
Roland Levillain44015862016-01-22 11:47:17 +00005594 } else {
5595 // /* HeapReference<Object> */ ref = *(obj + offset)
5596 MemOperand field = HeapOperand(obj, offset);
5597 if (use_load_acquire) {
5598 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5599 } else {
5600 Load(type, ref_reg, field);
5601 }
5602 }
5603
5604 // Object* ref = ref_addr->AsMirrorPtr()
5605 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5606
Vladimir Marko953437b2016-08-24 08:30:46 +00005607 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005608 SlowPathCodeARM64* slow_path;
5609 if (always_update_field) {
5610 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5611 // address of the form `obj + field_offset`, where `obj` is a
5612 // register and `field_offset` is a register. Thus `offset` and
5613 // `scale_factor` above are expected to be null in this code path.
5614 DCHECK_EQ(offset, 0u);
5615 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5616 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5617 instruction, ref, obj, /* field_offset */ index, temp);
5618 } else {
5619 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5620 }
Roland Levillain44015862016-01-22 11:47:17 +00005621 AddSlowPath(slow_path);
5622
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005623 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005624 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005625 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005626 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5627 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005628 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005629 __ Bind(slow_path->GetExitLabel());
5630}
5631
5632void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5633 Location out,
5634 Location ref,
5635 Location obj,
5636 uint32_t offset,
5637 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005638 DCHECK(kEmitCompilerReadBarrier);
5639
Roland Levillain44015862016-01-22 11:47:17 +00005640 // Insert a slow path based read barrier *after* the reference load.
5641 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005642 // If heap poisoning is enabled, the unpoisoning of the loaded
5643 // reference will be carried out by the runtime within the slow
5644 // path.
5645 //
5646 // Note that `ref` currently does not get unpoisoned (when heap
5647 // poisoning is enabled), which is alright as the `ref` argument is
5648 // not used by the artReadBarrierSlow entry point.
5649 //
5650 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5651 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5652 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5653 AddSlowPath(slow_path);
5654
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005655 __ B(slow_path->GetEntryLabel());
5656 __ Bind(slow_path->GetExitLabel());
5657}
5658
Roland Levillain44015862016-01-22 11:47:17 +00005659void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5660 Location out,
5661 Location ref,
5662 Location obj,
5663 uint32_t offset,
5664 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005665 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005666 // Baker's read barriers shall be handled by the fast path
5667 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5668 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005669 // If heap poisoning is enabled, unpoisoning will be taken care of
5670 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005671 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005672 } else if (kPoisonHeapReferences) {
5673 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5674 }
5675}
5676
Roland Levillain44015862016-01-22 11:47:17 +00005677void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5678 Location out,
5679 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005680 DCHECK(kEmitCompilerReadBarrier);
5681
Roland Levillain44015862016-01-22 11:47:17 +00005682 // Insert a slow path based read barrier *after* the GC root load.
5683 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005684 // Note that GC roots are not affected by heap poisoning, so we do
5685 // not need to do anything special for this here.
5686 SlowPathCodeARM64* slow_path =
5687 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5688 AddSlowPath(slow_path);
5689
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005690 __ B(slow_path->GetEntryLabel());
5691 __ Bind(slow_path->GetExitLabel());
5692}
5693
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005694void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5695 LocationSummary* locations =
5696 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5697 locations->SetInAt(0, Location::RequiresRegister());
5698 locations->SetOut(Location::RequiresRegister());
5699}
5700
5701void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5702 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005703 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005704 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005705 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005706 __ Ldr(XRegisterFrom(locations->Out()),
5707 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005708 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005709 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005710 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005711 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5712 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005713 __ Ldr(XRegisterFrom(locations->Out()),
5714 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005715 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005716}
5717
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005718static void PatchJitRootUse(uint8_t* code,
5719 const uint8_t* roots_data,
5720 vixl::aarch64::Literal<uint32_t>* literal,
5721 uint64_t index_in_table) {
5722 uint32_t literal_offset = literal->GetOffset();
5723 uintptr_t address =
5724 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5725 uint8_t* data = code + literal_offset;
5726 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5727}
5728
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005729void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5730 for (const auto& entry : jit_string_patches_) {
5731 const auto& it = jit_string_roots_.find(entry.first);
5732 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005733 PatchJitRootUse(code, roots_data, entry.second, it->second);
5734 }
5735 for (const auto& entry : jit_class_patches_) {
5736 const auto& it = jit_class_roots_.find(entry.first);
5737 DCHECK(it != jit_class_roots_.end());
5738 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005739 }
5740}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005741
Alexandre Rames67555f72014-11-18 10:55:16 +00005742#undef __
5743#undef QUICK_ENTRY_POINT
5744
Alexandre Rames5319def2014-10-23 10:03:10 +01005745} // namespace arm64
5746} // namespace art