blob: ea579907dffd4d163d7ed57c5af98abc13335a36 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = at_->GetLocations();
285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
293 : kQuickInitializeType;
294 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100296 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100298 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000300
301 // Move the class to the desired location.
302 Location out = locations->Out();
303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 __ B(GetExitLabel());
311 }
312
Alexandre Rames9931f312015-06-19 14:47:01 +0100313 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
314
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
319 // The instruction where this slow path is happening.
320 // (Might be the load class or an initialization check).
321 HInstruction* const at_;
322
323 // The dex PC of `at_`.
324 const uint32_t dex_pc_;
325
326 // Whether to initialize the class.
327 const bool do_clinit_;
328
329 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
330};
331
Vladimir Markoaad75c62016-10-03 08:46:48 +0000332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100334 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
335 : SlowPathCodeARM64(instruction),
336 temp_(temp),
337 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 LocationSummary* locations = instruction_->GetLocations();
341 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
343
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100344 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
345 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
346 temps.Exclude(temp_);
347
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, locations);
350
351 InvokeRuntimeCallingConvention calling_convention;
352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
356 Primitive::Type type = instruction_->GetType();
357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
358
359 RestoreLiveRegisters(codegen, locations);
360
361 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
364 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
367 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
368 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100371 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100376 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 }
378
379 __ B(GetExitLabel());
380 }
381
382 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
383
384 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100385 const Register temp_;
386 vixl::aarch64::Label* adrp_label_;
387
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
389};
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 if (instruction_->CanThrowIntoCatchBlock()) {
399 // Live registers will be restored in the catch block if caught.
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
401 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000402 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
403 instruction_,
404 instruction_->GetDexPc(),
405 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800406 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100407 }
408
Alexandre Rames8158f282015-08-07 10:26:17 +0100409 bool IsFatal() const OVERRIDE { return true; }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
412
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
415};
416
417class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100419 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000420 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 if (successor_ == nullptr) {
428 __ B(GetReturnLabel());
429 } else {
430 __ B(arm64_codegen->GetLabelOf(successor_));
431 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 }
433
Scott Wakeling97c72b72016-06-24 16:19:36 +0100434 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 DCHECK(successor_ == nullptr);
436 return &return_label_;
437 }
438
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100439 HBasicBlock* GetSuccessor() const {
440 return successor_;
441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
444
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 // If not null, the block to branch to after the suspend check.
447 HBasicBlock* const successor_;
448
449 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100450 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100451
452 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
453};
454
Alexandre Rames67555f72014-11-18 10:55:16 +0000455class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000457 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000458 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800462 Location arg0, arg1;
463 if (instruction_->IsInstanceOf()) {
464 arg0 = locations->InAt(1);
465 arg1 = locations->Out();
466 } else {
467 arg0 = locations->InAt(0);
468 arg1 = locations->InAt(1);
469 }
470
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 DCHECK(instruction_->IsCheckCast()
472 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
473 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100474 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475
Alexandre Rames67555f72014-11-18 10:55:16 +0000476 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000478 if (!is_fatal_) {
479 SaveLiveRegisters(codegen, locations);
480 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481
482 // We're moving two locations to locations that could overlap, so we need a parallel
483 // move resolver.
484 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800485 codegen->EmitParallelMoves(arg0,
486 LocationFrom(calling_convention.GetRegisterAt(0)),
487 Primitive::kPrimNot,
488 arg1,
489 LocationFrom(calling_convention.GetRegisterAt(1)),
490 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000491 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000492 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800493 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000494 Primitive::Type ret_type = instruction_->GetType();
495 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
496 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
497 } else {
498 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800499 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
500 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 }
502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000503 if (!is_fatal_) {
504 RestoreLiveRegisters(codegen, locations);
505 __ B(GetExitLabel());
506 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 }
508
Alexandre Rames9931f312015-06-19 14:47:01 +0100509 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100510 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100511
Alexandre Rames67555f72014-11-18 10:55:16 +0000512 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000514
Alexandre Rames67555f72014-11-18 10:55:16 +0000515 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
516};
517
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700518class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
519 public:
Aart Bik42249c32016-01-07 15:33:50 -0800520 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000521 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700522
523 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800524 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000526 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000527 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528 }
529
Alexandre Rames9931f312015-06-19 14:47:01 +0100530 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
531
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700532 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700533 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
534};
535
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100536class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
537 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000538 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100539
540 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
541 LocationSummary* locations = instruction_->GetLocations();
542 __ Bind(GetEntryLabel());
543 SaveLiveRegisters(codegen, locations);
544
545 InvokeRuntimeCallingConvention calling_convention;
546 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
547 parallel_move.AddMove(
548 locations->InAt(0),
549 LocationFrom(calling_convention.GetRegisterAt(0)),
550 Primitive::kPrimNot,
551 nullptr);
552 parallel_move.AddMove(
553 locations->InAt(1),
554 LocationFrom(calling_convention.GetRegisterAt(1)),
555 Primitive::kPrimInt,
556 nullptr);
557 parallel_move.AddMove(
558 locations->InAt(2),
559 LocationFrom(calling_convention.GetRegisterAt(2)),
560 Primitive::kPrimNot,
561 nullptr);
562 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
563
564 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000565 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
567 RestoreLiveRegisters(codegen, locations);
568 __ B(GetExitLabel());
569 }
570
571 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
572
573 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100574 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
575};
576
Zheng Xu3927c8b2015-11-18 17:46:25 +0800577void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
578 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000579 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800580
581 // We are about to use the assembler to place literals directly. Make sure we have enough
582 // underlying code buffer and we have generated the jump table with right size.
583 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
584 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
585
586 __ Bind(&table_start_);
587 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
588 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100589 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800590 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100591 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800592 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
593 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
594 Literal<int32_t> literal(jump_offset);
595 __ place(&literal);
596 }
597}
598
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100599// Slow path marking an object reference `ref` during a read
600// barrier. The field `obj.field` in the object `obj` holding this
601// reference does not get updated by this slow path after marking (see
602// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
603//
604// This means that after the execution of this slow path, `ref` will
605// always be up-to-date, but `obj.field` may not; i.e., after the
606// flip, `ref` will be a to-space reference, but `obj.field` will
607// probably still be a from-space reference (unless it gets updated by
608// another thread, or if another thread installed another object
609// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800610// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
611// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000612class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
613 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800614 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
615 Location ref,
616 Location entrypoint = Location::NoLocation())
617 : SlowPathCodeARM64(instruction),
618 ref_(ref),
619 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000620 DCHECK(kEmitCompilerReadBarrier);
621 }
622
623 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000627 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100628 DCHECK(ref_.IsRegister()) << ref_;
629 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000630 DCHECK(instruction_->IsInstanceFieldGet() ||
631 instruction_->IsStaticFieldGet() ||
632 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100633 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000634 instruction_->IsLoadClass() ||
635 instruction_->IsLoadString() ||
636 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100637 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100638 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
639 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000640 << "Unexpected instruction in read barrier marking slow path: "
641 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000642 // The read barrier instrumentation of object ArrayGet
643 // instructions does not support the HIntermediateAddress
644 // instruction.
645 DCHECK(!(instruction_->IsArrayGet() &&
646 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000647
648 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100649 // No need to save live registers; it's taken care of by the
650 // entrypoint. Also, there is no need to update the stack mask,
651 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000652 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100653 DCHECK_NE(ref_.reg(), LR);
654 DCHECK_NE(ref_.reg(), WSP);
655 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100656 // IP0 is used internally by the ReadBarrierMarkRegX entry point
657 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100658 DCHECK_NE(ref_.reg(), IP0);
659 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100660 // "Compact" slow path, saving two moves.
661 //
662 // Instead of using the standard runtime calling convention (input
663 // and output in W0):
664 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100665 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100666 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100667 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100668 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100669 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100670 // of a dedicated entrypoint:
671 //
672 // rX <- ReadBarrierMarkRegX(rX)
673 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800674 if (entrypoint_.IsValid()) {
675 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
676 __ Blr(XRegisterFrom(entrypoint_));
677 } else {
678 // Entrypoint is not already loaded, load from the thread.
679 int32_t entry_point_offset =
680 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
681 // This runtime call does not require a stack map.
682 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
683 }
Roland Levillain44015862016-01-22 11:47:17 +0000684 __ B(GetExitLabel());
685 }
686
687 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100688 // The location (register) of the marked object reference.
689 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000690
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800691 // The location of the entrypoint if it is already loaded.
692 const Location entrypoint_;
693
Roland Levillain44015862016-01-22 11:47:17 +0000694 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
695};
696
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100697// Slow path marking an object reference `ref` during a read barrier,
698// and if needed, atomically updating the field `obj.field` in the
699// object `obj` holding this reference after marking (contrary to
700// ReadBarrierMarkSlowPathARM64 above, which never tries to update
701// `obj.field`).
702//
703// This means that after the execution of this slow path, both `ref`
704// and `obj.field` will be up-to-date; i.e., after the flip, both will
705// hold the same to-space reference (unless another thread installed
706// another object reference (different from `ref`) in `obj.field`).
707class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
708 public:
709 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
710 Location ref,
711 Register obj,
712 Location field_offset,
713 Register temp)
714 : SlowPathCodeARM64(instruction),
715 ref_(ref),
716 obj_(obj),
717 field_offset_(field_offset),
718 temp_(temp) {
719 DCHECK(kEmitCompilerReadBarrier);
720 }
721
722 const char* GetDescription() const OVERRIDE {
723 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
724 }
725
726 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
727 LocationSummary* locations = instruction_->GetLocations();
728 Register ref_reg = WRegisterFrom(ref_);
729 DCHECK(locations->CanCall());
730 DCHECK(ref_.IsRegister()) << ref_;
731 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
732 // This slow path is only used by the UnsafeCASObject intrinsic.
733 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
734 << "Unexpected instruction in read barrier marking and field updating slow path: "
735 << instruction_->DebugName();
736 DCHECK(instruction_->GetLocations()->Intrinsified());
737 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
738 DCHECK(field_offset_.IsRegister()) << field_offset_;
739
740 __ Bind(GetEntryLabel());
741
742 // Save the old reference.
743 // Note that we cannot use IP to save the old reference, as IP is
744 // used internally by the ReadBarrierMarkRegX entry point, and we
745 // need the old reference after the call to that entry point.
746 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
747 __ Mov(temp_.W(), ref_reg);
748
749 // No need to save live registers; it's taken care of by the
750 // entrypoint. Also, there is no need to update the stack mask,
751 // as this runtime call will not trigger a garbage collection.
752 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
753 DCHECK_NE(ref_.reg(), LR);
754 DCHECK_NE(ref_.reg(), WSP);
755 DCHECK_NE(ref_.reg(), WZR);
756 // IP0 is used internally by the ReadBarrierMarkRegX entry point
757 // as a temporary, it cannot be the entry point's input/output.
758 DCHECK_NE(ref_.reg(), IP0);
759 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
760 // "Compact" slow path, saving two moves.
761 //
762 // Instead of using the standard runtime calling convention (input
763 // and output in W0):
764 //
765 // W0 <- ref
766 // W0 <- ReadBarrierMark(W0)
767 // ref <- W0
768 //
769 // we just use rX (the register containing `ref`) as input and output
770 // of a dedicated entrypoint:
771 //
772 // rX <- ReadBarrierMarkRegX(rX)
773 //
774 int32_t entry_point_offset =
775 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
776 // This runtime call does not require a stack map.
777 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
778
779 // If the new reference is different from the old reference,
780 // update the field in the holder (`*(obj_ + field_offset_)`).
781 //
782 // Note that this field could also hold a different object, if
783 // another thread had concurrently changed it. In that case, the
784 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
785 // (CAS) operation below would abort the CAS, leaving the field
786 // as-is.
787 vixl::aarch64::Label done;
788 __ Cmp(temp_.W(), ref_reg);
789 __ B(eq, &done);
790
791 // Update the the holder's field atomically. This may fail if
792 // mutator updates before us, but it's OK. This is achieved
793 // using a strong compare-and-set (CAS) operation with relaxed
794 // memory synchronization ordering, where the expected value is
795 // the old reference and the desired value is the new reference.
796
797 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
798 UseScratchRegisterScope temps(masm);
799
800 // Convenience aliases.
801 Register base = obj_.W();
802 Register offset = XRegisterFrom(field_offset_);
803 Register expected = temp_.W();
804 Register value = ref_reg;
805 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
806 Register tmp_value = temps.AcquireW(); // Value in memory.
807
808 __ Add(tmp_ptr, base.X(), Operand(offset));
809
810 if (kPoisonHeapReferences) {
811 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
812 if (value.Is(expected)) {
813 // Do not poison `value`, as it is the same register as
814 // `expected`, which has just been poisoned.
815 } else {
816 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
817 }
818 }
819
820 // do {
821 // tmp_value = [tmp_ptr] - expected;
822 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
823
Roland Levillain24a4d112016-10-26 13:10:46 +0100824 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100825 __ Bind(&loop_head);
826 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
827 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100828 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100829 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
830 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100831 __ B(&exit_loop);
832 __ Bind(&comparison_failed);
833 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100834 __ Bind(&exit_loop);
835
836 if (kPoisonHeapReferences) {
837 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
838 if (value.Is(expected)) {
839 // Do not unpoison `value`, as it is the same register as
840 // `expected`, which has just been unpoisoned.
841 } else {
842 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
843 }
844 }
845
846 __ Bind(&done);
847 __ B(GetExitLabel());
848 }
849
850 private:
851 // The location (register) of the marked object reference.
852 const Location ref_;
853 // The register containing the object holding the marked object reference field.
854 const Register obj_;
855 // The location of the offset of the marked reference field within `obj_`.
856 Location field_offset_;
857
858 const Register temp_;
859
860 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
861};
862
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000863// Slow path generating a read barrier for a heap reference.
864class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
865 public:
866 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
867 Location out,
868 Location ref,
869 Location obj,
870 uint32_t offset,
871 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000872 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000873 out_(out),
874 ref_(ref),
875 obj_(obj),
876 offset_(offset),
877 index_(index) {
878 DCHECK(kEmitCompilerReadBarrier);
879 // If `obj` is equal to `out` or `ref`, it means the initial object
880 // has been overwritten by (or after) the heap object reference load
881 // to be instrumented, e.g.:
882 //
883 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000884 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000885 //
886 // In that case, we have lost the information about the original
887 // object, and the emitted read barrier cannot work properly.
888 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
889 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
890 }
891
892 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
893 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
894 LocationSummary* locations = instruction_->GetLocations();
895 Primitive::Type type = Primitive::kPrimNot;
896 DCHECK(locations->CanCall());
897 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100898 DCHECK(instruction_->IsInstanceFieldGet() ||
899 instruction_->IsStaticFieldGet() ||
900 instruction_->IsArrayGet() ||
901 instruction_->IsInstanceOf() ||
902 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100903 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000904 << "Unexpected instruction in read barrier for heap reference slow path: "
905 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000906 // The read barrier instrumentation of object ArrayGet
907 // instructions does not support the HIntermediateAddress
908 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000909 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100910 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000911
912 __ Bind(GetEntryLabel());
913
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000914 SaveLiveRegisters(codegen, locations);
915
916 // We may have to change the index's value, but as `index_` is a
917 // constant member (like other "inputs" of this slow path),
918 // introduce a copy of it, `index`.
919 Location index = index_;
920 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100921 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000922 if (instruction_->IsArrayGet()) {
923 // Compute the actual memory offset and store it in `index`.
924 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
925 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
926 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
927 // We are about to change the value of `index_reg` (see the
928 // calls to vixl::MacroAssembler::Lsl and
929 // vixl::MacroAssembler::Mov below), but it has
930 // not been saved by the previous call to
931 // art::SlowPathCode::SaveLiveRegisters, as it is a
932 // callee-save register --
933 // art::SlowPathCode::SaveLiveRegisters does not consider
934 // callee-save registers, as it has been designed with the
935 // assumption that callee-save registers are supposed to be
936 // handled by the called function. So, as a callee-save
937 // register, `index_reg` _would_ eventually be saved onto
938 // the stack, but it would be too late: we would have
939 // changed its value earlier. Therefore, we manually save
940 // it here into another freely available register,
941 // `free_reg`, chosen of course among the caller-save
942 // registers (as a callee-save `free_reg` register would
943 // exhibit the same problem).
944 //
945 // Note we could have requested a temporary register from
946 // the register allocator instead; but we prefer not to, as
947 // this is a slow path, and we know we can find a
948 // caller-save register that is available.
949 Register free_reg = FindAvailableCallerSaveRegister(codegen);
950 __ Mov(free_reg.W(), index_reg);
951 index_reg = free_reg;
952 index = LocationFrom(index_reg);
953 } else {
954 // The initial register stored in `index_` has already been
955 // saved in the call to art::SlowPathCode::SaveLiveRegisters
956 // (as it is not a callee-save register), so we can freely
957 // use it.
958 }
959 // Shifting the index value contained in `index_reg` by the scale
960 // factor (2) cannot overflow in practice, as the runtime is
961 // unable to allocate object arrays with a size larger than
962 // 2^26 - 1 (that is, 2^28 - 4 bytes).
963 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
964 static_assert(
965 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
966 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
967 __ Add(index_reg, index_reg, Operand(offset_));
968 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100969 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
970 // intrinsics, `index_` is not shifted by a scale factor of 2
971 // (as in the case of ArrayGet), as it is actually an offset
972 // to an object field within an object.
973 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000974 DCHECK(instruction_->GetLocations()->Intrinsified());
975 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
976 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
977 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100978 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100979 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000980 }
981 }
982
983 // We're moving two or three locations to locations that could
984 // overlap, so we need a parallel move resolver.
985 InvokeRuntimeCallingConvention calling_convention;
986 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
987 parallel_move.AddMove(ref_,
988 LocationFrom(calling_convention.GetRegisterAt(0)),
989 type,
990 nullptr);
991 parallel_move.AddMove(obj_,
992 LocationFrom(calling_convention.GetRegisterAt(1)),
993 type,
994 nullptr);
995 if (index.IsValid()) {
996 parallel_move.AddMove(index,
997 LocationFrom(calling_convention.GetRegisterAt(2)),
998 Primitive::kPrimInt,
999 nullptr);
1000 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1001 } else {
1002 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1003 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1004 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001005 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001006 instruction_,
1007 instruction_->GetDexPc(),
1008 this);
1009 CheckEntrypointTypes<
1010 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1011 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1012
1013 RestoreLiveRegisters(codegen, locations);
1014
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001015 __ B(GetExitLabel());
1016 }
1017
1018 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1019
1020 private:
1021 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001022 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1023 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001024 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1025 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1026 return Register(VIXLRegCodeFromART(i), kXRegSize);
1027 }
1028 }
1029 // We shall never fail to find a free caller-save register, as
1030 // there are more than two core caller-save registers on ARM64
1031 // (meaning it is possible to find one which is different from
1032 // `ref` and `obj`).
1033 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1034 LOG(FATAL) << "Could not find a free register";
1035 UNREACHABLE();
1036 }
1037
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001038 const Location out_;
1039 const Location ref_;
1040 const Location obj_;
1041 const uint32_t offset_;
1042 // An additional location containing an index to an array.
1043 // Only used for HArrayGet and the UnsafeGetObject &
1044 // UnsafeGetObjectVolatile intrinsics.
1045 const Location index_;
1046
1047 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1048};
1049
1050// Slow path generating a read barrier for a GC root.
1051class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1052 public:
1053 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001054 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001055 DCHECK(kEmitCompilerReadBarrier);
1056 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001057
1058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1059 LocationSummary* locations = instruction_->GetLocations();
1060 Primitive::Type type = Primitive::kPrimNot;
1061 DCHECK(locations->CanCall());
1062 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001063 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1064 << "Unexpected instruction in read barrier for GC root slow path: "
1065 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001066
1067 __ Bind(GetEntryLabel());
1068 SaveLiveRegisters(codegen, locations);
1069
1070 InvokeRuntimeCallingConvention calling_convention;
1071 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1072 // The argument of the ReadBarrierForRootSlow is not a managed
1073 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1074 // thus we need a 64-bit move here, and we cannot use
1075 //
1076 // arm64_codegen->MoveLocation(
1077 // LocationFrom(calling_convention.GetRegisterAt(0)),
1078 // root_,
1079 // type);
1080 //
1081 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1082 // reference type (`Primitive::kPrimNot`).
1083 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001084 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001085 instruction_,
1086 instruction_->GetDexPc(),
1087 this);
1088 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1089 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1090
1091 RestoreLiveRegisters(codegen, locations);
1092 __ B(GetExitLabel());
1093 }
1094
1095 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1096
1097 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001098 const Location out_;
1099 const Location root_;
1100
1101 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1102};
1103
Alexandre Rames5319def2014-10-23 10:03:10 +01001104#undef __
1105
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001106Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 Location next_location;
1108 if (type == Primitive::kPrimVoid) {
1109 LOG(FATAL) << "Unreachable type " << type;
1110 }
1111
Alexandre Rames542361f2015-01-29 16:57:31 +00001112 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001113 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1114 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001115 } else if (!Primitive::IsFloatingPointType(type) &&
1116 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001117 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1118 } else {
1119 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001120 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1121 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 }
1123
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001124 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001125 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 return next_location;
1127}
1128
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001129Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001130 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001131}
1132
Serban Constantinescu579885a2015-02-22 20:51:33 +00001133CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1134 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001135 const CompilerOptions& compiler_options,
1136 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001137 : CodeGenerator(graph,
1138 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001139 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001140 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001141 callee_saved_core_registers.GetList(),
1142 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001143 compiler_options,
1144 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001145 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001146 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001147 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001148 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001149 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001150 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001151 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001152 uint32_literals_(std::less<uint32_t>(),
1153 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001154 uint64_literals_(std::less<uint64_t>(),
1155 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 method_patches_(MethodReferenceComparator(),
1157 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1158 call_patches_(MethodReferenceComparator(),
1159 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1160 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001161 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1162 boot_image_string_patches_(StringReferenceValueComparator(),
1163 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1164 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001165 boot_image_type_patches_(TypeReferenceValueComparator(),
1166 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1167 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001168 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00001169 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1170 jit_string_patches_(StringReferenceValueComparator(),
1171 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001172 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001173 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001174}
Alexandre Rames5319def2014-10-23 10:03:10 +01001175
Alexandre Rames67555f72014-11-18 10:55:16 +00001176#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001177
Zheng Xu3927c8b2015-11-18 17:46:25 +08001178void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001179 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001180 jump_table->EmitTable(this);
1181 }
1182}
1183
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001184void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001185 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001186 // Ensure we emit the literal pool.
1187 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001188
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001189 CodeGenerator::Finalize(allocator);
1190}
1191
Zheng Xuad4450e2015-04-17 18:48:56 +08001192void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1193 // Note: There are 6 kinds of moves:
1194 // 1. constant -> GPR/FPR (non-cycle)
1195 // 2. constant -> stack (non-cycle)
1196 // 3. GPR/FPR -> GPR/FPR
1197 // 4. GPR/FPR -> stack
1198 // 5. stack -> GPR/FPR
1199 // 6. stack -> stack (non-cycle)
1200 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1201 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1202 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1203 // dependency.
1204 vixl_temps_.Open(GetVIXLAssembler());
1205}
1206
1207void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1208 vixl_temps_.Close();
1209}
1210
1211Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1212 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1213 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1214 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1215 Location scratch = GetScratchLocation(kind);
1216 if (!scratch.Equals(Location::NoLocation())) {
1217 return scratch;
1218 }
1219 // Allocate from VIXL temp registers.
1220 if (kind == Location::kRegister) {
1221 scratch = LocationFrom(vixl_temps_.AcquireX());
1222 } else {
1223 DCHECK(kind == Location::kFpuRegister);
1224 scratch = LocationFrom(vixl_temps_.AcquireD());
1225 }
1226 AddScratchLocation(scratch);
1227 return scratch;
1228}
1229
1230void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1231 if (loc.IsRegister()) {
1232 vixl_temps_.Release(XRegisterFrom(loc));
1233 } else {
1234 DCHECK(loc.IsFpuRegister());
1235 vixl_temps_.Release(DRegisterFrom(loc));
1236 }
1237 RemoveScratchLocation(loc);
1238}
1239
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001241 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001242 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001243}
1244
Alexandre Rames5319def2014-10-23 10:03:10 +01001245void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001246 MacroAssembler* masm = GetVIXLAssembler();
1247 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001248 __ Bind(&frame_entry_label_);
1249
Serban Constantinescu02164b32014-11-13 14:05:07 +00001250 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1251 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001252 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001253 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001254 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001255 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001256 __ Ldr(wzr, MemOperand(temp, 0));
1257 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001258 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001259
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001260 if (!HasEmptyFrame()) {
1261 int frame_size = GetFrameSize();
1262 // Stack layout:
1263 // sp[frame_size - 8] : lr.
1264 // ... : other preserved core registers.
1265 // ... : other preserved fp registers.
1266 // ... : reserved frame space.
1267 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001268
1269 // Save the current method if we need it. Note that we do not
1270 // do this in HCurrentMethod, as the instruction might have been removed
1271 // in the SSA graph.
1272 if (RequiresCurrentMethod()) {
1273 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001274 } else {
1275 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001276 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001277 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001278 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1279 frame_size - GetCoreSpillSize());
1280 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1281 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001282 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001283}
1284
1285void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001286 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001287 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001288 if (!HasEmptyFrame()) {
1289 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001290 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1291 frame_size - FrameEntrySpillSize());
1292 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1293 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001294 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001295 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001296 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001297 __ Ret();
1298 GetAssembler()->cfi().RestoreState();
1299 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001300}
1301
Scott Wakeling97c72b72016-06-24 16:19:36 +01001302CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001303 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001304 return CPURegList(CPURegister::kRegister, kXRegSize,
1305 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001306}
1307
Scott Wakeling97c72b72016-06-24 16:19:36 +01001308CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001309 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1310 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001311 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1312 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001313}
1314
Alexandre Rames5319def2014-10-23 10:03:10 +01001315void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1316 __ Bind(GetLabelOf(block));
1317}
1318
Calin Juravle175dc732015-08-25 15:42:32 +01001319void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1320 DCHECK(location.IsRegister());
1321 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1322}
1323
Calin Juravlee460d1d2015-09-29 04:52:17 +01001324void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1325 if (location.IsRegister()) {
1326 locations->AddTemp(location);
1327 } else {
1328 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1329 }
1330}
1331
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001332void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001333 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001334 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001335 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001336 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001337 if (value_can_be_null) {
1338 __ Cbz(value, &done);
1339 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001340 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001341 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001342 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001343 if (value_can_be_null) {
1344 __ Bind(&done);
1345 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001346}
1347
David Brazdil58282f42016-01-14 12:45:10 +00001348void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001349 // Blocked core registers:
1350 // lr : Runtime reserved.
1351 // tr : Runtime reserved.
1352 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1353 // ip1 : VIXL core temp.
1354 // ip0 : VIXL core temp.
1355 //
1356 // Blocked fp registers:
1357 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001358 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1359 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001361 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001362 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001363
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001364 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001365 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001366 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001367 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001368
David Brazdil58282f42016-01-14 12:45:10 +00001369 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001370 // Stubs do not save callee-save floating point registers. If the graph
1371 // is debuggable, we need to deal with these registers differently. For
1372 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001373 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1374 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001375 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001376 }
1377 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001378}
1379
Alexandre Rames3e69f162014-12-10 10:36:50 +00001380size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1381 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1382 __ Str(reg, MemOperand(sp, stack_index));
1383 return kArm64WordSize;
1384}
1385
1386size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1387 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1388 __ Ldr(reg, MemOperand(sp, stack_index));
1389 return kArm64WordSize;
1390}
1391
1392size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1393 FPRegister reg = FPRegister(reg_id, kDRegSize);
1394 __ Str(reg, MemOperand(sp, stack_index));
1395 return kArm64WordSize;
1396}
1397
1398size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1399 FPRegister reg = FPRegister(reg_id, kDRegSize);
1400 __ Ldr(reg, MemOperand(sp, stack_index));
1401 return kArm64WordSize;
1402}
1403
Alexandre Rames5319def2014-10-23 10:03:10 +01001404void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001405 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001406}
1407
1408void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001409 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001410}
1411
Alexandre Rames67555f72014-11-18 10:55:16 +00001412void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001413 if (constant->IsIntConstant()) {
1414 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1415 } else if (constant->IsLongConstant()) {
1416 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1417 } else if (constant->IsNullConstant()) {
1418 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001419 } else if (constant->IsFloatConstant()) {
1420 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1421 } else {
1422 DCHECK(constant->IsDoubleConstant());
1423 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1424 }
1425}
1426
Alexandre Rames3e69f162014-12-10 10:36:50 +00001427
1428static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1429 DCHECK(constant.IsConstant());
1430 HConstant* cst = constant.GetConstant();
1431 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001432 // Null is mapped to a core W register, which we associate with kPrimInt.
1433 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001434 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1435 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1436 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1437}
1438
Calin Juravlee460d1d2015-09-29 04:52:17 +01001439void CodeGeneratorARM64::MoveLocation(Location destination,
1440 Location source,
1441 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001442 if (source.Equals(destination)) {
1443 return;
1444 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445
1446 // A valid move can always be inferred from the destination and source
1447 // locations. When moving from and to a register, the argument type can be
1448 // used to generate 32bit instead of 64bit moves. In debug mode we also
1449 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001450 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001451
1452 if (destination.IsRegister() || destination.IsFpuRegister()) {
1453 if (unspecified_type) {
1454 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1455 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001456 (src_cst != nullptr && (src_cst->IsIntConstant()
1457 || src_cst->IsFloatConstant()
1458 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001459 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001460 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001461 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001462 // If the source is a double stack slot or a 64bit constant, a 64bit
1463 // type is appropriate. Else the source is a register, and since the
1464 // type has not been specified, we chose a 64bit type to force a 64bit
1465 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001466 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001467 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001468 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001469 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1470 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1471 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001472 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1473 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1474 __ Ldr(dst, StackOperandFrom(source));
1475 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001476 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001477 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001478 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001479 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001480 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001481 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001482 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001483 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1484 ? Primitive::kPrimLong
1485 : Primitive::kPrimInt;
1486 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1487 }
1488 } else {
1489 DCHECK(source.IsFpuRegister());
1490 if (destination.IsRegister()) {
1491 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1492 ? Primitive::kPrimDouble
1493 : Primitive::kPrimFloat;
1494 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1495 } else {
1496 DCHECK(destination.IsFpuRegister());
1497 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498 }
1499 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001500 } else { // The destination is not a register. It must be a stack slot.
1501 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1502 if (source.IsRegister() || source.IsFpuRegister()) {
1503 if (unspecified_type) {
1504 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001505 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001507 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001508 }
1509 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001510 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1511 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1512 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001514 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1515 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001516 UseScratchRegisterScope temps(GetVIXLAssembler());
1517 HConstant* src_cst = source.GetConstant();
1518 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001519 if (src_cst->IsZeroBitPattern()) {
1520 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001521 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001522 if (src_cst->IsIntConstant()) {
1523 temp = temps.AcquireW();
1524 } else if (src_cst->IsLongConstant()) {
1525 temp = temps.AcquireX();
1526 } else if (src_cst->IsFloatConstant()) {
1527 temp = temps.AcquireS();
1528 } else {
1529 DCHECK(src_cst->IsDoubleConstant());
1530 temp = temps.AcquireD();
1531 }
1532 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001533 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001534 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001535 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001536 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001538 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 // There is generally less pressure on FP registers.
1540 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001541 __ Ldr(temp, StackOperandFrom(source));
1542 __ Str(temp, StackOperandFrom(destination));
1543 }
1544 }
1545}
1546
1547void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001548 CPURegister dst,
1549 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001550 switch (type) {
1551 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001552 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001553 break;
1554 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001555 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556 break;
1557 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001558 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001559 break;
1560 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001561 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001562 break;
1563 case Primitive::kPrimInt:
1564 case Primitive::kPrimNot:
1565 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001566 case Primitive::kPrimFloat:
1567 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001568 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 __ Ldr(dst, src);
1570 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001571 case Primitive::kPrimVoid:
1572 LOG(FATAL) << "Unreachable type " << type;
1573 }
1574}
1575
Calin Juravle77520bc2015-01-12 18:45:46 +00001576void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001577 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001578 const MemOperand& src,
1579 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001580 MacroAssembler* masm = GetVIXLAssembler();
1581 BlockPoolsScope block_pools(masm);
1582 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001583 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001584 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001585
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001586 DCHECK(!src.IsPreIndex());
1587 DCHECK(!src.IsPostIndex());
1588
1589 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001590 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001591 MemOperand base = MemOperand(temp_base);
1592 switch (type) {
1593 case Primitive::kPrimBoolean:
1594 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001595 if (needs_null_check) {
1596 MaybeRecordImplicitNullCheck(instruction);
1597 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001598 break;
1599 case Primitive::kPrimByte:
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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1605 break;
1606 case Primitive::kPrimChar:
1607 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001608 if (needs_null_check) {
1609 MaybeRecordImplicitNullCheck(instruction);
1610 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001611 break;
1612 case Primitive::kPrimShort:
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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1618 break;
1619 case Primitive::kPrimInt:
1620 case Primitive::kPrimNot:
1621 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001622 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001623 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001624 if (needs_null_check) {
1625 MaybeRecordImplicitNullCheck(instruction);
1626 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001627 break;
1628 case Primitive::kPrimFloat:
1629 case Primitive::kPrimDouble: {
1630 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001631 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001632
1633 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1634 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001635 if (needs_null_check) {
1636 MaybeRecordImplicitNullCheck(instruction);
1637 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001638 __ Fmov(FPRegister(dst), temp);
1639 break;
1640 }
1641 case Primitive::kPrimVoid:
1642 LOG(FATAL) << "Unreachable type " << type;
1643 }
1644}
1645
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001646void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001647 CPURegister src,
1648 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001649 switch (type) {
1650 case Primitive::kPrimBoolean:
1651 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001652 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001653 break;
1654 case Primitive::kPrimChar:
1655 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001656 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001657 break;
1658 case Primitive::kPrimInt:
1659 case Primitive::kPrimNot:
1660 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001661 case Primitive::kPrimFloat:
1662 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001663 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001664 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001665 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001666 case Primitive::kPrimVoid:
1667 LOG(FATAL) << "Unreachable type " << type;
1668 }
1669}
1670
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001671void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1672 CPURegister src,
1673 const MemOperand& dst) {
1674 UseScratchRegisterScope temps(GetVIXLAssembler());
1675 Register temp_base = temps.AcquireX();
1676
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001677 DCHECK(!dst.IsPreIndex());
1678 DCHECK(!dst.IsPostIndex());
1679
1680 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001681 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001682 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001683 MemOperand base = MemOperand(temp_base);
1684 switch (type) {
1685 case Primitive::kPrimBoolean:
1686 case Primitive::kPrimByte:
1687 __ Stlrb(Register(src), base);
1688 break;
1689 case Primitive::kPrimChar:
1690 case Primitive::kPrimShort:
1691 __ Stlrh(Register(src), base);
1692 break;
1693 case Primitive::kPrimInt:
1694 case Primitive::kPrimNot:
1695 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001696 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001697 __ Stlr(Register(src), base);
1698 break;
1699 case Primitive::kPrimFloat:
1700 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001701 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001702 Register temp_src;
1703 if (src.IsZero()) {
1704 // The zero register is used to avoid synthesizing zero constants.
1705 temp_src = Register(src);
1706 } else {
1707 DCHECK(src.IsFPRegister());
1708 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1709 __ Fmov(temp_src, FPRegister(src));
1710 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001711
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001712 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001713 break;
1714 }
1715 case Primitive::kPrimVoid:
1716 LOG(FATAL) << "Unreachable type " << type;
1717 }
1718}
1719
Calin Juravle175dc732015-08-25 15:42:32 +01001720void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1721 HInstruction* instruction,
1722 uint32_t dex_pc,
1723 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001724 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001725 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001726 if (EntrypointRequiresStackMap(entrypoint)) {
1727 RecordPcInfo(instruction, dex_pc, slow_path);
1728 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001729}
1730
Roland Levillaindec8f632016-07-22 17:10:06 +01001731void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1732 HInstruction* instruction,
1733 SlowPathCode* slow_path) {
1734 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001735 GenerateInvokeRuntime(entry_point_offset);
1736}
1737
1738void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001739 BlockPoolsScope block_pools(GetVIXLAssembler());
1740 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1741 __ Blr(lr);
1742}
1743
Alexandre Rames67555f72014-11-18 10:55:16 +00001744void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001745 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001746 UseScratchRegisterScope temps(GetVIXLAssembler());
1747 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001748 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1749
Serban Constantinescu02164b32014-11-13 14:05:07 +00001750 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001751 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1752 __ Add(temp, class_reg, status_offset);
1753 __ Ldar(temp, HeapOperand(temp));
1754 __ Cmp(temp, mirror::Class::kStatusInitialized);
1755 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001756 __ Bind(slow_path->GetExitLabel());
1757}
Alexandre Rames5319def2014-10-23 10:03:10 +01001758
Roland Levillain44015862016-01-22 11:47:17 +00001759void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001760 BarrierType type = BarrierAll;
1761
1762 switch (kind) {
1763 case MemBarrierKind::kAnyAny:
1764 case MemBarrierKind::kAnyStore: {
1765 type = BarrierAll;
1766 break;
1767 }
1768 case MemBarrierKind::kLoadAny: {
1769 type = BarrierReads;
1770 break;
1771 }
1772 case MemBarrierKind::kStoreStore: {
1773 type = BarrierWrites;
1774 break;
1775 }
1776 default:
1777 LOG(FATAL) << "Unexpected memory barrier " << kind;
1778 }
1779 __ Dmb(InnerShareable, type);
1780}
1781
Serban Constantinescu02164b32014-11-13 14:05:07 +00001782void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1783 HBasicBlock* successor) {
1784 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001785 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1786 if (slow_path == nullptr) {
1787 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1788 instruction->SetSlowPath(slow_path);
1789 codegen_->AddSlowPath(slow_path);
1790 if (successor != nullptr) {
1791 DCHECK(successor->IsLoopHeader());
1792 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1793 }
1794 } else {
1795 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1796 }
1797
Serban Constantinescu02164b32014-11-13 14:05:07 +00001798 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1799 Register temp = temps.AcquireW();
1800
Andreas Gampe542451c2016-07-26 09:02:02 -07001801 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001802 if (successor == nullptr) {
1803 __ Cbnz(temp, slow_path->GetEntryLabel());
1804 __ Bind(slow_path->GetReturnLabel());
1805 } else {
1806 __ Cbz(temp, codegen_->GetLabelOf(successor));
1807 __ B(slow_path->GetEntryLabel());
1808 // slow_path will return to GetLabelOf(successor).
1809 }
1810}
1811
Alexandre Rames5319def2014-10-23 10:03:10 +01001812InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1813 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001814 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001815 assembler_(codegen->GetAssembler()),
1816 codegen_(codegen) {}
1817
1818#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001819 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001820
1821#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1822
1823enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001824 // Using a base helps identify when we hit such breakpoints.
1825 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001826#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1827 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1828#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1829};
1830
1831#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001832 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001833 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1834 } \
1835 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1837 locations->SetOut(Location::Any()); \
1838 }
1839 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1840#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1841
1842#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001843#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001844
Alexandre Rames67555f72014-11-18 10:55:16 +00001845void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001846 DCHECK_EQ(instr->InputCount(), 2U);
1847 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1848 Primitive::Type type = instr->GetResultType();
1849 switch (type) {
1850 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001851 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001852 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001853 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001855 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001856
1857 case Primitive::kPrimFloat:
1858 case Primitive::kPrimDouble:
1859 locations->SetInAt(0, Location::RequiresFpuRegister());
1860 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001861 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001862 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001863
Alexandre Rames5319def2014-10-23 10:03:10 +01001864 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001865 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001866 }
1867}
1868
Alexandre Rames09a99962015-04-15 11:47:56 +01001869void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001870 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1871
1872 bool object_field_get_with_read_barrier =
1873 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001874 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001875 new (GetGraph()->GetArena()) LocationSummary(instruction,
1876 object_field_get_with_read_barrier ?
1877 LocationSummary::kCallOnSlowPath :
1878 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001879 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001880 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001881 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001882 locations->SetInAt(0, Location::RequiresRegister());
1883 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1884 locations->SetOut(Location::RequiresFpuRegister());
1885 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001886 // The output overlaps for an object field get when read barriers
1887 // are enabled: we do not want the load to overwrite the object's
1888 // location, as we need it to emit the read barrier.
1889 locations->SetOut(
1890 Location::RequiresRegister(),
1891 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001892 }
1893}
1894
1895void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1896 const FieldInfo& field_info) {
1897 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001898 LocationSummary* locations = instruction->GetLocations();
1899 Location base_loc = locations->InAt(0);
1900 Location out = locations->Out();
1901 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001902 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001903 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001904 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001905
Roland Levillain44015862016-01-22 11:47:17 +00001906 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1907 // Object FieldGet with Baker's read barrier case.
1908 MacroAssembler* masm = GetVIXLAssembler();
1909 UseScratchRegisterScope temps(masm);
1910 // /* HeapReference<Object> */ out = *(base + offset)
1911 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1912 Register temp = temps.AcquireW();
1913 // Note that potential implicit null checks are handled in this
1914 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1915 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1916 instruction,
1917 out,
1918 base,
1919 offset,
1920 temp,
1921 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001922 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001923 } else {
1924 // General case.
1925 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001926 // Note that a potential implicit null check is handled in this
1927 // CodeGeneratorARM64::LoadAcquire call.
1928 // NB: LoadAcquire will record the pc info if needed.
1929 codegen_->LoadAcquire(
1930 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001931 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001932 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001933 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001934 }
Roland Levillain44015862016-01-22 11:47:17 +00001935 if (field_type == Primitive::kPrimNot) {
1936 // If read barriers are enabled, emit read barriers other than
1937 // Baker's using a slow path (and also unpoison the loaded
1938 // reference, if heap poisoning is enabled).
1939 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1940 }
Roland Levillain4d027112015-07-01 15:41:14 +01001941 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001942}
1943
1944void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1945 LocationSummary* locations =
1946 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1947 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001948 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1949 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1950 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001951 locations->SetInAt(1, Location::RequiresFpuRegister());
1952 } else {
1953 locations->SetInAt(1, Location::RequiresRegister());
1954 }
1955}
1956
1957void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001958 const FieldInfo& field_info,
1959 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001960 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001961 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001962
1963 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001964 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001965 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001966 Offset offset = field_info.GetFieldOffset();
1967 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001968
Roland Levillain4d027112015-07-01 15:41:14 +01001969 {
1970 // We use a block to end the scratch scope before the write barrier, thus
1971 // freeing the temporary registers so they can be used in `MarkGCCard`.
1972 UseScratchRegisterScope temps(GetVIXLAssembler());
1973
1974 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1975 DCHECK(value.IsW());
1976 Register temp = temps.AcquireW();
1977 __ Mov(temp, value.W());
1978 GetAssembler()->PoisonHeapReference(temp.W());
1979 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001980 }
Roland Levillain4d027112015-07-01 15:41:14 +01001981
1982 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001983 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1984 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001985 } else {
1986 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1987 codegen_->MaybeRecordImplicitNullCheck(instruction);
1988 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001989 }
1990
1991 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001992 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001993 }
1994}
1995
Alexandre Rames67555f72014-11-18 10:55:16 +00001996void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001997 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001998
1999 switch (type) {
2000 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002001 case Primitive::kPrimLong: {
2002 Register dst = OutputRegister(instr);
2003 Register lhs = InputRegisterAt(instr, 0);
2004 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002005 if (instr->IsAdd()) {
2006 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002007 } else if (instr->IsAnd()) {
2008 __ And(dst, lhs, rhs);
2009 } else if (instr->IsOr()) {
2010 __ Orr(dst, lhs, rhs);
2011 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002012 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002013 } else if (instr->IsRor()) {
2014 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002015 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002016 __ Ror(dst, lhs, shift);
2017 } else {
2018 // Ensure shift distance is in the same size register as the result. If
2019 // we are rotating a long and the shift comes in a w register originally,
2020 // we don't need to sxtw for use as an x since the shift distances are
2021 // all & reg_bits - 1.
2022 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2023 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002024 } else {
2025 DCHECK(instr->IsXor());
2026 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002027 }
2028 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002029 }
2030 case Primitive::kPrimFloat:
2031 case Primitive::kPrimDouble: {
2032 FPRegister dst = OutputFPRegister(instr);
2033 FPRegister lhs = InputFPRegisterAt(instr, 0);
2034 FPRegister rhs = InputFPRegisterAt(instr, 1);
2035 if (instr->IsAdd()) {
2036 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002037 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002038 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002039 } else {
2040 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002041 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002042 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002043 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002044 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002045 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002046 }
2047}
2048
Serban Constantinescu02164b32014-11-13 14:05:07 +00002049void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2050 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2051
2052 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2053 Primitive::Type type = instr->GetResultType();
2054 switch (type) {
2055 case Primitive::kPrimInt:
2056 case Primitive::kPrimLong: {
2057 locations->SetInAt(0, Location::RequiresRegister());
2058 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2059 locations->SetOut(Location::RequiresRegister());
2060 break;
2061 }
2062 default:
2063 LOG(FATAL) << "Unexpected shift type " << type;
2064 }
2065}
2066
2067void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2068 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2069
2070 Primitive::Type type = instr->GetType();
2071 switch (type) {
2072 case Primitive::kPrimInt:
2073 case Primitive::kPrimLong: {
2074 Register dst = OutputRegister(instr);
2075 Register lhs = InputRegisterAt(instr, 0);
2076 Operand rhs = InputOperandAt(instr, 1);
2077 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002078 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002079 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002080 if (instr->IsShl()) {
2081 __ Lsl(dst, lhs, shift_value);
2082 } else if (instr->IsShr()) {
2083 __ Asr(dst, lhs, shift_value);
2084 } else {
2085 __ Lsr(dst, lhs, shift_value);
2086 }
2087 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002088 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002089
2090 if (instr->IsShl()) {
2091 __ Lsl(dst, lhs, rhs_reg);
2092 } else if (instr->IsShr()) {
2093 __ Asr(dst, lhs, rhs_reg);
2094 } else {
2095 __ Lsr(dst, lhs, rhs_reg);
2096 }
2097 }
2098 break;
2099 }
2100 default:
2101 LOG(FATAL) << "Unexpected shift operation type " << type;
2102 }
2103}
2104
Alexandre Rames5319def2014-10-23 10:03:10 +01002105void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002106 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002107}
2108
2109void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002110 HandleBinaryOp(instruction);
2111}
2112
2113void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2114 HandleBinaryOp(instruction);
2115}
2116
2117void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2118 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002119}
2120
Artem Serov7fc63502016-02-09 17:15:29 +00002121void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002122 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2124 locations->SetInAt(0, Location::RequiresRegister());
2125 // There is no immediate variant of negated bitwise instructions in AArch64.
2126 locations->SetInAt(1, Location::RequiresRegister());
2127 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2128}
2129
Artem Serov7fc63502016-02-09 17:15:29 +00002130void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002131 Register dst = OutputRegister(instr);
2132 Register lhs = InputRegisterAt(instr, 0);
2133 Register rhs = InputRegisterAt(instr, 1);
2134
2135 switch (instr->GetOpKind()) {
2136 case HInstruction::kAnd:
2137 __ Bic(dst, lhs, rhs);
2138 break;
2139 case HInstruction::kOr:
2140 __ Orn(dst, lhs, rhs);
2141 break;
2142 case HInstruction::kXor:
2143 __ Eon(dst, lhs, rhs);
2144 break;
2145 default:
2146 LOG(FATAL) << "Unreachable";
2147 }
2148}
2149
Alexandre Rames8626b742015-11-25 16:28:08 +00002150void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2151 HArm64DataProcWithShifterOp* instruction) {
2152 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2153 instruction->GetType() == Primitive::kPrimLong);
2154 LocationSummary* locations =
2155 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2156 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2157 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2158 } else {
2159 locations->SetInAt(0, Location::RequiresRegister());
2160 }
2161 locations->SetInAt(1, Location::RequiresRegister());
2162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2163}
2164
2165void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2166 HArm64DataProcWithShifterOp* instruction) {
2167 Primitive::Type type = instruction->GetType();
2168 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2169 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2170 Register out = OutputRegister(instruction);
2171 Register left;
2172 if (kind != HInstruction::kNeg) {
2173 left = InputRegisterAt(instruction, 0);
2174 }
2175 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2176 // shifter operand operation, the IR generating `right_reg` (input to the type
2177 // conversion) can have a different type from the current instruction's type,
2178 // so we manually indicate the type.
2179 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002180 int64_t shift_amount = instruction->GetShiftAmount() &
2181 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002182
2183 Operand right_operand(0);
2184
2185 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2186 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2187 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2188 } else {
2189 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2190 }
2191
2192 // Logical binary operations do not support extension operations in the
2193 // operand. Note that VIXL would still manage if it was passed by generating
2194 // the extension as a separate instruction.
2195 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2196 DCHECK(!right_operand.IsExtendedRegister() ||
2197 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2198 kind != HInstruction::kNeg));
2199 switch (kind) {
2200 case HInstruction::kAdd:
2201 __ Add(out, left, right_operand);
2202 break;
2203 case HInstruction::kAnd:
2204 __ And(out, left, right_operand);
2205 break;
2206 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002207 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002208 __ Neg(out, right_operand);
2209 break;
2210 case HInstruction::kOr:
2211 __ Orr(out, left, right_operand);
2212 break;
2213 case HInstruction::kSub:
2214 __ Sub(out, left, right_operand);
2215 break;
2216 case HInstruction::kXor:
2217 __ Eor(out, left, right_operand);
2218 break;
2219 default:
2220 LOG(FATAL) << "Unexpected operation kind: " << kind;
2221 UNREACHABLE();
2222 }
2223}
2224
Artem Serov328429f2016-07-06 16:23:04 +01002225void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002226 LocationSummary* locations =
2227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2228 locations->SetInAt(0, Location::RequiresRegister());
2229 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2230 locations->SetOut(Location::RequiresRegister());
2231}
2232
Roland Levillain19c54192016-11-04 13:44:09 +00002233void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002234 __ Add(OutputRegister(instruction),
2235 InputRegisterAt(instruction, 0),
2236 Operand(InputOperandAt(instruction, 1)));
2237}
2238
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002239void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002240 LocationSummary* locations =
2241 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002242 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2243 if (instr->GetOpKind() == HInstruction::kSub &&
2244 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002245 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002246 // Don't allocate register for Mneg instruction.
2247 } else {
2248 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2249 Location::RequiresRegister());
2250 }
2251 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2252 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2254}
2255
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002256void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002257 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002258 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2259 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002260
2261 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2262 // This fixup should be carried out for all multiply-accumulate instructions:
2263 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2264 if (instr->GetType() == Primitive::kPrimLong &&
2265 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2266 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002267 vixl::aarch64::Instruction* prev =
2268 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002269 if (prev->IsLoadOrStore()) {
2270 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002271 vixl::aarch64::CodeBufferCheckScope scope(masm,
2272 kInstructionSize,
2273 vixl::aarch64::CodeBufferCheckScope::kCheck,
2274 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002275 __ nop();
2276 }
2277 }
2278
2279 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002280 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002281 __ Madd(res, mul_left, mul_right, accumulator);
2282 } else {
2283 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002284 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002285 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002286 __ Mneg(res, mul_left, mul_right);
2287 } else {
2288 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2289 __ Msub(res, mul_left, mul_right, accumulator);
2290 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002291 }
2292}
2293
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002294void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002295 bool object_array_get_with_read_barrier =
2296 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002297 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002298 new (GetGraph()->GetArena()) LocationSummary(instruction,
2299 object_array_get_with_read_barrier ?
2300 LocationSummary::kCallOnSlowPath :
2301 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002302 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002303 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002304 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002305 locations->SetInAt(0, Location::RequiresRegister());
2306 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002307 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2308 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2309 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002310 // The output overlaps in the case of an object array get with
2311 // read barriers enabled: we do not want the move to overwrite the
2312 // array's location, as we need it to emit the read barrier.
2313 locations->SetOut(
2314 Location::RequiresRegister(),
2315 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002316 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002317}
2318
2319void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002320 Primitive::Type type = instruction->GetType();
2321 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002322 LocationSummary* locations = instruction->GetLocations();
2323 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002324 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002325 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002326 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2327 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002328 MacroAssembler* masm = GetVIXLAssembler();
2329 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002330 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002331 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002332
Roland Levillain19c54192016-11-04 13:44:09 +00002333 // The read barrier instrumentation of object ArrayGet instructions
2334 // does not support the HIntermediateAddress instruction.
2335 DCHECK(!((type == Primitive::kPrimNot) &&
2336 instruction->GetArray()->IsIntermediateAddress() &&
2337 kEmitCompilerReadBarrier));
2338
Roland Levillain44015862016-01-22 11:47:17 +00002339 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2340 // Object ArrayGet with Baker's read barrier case.
2341 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002342 // Note that a potential implicit null check is handled in the
2343 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2344 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2345 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002346 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002347 // General case.
2348 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002349 Register length;
2350 if (maybe_compressed_char_at) {
2351 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2352 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002353 if (instruction->GetArray()->IsIntermediateAddress()) {
2354 DCHECK_LT(count_offset, offset);
2355 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2356 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2357 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2358 } else {
2359 __ Ldr(length, HeapOperand(obj, count_offset));
2360 }
jessicahandojo05765752016-09-09 19:01:32 -07002361 codegen_->MaybeRecordImplicitNullCheck(instruction);
2362 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002363 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002364 if (maybe_compressed_char_at) {
2365 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002366 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2367 "Expecting 0=compressed, 1=uncompressed");
2368 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002369 __ Ldrb(Register(OutputCPURegister(instruction)),
2370 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2371 __ B(&done);
2372 __ Bind(&uncompressed_load);
2373 __ Ldrh(Register(OutputCPURegister(instruction)),
2374 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2375 __ Bind(&done);
2376 } else {
2377 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2378 source = HeapOperand(obj, offset);
2379 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002380 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002381 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002382 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002383 // We do not need to compute the intermediate address from the array: the
2384 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002385 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002386 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002387 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002388 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2389 }
2390 temp = obj;
2391 } else {
2392 __ Add(temp, obj, offset);
2393 }
jessicahandojo05765752016-09-09 19:01:32 -07002394 if (maybe_compressed_char_at) {
2395 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002396 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2397 "Expecting 0=compressed, 1=uncompressed");
2398 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002399 __ Ldrb(Register(OutputCPURegister(instruction)),
2400 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2401 __ B(&done);
2402 __ Bind(&uncompressed_load);
2403 __ Ldrh(Register(OutputCPURegister(instruction)),
2404 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2405 __ Bind(&done);
2406 } else {
2407 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2408 }
Roland Levillain44015862016-01-22 11:47:17 +00002409 }
jessicahandojo05765752016-09-09 19:01:32 -07002410 if (!maybe_compressed_char_at) {
2411 codegen_->Load(type, OutputCPURegister(instruction), source);
2412 codegen_->MaybeRecordImplicitNullCheck(instruction);
2413 }
Roland Levillain44015862016-01-22 11:47:17 +00002414
2415 if (type == Primitive::kPrimNot) {
2416 static_assert(
2417 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2418 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2419 Location obj_loc = locations->InAt(0);
2420 if (index.IsConstant()) {
2421 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2422 } else {
2423 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2424 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002425 }
Roland Levillain4d027112015-07-01 15:41:14 +01002426 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002427}
2428
Alexandre Rames5319def2014-10-23 10:03:10 +01002429void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2431 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002433}
2434
2435void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002436 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002437 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002438 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002439 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002440 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002441 // Mask out compression flag from String's array length.
2442 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002443 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002444 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002445}
2446
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002447void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002448 Primitive::Type value_type = instruction->GetComponentType();
2449
2450 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2452 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002453 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002454 LocationSummary::kCallOnSlowPath :
2455 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002458 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2459 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2460 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002461 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002462 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002463 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002464 }
2465}
2466
2467void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2468 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002469 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002470 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002471 bool needs_write_barrier =
2472 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002473
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002474 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002475 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002476 CPURegister source = value;
2477 Location index = locations->InAt(1);
2478 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2479 MemOperand destination = HeapOperand(array);
2480 MacroAssembler* masm = GetVIXLAssembler();
2481 BlockPoolsScope block_pools(masm);
2482
2483 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002484 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002485 if (index.IsConstant()) {
2486 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2487 destination = HeapOperand(array, offset);
2488 } else {
2489 UseScratchRegisterScope temps(masm);
2490 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002491 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002492 // We do not need to compute the intermediate address from the array: the
2493 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002494 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002495 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002496 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002497 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2498 }
2499 temp = array;
2500 } else {
2501 __ Add(temp, array, offset);
2502 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002503 destination = HeapOperand(temp,
2504 XRegisterFrom(index),
2505 LSL,
2506 Primitive::ComponentSizeShift(value_type));
2507 }
2508 codegen_->Store(value_type, value, destination);
2509 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002510 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002511 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002512 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002513 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002514 {
2515 // We use a block to end the scratch scope before the write barrier, thus
2516 // freeing the temporary registers so they can be used in `MarkGCCard`.
2517 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002518 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002519 if (index.IsConstant()) {
2520 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002521 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002522 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002523 destination = HeapOperand(temp,
2524 XRegisterFrom(index),
2525 LSL,
2526 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002527 }
2528
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002529 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2530 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2531 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2532
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002533 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002534 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2535 codegen_->AddSlowPath(slow_path);
2536 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002537 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002538 __ Cbnz(Register(value), &non_zero);
2539 if (!index.IsConstant()) {
2540 __ Add(temp, array, offset);
2541 }
2542 __ Str(wzr, destination);
2543 codegen_->MaybeRecordImplicitNullCheck(instruction);
2544 __ B(&done);
2545 __ Bind(&non_zero);
2546 }
2547
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002548 // Note that when Baker read barriers are enabled, the type
2549 // checks are performed without read barriers. This is fine,
2550 // even in the case where a class object is in the from-space
2551 // after the flip, as a comparison involving such a type would
2552 // not produce a false positive; it may of course produce a
2553 // false negative, in which case we would take the ArraySet
2554 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002555
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002556 Register temp2 = temps.AcquireSameSizeAs(array);
2557 // /* HeapReference<Class> */ temp = array->klass_
2558 __ Ldr(temp, HeapOperand(array, class_offset));
2559 codegen_->MaybeRecordImplicitNullCheck(instruction);
2560 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002561
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002562 // /* HeapReference<Class> */ temp = temp->component_type_
2563 __ Ldr(temp, HeapOperand(temp, component_offset));
2564 // /* HeapReference<Class> */ temp2 = value->klass_
2565 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2566 // If heap poisoning is enabled, no need to unpoison `temp`
2567 // nor `temp2`, as we are comparing two poisoned references.
2568 __ Cmp(temp, temp2);
2569 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002570
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002571 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2572 vixl::aarch64::Label do_put;
2573 __ B(eq, &do_put);
2574 // If heap poisoning is enabled, the `temp` reference has
2575 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002576 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2577
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002578 // /* HeapReference<Class> */ temp = temp->super_class_
2579 __ Ldr(temp, HeapOperand(temp, super_offset));
2580 // If heap poisoning is enabled, no need to unpoison
2581 // `temp`, as we are comparing against null below.
2582 __ Cbnz(temp, slow_path->GetEntryLabel());
2583 __ Bind(&do_put);
2584 } else {
2585 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002586 }
2587 }
2588
2589 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002590 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002591 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002592 __ Mov(temp2, value.W());
2593 GetAssembler()->PoisonHeapReference(temp2);
2594 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002595 }
2596
2597 if (!index.IsConstant()) {
2598 __ Add(temp, array, offset);
2599 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002600 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002601
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002602 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002603 codegen_->MaybeRecordImplicitNullCheck(instruction);
2604 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002605 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002606
2607 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2608
2609 if (done.IsLinked()) {
2610 __ Bind(&done);
2611 }
2612
2613 if (slow_path != nullptr) {
2614 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002615 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002616 }
2617}
2618
Alexandre Rames67555f72014-11-18 10:55:16 +00002619void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002620 RegisterSet caller_saves = RegisterSet::Empty();
2621 InvokeRuntimeCallingConvention calling_convention;
2622 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2623 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2624 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002625 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002626 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002627}
2628
2629void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002630 BoundsCheckSlowPathARM64* slow_path =
2631 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002632 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002633 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2634 __ B(slow_path->GetEntryLabel(), hs);
2635}
2636
Alexandre Rames67555f72014-11-18 10:55:16 +00002637void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2638 LocationSummary* locations =
2639 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2640 locations->SetInAt(0, Location::RequiresRegister());
2641 if (check->HasUses()) {
2642 locations->SetOut(Location::SameAsFirstInput());
2643 }
2644}
2645
2646void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2647 // We assume the class is not null.
2648 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2649 check->GetLoadClass(), check, check->GetDexPc(), true);
2650 codegen_->AddSlowPath(slow_path);
2651 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2652}
2653
Roland Levillain1a653882016-03-18 18:05:57 +00002654static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2655 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2656 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2657}
2658
2659void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2660 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2661 Location rhs_loc = instruction->GetLocations()->InAt(1);
2662 if (rhs_loc.IsConstant()) {
2663 // 0.0 is the only immediate that can be encoded directly in
2664 // an FCMP instruction.
2665 //
2666 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2667 // specify that in a floating-point comparison, positive zero
2668 // and negative zero are considered equal, so we can use the
2669 // literal 0.0 for both cases here.
2670 //
2671 // Note however that some methods (Float.equal, Float.compare,
2672 // Float.compareTo, Double.equal, Double.compare,
2673 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2674 // StrictMath.min) consider 0.0 to be (strictly) greater than
2675 // -0.0. So if we ever translate calls to these methods into a
2676 // HCompare instruction, we must handle the -0.0 case with
2677 // care here.
2678 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2679 __ Fcmp(lhs_reg, 0.0);
2680 } else {
2681 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2682 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002683}
2684
Serban Constantinescu02164b32014-11-13 14:05:07 +00002685void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002686 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002687 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2688 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002689 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002690 case Primitive::kPrimBoolean:
2691 case Primitive::kPrimByte:
2692 case Primitive::kPrimShort:
2693 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002694 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002695 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002696 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002697 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2699 break;
2700 }
2701 case Primitive::kPrimFloat:
2702 case Primitive::kPrimDouble: {
2703 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002704 locations->SetInAt(1,
2705 IsFloatingPointZeroConstant(compare->InputAt(1))
2706 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2707 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002708 locations->SetOut(Location::RequiresRegister());
2709 break;
2710 }
2711 default:
2712 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2713 }
2714}
2715
2716void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2717 Primitive::Type in_type = compare->InputAt(0)->GetType();
2718
2719 // 0 if: left == right
2720 // 1 if: left > right
2721 // -1 if: left < right
2722 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002723 case Primitive::kPrimBoolean:
2724 case Primitive::kPrimByte:
2725 case Primitive::kPrimShort:
2726 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002727 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002728 case Primitive::kPrimLong: {
2729 Register result = OutputRegister(compare);
2730 Register left = InputRegisterAt(compare, 0);
2731 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002732 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002733 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2734 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002735 break;
2736 }
2737 case Primitive::kPrimFloat:
2738 case Primitive::kPrimDouble: {
2739 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002740 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002741 __ Cset(result, ne);
2742 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002743 break;
2744 }
2745 default:
2746 LOG(FATAL) << "Unimplemented compare type " << in_type;
2747 }
2748}
2749
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002750void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002752
2753 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2754 locations->SetInAt(0, Location::RequiresFpuRegister());
2755 locations->SetInAt(1,
2756 IsFloatingPointZeroConstant(instruction->InputAt(1))
2757 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2758 : Location::RequiresFpuRegister());
2759 } else {
2760 // Integer cases.
2761 locations->SetInAt(0, Location::RequiresRegister());
2762 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2763 }
2764
David Brazdilb3e773e2016-01-26 11:28:37 +00002765 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002766 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002767 }
2768}
2769
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002770void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002771 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002772 return;
2773 }
2774
2775 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002776 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002777 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002778
Roland Levillain7f63c522015-07-13 15:54:55 +00002779 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002780 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002781 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002782 } else {
2783 // Integer cases.
2784 Register lhs = InputRegisterAt(instruction, 0);
2785 Operand rhs = InputOperandAt(instruction, 1);
2786 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002787 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002788 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002789}
2790
2791#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2792 M(Equal) \
2793 M(NotEqual) \
2794 M(LessThan) \
2795 M(LessThanOrEqual) \
2796 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002797 M(GreaterThanOrEqual) \
2798 M(Below) \
2799 M(BelowOrEqual) \
2800 M(Above) \
2801 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002802#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002803void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2804void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002805FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002806#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002807#undef FOR_EACH_CONDITION_INSTRUCTION
2808
Zheng Xuc6667102015-05-15 16:08:45 +08002809void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2810 DCHECK(instruction->IsDiv() || instruction->IsRem());
2811
2812 LocationSummary* locations = instruction->GetLocations();
2813 Location second = locations->InAt(1);
2814 DCHECK(second.IsConstant());
2815
2816 Register out = OutputRegister(instruction);
2817 Register dividend = InputRegisterAt(instruction, 0);
2818 int64_t imm = Int64FromConstant(second.GetConstant());
2819 DCHECK(imm == 1 || imm == -1);
2820
2821 if (instruction->IsRem()) {
2822 __ Mov(out, 0);
2823 } else {
2824 if (imm == 1) {
2825 __ Mov(out, dividend);
2826 } else {
2827 __ Neg(out, dividend);
2828 }
2829 }
2830}
2831
2832void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2833 DCHECK(instruction->IsDiv() || instruction->IsRem());
2834
2835 LocationSummary* locations = instruction->GetLocations();
2836 Location second = locations->InAt(1);
2837 DCHECK(second.IsConstant());
2838
2839 Register out = OutputRegister(instruction);
2840 Register dividend = InputRegisterAt(instruction, 0);
2841 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002842 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002843 int ctz_imm = CTZ(abs_imm);
2844
2845 UseScratchRegisterScope temps(GetVIXLAssembler());
2846 Register temp = temps.AcquireSameSizeAs(out);
2847
2848 if (instruction->IsDiv()) {
2849 __ Add(temp, dividend, abs_imm - 1);
2850 __ Cmp(dividend, 0);
2851 __ Csel(out, temp, dividend, lt);
2852 if (imm > 0) {
2853 __ Asr(out, out, ctz_imm);
2854 } else {
2855 __ Neg(out, Operand(out, ASR, ctz_imm));
2856 }
2857 } else {
2858 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2859 __ Asr(temp, dividend, bits - 1);
2860 __ Lsr(temp, temp, bits - ctz_imm);
2861 __ Add(out, dividend, temp);
2862 __ And(out, out, abs_imm - 1);
2863 __ Sub(out, out, temp);
2864 }
2865}
2866
2867void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2868 DCHECK(instruction->IsDiv() || instruction->IsRem());
2869
2870 LocationSummary* locations = instruction->GetLocations();
2871 Location second = locations->InAt(1);
2872 DCHECK(second.IsConstant());
2873
2874 Register out = OutputRegister(instruction);
2875 Register dividend = InputRegisterAt(instruction, 0);
2876 int64_t imm = Int64FromConstant(second.GetConstant());
2877
2878 Primitive::Type type = instruction->GetResultType();
2879 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2880
2881 int64_t magic;
2882 int shift;
2883 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2884
2885 UseScratchRegisterScope temps(GetVIXLAssembler());
2886 Register temp = temps.AcquireSameSizeAs(out);
2887
2888 // temp = get_high(dividend * magic)
2889 __ Mov(temp, magic);
2890 if (type == Primitive::kPrimLong) {
2891 __ Smulh(temp, dividend, temp);
2892 } else {
2893 __ Smull(temp.X(), dividend, temp);
2894 __ Lsr(temp.X(), temp.X(), 32);
2895 }
2896
2897 if (imm > 0 && magic < 0) {
2898 __ Add(temp, temp, dividend);
2899 } else if (imm < 0 && magic > 0) {
2900 __ Sub(temp, temp, dividend);
2901 }
2902
2903 if (shift != 0) {
2904 __ Asr(temp, temp, shift);
2905 }
2906
2907 if (instruction->IsDiv()) {
2908 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2909 } else {
2910 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2911 // TODO: Strength reduction for msub.
2912 Register temp_imm = temps.AcquireSameSizeAs(out);
2913 __ Mov(temp_imm, imm);
2914 __ Msub(out, temp, temp_imm, dividend);
2915 }
2916}
2917
2918void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2919 DCHECK(instruction->IsDiv() || instruction->IsRem());
2920 Primitive::Type type = instruction->GetResultType();
2921 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2922
2923 LocationSummary* locations = instruction->GetLocations();
2924 Register out = OutputRegister(instruction);
2925 Location second = locations->InAt(1);
2926
2927 if (second.IsConstant()) {
2928 int64_t imm = Int64FromConstant(second.GetConstant());
2929
2930 if (imm == 0) {
2931 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2932 } else if (imm == 1 || imm == -1) {
2933 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002934 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002935 DivRemByPowerOfTwo(instruction);
2936 } else {
2937 DCHECK(imm <= -2 || imm >= 2);
2938 GenerateDivRemWithAnyConstant(instruction);
2939 }
2940 } else {
2941 Register dividend = InputRegisterAt(instruction, 0);
2942 Register divisor = InputRegisterAt(instruction, 1);
2943 if (instruction->IsDiv()) {
2944 __ Sdiv(out, dividend, divisor);
2945 } else {
2946 UseScratchRegisterScope temps(GetVIXLAssembler());
2947 Register temp = temps.AcquireSameSizeAs(out);
2948 __ Sdiv(temp, dividend, divisor);
2949 __ Msub(out, temp, divisor, dividend);
2950 }
2951 }
2952}
2953
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002954void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2955 LocationSummary* locations =
2956 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2957 switch (div->GetResultType()) {
2958 case Primitive::kPrimInt:
2959 case Primitive::kPrimLong:
2960 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002961 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2963 break;
2964
2965 case Primitive::kPrimFloat:
2966 case Primitive::kPrimDouble:
2967 locations->SetInAt(0, Location::RequiresFpuRegister());
2968 locations->SetInAt(1, Location::RequiresFpuRegister());
2969 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2970 break;
2971
2972 default:
2973 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2974 }
2975}
2976
2977void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2978 Primitive::Type type = div->GetResultType();
2979 switch (type) {
2980 case Primitive::kPrimInt:
2981 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002982 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002983 break;
2984
2985 case Primitive::kPrimFloat:
2986 case Primitive::kPrimDouble:
2987 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2988 break;
2989
2990 default:
2991 LOG(FATAL) << "Unexpected div type " << type;
2992 }
2993}
2994
Alexandre Rames67555f72014-11-18 10:55:16 +00002995void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002996 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002997 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002998}
2999
3000void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3001 SlowPathCodeARM64* slow_path =
3002 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3003 codegen_->AddSlowPath(slow_path);
3004 Location value = instruction->GetLocations()->InAt(0);
3005
Alexandre Rames3e69f162014-12-10 10:36:50 +00003006 Primitive::Type type = instruction->GetType();
3007
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003008 if (!Primitive::IsIntegralType(type)) {
3009 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003010 return;
3011 }
3012
Alexandre Rames67555f72014-11-18 10:55:16 +00003013 if (value.IsConstant()) {
3014 int64_t divisor = Int64ConstantFrom(value);
3015 if (divisor == 0) {
3016 __ B(slow_path->GetEntryLabel());
3017 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003018 // A division by a non-null constant is valid. We don't need to perform
3019 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003020 }
3021 } else {
3022 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3023 }
3024}
3025
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003026void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3027 LocationSummary* locations =
3028 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3029 locations->SetOut(Location::ConstantLocation(constant));
3030}
3031
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003032void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3033 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003034 // Will be generated at use site.
3035}
3036
Alexandre Rames5319def2014-10-23 10:03:10 +01003037void LocationsBuilderARM64::VisitExit(HExit* exit) {
3038 exit->SetLocations(nullptr);
3039}
3040
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003041void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003042}
3043
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003044void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3045 LocationSummary* locations =
3046 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3047 locations->SetOut(Location::ConstantLocation(constant));
3048}
3049
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003050void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003051 // Will be generated at use site.
3052}
3053
David Brazdilfc6a86a2015-06-26 10:33:45 +00003054void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003055 DCHECK(!successor->IsExitBlock());
3056 HBasicBlock* block = got->GetBlock();
3057 HInstruction* previous = got->GetPrevious();
3058 HLoopInformation* info = block->GetLoopInformation();
3059
David Brazdil46e2a392015-03-16 17:31:52 +00003060 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003061 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3062 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3063 return;
3064 }
3065 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3066 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3067 }
3068 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003069 __ B(codegen_->GetLabelOf(successor));
3070 }
3071}
3072
David Brazdilfc6a86a2015-06-26 10:33:45 +00003073void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3074 got->SetLocations(nullptr);
3075}
3076
3077void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3078 HandleGoto(got, got->GetSuccessor());
3079}
3080
3081void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3082 try_boundary->SetLocations(nullptr);
3083}
3084
3085void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3086 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3087 if (!successor->IsExitBlock()) {
3088 HandleGoto(try_boundary, successor);
3089 }
3090}
3091
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003092void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003093 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003094 vixl::aarch64::Label* true_target,
3095 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003096 // FP branching requires both targets to be explicit. If either of the targets
3097 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003098 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003099 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003100
David Brazdil0debae72015-11-12 18:37:00 +00003101 if (true_target == nullptr && false_target == nullptr) {
3102 // Nothing to do. The code always falls through.
3103 return;
3104 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003105 // Constant condition, statically compared against "true" (integer value 1).
3106 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003107 if (true_target != nullptr) {
3108 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003109 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003110 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003111 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003112 if (false_target != nullptr) {
3113 __ B(false_target);
3114 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003115 }
David Brazdil0debae72015-11-12 18:37:00 +00003116 return;
3117 }
3118
3119 // The following code generates these patterns:
3120 // (1) true_target == nullptr && false_target != nullptr
3121 // - opposite condition true => branch to false_target
3122 // (2) true_target != nullptr && false_target == nullptr
3123 // - condition true => branch to true_target
3124 // (3) true_target != nullptr && false_target != nullptr
3125 // - condition true => branch to true_target
3126 // - branch to false_target
3127 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003128 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003129 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003130 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003131 if (true_target == nullptr) {
3132 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3133 } else {
3134 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3135 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003136 } else {
3137 // The condition instruction has not been materialized, use its inputs as
3138 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003139 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003140
David Brazdil0debae72015-11-12 18:37:00 +00003141 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003142 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003143 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003144 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003145 IfCondition opposite_condition = condition->GetOppositeCondition();
3146 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003147 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003148 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003149 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003150 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003151 // Integer cases.
3152 Register lhs = InputRegisterAt(condition, 0);
3153 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003154
3155 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003156 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003157 if (true_target == nullptr) {
3158 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3159 non_fallthrough_target = false_target;
3160 } else {
3161 arm64_cond = ARM64Condition(condition->GetCondition());
3162 non_fallthrough_target = true_target;
3163 }
3164
Aart Bik086d27e2016-01-20 17:02:00 -08003165 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003166 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003167 switch (arm64_cond) {
3168 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003169 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003170 break;
3171 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003172 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003173 break;
3174 case lt:
3175 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003176 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003177 break;
3178 case ge:
3179 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003180 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003181 break;
3182 default:
3183 // Without the `static_cast` the compiler throws an error for
3184 // `-Werror=sign-promo`.
3185 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3186 }
3187 } else {
3188 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003189 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003190 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003191 }
3192 }
David Brazdil0debae72015-11-12 18:37:00 +00003193
3194 // If neither branch falls through (case 3), the conditional branch to `true_target`
3195 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3196 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003197 __ B(false_target);
3198 }
David Brazdil0debae72015-11-12 18:37:00 +00003199
3200 if (fallthrough_target.IsLinked()) {
3201 __ Bind(&fallthrough_target);
3202 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003203}
3204
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003205void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003207 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003208 locations->SetInAt(0, Location::RequiresRegister());
3209 }
3210}
3211
3212void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003213 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3214 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003215 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3216 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3217 true_target = nullptr;
3218 }
3219 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3220 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3221 false_target = nullptr;
3222 }
David Brazdil0debae72015-11-12 18:37:00 +00003223 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003224}
3225
3226void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3227 LocationSummary* locations = new (GetGraph()->GetArena())
3228 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003229 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003230 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003231 locations->SetInAt(0, Location::RequiresRegister());
3232 }
3233}
3234
3235void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003236 SlowPathCodeARM64* slow_path =
3237 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003238 GenerateTestAndBranch(deoptimize,
3239 /* condition_input_index */ 0,
3240 slow_path->GetEntryLabel(),
3241 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003242}
3243
David Brazdilc0b601b2016-02-08 14:20:45 +00003244static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3245 return condition->IsCondition() &&
3246 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3247}
3248
Alexandre Rames880f1192016-06-13 16:04:50 +01003249static inline Condition GetConditionForSelect(HCondition* condition) {
3250 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003251 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3252 : ARM64Condition(cond);
3253}
3254
David Brazdil74eb1b22015-12-14 11:44:01 +00003255void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003257 if (Primitive::IsFloatingPointType(select->GetType())) {
3258 locations->SetInAt(0, Location::RequiresFpuRegister());
3259 locations->SetInAt(1, Location::RequiresFpuRegister());
3260 locations->SetOut(Location::RequiresFpuRegister());
3261 } else {
3262 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3263 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3264 bool is_true_value_constant = cst_true_value != nullptr;
3265 bool is_false_value_constant = cst_false_value != nullptr;
3266 // Ask VIXL whether we should synthesize constants in registers.
3267 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3268 Operand true_op = is_true_value_constant ?
3269 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3270 Operand false_op = is_false_value_constant ?
3271 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3272 bool true_value_in_register = false;
3273 bool false_value_in_register = false;
3274 MacroAssembler::GetCselSynthesisInformation(
3275 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3276 true_value_in_register |= !is_true_value_constant;
3277 false_value_in_register |= !is_false_value_constant;
3278
3279 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3280 : Location::ConstantLocation(cst_true_value));
3281 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3282 : Location::ConstantLocation(cst_false_value));
3283 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003284 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003285
David Brazdil74eb1b22015-12-14 11:44:01 +00003286 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3287 locations->SetInAt(2, Location::RequiresRegister());
3288 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003289}
3290
3291void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003292 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003293 Condition csel_cond;
3294
3295 if (IsBooleanValueOrMaterializedCondition(cond)) {
3296 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003297 // Use the condition flags set by the previous instruction.
3298 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003299 } else {
3300 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003301 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003302 }
3303 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003304 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003305 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003306 } else {
3307 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003308 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003309 }
3310
Alexandre Rames880f1192016-06-13 16:04:50 +01003311 if (Primitive::IsFloatingPointType(select->GetType())) {
3312 __ Fcsel(OutputFPRegister(select),
3313 InputFPRegisterAt(select, 1),
3314 InputFPRegisterAt(select, 0),
3315 csel_cond);
3316 } else {
3317 __ Csel(OutputRegister(select),
3318 InputOperandAt(select, 1),
3319 InputOperandAt(select, 0),
3320 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003321 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003322}
3323
David Srbecky0cf44932015-12-09 14:09:59 +00003324void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3325 new (GetGraph()->GetArena()) LocationSummary(info);
3326}
3327
David Srbeckyd28f4a02016-03-14 17:14:24 +00003328void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3329 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003330}
3331
3332void CodeGeneratorARM64::GenerateNop() {
3333 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003334}
3335
Alexandre Rames5319def2014-10-23 10:03:10 +01003336void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003337 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003338}
3339
3340void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003341 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003342}
3343
3344void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003345 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003346}
3347
3348void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003349 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003350}
3351
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003352// Temp is used for read barrier.
3353static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3354 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003355 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003356 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3357 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3358 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3359 return 1;
3360 }
3361 return 0;
3362}
3363
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003364// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003365// interface pointer, one for loading the current interface.
3366// The other checks have one temp for loading the object's class.
3367static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3368 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3369 return 3;
3370 }
3371 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003372}
3373
Alexandre Rames67555f72014-11-18 10:55:16 +00003374void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003375 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003377 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003379 case TypeCheckKind::kExactCheck:
3380 case TypeCheckKind::kAbstractClassCheck:
3381 case TypeCheckKind::kClassHierarchyCheck:
3382 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003383 call_kind =
3384 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003385 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003386 break;
3387 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003388 case TypeCheckKind::kUnresolvedCheck:
3389 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003390 call_kind = LocationSummary::kCallOnSlowPath;
3391 break;
3392 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003393
Alexandre Rames67555f72014-11-18 10:55:16 +00003394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003395 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003396 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003397 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetInAt(1, Location::RequiresRegister());
3400 // The "out" register is used as a temporary, so it overlaps with the inputs.
3401 // Note that TypeCheckSlowPathARM64 uses this register too.
3402 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003403 // Add temps if necessary for read barriers.
3404 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003405}
3406
3407void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003408 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003409 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003411 Register obj = InputRegisterAt(instruction, 0);
3412 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003414 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003415 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3416 DCHECK_LE(num_temps, 1u);
3417 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3419 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3420 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3421 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003422
Scott Wakeling97c72b72016-06-24 16:19:36 +01003423 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003424 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003425
3426 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003427 // Avoid null check if we know `obj` is not null.
3428 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003429 __ Cbz(obj, &zero);
3430 }
3431
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003432 // /* HeapReference<Class> */ out = obj->klass_
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003433 GenerateReferenceLoadTwoRegisters(instruction,
3434 out_loc,
3435 obj_loc,
3436 class_offset,
3437 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003438 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003439
Roland Levillain44015862016-01-22 11:47:17 +00003440 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003441 case TypeCheckKind::kExactCheck: {
3442 __ Cmp(out, cls);
3443 __ Cset(out, eq);
3444 if (zero.IsLinked()) {
3445 __ B(&done);
3446 }
3447 break;
3448 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450 case TypeCheckKind::kAbstractClassCheck: {
3451 // If the class is abstract, we eagerly fetch the super class of the
3452 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003453 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003454 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003455 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003456 GenerateReferenceLoadOneRegister(instruction,
3457 out_loc,
3458 super_offset,
3459 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003460 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003461 // If `out` is null, we use it for the result, and jump to `done`.
3462 __ Cbz(out, &done);
3463 __ Cmp(out, cls);
3464 __ B(ne, &loop);
3465 __ Mov(out, 1);
3466 if (zero.IsLinked()) {
3467 __ B(&done);
3468 }
3469 break;
3470 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003471
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003472 case TypeCheckKind::kClassHierarchyCheck: {
3473 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003474 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003475 __ Bind(&loop);
3476 __ Cmp(out, cls);
3477 __ B(eq, &success);
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 __ Cbnz(out, &loop);
3485 // If `out` is null, we use it for the result, and jump to `done`.
3486 __ B(&done);
3487 __ Bind(&success);
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::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003496 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003497 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003498 __ Cmp(out, cls);
3499 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003500 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003501 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003502 GenerateReferenceLoadOneRegister(instruction,
3503 out_loc,
3504 component_offset,
3505 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003506 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003507 // If `out` is null, we use it for the result, and jump to `done`.
3508 __ Cbz(out, &done);
3509 __ Ldrh(out, HeapOperand(out, primitive_offset));
3510 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3511 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003512 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003513 __ Mov(out, 1);
3514 __ B(&done);
3515 break;
3516 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003517
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003518 case TypeCheckKind::kArrayCheck: {
3519 __ Cmp(out, cls);
3520 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003521 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3522 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003523 codegen_->AddSlowPath(slow_path);
3524 __ B(ne, slow_path->GetEntryLabel());
3525 __ Mov(out, 1);
3526 if (zero.IsLinked()) {
3527 __ B(&done);
3528 }
3529 break;
3530 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003531
Calin Juravle98893e12015-10-02 21:05:03 +01003532 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003533 case TypeCheckKind::kInterfaceCheck: {
3534 // Note that we indeed only call on slow path, but we always go
3535 // into the slow path for the unresolved and interface check
3536 // cases.
3537 //
3538 // We cannot directly call the InstanceofNonTrivial runtime
3539 // entry point without resorting to a type checking slow path
3540 // here (i.e. by calling InvokeRuntime directly), as it would
3541 // require to assign fixed registers for the inputs of this
3542 // HInstanceOf instruction (following the runtime calling
3543 // convention), which might be cluttered by the potential first
3544 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003545 //
3546 // TODO: Introduce a new runtime entry point taking the object
3547 // to test (instead of its class) as argument, and let it deal
3548 // with the read barrier issues. This will let us refactor this
3549 // case of the `switch` code as it was previously (with a direct
3550 // call to the runtime not using a type checking slow path).
3551 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003552 DCHECK(locations->OnlyCallsOnSlowPath());
3553 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3554 /* is_fatal */ false);
3555 codegen_->AddSlowPath(slow_path);
3556 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003557 if (zero.IsLinked()) {
3558 __ B(&done);
3559 }
3560 break;
3561 }
3562 }
3563
3564 if (zero.IsLinked()) {
3565 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003566 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003567 }
3568
3569 if (done.IsLinked()) {
3570 __ Bind(&done);
3571 }
3572
3573 if (slow_path != nullptr) {
3574 __ Bind(slow_path->GetExitLabel());
3575 }
3576}
3577
3578void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3579 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3580 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3581
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003582 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3583 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003584 case TypeCheckKind::kExactCheck:
3585 case TypeCheckKind::kAbstractClassCheck:
3586 case TypeCheckKind::kClassHierarchyCheck:
3587 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003588 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3589 LocationSummary::kCallOnSlowPath :
3590 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003591 break;
3592 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003593 case TypeCheckKind::kUnresolvedCheck:
3594 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003595 call_kind = LocationSummary::kCallOnSlowPath;
3596 break;
3597 }
3598
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3600 locations->SetInAt(0, Location::RequiresRegister());
3601 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003602 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3603 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003604}
3605
3606void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003607 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003608 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003609 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003610 Register obj = InputRegisterAt(instruction, 0);
3611 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003612 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3613 DCHECK_GE(num_temps, 1u);
3614 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003615 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003616 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3617 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003618 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003619 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3620 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3621 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3622 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3623 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3624 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3625 const uint32_t object_array_data_offset =
3626 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003627
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003628 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003629 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3630 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3631 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003632 if (!kEmitCompilerReadBarrier) {
3633 is_type_check_slow_path_fatal =
3634 (type_check_kind == TypeCheckKind::kExactCheck ||
3635 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3636 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3637 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3638 !instruction->CanThrowIntoCatchBlock();
3639 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 SlowPathCodeARM64* type_check_slow_path =
3641 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3642 is_type_check_slow_path_fatal);
3643 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003644
Scott Wakeling97c72b72016-06-24 16:19:36 +01003645 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003646 // Avoid null check if we know obj is not null.
3647 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003648 __ Cbz(obj, &done);
3649 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003650
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003651 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003652 case TypeCheckKind::kExactCheck:
3653 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003654 // /* HeapReference<Class> */ temp = obj->klass_
3655 GenerateReferenceLoadTwoRegisters(instruction,
3656 temp_loc,
3657 obj_loc,
3658 class_offset,
3659 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003660 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003661
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003662 __ Cmp(temp, cls);
3663 // Jump to slow path for throwing the exception or doing a
3664 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003665 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003666 break;
3667 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003668
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003669 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003670 // /* HeapReference<Class> */ temp = obj->klass_
3671 GenerateReferenceLoadTwoRegisters(instruction,
3672 temp_loc,
3673 obj_loc,
3674 class_offset,
3675 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003676 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003677
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003678 // If the class is abstract, we eagerly fetch the super class of the
3679 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003680 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003681 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003682 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003683 GenerateReferenceLoadOneRegister(instruction,
3684 temp_loc,
3685 super_offset,
3686 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003687 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003688
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003689 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3690 // exception.
3691 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3692 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003693 __ Cmp(temp, cls);
3694 __ B(ne, &loop);
3695 break;
3696 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003697
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003698 case TypeCheckKind::kClassHierarchyCheck: {
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 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003708 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003709 __ Bind(&loop);
3710 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003711 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003712
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003713 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003714 GenerateReferenceLoadOneRegister(instruction,
3715 temp_loc,
3716 super_offset,
3717 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003718 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003719
3720 // If the class reference currently in `temp` is not null, jump
3721 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003722 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003723 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003724 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003725 break;
3726 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003727
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003728 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003729 // /* HeapReference<Class> */ temp = obj->klass_
3730 GenerateReferenceLoadTwoRegisters(instruction,
3731 temp_loc,
3732 obj_loc,
3733 class_offset,
3734 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003735 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003736
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003737 // Do an exact check.
3738 __ Cmp(temp, cls);
3739 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003740
3741 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003742 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003743 GenerateReferenceLoadOneRegister(instruction,
3744 temp_loc,
3745 component_offset,
3746 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003747 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003748
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003749 // If the component type is null, jump to the slow path to throw the exception.
3750 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3751 // Otherwise, the object is indeed an array. Further check that this component type is not a
3752 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003753 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3754 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003755 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003756 break;
3757 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003758
Calin Juravle98893e12015-10-02 21:05:03 +01003759 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003760 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003761 //
3762 // We cannot directly call the CheckCast runtime entry point
3763 // without resorting to a type checking slow path here (i.e. by
3764 // calling InvokeRuntime directly), as it would require to
3765 // assign fixed registers for the inputs of this HInstanceOf
3766 // instruction (following the runtime calling convention), which
3767 // might be cluttered by the potential first read barrier
3768 // emission at the beginning of this method.
3769 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003770 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003771 case TypeCheckKind::kInterfaceCheck: {
3772 // /* HeapReference<Class> */ temp = obj->klass_
3773 GenerateReferenceLoadTwoRegisters(instruction,
3774 temp_loc,
3775 obj_loc,
3776 class_offset,
3777 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003778 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003779
3780 // /* HeapReference<Class> */ temp = temp->iftable_
3781 GenerateReferenceLoadTwoRegisters(instruction,
3782 temp_loc,
3783 temp_loc,
3784 iftable_offset,
3785 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003786 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003787 vixl::aarch64::Label is_null;
3788 // Null iftable means it is empty and will always fail the check.
3789 __ Cbz(temp, &is_null);
3790
3791 // Loop through the iftable and check if any class matches.
3792 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
3793
3794 vixl::aarch64::Label start_loop;
3795 __ Bind(&start_loop);
3796 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3797 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
3798 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3799 __ B(eq, &done); // Return if same class.
3800 // Go to next interface.
3801 __ Add(temp, temp, 2 * kHeapReferenceSize);
3802 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
3803 __ Cbnz(WRegisterFrom(maybe_temp2_loc), &start_loop);
3804 __ Bind(&is_null);
3805
3806 __ B(type_check_slow_path->GetEntryLabel());
3807 break;
3808 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003809 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003810 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003811
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003812 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003813}
3814
Alexandre Rames5319def2014-10-23 10:03:10 +01003815void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3816 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3817 locations->SetOut(Location::ConstantLocation(constant));
3818}
3819
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003820void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003821 // Will be generated at use site.
3822}
3823
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003824void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3826 locations->SetOut(Location::ConstantLocation(constant));
3827}
3828
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003829void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003830 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003831}
3832
Calin Juravle175dc732015-08-25 15:42:32 +01003833void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3834 // The trampoline uses the same calling convention as dex calling conventions,
3835 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3836 // the method_idx.
3837 HandleInvoke(invoke);
3838}
3839
3840void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3841 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3842}
3843
Alexandre Rames5319def2014-10-23 10:03:10 +01003844void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003845 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003846 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003847}
3848
Alexandre Rames67555f72014-11-18 10:55:16 +00003849void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3850 HandleInvoke(invoke);
3851}
3852
3853void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3854 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003855 LocationSummary* locations = invoke->GetLocations();
3856 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003857 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003858 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003859 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003860
3861 // The register ip1 is required to be used for the hidden argument in
3862 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003863 MacroAssembler* masm = GetVIXLAssembler();
3864 UseScratchRegisterScope scratch_scope(masm);
3865 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003866 scratch_scope.Exclude(ip1);
3867 __ Mov(ip1, invoke->GetDexMethodIndex());
3868
Alexandre Rames67555f72014-11-18 10:55:16 +00003869 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003870 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003871 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003872 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003873 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003874 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003875 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003876 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003877 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003878 // Instead of simply (possibly) unpoisoning `temp` here, we should
3879 // emit a read barrier for the previous class reference load.
3880 // However this is not required in practice, as this is an
3881 // intermediate/temporary reference and because the current
3882 // concurrent copying collector keeps the from-space memory
3883 // intact/accessible until the end of the marking phase (the
3884 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003885 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003886 __ Ldr(temp,
3887 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3888 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003889 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003890 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003891 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003892 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003893 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003894 // lr();
3895 __ Blr(lr);
3896 DCHECK(!codegen_->IsLeafMethod());
3897 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3898}
3899
3900void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003901 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3902 if (intrinsic.TryDispatch(invoke)) {
3903 return;
3904 }
3905
Alexandre Rames67555f72014-11-18 10:55:16 +00003906 HandleInvoke(invoke);
3907}
3908
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003909void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003910 // Explicit clinit checks triggered by static invokes must have been pruned by
3911 // art::PrepareForRegisterAllocation.
3912 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003913
Andreas Gampe878d58c2015-01-15 23:24:00 -08003914 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3915 if (intrinsic.TryDispatch(invoke)) {
3916 return;
3917 }
3918
Alexandre Rames67555f72014-11-18 10:55:16 +00003919 HandleInvoke(invoke);
3920}
3921
Andreas Gampe878d58c2015-01-15 23:24:00 -08003922static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3923 if (invoke->GetLocations()->Intrinsified()) {
3924 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3925 intrinsic.Dispatch(invoke);
3926 return true;
3927 }
3928 return false;
3929}
3930
Vladimir Markodc151b22015-10-15 18:02:30 +01003931HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3932 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003933 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003934 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003935 return desired_dispatch_info;
3936}
3937
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003938void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003939 // For better instruction scheduling we load the direct code pointer before the method pointer.
3940 bool direct_code_loaded = false;
3941 switch (invoke->GetCodePtrLocation()) {
3942 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3943 // LR = code address from literal pool with link-time patch.
3944 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3945 direct_code_loaded = true;
3946 break;
3947 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3948 // LR = invoke->GetDirectCodePtr();
3949 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3950 direct_code_loaded = true;
3951 break;
3952 default:
3953 break;
3954 }
3955
Andreas Gampe878d58c2015-01-15 23:24:00 -08003956 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003957 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3958 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003959 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3960 uint32_t offset =
3961 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003962 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003963 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003964 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003965 }
Vladimir Marko58155012015-08-19 12:49:41 +00003966 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003967 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003968 break;
3969 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3970 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003971 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003972 break;
3973 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3974 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003975 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003976 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3977 break;
3978 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3979 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003980 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003981 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003982 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003983 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003984 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003985 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003986 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003987 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003988 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003989 }
Vladimir Marko58155012015-08-19 12:49:41 +00003990 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003991 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003992 Register reg = XRegisterFrom(temp);
3993 Register method_reg;
3994 if (current_method.IsRegister()) {
3995 method_reg = XRegisterFrom(current_method);
3996 } else {
3997 DCHECK(invoke->GetLocations()->Intrinsified());
3998 DCHECK(!current_method.IsValid());
3999 method_reg = reg;
4000 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4001 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004002
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004003 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004004 __ Ldr(reg.X(),
4005 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004006 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004007 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004008 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4009 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004010 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4011 break;
4012 }
4013 }
4014
4015 switch (invoke->GetCodePtrLocation()) {
4016 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4017 __ Bl(&frame_entry_label_);
4018 break;
4019 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004020 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4021 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01004022 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
4023 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00004024 __ Bind(label);
4025 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00004026 break;
4027 }
4028 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4029 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4030 // LR prepared above for better instruction scheduling.
4031 DCHECK(direct_code_loaded);
4032 // lr()
4033 __ Blr(lr);
4034 break;
4035 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4036 // LR = callee_method->entry_point_from_quick_compiled_code_;
4037 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004038 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004039 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004040 // lr()
4041 __ Blr(lr);
4042 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004043 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004044
Andreas Gampe878d58c2015-01-15 23:24:00 -08004045 DCHECK(!IsLeafMethod());
4046}
4047
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004048void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004049 // Use the calling convention instead of the location of the receiver, as
4050 // intrinsics may have put the receiver in a different register. In the intrinsics
4051 // slow path, the arguments have been moved to the right place, so here we are
4052 // guaranteed that the receiver is the first register of the calling convention.
4053 InvokeDexCallingConvention calling_convention;
4054 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004055 Register temp = XRegisterFrom(temp_in);
4056 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4057 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4058 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004059 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004060
4061 BlockPoolsScope block_pools(GetVIXLAssembler());
4062
4063 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004064 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004065 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004066 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004067 // Instead of simply (possibly) unpoisoning `temp` here, we should
4068 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004069 // intermediate/temporary reference and because the current
4070 // concurrent copying collector keeps the from-space memory
4071 // intact/accessible until the end of the marking phase (the
4072 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004073 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4074 // temp = temp->GetMethodAt(method_offset);
4075 __ Ldr(temp, MemOperand(temp, method_offset));
4076 // lr = temp->GetEntryPoint();
4077 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4078 // lr();
4079 __ Blr(lr);
4080}
4081
Scott Wakeling97c72b72016-06-24 16:19:36 +01004082vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4083 const DexFile& dex_file,
4084 uint32_t string_index,
4085 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004086 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4087}
4088
Scott Wakeling97c72b72016-06-24 16:19:36 +01004089vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4090 const DexFile& dex_file,
4091 uint32_t type_index,
4092 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004093 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
4094}
4095
Scott Wakeling97c72b72016-06-24 16:19:36 +01004096vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4097 const DexFile& dex_file,
4098 uint32_t element_offset,
4099 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004100 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4101}
4102
Scott Wakeling97c72b72016-06-24 16:19:36 +01004103vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4104 const DexFile& dex_file,
4105 uint32_t offset_or_index,
4106 vixl::aarch64::Label* adrp_label,
4107 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004108 // Add a patch entry and return the label.
4109 patches->emplace_back(dex_file, offset_or_index);
4110 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004111 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004112 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4113 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4114 return label;
4115}
4116
Scott Wakeling97c72b72016-06-24 16:19:36 +01004117vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004118 const DexFile& dex_file, uint32_t string_index) {
4119 return boot_image_string_patches_.GetOrCreate(
4120 StringReference(&dex_file, string_index),
4121 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4122}
4123
Scott Wakeling97c72b72016-06-24 16:19:36 +01004124vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004125 const DexFile& dex_file, uint32_t type_index) {
4126 return boot_image_type_patches_.GetOrCreate(
4127 TypeReference(&dex_file, type_index),
4128 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4129}
4130
Scott Wakeling97c72b72016-06-24 16:19:36 +01004131vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4132 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004133 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4134 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4135 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4136}
4137
Scott Wakeling97c72b72016-06-24 16:19:36 +01004138vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4139 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004140 return DeduplicateUint64Literal(address);
4141}
4142
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00004143vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
4144 const DexFile& dex_file, uint32_t string_index) {
4145 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index), /* placeholder */ 0u);
4146 return jit_string_patches_.GetOrCreate(
4147 StringReference(&dex_file, string_index),
4148 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4149}
4150
Vladimir Markoaad75c62016-10-03 08:46:48 +00004151void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4152 vixl::aarch64::Register reg) {
4153 DCHECK(reg.IsX());
4154 SingleEmissionCheckScope guard(GetVIXLAssembler());
4155 __ Bind(fixup_label);
4156 __ adrp(reg, /* offset placeholder */ 0);
4157}
4158
4159void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4160 vixl::aarch64::Register out,
4161 vixl::aarch64::Register base) {
4162 DCHECK(out.IsX());
4163 DCHECK(base.IsX());
4164 SingleEmissionCheckScope guard(GetVIXLAssembler());
4165 __ Bind(fixup_label);
4166 __ add(out, base, Operand(/* offset placeholder */ 0));
4167}
4168
4169void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4170 vixl::aarch64::Register out,
4171 vixl::aarch64::Register base) {
4172 DCHECK(base.IsX());
4173 SingleEmissionCheckScope guard(GetVIXLAssembler());
4174 __ Bind(fixup_label);
4175 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4176}
4177
4178template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4179inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4180 const ArenaDeque<PcRelativePatchInfo>& infos,
4181 ArenaVector<LinkerPatch>* linker_patches) {
4182 for (const PcRelativePatchInfo& info : infos) {
4183 linker_patches->push_back(Factory(info.label.GetLocation(),
4184 &info.target_dex_file,
4185 info.pc_insn_label->GetLocation(),
4186 info.offset_or_index));
4187 }
4188}
4189
Vladimir Marko58155012015-08-19 12:49:41 +00004190void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4191 DCHECK(linker_patches->empty());
4192 size_t size =
4193 method_patches_.size() +
4194 call_patches_.size() +
4195 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004196 pc_relative_dex_cache_patches_.size() +
4197 boot_image_string_patches_.size() +
4198 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004199 boot_image_type_patches_.size() +
4200 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004201 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004202 linker_patches->reserve(size);
4203 for (const auto& entry : method_patches_) {
4204 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004205 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4206 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004207 target_method.dex_file,
4208 target_method.dex_method_index));
4209 }
4210 for (const auto& entry : call_patches_) {
4211 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004212 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4213 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004214 target_method.dex_file,
4215 target_method.dex_method_index));
4216 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004217 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4218 linker_patches->push_back(
4219 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004220 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004221 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004222 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004223 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004224 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004225 info.offset_or_index));
4226 }
4227 for (const auto& entry : boot_image_string_patches_) {
4228 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004229 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4230 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004231 target_string.dex_file,
4232 target_string.string_index));
4233 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004234 if (!GetCompilerOptions().IsBootImage()) {
4235 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4236 linker_patches);
4237 } else {
4238 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4239 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004240 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004241 for (const auto& entry : boot_image_type_patches_) {
4242 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004243 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4244 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004245 target_type.dex_file,
4246 target_type.type_index));
4247 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004248 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4249 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004250 for (const auto& entry : boot_image_address_patches_) {
4251 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004252 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4253 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004254 }
4255}
4256
Scott Wakeling97c72b72016-06-24 16:19:36 +01004257vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004258 Uint32ToLiteralMap* map) {
4259 return map->GetOrCreate(
4260 value,
4261 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4262}
4263
Scott Wakeling97c72b72016-06-24 16:19:36 +01004264vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004265 return uint64_literals_.GetOrCreate(
4266 value,
4267 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004268}
4269
Scott Wakeling97c72b72016-06-24 16:19:36 +01004270vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004271 MethodReference target_method,
4272 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004273 return map->GetOrCreate(
4274 target_method,
4275 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004276}
4277
Scott Wakeling97c72b72016-06-24 16:19:36 +01004278vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004279 MethodReference target_method) {
4280 return DeduplicateMethodLiteral(target_method, &method_patches_);
4281}
4282
Scott Wakeling97c72b72016-06-24 16:19:36 +01004283vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004284 MethodReference target_method) {
4285 return DeduplicateMethodLiteral(target_method, &call_patches_);
4286}
4287
4288
Andreas Gampe878d58c2015-01-15 23:24:00 -08004289void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004290 // Explicit clinit checks triggered by static invokes must have been pruned by
4291 // art::PrepareForRegisterAllocation.
4292 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004293
Andreas Gampe878d58c2015-01-15 23:24:00 -08004294 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4295 return;
4296 }
4297
Alexandre Ramesd921d642015-04-16 15:07:16 +01004298 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004299 LocationSummary* locations = invoke->GetLocations();
4300 codegen_->GenerateStaticOrDirectCall(
4301 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004302 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004303}
4304
4305void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004306 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4307 return;
4308 }
4309
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004310 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004311 DCHECK(!codegen_->IsLeafMethod());
4312 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4313}
4314
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004315HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4316 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004317 switch (desired_class_load_kind) {
4318 case HLoadClass::LoadKind::kReferrersClass:
4319 break;
4320 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4321 DCHECK(!GetCompilerOptions().GetCompilePic());
4322 break;
4323 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4324 DCHECK(GetCompilerOptions().GetCompilePic());
4325 break;
4326 case HLoadClass::LoadKind::kBootImageAddress:
4327 break;
4328 case HLoadClass::LoadKind::kDexCacheAddress:
4329 DCHECK(Runtime::Current()->UseJitCompilation());
4330 break;
4331 case HLoadClass::LoadKind::kDexCachePcRelative:
4332 DCHECK(!Runtime::Current()->UseJitCompilation());
4333 break;
4334 case HLoadClass::LoadKind::kDexCacheViaMethod:
4335 break;
4336 }
4337 return desired_class_load_kind;
4338}
4339
Alexandre Rames67555f72014-11-18 10:55:16 +00004340void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004341 if (cls->NeedsAccessCheck()) {
4342 InvokeRuntimeCallingConvention calling_convention;
4343 CodeGenerator::CreateLoadClassLocationSummary(
4344 cls,
4345 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004346 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004347 /* code_generator_supports_read_barrier */ true);
4348 return;
4349 }
4350
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004351 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4352 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004353 ? LocationSummary::kCallOnSlowPath
4354 : LocationSummary::kNoCall;
4355 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004356 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004357 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004358 }
4359
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004360 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4361 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4362 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4363 locations->SetInAt(0, Location::RequiresRegister());
4364 }
4365 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004366}
4367
4368void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004369 if (cls->NeedsAccessCheck()) {
4370 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004371 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004372 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004373 return;
4374 }
4375
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004376 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004377 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004378
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004379 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4380 ? kWithoutReadBarrier
4381 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004382 bool generate_null_check = false;
4383 switch (cls->GetLoadKind()) {
4384 case HLoadClass::LoadKind::kReferrersClass: {
4385 DCHECK(!cls->CanCallRuntime());
4386 DCHECK(!cls->MustGenerateClinitCheck());
4387 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4388 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004389 GenerateGcRootFieldLoad(cls,
4390 out_loc,
4391 current_method,
4392 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004393 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004394 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004395 break;
4396 }
4397 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004398 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004399 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4400 cls->GetTypeIndex()));
4401 break;
4402 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004403 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004404 // Add ADRP with its PC-relative type patch.
4405 const DexFile& dex_file = cls->GetDexFile();
4406 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004407 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004408 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004409 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004410 vixl::aarch64::Label* add_label =
4411 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004412 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004413 break;
4414 }
4415 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004416 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004417 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4418 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4419 break;
4420 }
4421 case HLoadClass::LoadKind::kDexCacheAddress: {
4422 DCHECK_NE(cls->GetAddress(), 0u);
4423 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4424 // that gives a 16KiB range. To try and reduce the number of literals if we load
4425 // multiple types, simply split the dex cache address to a 16KiB aligned base
4426 // loaded from a literal and the remaining offset embedded in the load.
4427 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4428 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4429 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4430 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4431 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4432 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4433 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004434 GenerateGcRootFieldLoad(cls,
4435 out_loc,
4436 out.X(),
4437 offset,
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 case HLoadClass::LoadKind::kDexCachePcRelative: {
4444 // Add ADRP with its PC-relative DexCache access patch.
4445 const DexFile& dex_file = cls->GetDexFile();
4446 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004447 vixl::aarch64::Label* adrp_label =
4448 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004449 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004450 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004451 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004452 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4453 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004454 GenerateGcRootFieldLoad(cls,
4455 out_loc,
4456 out.X(),
4457 /* offset placeholder */ 0,
4458 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004459 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004460 generate_null_check = !cls->IsInDexCache();
4461 break;
4462 }
4463 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4464 MemberOffset resolved_types_offset =
4465 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4466 // /* GcRoot<mirror::Class>[] */ out =
4467 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4468 Register current_method = InputRegisterAt(cls, 0);
4469 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4470 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004471 GenerateGcRootFieldLoad(cls,
4472 out_loc,
4473 out.X(),
4474 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
Roland Levillain00468f32016-10-27 18:02:48 +01004475 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004476 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004477 generate_null_check = !cls->IsInDexCache();
4478 break;
4479 }
4480 }
4481
4482 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4483 DCHECK(cls->CanCallRuntime());
4484 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4485 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4486 codegen_->AddSlowPath(slow_path);
4487 if (generate_null_check) {
4488 __ Cbz(out, slow_path->GetEntryLabel());
4489 }
4490 if (cls->MustGenerateClinitCheck()) {
4491 GenerateClassInitializationCheck(slow_path, out);
4492 } else {
4493 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004494 }
4495 }
4496}
4497
David Brazdilcb1c0552015-08-04 16:22:25 +01004498static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004499 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004500}
4501
Alexandre Rames67555f72014-11-18 10:55:16 +00004502void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4503 LocationSummary* locations =
4504 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4505 locations->SetOut(Location::RequiresRegister());
4506}
4507
4508void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004509 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4510}
4511
4512void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4513 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4514}
4515
4516void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4517 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004518}
4519
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004520HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4521 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004522 switch (desired_string_load_kind) {
4523 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4524 DCHECK(!GetCompilerOptions().GetCompilePic());
4525 break;
4526 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4527 DCHECK(GetCompilerOptions().GetCompilePic());
4528 break;
4529 case HLoadString::LoadKind::kBootImageAddress:
4530 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004531 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004532 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004533 break;
4534 case HLoadString::LoadKind::kDexCacheViaMethod:
4535 break;
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00004536 case HLoadString::LoadKind::kJitTableAddress:
4537 DCHECK(Runtime::Current()->UseJitCompilation());
4538 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004539 }
4540 return desired_string_load_kind;
4541}
4542
Alexandre Rames67555f72014-11-18 10:55:16 +00004543void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00004544 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004546 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004547 InvokeRuntimeCallingConvention calling_convention;
4548 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4549 } else {
4550 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004551 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4552 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4553 // Rely on the pResolveString and/or marking to save everything, including temps.
4554 RegisterSet caller_saves = RegisterSet::Empty();
4555 InvokeRuntimeCallingConvention calling_convention;
4556 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4557 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4558 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4559 Primitive::kPrimNot).GetCode());
4560 locations->SetCustomSlowPathCallerSaves(caller_saves);
4561 } else {
4562 // For non-Baker read barrier we have a temp-clobbering call.
4563 }
4564 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004565 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004566}
4567
4568void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004569 Register out = OutputRegister(load);
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00004570 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004571
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004572 switch (load->GetLoadKind()) {
4573 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004574 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4575 load->GetStringIndex()));
4576 return; // No dex cache slow path.
4577 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004578 // Add ADRP with its PC-relative String patch.
4579 const DexFile& dex_file = load->GetDexFile();
4580 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004581 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004582 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004583 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004584 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004585 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004586 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004587 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004588 return; // No dex cache slow path.
4589 }
4590 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004591 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4592 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4593 return; // No dex cache slow path.
4594 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004595 case HLoadString::LoadKind::kBssEntry: {
4596 // Add ADRP with its PC-relative String .bss entry patch.
4597 const DexFile& dex_file = load->GetDexFile();
4598 uint32_t string_index = load->GetStringIndex();
4599 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004600 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4601 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004602 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004603 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004604 // Add LDR with its PC-relative String patch.
4605 vixl::aarch64::Label* ldr_label =
4606 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00004607 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004608 GenerateGcRootFieldLoad(load,
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00004609 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004610 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004611 /* offset placeholder */ 0u,
4612 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004613 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004614 SlowPathCodeARM64* slow_path =
4615 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004616 codegen_->AddSlowPath(slow_path);
4617 __ Cbz(out.X(), slow_path->GetEntryLabel());
4618 __ Bind(slow_path->GetExitLabel());
4619 return;
4620 }
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00004621 case HLoadString::LoadKind::kJitTableAddress: {
4622 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
4623 load->GetStringIndex()));
4624 GenerateGcRootFieldLoad(load,
4625 out_loc,
4626 out.X(),
4627 /* offset */ 0,
4628 /* fixup_label */ nullptr,
4629 kCompilerReadBarrierOption);
4630 return;
4631 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004632 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004633 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004634 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004635
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004636 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004637 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004638 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004639 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4640 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4641 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004642}
4643
Alexandre Rames5319def2014-10-23 10:03:10 +01004644void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4646 locations->SetOut(Location::ConstantLocation(constant));
4647}
4648
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004649void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004650 // Will be generated at use site.
4651}
4652
Alexandre Rames67555f72014-11-18 10:55:16 +00004653void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4654 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004655 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004656 InvokeRuntimeCallingConvention calling_convention;
4657 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4658}
4659
4660void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004661 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4662 instruction,
4663 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004664 if (instruction->IsEnter()) {
4665 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4666 } else {
4667 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4668 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004669}
4670
Alexandre Rames42d641b2014-10-27 14:00:51 +00004671void LocationsBuilderARM64::VisitMul(HMul* mul) {
4672 LocationSummary* locations =
4673 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4674 switch (mul->GetResultType()) {
4675 case Primitive::kPrimInt:
4676 case Primitive::kPrimLong:
4677 locations->SetInAt(0, Location::RequiresRegister());
4678 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004680 break;
4681
4682 case Primitive::kPrimFloat:
4683 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004684 locations->SetInAt(0, Location::RequiresFpuRegister());
4685 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004686 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004687 break;
4688
4689 default:
4690 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4691 }
4692}
4693
4694void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4695 switch (mul->GetResultType()) {
4696 case Primitive::kPrimInt:
4697 case Primitive::kPrimLong:
4698 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4699 break;
4700
4701 case Primitive::kPrimFloat:
4702 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004703 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004704 break;
4705
4706 default:
4707 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4708 }
4709}
4710
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004711void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4712 LocationSummary* locations =
4713 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4714 switch (neg->GetResultType()) {
4715 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004716 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004717 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004718 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004719 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004720
4721 case Primitive::kPrimFloat:
4722 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004723 locations->SetInAt(0, Location::RequiresFpuRegister());
4724 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004725 break;
4726
4727 default:
4728 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4729 }
4730}
4731
4732void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4733 switch (neg->GetResultType()) {
4734 case Primitive::kPrimInt:
4735 case Primitive::kPrimLong:
4736 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4737 break;
4738
4739 case Primitive::kPrimFloat:
4740 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004741 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004742 break;
4743
4744 default:
4745 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4746 }
4747}
4748
4749void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4750 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004752 InvokeRuntimeCallingConvention calling_convention;
4753 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004754 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004755 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004756 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004757}
4758
4759void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4760 LocationSummary* locations = instruction->GetLocations();
4761 InvokeRuntimeCallingConvention calling_convention;
4762 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4763 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004764 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004765 // Note: if heap poisoning is enabled, the entry point takes cares
4766 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004767 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004768 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004769}
4770
Alexandre Rames5319def2014-10-23 10:03:10 +01004771void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4772 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004773 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004774 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004775 if (instruction->IsStringAlloc()) {
4776 locations->AddTemp(LocationFrom(kArtMethodRegister));
4777 } else {
4778 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4779 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4780 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004781 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4782}
4783
4784void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004785 // Note: if heap poisoning is enabled, the entry point takes cares
4786 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004787 if (instruction->IsStringAlloc()) {
4788 // String is allocated through StringFactory. Call NewEmptyString entry point.
4789 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004790 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004791 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4792 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4793 __ Blr(lr);
4794 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4795 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004796 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004797 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4798 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004799}
4800
4801void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004803 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004804 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004805}
4806
4807void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004808 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004809 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004810 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004811 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004812 break;
4813
4814 default:
4815 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4816 }
4817}
4818
David Brazdil66d126e2015-04-03 16:02:44 +01004819void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4821 locations->SetInAt(0, Location::RequiresRegister());
4822 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4823}
4824
4825void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004826 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004827}
4828
Alexandre Rames5319def2014-10-23 10:03:10 +01004829void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004830 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4831 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004832}
4833
Calin Juravle2ae48182016-03-16 14:05:09 +00004834void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4835 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004836 return;
4837 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004838
Alexandre Ramesd921d642015-04-16 15:07:16 +01004839 BlockPoolsScope block_pools(GetVIXLAssembler());
4840 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004841 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004842 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004843}
4844
Calin Juravle2ae48182016-03-16 14:05:09 +00004845void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004846 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004847 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004848
4849 LocationSummary* locations = instruction->GetLocations();
4850 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004851
4852 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004853}
4854
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004855void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004856 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004857}
4858
Alexandre Rames67555f72014-11-18 10:55:16 +00004859void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4860 HandleBinaryOp(instruction);
4861}
4862
4863void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4864 HandleBinaryOp(instruction);
4865}
4866
Alexandre Rames3e69f162014-12-10 10:36:50 +00004867void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4868 LOG(FATAL) << "Unreachable";
4869}
4870
4871void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4872 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4873}
4874
Alexandre Rames5319def2014-10-23 10:03:10 +01004875void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4877 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4878 if (location.IsStackSlot()) {
4879 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4880 } else if (location.IsDoubleStackSlot()) {
4881 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4882 }
4883 locations->SetOut(location);
4884}
4885
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004886void InstructionCodeGeneratorARM64::VisitParameterValue(
4887 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004888 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004889}
4890
4891void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4892 LocationSummary* locations =
4893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004894 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004895}
4896
4897void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4898 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4899 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004900}
4901
4902void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4903 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004904 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004905 locations->SetInAt(i, Location::Any());
4906 }
4907 locations->SetOut(Location::Any());
4908}
4909
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004910void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004911 LOG(FATAL) << "Unreachable";
4912}
4913
Serban Constantinescu02164b32014-11-13 14:05:07 +00004914void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004915 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004916 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004917 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4918 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4920
4921 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004922 case Primitive::kPrimInt:
4923 case Primitive::kPrimLong:
4924 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004925 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4927 break;
4928
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004929 case Primitive::kPrimFloat:
4930 case Primitive::kPrimDouble: {
4931 InvokeRuntimeCallingConvention calling_convention;
4932 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4933 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4934 locations->SetOut(calling_convention.GetReturnLocation(type));
4935
4936 break;
4937 }
4938
Serban Constantinescu02164b32014-11-13 14:05:07 +00004939 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004940 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004941 }
4942}
4943
4944void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4945 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004946
Serban Constantinescu02164b32014-11-13 14:05:07 +00004947 switch (type) {
4948 case Primitive::kPrimInt:
4949 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004950 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004951 break;
4952 }
4953
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004954 case Primitive::kPrimFloat:
4955 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004956 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4957 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004958 if (type == Primitive::kPrimFloat) {
4959 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4960 } else {
4961 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4962 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004963 break;
4964 }
4965
Serban Constantinescu02164b32014-11-13 14:05:07 +00004966 default:
4967 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004968 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004969 }
4970}
4971
Calin Juravle27df7582015-04-17 19:12:31 +01004972void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4973 memory_barrier->SetLocations(nullptr);
4974}
4975
4976void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004977 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004978}
4979
Alexandre Rames5319def2014-10-23 10:03:10 +01004980void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4982 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004983 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004984}
4985
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004986void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004987 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004988}
4989
4990void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4991 instruction->SetLocations(nullptr);
4992}
4993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004994void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004995 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004996}
4997
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004998void LocationsBuilderARM64::VisitRor(HRor* ror) {
4999 HandleBinaryOp(ror);
5000}
5001
5002void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5003 HandleBinaryOp(ror);
5004}
5005
Serban Constantinescu02164b32014-11-13 14:05:07 +00005006void LocationsBuilderARM64::VisitShl(HShl* shl) {
5007 HandleShift(shl);
5008}
5009
5010void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5011 HandleShift(shl);
5012}
5013
5014void LocationsBuilderARM64::VisitShr(HShr* shr) {
5015 HandleShift(shr);
5016}
5017
5018void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5019 HandleShift(shr);
5020}
5021
Alexandre Rames5319def2014-10-23 10:03:10 +01005022void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005023 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005024}
5025
5026void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005027 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005028}
5029
Alexandre Rames67555f72014-11-18 10:55:16 +00005030void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005031 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005032}
5033
5034void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005035 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005036}
5037
5038void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005039 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005040}
5041
Alexandre Rames67555f72014-11-18 10:55:16 +00005042void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005043 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005044}
5045
Calin Juravlee460d1d2015-09-29 04:52:17 +01005046void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5047 HUnresolvedInstanceFieldGet* instruction) {
5048 FieldAccessCallingConventionARM64 calling_convention;
5049 codegen_->CreateUnresolvedFieldLocationSummary(
5050 instruction, instruction->GetFieldType(), calling_convention);
5051}
5052
5053void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5054 HUnresolvedInstanceFieldGet* instruction) {
5055 FieldAccessCallingConventionARM64 calling_convention;
5056 codegen_->GenerateUnresolvedFieldAccess(instruction,
5057 instruction->GetFieldType(),
5058 instruction->GetFieldIndex(),
5059 instruction->GetDexPc(),
5060 calling_convention);
5061}
5062
5063void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5064 HUnresolvedInstanceFieldSet* instruction) {
5065 FieldAccessCallingConventionARM64 calling_convention;
5066 codegen_->CreateUnresolvedFieldLocationSummary(
5067 instruction, instruction->GetFieldType(), calling_convention);
5068}
5069
5070void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5071 HUnresolvedInstanceFieldSet* instruction) {
5072 FieldAccessCallingConventionARM64 calling_convention;
5073 codegen_->GenerateUnresolvedFieldAccess(instruction,
5074 instruction->GetFieldType(),
5075 instruction->GetFieldIndex(),
5076 instruction->GetDexPc(),
5077 calling_convention);
5078}
5079
5080void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5081 HUnresolvedStaticFieldGet* instruction) {
5082 FieldAccessCallingConventionARM64 calling_convention;
5083 codegen_->CreateUnresolvedFieldLocationSummary(
5084 instruction, instruction->GetFieldType(), calling_convention);
5085}
5086
5087void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5088 HUnresolvedStaticFieldGet* instruction) {
5089 FieldAccessCallingConventionARM64 calling_convention;
5090 codegen_->GenerateUnresolvedFieldAccess(instruction,
5091 instruction->GetFieldType(),
5092 instruction->GetFieldIndex(),
5093 instruction->GetDexPc(),
5094 calling_convention);
5095}
5096
5097void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5098 HUnresolvedStaticFieldSet* instruction) {
5099 FieldAccessCallingConventionARM64 calling_convention;
5100 codegen_->CreateUnresolvedFieldLocationSummary(
5101 instruction, instruction->GetFieldType(), calling_convention);
5102}
5103
5104void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5105 HUnresolvedStaticFieldSet* instruction) {
5106 FieldAccessCallingConventionARM64 calling_convention;
5107 codegen_->GenerateUnresolvedFieldAccess(instruction,
5108 instruction->GetFieldType(),
5109 instruction->GetFieldIndex(),
5110 instruction->GetDexPc(),
5111 calling_convention);
5112}
5113
Alexandre Rames5319def2014-10-23 10:03:10 +01005114void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005115 LocationSummary* locations =
5116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005117 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005118}
5119
5120void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005121 HBasicBlock* block = instruction->GetBlock();
5122 if (block->GetLoopInformation() != nullptr) {
5123 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5124 // The back edge will generate the suspend check.
5125 return;
5126 }
5127 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5128 // The goto will generate the suspend check.
5129 return;
5130 }
5131 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005132}
5133
Alexandre Rames67555f72014-11-18 10:55:16 +00005134void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5135 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005136 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005137 InvokeRuntimeCallingConvention calling_convention;
5138 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5139}
5140
5141void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005142 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005143 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005144}
5145
5146void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5147 LocationSummary* locations =
5148 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5149 Primitive::Type input_type = conversion->GetInputType();
5150 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005151 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005152 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5153 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5154 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5155 }
5156
Alexandre Rames542361f2015-01-29 16:57:31 +00005157 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005158 locations->SetInAt(0, Location::RequiresFpuRegister());
5159 } else {
5160 locations->SetInAt(0, Location::RequiresRegister());
5161 }
5162
Alexandre Rames542361f2015-01-29 16:57:31 +00005163 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005164 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5165 } else {
5166 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5167 }
5168}
5169
5170void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5171 Primitive::Type result_type = conversion->GetResultType();
5172 Primitive::Type input_type = conversion->GetInputType();
5173
5174 DCHECK_NE(input_type, result_type);
5175
Alexandre Rames542361f2015-01-29 16:57:31 +00005176 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005177 int result_size = Primitive::ComponentSize(result_type);
5178 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005179 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005180 Register output = OutputRegister(conversion);
5181 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005182 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005183 // 'int' values are used directly as W registers, discarding the top
5184 // bits, so we don't need to sign-extend and can just perform a move.
5185 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5186 // top 32 bits of the target register. We theoretically could leave those
5187 // bits unchanged, but we would have to make sure that no code uses a
5188 // 32bit input value as a 64bit value assuming that the top 32 bits are
5189 // zero.
5190 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005191 } else if (result_type == Primitive::kPrimChar ||
5192 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5193 __ Ubfx(output,
5194 output.IsX() ? source.X() : source.W(),
5195 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005196 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005197 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005198 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005199 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005200 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005201 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005202 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5203 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005204 } else if (Primitive::IsFloatingPointType(result_type) &&
5205 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005206 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5207 } else {
5208 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5209 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005210 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005211}
Alexandre Rames67555f72014-11-18 10:55:16 +00005212
Serban Constantinescu02164b32014-11-13 14:05:07 +00005213void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5214 HandleShift(ushr);
5215}
5216
5217void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5218 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005219}
5220
5221void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5222 HandleBinaryOp(instruction);
5223}
5224
5225void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5226 HandleBinaryOp(instruction);
5227}
5228
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005229void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005230 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005231 LOG(FATAL) << "Unreachable";
5232}
5233
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005234void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005235 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005236 LOG(FATAL) << "Unreachable";
5237}
5238
Mark Mendellfe57faa2015-09-18 09:26:15 -04005239// Simple implementation of packed switch - generate cascaded compare/jumps.
5240void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5241 LocationSummary* locations =
5242 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5243 locations->SetInAt(0, Location::RequiresRegister());
5244}
5245
5246void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5247 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005248 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005249 Register value_reg = InputRegisterAt(switch_instr, 0);
5250 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5251
Zheng Xu3927c8b2015-11-18 17:46:25 +08005252 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005253 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005254 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5255 // make sure we don't emit it if the target may run out of range.
5256 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5257 // ranges and emit the tables only as required.
5258 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005259
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005260 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005261 // Current instruction id is an upper bound of the number of HIRs in the graph.
5262 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5263 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005264 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5265 Register temp = temps.AcquireW();
5266 __ Subs(temp, value_reg, Operand(lower_bound));
5267
Zheng Xu3927c8b2015-11-18 17:46:25 +08005268 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005269 // Jump to successors[0] if value == lower_bound.
5270 __ B(eq, codegen_->GetLabelOf(successors[0]));
5271 int32_t last_index = 0;
5272 for (; num_entries - last_index > 2; last_index += 2) {
5273 __ Subs(temp, temp, Operand(2));
5274 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5275 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5276 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5277 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5278 }
5279 if (num_entries - last_index == 2) {
5280 // The last missing case_value.
5281 __ Cmp(temp, Operand(1));
5282 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005283 }
5284
5285 // And the default for any other value.
5286 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5287 __ B(codegen_->GetLabelOf(default_block));
5288 }
5289 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005290 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005291
5292 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5293
5294 // Below instructions should use at most one blocked register. Since there are two blocked
5295 // registers, we are free to block one.
5296 Register temp_w = temps.AcquireW();
5297 Register index;
5298 // Remove the bias.
5299 if (lower_bound != 0) {
5300 index = temp_w;
5301 __ Sub(index, value_reg, Operand(lower_bound));
5302 } else {
5303 index = value_reg;
5304 }
5305
5306 // Jump to default block if index is out of the range.
5307 __ Cmp(index, Operand(num_entries));
5308 __ B(hs, codegen_->GetLabelOf(default_block));
5309
5310 // In current VIXL implementation, it won't require any blocked registers to encode the
5311 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5312 // register pressure.
5313 Register table_base = temps.AcquireX();
5314 // Load jump offset from the table.
5315 __ Adr(table_base, jump_table->GetTableStartLabel());
5316 Register jump_offset = temp_w;
5317 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5318
5319 // Jump to target block by branching to table_base(pc related) + offset.
5320 Register target_address = table_base;
5321 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5322 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005323 }
5324}
5325
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005326void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5327 HInstruction* instruction,
5328 Location out,
5329 uint32_t offset,
5330 Location maybe_temp,
5331 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005332 Primitive::Type type = Primitive::kPrimNot;
5333 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005334 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005335 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005336 Register temp_reg = RegisterFrom(maybe_temp, type);
5337 if (kUseBakerReadBarrier) {
5338 // Load with fast path based Baker's read barrier.
5339 // /* HeapReference<Object> */ out = *(out + offset)
5340 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5341 out,
5342 out_reg,
5343 offset,
5344 temp_reg,
5345 /* needs_null_check */ false,
5346 /* use_load_acquire */ false);
5347 } else {
5348 // Load with slow path based read barrier.
5349 // Save the value of `out` into `maybe_temp` before overwriting it
5350 // in the following move operation, as we will need it for the
5351 // read barrier below.
5352 __ Mov(temp_reg, out_reg);
5353 // /* HeapReference<Object> */ out = *(out + offset)
5354 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5355 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5356 }
5357 } else {
5358 // Plain load with no read barrier.
5359 // /* HeapReference<Object> */ out = *(out + offset)
5360 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5361 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5362 }
5363}
5364
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005365void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5366 HInstruction* instruction,
5367 Location out,
5368 Location obj,
5369 uint32_t offset,
5370 Location maybe_temp,
5371 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005372 Primitive::Type type = Primitive::kPrimNot;
5373 Register out_reg = RegisterFrom(out, type);
5374 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005375 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005376 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005377 if (kUseBakerReadBarrier) {
5378 // Load with fast path based Baker's read barrier.
5379 Register temp_reg = RegisterFrom(maybe_temp, type);
5380 // /* HeapReference<Object> */ out = *(obj + offset)
5381 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5382 out,
5383 obj_reg,
5384 offset,
5385 temp_reg,
5386 /* needs_null_check */ false,
5387 /* use_load_acquire */ false);
5388 } else {
5389 // Load with slow path based read barrier.
5390 // /* HeapReference<Object> */ out = *(obj + offset)
5391 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5392 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5393 }
5394 } else {
5395 // Plain load with no read barrier.
5396 // /* HeapReference<Object> */ out = *(obj + offset)
5397 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5398 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5399 }
5400}
5401
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005402void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5403 HInstruction* instruction,
5404 Location root,
5405 Register obj,
5406 uint32_t offset,
5407 vixl::aarch64::Label* fixup_label,
5408 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005409 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005410 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005411 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005412 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005413 if (kUseBakerReadBarrier) {
5414 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5415 // Baker's read barrier are used:
5416 //
5417 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005418 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5419 // if (temp != null) {
5420 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005421 // }
5422
5423 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005424 if (fixup_label == nullptr) {
5425 __ Ldr(root_reg, MemOperand(obj, offset));
5426 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005427 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005428 }
Roland Levillain44015862016-01-22 11:47:17 +00005429 static_assert(
5430 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5431 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5432 "have different sizes.");
5433 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5434 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5435 "have different sizes.");
5436
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005437 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005438
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005439 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5440 SlowPathCodeARM64* slow_path =
5441 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5442 root,
5443 LocationFrom(temp));
5444 codegen_->AddSlowPath(slow_path);
5445 const int32_t entry_point_offset =
5446 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5447 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5448 // Loading the entrypoint does not require a load acquire since it is only changed when
5449 // threads are suspended or running a checkpoint.
5450 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5451 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5452 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005453 __ Cbnz(temp, slow_path->GetEntryLabel());
5454 __ Bind(slow_path->GetExitLabel());
5455 } else {
5456 // GC root loaded through a slow path for read barriers other
5457 // than Baker's.
5458 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005459 if (fixup_label == nullptr) {
5460 __ Add(root_reg.X(), obj.X(), offset);
5461 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005462 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005463 }
Roland Levillain44015862016-01-22 11:47:17 +00005464 // /* mirror::Object* */ root = root->Read()
5465 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5466 }
5467 } else {
5468 // Plain GC root load with no read barrier.
5469 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005470 if (fixup_label == nullptr) {
5471 __ Ldr(root_reg, MemOperand(obj, offset));
5472 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005473 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005474 }
Roland Levillain44015862016-01-22 11:47:17 +00005475 // Note that GC roots are not affected by heap poisoning, thus we
5476 // do not have to unpoison `root_reg` here.
5477 }
5478}
5479
5480void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5481 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005482 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005483 uint32_t offset,
5484 Register temp,
5485 bool needs_null_check,
5486 bool use_load_acquire) {
5487 DCHECK(kEmitCompilerReadBarrier);
5488 DCHECK(kUseBakerReadBarrier);
5489
5490 // /* HeapReference<Object> */ ref = *(obj + offset)
5491 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005492 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005493 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5494 ref,
5495 obj,
5496 offset,
5497 no_index,
5498 no_scale_factor,
5499 temp,
5500 needs_null_check,
5501 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005502}
5503
5504void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5505 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005506 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005507 uint32_t data_offset,
5508 Location index,
5509 Register temp,
5510 bool needs_null_check) {
5511 DCHECK(kEmitCompilerReadBarrier);
5512 DCHECK(kUseBakerReadBarrier);
5513
5514 // Array cells are never volatile variables, therefore array loads
5515 // never use Load-Acquire instructions on ARM64.
5516 const bool use_load_acquire = false;
5517
Roland Levillainbfea3352016-06-23 13:48:47 +01005518 static_assert(
5519 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5520 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005521 // /* HeapReference<Object> */ ref =
5522 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005523 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5524 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5525 ref,
5526 obj,
5527 data_offset,
5528 index,
5529 scale_factor,
5530 temp,
5531 needs_null_check,
5532 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005533}
5534
5535void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5536 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005537 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005538 uint32_t offset,
5539 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005540 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005541 Register temp,
5542 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005543 bool use_load_acquire,
5544 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005545 DCHECK(kEmitCompilerReadBarrier);
5546 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005547 // If we are emitting an array load, we should not be using a
5548 // Load Acquire instruction. In other words:
5549 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5550 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005551
5552 MacroAssembler* masm = GetVIXLAssembler();
5553 UseScratchRegisterScope temps(masm);
5554
5555 // In slow path based read barriers, the read barrier call is
5556 // inserted after the original load. However, in fast path based
5557 // Baker's read barriers, we need to perform the load of
5558 // mirror::Object::monitor_ *before* the original reference load.
5559 // This load-load ordering is required by the read barrier.
5560 // The fast path/slow path (for Baker's algorithm) should look like:
5561 //
5562 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5563 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5564 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005565 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005566 // if (is_gray) {
5567 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5568 // }
5569 //
5570 // Note: the original implementation in ReadBarrier::Barrier is
5571 // slightly more complex as it performs additional checks that we do
5572 // not do here for performance reasons.
5573
5574 Primitive::Type type = Primitive::kPrimNot;
5575 Register ref_reg = RegisterFrom(ref, type);
5576 DCHECK(obj.IsW());
5577 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5578
5579 // /* int32_t */ monitor = obj->monitor_
5580 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5581 if (needs_null_check) {
5582 MaybeRecordImplicitNullCheck(instruction);
5583 }
5584 // /* LockWord */ lock_word = LockWord(monitor)
5585 static_assert(sizeof(LockWord) == sizeof(int32_t),
5586 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005587
Vladimir Marko877a0332016-07-11 19:30:56 +01005588 // Introduce a dependency on the lock_word including rb_state,
5589 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005590 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005591 // `obj` is unchanged by this operation, but its value now depends
5592 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005593 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005594
5595 // The actual reference load.
5596 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005597 // Load types involving an "index": ArrayGet,
5598 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5599 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005600 if (use_load_acquire) {
5601 // UnsafeGetObjectVolatile intrinsic case.
5602 // Register `index` is not an index in an object array, but an
5603 // offset to an object reference field within object `obj`.
5604 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5605 DCHECK(instruction->GetLocations()->Intrinsified());
5606 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5607 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005608 DCHECK_EQ(offset, 0u);
5609 DCHECK_EQ(scale_factor, 0u);
5610 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005611 // /* HeapReference<Object> */ ref = *(obj + index)
5612 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5613 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005614 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005615 // ArrayGet and UnsafeGetObject intrinsics cases.
5616 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5617 if (index.IsConstant()) {
5618 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5619 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5620 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005621 Register temp3 = temps.AcquireW();
5622 __ Add(temp3, obj, offset);
5623 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5624 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005625 }
Roland Levillain44015862016-01-22 11:47:17 +00005626 }
Roland Levillain44015862016-01-22 11:47:17 +00005627 } else {
5628 // /* HeapReference<Object> */ ref = *(obj + offset)
5629 MemOperand field = HeapOperand(obj, offset);
5630 if (use_load_acquire) {
5631 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5632 } else {
5633 Load(type, ref_reg, field);
5634 }
5635 }
5636
5637 // Object* ref = ref_addr->AsMirrorPtr()
5638 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5639
Vladimir Marko953437b2016-08-24 08:30:46 +00005640 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005641 SlowPathCodeARM64* slow_path;
5642 if (always_update_field) {
5643 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5644 // address of the form `obj + field_offset`, where `obj` is a
5645 // register and `field_offset` is a register. Thus `offset` and
5646 // `scale_factor` above are expected to be null in this code path.
5647 DCHECK_EQ(offset, 0u);
5648 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5649 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5650 instruction, ref, obj, /* field_offset */ index, temp);
5651 } else {
5652 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5653 }
Roland Levillain44015862016-01-22 11:47:17 +00005654 AddSlowPath(slow_path);
5655
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005656 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005657 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005658 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005659 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5660 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005661 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005662 __ Bind(slow_path->GetExitLabel());
5663}
5664
5665void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5666 Location out,
5667 Location ref,
5668 Location obj,
5669 uint32_t offset,
5670 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005671 DCHECK(kEmitCompilerReadBarrier);
5672
Roland Levillain44015862016-01-22 11:47:17 +00005673 // Insert a slow path based read barrier *after* the reference load.
5674 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005675 // If heap poisoning is enabled, the unpoisoning of the loaded
5676 // reference will be carried out by the runtime within the slow
5677 // path.
5678 //
5679 // Note that `ref` currently does not get unpoisoned (when heap
5680 // poisoning is enabled), which is alright as the `ref` argument is
5681 // not used by the artReadBarrierSlow entry point.
5682 //
5683 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5684 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5685 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5686 AddSlowPath(slow_path);
5687
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005688 __ B(slow_path->GetEntryLabel());
5689 __ Bind(slow_path->GetExitLabel());
5690}
5691
Roland Levillain44015862016-01-22 11:47:17 +00005692void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5693 Location out,
5694 Location ref,
5695 Location obj,
5696 uint32_t offset,
5697 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005698 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005699 // Baker's read barriers shall be handled by the fast path
5700 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5701 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005702 // If heap poisoning is enabled, unpoisoning will be taken care of
5703 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005704 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005705 } else if (kPoisonHeapReferences) {
5706 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5707 }
5708}
5709
Roland Levillain44015862016-01-22 11:47:17 +00005710void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5711 Location out,
5712 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005713 DCHECK(kEmitCompilerReadBarrier);
5714
Roland Levillain44015862016-01-22 11:47:17 +00005715 // Insert a slow path based read barrier *after* the GC root load.
5716 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005717 // Note that GC roots are not affected by heap poisoning, so we do
5718 // not need to do anything special for this here.
5719 SlowPathCodeARM64* slow_path =
5720 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5721 AddSlowPath(slow_path);
5722
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005723 __ B(slow_path->GetEntryLabel());
5724 __ Bind(slow_path->GetExitLabel());
5725}
5726
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005727void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5728 LocationSummary* locations =
5729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5730 locations->SetInAt(0, Location::RequiresRegister());
5731 locations->SetOut(Location::RequiresRegister());
5732}
5733
5734void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5735 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005736 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005737 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005738 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005739 __ Ldr(XRegisterFrom(locations->Out()),
5740 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005741 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005742 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005743 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005744 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5745 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005746 __ Ldr(XRegisterFrom(locations->Out()),
5747 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005748 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005749}
5750
Nicolas Geoffray75afcdd2016-11-10 10:38:11 +00005751void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5752 for (const auto& entry : jit_string_patches_) {
5753 const auto& it = jit_string_roots_.find(entry.first);
5754 DCHECK(it != jit_string_roots_.end());
5755 size_t index_in_table = it->second;
5756 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
5757 uint32_t literal_offset = literal->GetOffset();
5758 uintptr_t address =
5759 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5760 uint8_t* data = code + literal_offset;
5761 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5762 }
5763}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005764
Alexandre Rames67555f72014-11-18 10:55:16 +00005765#undef __
5766#undef QUICK_ENTRY_POINT
5767
Alexandre Rames5319def2014-10-23 10:03:10 +01005768} // namespace arm64
5769} // namespace art