blob: 59e17841e4aeb3413e782e8d76c97e499227e77c [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = at_->GetLocations();
285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex().index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
293 : kQuickInitializeType;
294 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100296 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100298 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000300
301 // Move the class to the desired location.
302 Location out = locations->Out();
303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 __ B(GetExitLabel());
311 }
312
Alexandre Rames9931f312015-06-19 14:47:01 +0100313 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
314
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
319 // The instruction where this slow path is happening.
320 // (Might be the load class or an initialization check).
321 HInstruction* const at_;
322
323 // The dex PC of `at_`.
324 const uint32_t dex_pc_;
325
326 // Whether to initialize the class.
327 const bool do_clinit_;
328
329 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
330};
331
Vladimir Markoaad75c62016-10-03 08:46:48 +0000332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100334 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
335 : SlowPathCodeARM64(instruction),
336 temp_(temp),
337 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 LocationSummary* locations = instruction_->GetLocations();
341 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
343
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100344 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
345 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
346 temps.Exclude(temp_);
347
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, locations);
350
351 InvokeRuntimeCallingConvention calling_convention;
352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
356 Primitive::Type type = instruction_->GetType();
357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
358
359 RestoreLiveRegisters(codegen, locations);
360
361 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
364 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
367 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
368 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100371 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100376 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 }
378
379 __ B(GetExitLabel());
380 }
381
382 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
383
384 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100385 const Register temp_;
386 vixl::aarch64::Label* adrp_label_;
387
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
389};
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 if (instruction_->CanThrowIntoCatchBlock()) {
399 // Live registers will be restored in the catch block if caught.
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
401 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000402 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
403 instruction_,
404 instruction_->GetDexPc(),
405 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800406 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100407 }
408
Alexandre Rames8158f282015-08-07 10:26:17 +0100409 bool IsFatal() const OVERRIDE { return true; }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
412
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
415};
416
417class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100419 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000420 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 if (successor_ == nullptr) {
428 __ B(GetReturnLabel());
429 } else {
430 __ B(arm64_codegen->GetLabelOf(successor_));
431 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 }
433
Scott Wakeling97c72b72016-06-24 16:19:36 +0100434 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 DCHECK(successor_ == nullptr);
436 return &return_label_;
437 }
438
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100439 HBasicBlock* GetSuccessor() const {
440 return successor_;
441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
444
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 // If not null, the block to branch to after the suspend check.
447 HBasicBlock* const successor_;
448
449 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100450 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100451
452 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
453};
454
Alexandre Rames67555f72014-11-18 10:55:16 +0000455class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000457 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000458 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800462
Alexandre Rames3e69f162014-12-10 10:36:50 +0000463 DCHECK(instruction_->IsCheckCast()
464 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100466 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000470 if (!is_fatal_) {
471 SaveLiveRegisters(codegen, locations);
472 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473
474 // We're moving two locations to locations that could overlap, so we need a parallel
475 // move resolver.
476 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800477 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800478 LocationFrom(calling_convention.GetRegisterAt(0)),
479 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800480 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800481 LocationFrom(calling_convention.GetRegisterAt(1)),
482 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000483 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000484 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800485 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000486 Primitive::Type ret_type = instruction_->GetType();
487 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
488 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
489 } else {
490 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800491 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
492 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493 }
494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000495 if (!is_fatal_) {
496 RestoreLiveRegisters(codegen, locations);
497 __ B(GetExitLabel());
498 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 }
500
Alexandre Rames9931f312015-06-19 14:47:01 +0100501 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100502 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000506
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
508};
509
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
511 public:
Aart Bik42249c32016-01-07 15:33:50 -0800512 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000513 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800516 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700517 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000518 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000519 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700520 }
521
Alexandre Rames9931f312015-06-19 14:47:01 +0100522 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
526};
527
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100528class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
533 LocationSummary* locations = instruction_->GetLocations();
534 __ Bind(GetEntryLabel());
535 SaveLiveRegisters(codegen, locations);
536
537 InvokeRuntimeCallingConvention calling_convention;
538 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
539 parallel_move.AddMove(
540 locations->InAt(0),
541 LocationFrom(calling_convention.GetRegisterAt(0)),
542 Primitive::kPrimNot,
543 nullptr);
544 parallel_move.AddMove(
545 locations->InAt(1),
546 LocationFrom(calling_convention.GetRegisterAt(1)),
547 Primitive::kPrimInt,
548 nullptr);
549 parallel_move.AddMove(
550 locations->InAt(2),
551 LocationFrom(calling_convention.GetRegisterAt(2)),
552 Primitive::kPrimNot,
553 nullptr);
554 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
555
556 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000557 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
559 RestoreLiveRegisters(codegen, locations);
560 __ B(GetExitLabel());
561 }
562
563 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
564
565 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
567};
568
Zheng Xu3927c8b2015-11-18 17:46:25 +0800569void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
570 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000571 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800572
573 // We are about to use the assembler to place literals directly. Make sure we have enough
574 // underlying code buffer and we have generated the jump table with right size.
575 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
576 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
577
578 __ Bind(&table_start_);
579 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
580 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100581 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800582 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100583 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800584 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
585 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
586 Literal<int32_t> literal(jump_offset);
587 __ place(&literal);
588 }
589}
590
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100591// Slow path marking an object reference `ref` during a read
592// barrier. The field `obj.field` in the object `obj` holding this
593// reference does not get updated by this slow path after marking (see
594// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
595//
596// This means that after the execution of this slow path, `ref` will
597// always be up-to-date, but `obj.field` may not; i.e., after the
598// flip, `ref` will be a to-space reference, but `obj.field` will
599// probably still be a from-space reference (unless it gets updated by
600// another thread, or if another thread installed another object
601// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800602// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
603// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000604class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
605 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800606 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
607 Location ref,
608 Location entrypoint = Location::NoLocation())
609 : SlowPathCodeARM64(instruction),
610 ref_(ref),
611 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000612 DCHECK(kEmitCompilerReadBarrier);
613 }
614
615 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000619 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100620 DCHECK(ref_.IsRegister()) << ref_;
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000622 DCHECK(instruction_->IsInstanceFieldGet() ||
623 instruction_->IsStaticFieldGet() ||
624 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100625 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000626 instruction_->IsLoadClass() ||
627 instruction_->IsLoadString() ||
628 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100629 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100630 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
631 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000632 << "Unexpected instruction in read barrier marking slow path: "
633 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000634 // The read barrier instrumentation of object ArrayGet
635 // instructions does not support the HIntermediateAddress
636 // instruction.
637 DCHECK(!(instruction_->IsArrayGet() &&
638 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000639
640 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100641 // No need to save live registers; it's taken care of by the
642 // entrypoint. Also, there is no need to update the stack mask,
643 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000644 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100645 DCHECK_NE(ref_.reg(), LR);
646 DCHECK_NE(ref_.reg(), WSP);
647 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100648 // IP0 is used internally by the ReadBarrierMarkRegX entry point
649 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100650 DCHECK_NE(ref_.reg(), IP0);
651 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100652 // "Compact" slow path, saving two moves.
653 //
654 // Instead of using the standard runtime calling convention (input
655 // and output in W0):
656 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100657 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100658 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100659 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100660 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100661 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100662 // of a dedicated entrypoint:
663 //
664 // rX <- ReadBarrierMarkRegX(rX)
665 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800666 if (entrypoint_.IsValid()) {
667 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
668 __ Blr(XRegisterFrom(entrypoint_));
669 } else {
670 // Entrypoint is not already loaded, load from the thread.
671 int32_t entry_point_offset =
672 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
673 // This runtime call does not require a stack map.
674 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
675 }
Roland Levillain44015862016-01-22 11:47:17 +0000676 __ B(GetExitLabel());
677 }
678
679 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 // The location (register) of the marked object reference.
681 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000682
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800683 // The location of the entrypoint if it is already loaded.
684 const Location entrypoint_;
685
Roland Levillain44015862016-01-22 11:47:17 +0000686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
687};
688
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100689// Slow path marking an object reference `ref` during a read barrier,
690// and if needed, atomically updating the field `obj.field` in the
691// object `obj` holding this reference after marking (contrary to
692// ReadBarrierMarkSlowPathARM64 above, which never tries to update
693// `obj.field`).
694//
695// This means that after the execution of this slow path, both `ref`
696// and `obj.field` will be up-to-date; i.e., after the flip, both will
697// hold the same to-space reference (unless another thread installed
698// another object reference (different from `ref`) in `obj.field`).
699class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
700 public:
701 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
702 Location ref,
703 Register obj,
704 Location field_offset,
705 Register temp)
706 : SlowPathCodeARM64(instruction),
707 ref_(ref),
708 obj_(obj),
709 field_offset_(field_offset),
710 temp_(temp) {
711 DCHECK(kEmitCompilerReadBarrier);
712 }
713
714 const char* GetDescription() const OVERRIDE {
715 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
716 }
717
718 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
719 LocationSummary* locations = instruction_->GetLocations();
720 Register ref_reg = WRegisterFrom(ref_);
721 DCHECK(locations->CanCall());
722 DCHECK(ref_.IsRegister()) << ref_;
723 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
724 // This slow path is only used by the UnsafeCASObject intrinsic.
725 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
726 << "Unexpected instruction in read barrier marking and field updating slow path: "
727 << instruction_->DebugName();
728 DCHECK(instruction_->GetLocations()->Intrinsified());
729 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
730 DCHECK(field_offset_.IsRegister()) << field_offset_;
731
732 __ Bind(GetEntryLabel());
733
734 // Save the old reference.
735 // Note that we cannot use IP to save the old reference, as IP is
736 // used internally by the ReadBarrierMarkRegX entry point, and we
737 // need the old reference after the call to that entry point.
738 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
739 __ Mov(temp_.W(), ref_reg);
740
741 // No need to save live registers; it's taken care of by the
742 // entrypoint. Also, there is no need to update the stack mask,
743 // as this runtime call will not trigger a garbage collection.
744 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
745 DCHECK_NE(ref_.reg(), LR);
746 DCHECK_NE(ref_.reg(), WSP);
747 DCHECK_NE(ref_.reg(), WZR);
748 // IP0 is used internally by the ReadBarrierMarkRegX entry point
749 // as a temporary, it cannot be the entry point's input/output.
750 DCHECK_NE(ref_.reg(), IP0);
751 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
752 // "Compact" slow path, saving two moves.
753 //
754 // Instead of using the standard runtime calling convention (input
755 // and output in W0):
756 //
757 // W0 <- ref
758 // W0 <- ReadBarrierMark(W0)
759 // ref <- W0
760 //
761 // we just use rX (the register containing `ref`) as input and output
762 // of a dedicated entrypoint:
763 //
764 // rX <- ReadBarrierMarkRegX(rX)
765 //
766 int32_t entry_point_offset =
767 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
768 // This runtime call does not require a stack map.
769 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
770
771 // If the new reference is different from the old reference,
772 // update the field in the holder (`*(obj_ + field_offset_)`).
773 //
774 // Note that this field could also hold a different object, if
775 // another thread had concurrently changed it. In that case, the
776 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
777 // (CAS) operation below would abort the CAS, leaving the field
778 // as-is.
779 vixl::aarch64::Label done;
780 __ Cmp(temp_.W(), ref_reg);
781 __ B(eq, &done);
782
783 // Update the the holder's field atomically. This may fail if
784 // mutator updates before us, but it's OK. This is achieved
785 // using a strong compare-and-set (CAS) operation with relaxed
786 // memory synchronization ordering, where the expected value is
787 // the old reference and the desired value is the new reference.
788
789 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
790 UseScratchRegisterScope temps(masm);
791
792 // Convenience aliases.
793 Register base = obj_.W();
794 Register offset = XRegisterFrom(field_offset_);
795 Register expected = temp_.W();
796 Register value = ref_reg;
797 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
798 Register tmp_value = temps.AcquireW(); // Value in memory.
799
800 __ Add(tmp_ptr, base.X(), Operand(offset));
801
802 if (kPoisonHeapReferences) {
803 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
804 if (value.Is(expected)) {
805 // Do not poison `value`, as it is the same register as
806 // `expected`, which has just been poisoned.
807 } else {
808 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
809 }
810 }
811
812 // do {
813 // tmp_value = [tmp_ptr] - expected;
814 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
815
Roland Levillain24a4d112016-10-26 13:10:46 +0100816 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100817 __ Bind(&loop_head);
818 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
819 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100820 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100821 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
822 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100823 __ B(&exit_loop);
824 __ Bind(&comparison_failed);
825 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100826 __ Bind(&exit_loop);
827
828 if (kPoisonHeapReferences) {
829 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
830 if (value.Is(expected)) {
831 // Do not unpoison `value`, as it is the same register as
832 // `expected`, which has just been unpoisoned.
833 } else {
834 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
835 }
836 }
837
838 __ Bind(&done);
839 __ B(GetExitLabel());
840 }
841
842 private:
843 // The location (register) of the marked object reference.
844 const Location ref_;
845 // The register containing the object holding the marked object reference field.
846 const Register obj_;
847 // The location of the offset of the marked reference field within `obj_`.
848 Location field_offset_;
849
850 const Register temp_;
851
852 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
853};
854
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000855// Slow path generating a read barrier for a heap reference.
856class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
857 public:
858 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
859 Location out,
860 Location ref,
861 Location obj,
862 uint32_t offset,
863 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000864 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000865 out_(out),
866 ref_(ref),
867 obj_(obj),
868 offset_(offset),
869 index_(index) {
870 DCHECK(kEmitCompilerReadBarrier);
871 // If `obj` is equal to `out` or `ref`, it means the initial object
872 // has been overwritten by (or after) the heap object reference load
873 // to be instrumented, e.g.:
874 //
875 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000876 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000877 //
878 // In that case, we have lost the information about the original
879 // object, and the emitted read barrier cannot work properly.
880 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
881 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
882 }
883
884 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
885 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
886 LocationSummary* locations = instruction_->GetLocations();
887 Primitive::Type type = Primitive::kPrimNot;
888 DCHECK(locations->CanCall());
889 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100890 DCHECK(instruction_->IsInstanceFieldGet() ||
891 instruction_->IsStaticFieldGet() ||
892 instruction_->IsArrayGet() ||
893 instruction_->IsInstanceOf() ||
894 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100895 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000896 << "Unexpected instruction in read barrier for heap reference slow path: "
897 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000898 // The read barrier instrumentation of object ArrayGet
899 // instructions does not support the HIntermediateAddress
900 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000901 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100902 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000903
904 __ Bind(GetEntryLabel());
905
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000906 SaveLiveRegisters(codegen, locations);
907
908 // We may have to change the index's value, but as `index_` is a
909 // constant member (like other "inputs" of this slow path),
910 // introduce a copy of it, `index`.
911 Location index = index_;
912 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100913 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000914 if (instruction_->IsArrayGet()) {
915 // Compute the actual memory offset and store it in `index`.
916 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
917 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
918 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
919 // We are about to change the value of `index_reg` (see the
920 // calls to vixl::MacroAssembler::Lsl and
921 // vixl::MacroAssembler::Mov below), but it has
922 // not been saved by the previous call to
923 // art::SlowPathCode::SaveLiveRegisters, as it is a
924 // callee-save register --
925 // art::SlowPathCode::SaveLiveRegisters does not consider
926 // callee-save registers, as it has been designed with the
927 // assumption that callee-save registers are supposed to be
928 // handled by the called function. So, as a callee-save
929 // register, `index_reg` _would_ eventually be saved onto
930 // the stack, but it would be too late: we would have
931 // changed its value earlier. Therefore, we manually save
932 // it here into another freely available register,
933 // `free_reg`, chosen of course among the caller-save
934 // registers (as a callee-save `free_reg` register would
935 // exhibit the same problem).
936 //
937 // Note we could have requested a temporary register from
938 // the register allocator instead; but we prefer not to, as
939 // this is a slow path, and we know we can find a
940 // caller-save register that is available.
941 Register free_reg = FindAvailableCallerSaveRegister(codegen);
942 __ Mov(free_reg.W(), index_reg);
943 index_reg = free_reg;
944 index = LocationFrom(index_reg);
945 } else {
946 // The initial register stored in `index_` has already been
947 // saved in the call to art::SlowPathCode::SaveLiveRegisters
948 // (as it is not a callee-save register), so we can freely
949 // use it.
950 }
951 // Shifting the index value contained in `index_reg` by the scale
952 // factor (2) cannot overflow in practice, as the runtime is
953 // unable to allocate object arrays with a size larger than
954 // 2^26 - 1 (that is, 2^28 - 4 bytes).
955 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
956 static_assert(
957 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
958 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
959 __ Add(index_reg, index_reg, Operand(offset_));
960 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100961 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
962 // intrinsics, `index_` is not shifted by a scale factor of 2
963 // (as in the case of ArrayGet), as it is actually an offset
964 // to an object field within an object.
965 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000966 DCHECK(instruction_->GetLocations()->Intrinsified());
967 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
968 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
969 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100971 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000972 }
973 }
974
975 // We're moving two or three locations to locations that could
976 // overlap, so we need a parallel move resolver.
977 InvokeRuntimeCallingConvention calling_convention;
978 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
979 parallel_move.AddMove(ref_,
980 LocationFrom(calling_convention.GetRegisterAt(0)),
981 type,
982 nullptr);
983 parallel_move.AddMove(obj_,
984 LocationFrom(calling_convention.GetRegisterAt(1)),
985 type,
986 nullptr);
987 if (index.IsValid()) {
988 parallel_move.AddMove(index,
989 LocationFrom(calling_convention.GetRegisterAt(2)),
990 Primitive::kPrimInt,
991 nullptr);
992 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
993 } else {
994 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
995 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
996 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000997 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000998 instruction_,
999 instruction_->GetDexPc(),
1000 this);
1001 CheckEntrypointTypes<
1002 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1003 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1004
1005 RestoreLiveRegisters(codegen, locations);
1006
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001007 __ B(GetExitLabel());
1008 }
1009
1010 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1011
1012 private:
1013 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001014 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1015 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001016 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1017 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1018 return Register(VIXLRegCodeFromART(i), kXRegSize);
1019 }
1020 }
1021 // We shall never fail to find a free caller-save register, as
1022 // there are more than two core caller-save registers on ARM64
1023 // (meaning it is possible to find one which is different from
1024 // `ref` and `obj`).
1025 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1026 LOG(FATAL) << "Could not find a free register";
1027 UNREACHABLE();
1028 }
1029
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001030 const Location out_;
1031 const Location ref_;
1032 const Location obj_;
1033 const uint32_t offset_;
1034 // An additional location containing an index to an array.
1035 // Only used for HArrayGet and the UnsafeGetObject &
1036 // UnsafeGetObjectVolatile intrinsics.
1037 const Location index_;
1038
1039 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1040};
1041
1042// Slow path generating a read barrier for a GC root.
1043class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1044 public:
1045 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001046 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001047 DCHECK(kEmitCompilerReadBarrier);
1048 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001049
1050 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1051 LocationSummary* locations = instruction_->GetLocations();
1052 Primitive::Type type = Primitive::kPrimNot;
1053 DCHECK(locations->CanCall());
1054 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001055 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1056 << "Unexpected instruction in read barrier for GC root slow path: "
1057 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001058
1059 __ Bind(GetEntryLabel());
1060 SaveLiveRegisters(codegen, locations);
1061
1062 InvokeRuntimeCallingConvention calling_convention;
1063 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1064 // The argument of the ReadBarrierForRootSlow is not a managed
1065 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1066 // thus we need a 64-bit move here, and we cannot use
1067 //
1068 // arm64_codegen->MoveLocation(
1069 // LocationFrom(calling_convention.GetRegisterAt(0)),
1070 // root_,
1071 // type);
1072 //
1073 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1074 // reference type (`Primitive::kPrimNot`).
1075 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001076 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001077 instruction_,
1078 instruction_->GetDexPc(),
1079 this);
1080 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1081 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1082
1083 RestoreLiveRegisters(codegen, locations);
1084 __ B(GetExitLabel());
1085 }
1086
1087 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1088
1089 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001090 const Location out_;
1091 const Location root_;
1092
1093 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1094};
1095
Alexandre Rames5319def2014-10-23 10:03:10 +01001096#undef __
1097
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001098Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001099 Location next_location;
1100 if (type == Primitive::kPrimVoid) {
1101 LOG(FATAL) << "Unreachable type " << type;
1102 }
1103
Alexandre Rames542361f2015-01-29 16:57:31 +00001104 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001105 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1106 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001107 } else if (!Primitive::IsFloatingPointType(type) &&
1108 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001109 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1110 } else {
1111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001112 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1113 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001114 }
1115
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001116 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001117 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 return next_location;
1119}
1120
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001121Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001122 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001123}
1124
Serban Constantinescu579885a2015-02-22 20:51:33 +00001125CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1126 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001127 const CompilerOptions& compiler_options,
1128 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001129 : CodeGenerator(graph,
1130 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001132 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001133 callee_saved_core_registers.GetList(),
1134 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001135 compiler_options,
1136 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001137 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001138 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001139 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001140 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001141 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001142 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001143 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001144 uint32_literals_(std::less<uint32_t>(),
1145 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001146 uint64_literals_(std::less<uint64_t>(),
1147 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1148 method_patches_(MethodReferenceComparator(),
1149 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1150 call_patches_(MethodReferenceComparator(),
1151 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1152 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001153 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1154 boot_image_string_patches_(StringReferenceValueComparator(),
1155 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1156 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001157 boot_image_type_patches_(TypeReferenceValueComparator(),
1158 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1159 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001160 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001161 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1162 jit_string_patches_(StringReferenceValueComparator(),
1163 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001164 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001165 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001166}
Alexandre Rames5319def2014-10-23 10:03:10 +01001167
Alexandre Rames67555f72014-11-18 10:55:16 +00001168#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001169
Zheng Xu3927c8b2015-11-18 17:46:25 +08001170void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001171 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001172 jump_table->EmitTable(this);
1173 }
1174}
1175
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001176void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001177 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001178 // Ensure we emit the literal pool.
1179 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001180
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001181 CodeGenerator::Finalize(allocator);
1182}
1183
Zheng Xuad4450e2015-04-17 18:48:56 +08001184void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1185 // Note: There are 6 kinds of moves:
1186 // 1. constant -> GPR/FPR (non-cycle)
1187 // 2. constant -> stack (non-cycle)
1188 // 3. GPR/FPR -> GPR/FPR
1189 // 4. GPR/FPR -> stack
1190 // 5. stack -> GPR/FPR
1191 // 6. stack -> stack (non-cycle)
1192 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1193 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1194 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1195 // dependency.
1196 vixl_temps_.Open(GetVIXLAssembler());
1197}
1198
1199void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1200 vixl_temps_.Close();
1201}
1202
1203Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1204 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1205 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1206 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1207 Location scratch = GetScratchLocation(kind);
1208 if (!scratch.Equals(Location::NoLocation())) {
1209 return scratch;
1210 }
1211 // Allocate from VIXL temp registers.
1212 if (kind == Location::kRegister) {
1213 scratch = LocationFrom(vixl_temps_.AcquireX());
1214 } else {
1215 DCHECK(kind == Location::kFpuRegister);
1216 scratch = LocationFrom(vixl_temps_.AcquireD());
1217 }
1218 AddScratchLocation(scratch);
1219 return scratch;
1220}
1221
1222void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1223 if (loc.IsRegister()) {
1224 vixl_temps_.Release(XRegisterFrom(loc));
1225 } else {
1226 DCHECK(loc.IsFpuRegister());
1227 vixl_temps_.Release(DRegisterFrom(loc));
1228 }
1229 RemoveScratchLocation(loc);
1230}
1231
Alexandre Rames3e69f162014-12-10 10:36:50 +00001232void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001233 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001235}
1236
Alexandre Rames5319def2014-10-23 10:03:10 +01001237void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001238 MacroAssembler* masm = GetVIXLAssembler();
1239 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001240 __ Bind(&frame_entry_label_);
1241
Serban Constantinescu02164b32014-11-13 14:05:07 +00001242 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1243 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001244 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001245 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001246 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001247 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001248 __ Ldr(wzr, MemOperand(temp, 0));
1249 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001250 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001251
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001252 if (!HasEmptyFrame()) {
1253 int frame_size = GetFrameSize();
1254 // Stack layout:
1255 // sp[frame_size - 8] : lr.
1256 // ... : other preserved core registers.
1257 // ... : other preserved fp registers.
1258 // ... : reserved frame space.
1259 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001260
1261 // Save the current method if we need it. Note that we do not
1262 // do this in HCurrentMethod, as the instruction might have been removed
1263 // in the SSA graph.
1264 if (RequiresCurrentMethod()) {
1265 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001266 } else {
1267 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001268 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001269 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001270 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1271 frame_size - GetCoreSpillSize());
1272 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1273 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001274 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001275}
1276
1277void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001278 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001279 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001280 if (!HasEmptyFrame()) {
1281 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001282 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1283 frame_size - FrameEntrySpillSize());
1284 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1285 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001286 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001287 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001288 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001289 __ Ret();
1290 GetAssembler()->cfi().RestoreState();
1291 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001292}
1293
Scott Wakeling97c72b72016-06-24 16:19:36 +01001294CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001295 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001296 return CPURegList(CPURegister::kRegister, kXRegSize,
1297 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001298}
1299
Scott Wakeling97c72b72016-06-24 16:19:36 +01001300CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001301 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1302 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001303 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1304 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001305}
1306
Alexandre Rames5319def2014-10-23 10:03:10 +01001307void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1308 __ Bind(GetLabelOf(block));
1309}
1310
Calin Juravle175dc732015-08-25 15:42:32 +01001311void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1312 DCHECK(location.IsRegister());
1313 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1314}
1315
Calin Juravlee460d1d2015-09-29 04:52:17 +01001316void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1317 if (location.IsRegister()) {
1318 locations->AddTemp(location);
1319 } else {
1320 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1321 }
1322}
1323
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001324void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001325 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001326 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001327 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001328 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001329 if (value_can_be_null) {
1330 __ Cbz(value, &done);
1331 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001332 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001333 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001334 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001335 if (value_can_be_null) {
1336 __ Bind(&done);
1337 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001338}
1339
David Brazdil58282f42016-01-14 12:45:10 +00001340void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001341 // Blocked core registers:
1342 // lr : Runtime reserved.
1343 // tr : Runtime reserved.
1344 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1345 // ip1 : VIXL core temp.
1346 // ip0 : VIXL core temp.
1347 //
1348 // Blocked fp registers:
1349 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1351 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001353 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001355
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001356 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001357 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001358 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001359 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001360
David Brazdil58282f42016-01-14 12:45:10 +00001361 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001362 // Stubs do not save callee-save floating point registers. If the graph
1363 // is debuggable, we need to deal with these registers differently. For
1364 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001365 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1366 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001367 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001368 }
1369 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001370}
1371
Alexandre Rames3e69f162014-12-10 10:36:50 +00001372size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1373 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1374 __ Str(reg, MemOperand(sp, stack_index));
1375 return kArm64WordSize;
1376}
1377
1378size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1379 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1380 __ Ldr(reg, MemOperand(sp, stack_index));
1381 return kArm64WordSize;
1382}
1383
1384size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1385 FPRegister reg = FPRegister(reg_id, kDRegSize);
1386 __ Str(reg, MemOperand(sp, stack_index));
1387 return kArm64WordSize;
1388}
1389
1390size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1391 FPRegister reg = FPRegister(reg_id, kDRegSize);
1392 __ Ldr(reg, MemOperand(sp, stack_index));
1393 return kArm64WordSize;
1394}
1395
Alexandre Rames5319def2014-10-23 10:03:10 +01001396void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001397 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001398}
1399
1400void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001401 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001402}
1403
Alexandre Rames67555f72014-11-18 10:55:16 +00001404void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001405 if (constant->IsIntConstant()) {
1406 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1407 } else if (constant->IsLongConstant()) {
1408 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1409 } else if (constant->IsNullConstant()) {
1410 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001411 } else if (constant->IsFloatConstant()) {
1412 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1413 } else {
1414 DCHECK(constant->IsDoubleConstant());
1415 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1416 }
1417}
1418
Alexandre Rames3e69f162014-12-10 10:36:50 +00001419
1420static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1421 DCHECK(constant.IsConstant());
1422 HConstant* cst = constant.GetConstant();
1423 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001424 // Null is mapped to a core W register, which we associate with kPrimInt.
1425 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001426 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1427 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1428 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1429}
1430
Calin Juravlee460d1d2015-09-29 04:52:17 +01001431void CodeGeneratorARM64::MoveLocation(Location destination,
1432 Location source,
1433 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001434 if (source.Equals(destination)) {
1435 return;
1436 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001437
1438 // A valid move can always be inferred from the destination and source
1439 // locations. When moving from and to a register, the argument type can be
1440 // used to generate 32bit instead of 64bit moves. In debug mode we also
1441 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001442 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443
1444 if (destination.IsRegister() || destination.IsFpuRegister()) {
1445 if (unspecified_type) {
1446 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1447 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001448 (src_cst != nullptr && (src_cst->IsIntConstant()
1449 || src_cst->IsFloatConstant()
1450 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001451 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001452 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001453 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001454 // If the source is a double stack slot or a 64bit constant, a 64bit
1455 // type is appropriate. Else the source is a register, and since the
1456 // type has not been specified, we chose a 64bit type to force a 64bit
1457 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001458 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001459 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001460 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001461 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1462 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1463 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001464 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1465 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1466 __ Ldr(dst, StackOperandFrom(source));
1467 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001470 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001471 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001472 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001473 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001474 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001475 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1476 ? Primitive::kPrimLong
1477 : Primitive::kPrimInt;
1478 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1479 }
1480 } else {
1481 DCHECK(source.IsFpuRegister());
1482 if (destination.IsRegister()) {
1483 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1484 ? Primitive::kPrimDouble
1485 : Primitive::kPrimFloat;
1486 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1487 } else {
1488 DCHECK(destination.IsFpuRegister());
1489 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001490 }
1491 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001492 } else { // The destination is not a register. It must be a stack slot.
1493 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1494 if (source.IsRegister() || source.IsFpuRegister()) {
1495 if (unspecified_type) {
1496 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001497 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001499 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001500 }
1501 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001502 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1503 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1504 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001505 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001506 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1507 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001508 UseScratchRegisterScope temps(GetVIXLAssembler());
1509 HConstant* src_cst = source.GetConstant();
1510 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001511 if (src_cst->IsZeroBitPattern()) {
1512 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001514 if (src_cst->IsIntConstant()) {
1515 temp = temps.AcquireW();
1516 } else if (src_cst->IsLongConstant()) {
1517 temp = temps.AcquireX();
1518 } else if (src_cst->IsFloatConstant()) {
1519 temp = temps.AcquireS();
1520 } else {
1521 DCHECK(src_cst->IsDoubleConstant());
1522 temp = temps.AcquireD();
1523 }
1524 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001525 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001526 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001527 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001528 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001530 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531 // There is generally less pressure on FP registers.
1532 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001533 __ Ldr(temp, StackOperandFrom(source));
1534 __ Str(temp, StackOperandFrom(destination));
1535 }
1536 }
1537}
1538
1539void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001540 CPURegister dst,
1541 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001542 switch (type) {
1543 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001544 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001545 break;
1546 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001547 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001548 break;
1549 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001550 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001551 break;
1552 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001553 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001554 break;
1555 case Primitive::kPrimInt:
1556 case Primitive::kPrimNot:
1557 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001558 case Primitive::kPrimFloat:
1559 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001560 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001561 __ Ldr(dst, src);
1562 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001563 case Primitive::kPrimVoid:
1564 LOG(FATAL) << "Unreachable type " << type;
1565 }
1566}
1567
Calin Juravle77520bc2015-01-12 18:45:46 +00001568void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001569 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001570 const MemOperand& src,
1571 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001572 MacroAssembler* masm = GetVIXLAssembler();
1573 BlockPoolsScope block_pools(masm);
1574 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001575 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001576 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001577
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001578 DCHECK(!src.IsPreIndex());
1579 DCHECK(!src.IsPostIndex());
1580
1581 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001582 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001583 MemOperand base = MemOperand(temp_base);
1584 switch (type) {
1585 case Primitive::kPrimBoolean:
1586 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001587 if (needs_null_check) {
1588 MaybeRecordImplicitNullCheck(instruction);
1589 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001590 break;
1591 case Primitive::kPrimByte:
1592 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001593 if (needs_null_check) {
1594 MaybeRecordImplicitNullCheck(instruction);
1595 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001596 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1597 break;
1598 case Primitive::kPrimChar:
1599 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001600 if (needs_null_check) {
1601 MaybeRecordImplicitNullCheck(instruction);
1602 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001603 break;
1604 case Primitive::kPrimShort:
1605 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001606 if (needs_null_check) {
1607 MaybeRecordImplicitNullCheck(instruction);
1608 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001609 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1610 break;
1611 case Primitive::kPrimInt:
1612 case Primitive::kPrimNot:
1613 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001614 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001615 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001616 if (needs_null_check) {
1617 MaybeRecordImplicitNullCheck(instruction);
1618 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001619 break;
1620 case Primitive::kPrimFloat:
1621 case Primitive::kPrimDouble: {
1622 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001623 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001624
1625 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1626 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001627 if (needs_null_check) {
1628 MaybeRecordImplicitNullCheck(instruction);
1629 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001630 __ Fmov(FPRegister(dst), temp);
1631 break;
1632 }
1633 case Primitive::kPrimVoid:
1634 LOG(FATAL) << "Unreachable type " << type;
1635 }
1636}
1637
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001638void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001639 CPURegister src,
1640 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001641 switch (type) {
1642 case Primitive::kPrimBoolean:
1643 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001644 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001645 break;
1646 case Primitive::kPrimChar:
1647 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001648 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001649 break;
1650 case Primitive::kPrimInt:
1651 case Primitive::kPrimNot:
1652 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001653 case Primitive::kPrimFloat:
1654 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001655 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001656 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001657 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001658 case Primitive::kPrimVoid:
1659 LOG(FATAL) << "Unreachable type " << type;
1660 }
1661}
1662
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001663void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1664 CPURegister src,
1665 const MemOperand& dst) {
1666 UseScratchRegisterScope temps(GetVIXLAssembler());
1667 Register temp_base = temps.AcquireX();
1668
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001669 DCHECK(!dst.IsPreIndex());
1670 DCHECK(!dst.IsPostIndex());
1671
1672 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001673 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001674 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001675 MemOperand base = MemOperand(temp_base);
1676 switch (type) {
1677 case Primitive::kPrimBoolean:
1678 case Primitive::kPrimByte:
1679 __ Stlrb(Register(src), base);
1680 break;
1681 case Primitive::kPrimChar:
1682 case Primitive::kPrimShort:
1683 __ Stlrh(Register(src), base);
1684 break;
1685 case Primitive::kPrimInt:
1686 case Primitive::kPrimNot:
1687 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001688 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001689 __ Stlr(Register(src), base);
1690 break;
1691 case Primitive::kPrimFloat:
1692 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001693 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001694 Register temp_src;
1695 if (src.IsZero()) {
1696 // The zero register is used to avoid synthesizing zero constants.
1697 temp_src = Register(src);
1698 } else {
1699 DCHECK(src.IsFPRegister());
1700 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1701 __ Fmov(temp_src, FPRegister(src));
1702 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001703
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001704 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001705 break;
1706 }
1707 case Primitive::kPrimVoid:
1708 LOG(FATAL) << "Unreachable type " << type;
1709 }
1710}
1711
Calin Juravle175dc732015-08-25 15:42:32 +01001712void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1713 HInstruction* instruction,
1714 uint32_t dex_pc,
1715 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001716 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001717 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001718 if (EntrypointRequiresStackMap(entrypoint)) {
1719 RecordPcInfo(instruction, dex_pc, slow_path);
1720 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001721}
1722
Roland Levillaindec8f632016-07-22 17:10:06 +01001723void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1724 HInstruction* instruction,
1725 SlowPathCode* slow_path) {
1726 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001727 GenerateInvokeRuntime(entry_point_offset);
1728}
1729
1730void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001731 BlockPoolsScope block_pools(GetVIXLAssembler());
1732 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1733 __ Blr(lr);
1734}
1735
Alexandre Rames67555f72014-11-18 10:55:16 +00001736void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001737 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001738 UseScratchRegisterScope temps(GetVIXLAssembler());
1739 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001740 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1741
Serban Constantinescu02164b32014-11-13 14:05:07 +00001742 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001743 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1744 __ Add(temp, class_reg, status_offset);
1745 __ Ldar(temp, HeapOperand(temp));
1746 __ Cmp(temp, mirror::Class::kStatusInitialized);
1747 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 __ Bind(slow_path->GetExitLabel());
1749}
Alexandre Rames5319def2014-10-23 10:03:10 +01001750
Roland Levillain44015862016-01-22 11:47:17 +00001751void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001752 BarrierType type = BarrierAll;
1753
1754 switch (kind) {
1755 case MemBarrierKind::kAnyAny:
1756 case MemBarrierKind::kAnyStore: {
1757 type = BarrierAll;
1758 break;
1759 }
1760 case MemBarrierKind::kLoadAny: {
1761 type = BarrierReads;
1762 break;
1763 }
1764 case MemBarrierKind::kStoreStore: {
1765 type = BarrierWrites;
1766 break;
1767 }
1768 default:
1769 LOG(FATAL) << "Unexpected memory barrier " << kind;
1770 }
1771 __ Dmb(InnerShareable, type);
1772}
1773
Serban Constantinescu02164b32014-11-13 14:05:07 +00001774void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1775 HBasicBlock* successor) {
1776 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001777 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1778 if (slow_path == nullptr) {
1779 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1780 instruction->SetSlowPath(slow_path);
1781 codegen_->AddSlowPath(slow_path);
1782 if (successor != nullptr) {
1783 DCHECK(successor->IsLoopHeader());
1784 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1785 }
1786 } else {
1787 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1788 }
1789
Serban Constantinescu02164b32014-11-13 14:05:07 +00001790 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1791 Register temp = temps.AcquireW();
1792
Andreas Gampe542451c2016-07-26 09:02:02 -07001793 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001794 if (successor == nullptr) {
1795 __ Cbnz(temp, slow_path->GetEntryLabel());
1796 __ Bind(slow_path->GetReturnLabel());
1797 } else {
1798 __ Cbz(temp, codegen_->GetLabelOf(successor));
1799 __ B(slow_path->GetEntryLabel());
1800 // slow_path will return to GetLabelOf(successor).
1801 }
1802}
1803
Alexandre Rames5319def2014-10-23 10:03:10 +01001804InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1805 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001806 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001807 assembler_(codegen->GetAssembler()),
1808 codegen_(codegen) {}
1809
1810#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001811 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001812
1813#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1814
1815enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001816 // Using a base helps identify when we hit such breakpoints.
1817 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001818#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1819 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1820#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1821};
1822
1823#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001824 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001825 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1826 } \
1827 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1828 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1829 locations->SetOut(Location::Any()); \
1830 }
1831 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1832#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1833
1834#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001835#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001836
Alexandre Rames67555f72014-11-18 10:55:16 +00001837void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001838 DCHECK_EQ(instr->InputCount(), 2U);
1839 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1840 Primitive::Type type = instr->GetResultType();
1841 switch (type) {
1842 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001843 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001844 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001845 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001846 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001847 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001848
1849 case Primitive::kPrimFloat:
1850 case Primitive::kPrimDouble:
1851 locations->SetInAt(0, Location::RequiresFpuRegister());
1852 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001854 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001855
Alexandre Rames5319def2014-10-23 10:03:10 +01001856 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001857 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001858 }
1859}
1860
Alexandre Rames09a99962015-04-15 11:47:56 +01001861void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001862 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1863
1864 bool object_field_get_with_read_barrier =
1865 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001866 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001867 new (GetGraph()->GetArena()) LocationSummary(instruction,
1868 object_field_get_with_read_barrier ?
1869 LocationSummary::kCallOnSlowPath :
1870 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001871 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001872 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001873 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001874 locations->SetInAt(0, Location::RequiresRegister());
1875 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1876 locations->SetOut(Location::RequiresFpuRegister());
1877 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001878 // The output overlaps for an object field get when read barriers
1879 // are enabled: we do not want the load to overwrite the object's
1880 // location, as we need it to emit the read barrier.
1881 locations->SetOut(
1882 Location::RequiresRegister(),
1883 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001884 }
1885}
1886
1887void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1888 const FieldInfo& field_info) {
1889 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001890 LocationSummary* locations = instruction->GetLocations();
1891 Location base_loc = locations->InAt(0);
1892 Location out = locations->Out();
1893 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001894 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001895 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001896 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001897
Roland Levillain44015862016-01-22 11:47:17 +00001898 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1899 // Object FieldGet with Baker's read barrier case.
1900 MacroAssembler* masm = GetVIXLAssembler();
1901 UseScratchRegisterScope temps(masm);
1902 // /* HeapReference<Object> */ out = *(base + offset)
1903 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1904 Register temp = temps.AcquireW();
1905 // Note that potential implicit null checks are handled in this
1906 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1907 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1908 instruction,
1909 out,
1910 base,
1911 offset,
1912 temp,
1913 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001914 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001915 } else {
1916 // General case.
1917 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001918 // Note that a potential implicit null check is handled in this
1919 // CodeGeneratorARM64::LoadAcquire call.
1920 // NB: LoadAcquire will record the pc info if needed.
1921 codegen_->LoadAcquire(
1922 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001923 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001924 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001925 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001926 }
Roland Levillain44015862016-01-22 11:47:17 +00001927 if (field_type == Primitive::kPrimNot) {
1928 // If read barriers are enabled, emit read barriers other than
1929 // Baker's using a slow path (and also unpoison the loaded
1930 // reference, if heap poisoning is enabled).
1931 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1932 }
Roland Levillain4d027112015-07-01 15:41:14 +01001933 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001934}
1935
1936void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1937 LocationSummary* locations =
1938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1939 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001940 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1941 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1942 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001943 locations->SetInAt(1, Location::RequiresFpuRegister());
1944 } else {
1945 locations->SetInAt(1, Location::RequiresRegister());
1946 }
1947}
1948
1949void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001950 const FieldInfo& field_info,
1951 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001952 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001953 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001954
1955 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001956 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001957 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001958 Offset offset = field_info.GetFieldOffset();
1959 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001960
Roland Levillain4d027112015-07-01 15:41:14 +01001961 {
1962 // We use a block to end the scratch scope before the write barrier, thus
1963 // freeing the temporary registers so they can be used in `MarkGCCard`.
1964 UseScratchRegisterScope temps(GetVIXLAssembler());
1965
1966 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1967 DCHECK(value.IsW());
1968 Register temp = temps.AcquireW();
1969 __ Mov(temp, value.W());
1970 GetAssembler()->PoisonHeapReference(temp.W());
1971 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001972 }
Roland Levillain4d027112015-07-01 15:41:14 +01001973
1974 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001975 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1976 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001977 } else {
1978 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1979 codegen_->MaybeRecordImplicitNullCheck(instruction);
1980 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001981 }
1982
1983 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001984 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001985 }
1986}
1987
Alexandre Rames67555f72014-11-18 10:55:16 +00001988void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001989 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001990
1991 switch (type) {
1992 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001993 case Primitive::kPrimLong: {
1994 Register dst = OutputRegister(instr);
1995 Register lhs = InputRegisterAt(instr, 0);
1996 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001997 if (instr->IsAdd()) {
1998 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001999 } else if (instr->IsAnd()) {
2000 __ And(dst, lhs, rhs);
2001 } else if (instr->IsOr()) {
2002 __ Orr(dst, lhs, rhs);
2003 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002004 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002005 } else if (instr->IsRor()) {
2006 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002007 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002008 __ Ror(dst, lhs, shift);
2009 } else {
2010 // Ensure shift distance is in the same size register as the result. If
2011 // we are rotating a long and the shift comes in a w register originally,
2012 // we don't need to sxtw for use as an x since the shift distances are
2013 // all & reg_bits - 1.
2014 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2015 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002016 } else {
2017 DCHECK(instr->IsXor());
2018 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002019 }
2020 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002021 }
2022 case Primitive::kPrimFloat:
2023 case Primitive::kPrimDouble: {
2024 FPRegister dst = OutputFPRegister(instr);
2025 FPRegister lhs = InputFPRegisterAt(instr, 0);
2026 FPRegister rhs = InputFPRegisterAt(instr, 1);
2027 if (instr->IsAdd()) {
2028 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002029 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002030 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002031 } else {
2032 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002033 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002034 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002035 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002036 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002037 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002038 }
2039}
2040
Serban Constantinescu02164b32014-11-13 14:05:07 +00002041void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2042 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2043
2044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2045 Primitive::Type type = instr->GetResultType();
2046 switch (type) {
2047 case Primitive::kPrimInt:
2048 case Primitive::kPrimLong: {
2049 locations->SetInAt(0, Location::RequiresRegister());
2050 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2051 locations->SetOut(Location::RequiresRegister());
2052 break;
2053 }
2054 default:
2055 LOG(FATAL) << "Unexpected shift type " << type;
2056 }
2057}
2058
2059void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2060 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2061
2062 Primitive::Type type = instr->GetType();
2063 switch (type) {
2064 case Primitive::kPrimInt:
2065 case Primitive::kPrimLong: {
2066 Register dst = OutputRegister(instr);
2067 Register lhs = InputRegisterAt(instr, 0);
2068 Operand rhs = InputOperandAt(instr, 1);
2069 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002070 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002071 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002072 if (instr->IsShl()) {
2073 __ Lsl(dst, lhs, shift_value);
2074 } else if (instr->IsShr()) {
2075 __ Asr(dst, lhs, shift_value);
2076 } else {
2077 __ Lsr(dst, lhs, shift_value);
2078 }
2079 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002080 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002081
2082 if (instr->IsShl()) {
2083 __ Lsl(dst, lhs, rhs_reg);
2084 } else if (instr->IsShr()) {
2085 __ Asr(dst, lhs, rhs_reg);
2086 } else {
2087 __ Lsr(dst, lhs, rhs_reg);
2088 }
2089 }
2090 break;
2091 }
2092 default:
2093 LOG(FATAL) << "Unexpected shift operation type " << type;
2094 }
2095}
2096
Alexandre Rames5319def2014-10-23 10:03:10 +01002097void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002098 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002099}
2100
2101void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002102 HandleBinaryOp(instruction);
2103}
2104
2105void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2106 HandleBinaryOp(instruction);
2107}
2108
2109void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2110 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002111}
2112
Artem Serov7fc63502016-02-09 17:15:29 +00002113void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002114 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2115 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2116 locations->SetInAt(0, Location::RequiresRegister());
2117 // There is no immediate variant of negated bitwise instructions in AArch64.
2118 locations->SetInAt(1, Location::RequiresRegister());
2119 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2120}
2121
Artem Serov7fc63502016-02-09 17:15:29 +00002122void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002123 Register dst = OutputRegister(instr);
2124 Register lhs = InputRegisterAt(instr, 0);
2125 Register rhs = InputRegisterAt(instr, 1);
2126
2127 switch (instr->GetOpKind()) {
2128 case HInstruction::kAnd:
2129 __ Bic(dst, lhs, rhs);
2130 break;
2131 case HInstruction::kOr:
2132 __ Orn(dst, lhs, rhs);
2133 break;
2134 case HInstruction::kXor:
2135 __ Eon(dst, lhs, rhs);
2136 break;
2137 default:
2138 LOG(FATAL) << "Unreachable";
2139 }
2140}
2141
Alexandre Rames8626b742015-11-25 16:28:08 +00002142void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2143 HArm64DataProcWithShifterOp* instruction) {
2144 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2145 instruction->GetType() == Primitive::kPrimLong);
2146 LocationSummary* locations =
2147 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2148 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2149 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2150 } else {
2151 locations->SetInAt(0, Location::RequiresRegister());
2152 }
2153 locations->SetInAt(1, Location::RequiresRegister());
2154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2155}
2156
2157void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2158 HArm64DataProcWithShifterOp* instruction) {
2159 Primitive::Type type = instruction->GetType();
2160 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2161 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2162 Register out = OutputRegister(instruction);
2163 Register left;
2164 if (kind != HInstruction::kNeg) {
2165 left = InputRegisterAt(instruction, 0);
2166 }
2167 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2168 // shifter operand operation, the IR generating `right_reg` (input to the type
2169 // conversion) can have a different type from the current instruction's type,
2170 // so we manually indicate the type.
2171 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002172 int64_t shift_amount = instruction->GetShiftAmount() &
2173 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002174
2175 Operand right_operand(0);
2176
2177 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2178 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2179 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2180 } else {
2181 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2182 }
2183
2184 // Logical binary operations do not support extension operations in the
2185 // operand. Note that VIXL would still manage if it was passed by generating
2186 // the extension as a separate instruction.
2187 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2188 DCHECK(!right_operand.IsExtendedRegister() ||
2189 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2190 kind != HInstruction::kNeg));
2191 switch (kind) {
2192 case HInstruction::kAdd:
2193 __ Add(out, left, right_operand);
2194 break;
2195 case HInstruction::kAnd:
2196 __ And(out, left, right_operand);
2197 break;
2198 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002199 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002200 __ Neg(out, right_operand);
2201 break;
2202 case HInstruction::kOr:
2203 __ Orr(out, left, right_operand);
2204 break;
2205 case HInstruction::kSub:
2206 __ Sub(out, left, right_operand);
2207 break;
2208 case HInstruction::kXor:
2209 __ Eor(out, left, right_operand);
2210 break;
2211 default:
2212 LOG(FATAL) << "Unexpected operation kind: " << kind;
2213 UNREACHABLE();
2214 }
2215}
2216
Artem Serov328429f2016-07-06 16:23:04 +01002217void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002218 LocationSummary* locations =
2219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2220 locations->SetInAt(0, Location::RequiresRegister());
2221 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2222 locations->SetOut(Location::RequiresRegister());
2223}
2224
Roland Levillain19c54192016-11-04 13:44:09 +00002225void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002226 __ Add(OutputRegister(instruction),
2227 InputRegisterAt(instruction, 0),
2228 Operand(InputOperandAt(instruction, 1)));
2229}
2230
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002231void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002232 LocationSummary* locations =
2233 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002234 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2235 if (instr->GetOpKind() == HInstruction::kSub &&
2236 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002237 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002238 // Don't allocate register for Mneg instruction.
2239 } else {
2240 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2241 Location::RequiresRegister());
2242 }
2243 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2244 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2246}
2247
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002248void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002249 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002250 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2251 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002252
2253 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2254 // This fixup should be carried out for all multiply-accumulate instructions:
2255 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2256 if (instr->GetType() == Primitive::kPrimLong &&
2257 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2258 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002259 vixl::aarch64::Instruction* prev =
2260 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002261 if (prev->IsLoadOrStore()) {
2262 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002263 vixl::aarch64::CodeBufferCheckScope scope(masm,
2264 kInstructionSize,
2265 vixl::aarch64::CodeBufferCheckScope::kCheck,
2266 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002267 __ nop();
2268 }
2269 }
2270
2271 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002272 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002273 __ Madd(res, mul_left, mul_right, accumulator);
2274 } else {
2275 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002276 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002277 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002278 __ Mneg(res, mul_left, mul_right);
2279 } else {
2280 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2281 __ Msub(res, mul_left, mul_right, accumulator);
2282 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002283 }
2284}
2285
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002286void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002287 bool object_array_get_with_read_barrier =
2288 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002289 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002290 new (GetGraph()->GetArena()) LocationSummary(instruction,
2291 object_array_get_with_read_barrier ?
2292 LocationSummary::kCallOnSlowPath :
2293 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002294 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002295 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002296 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002297 locations->SetInAt(0, Location::RequiresRegister());
2298 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002299 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2300 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2301 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002302 // The output overlaps in the case of an object array get with
2303 // read barriers enabled: we do not want the move to overwrite the
2304 // array's location, as we need it to emit the read barrier.
2305 locations->SetOut(
2306 Location::RequiresRegister(),
2307 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002308 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002309}
2310
2311void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002312 Primitive::Type type = instruction->GetType();
2313 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002314 LocationSummary* locations = instruction->GetLocations();
2315 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002316 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002317 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002318 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2319 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002320 MacroAssembler* masm = GetVIXLAssembler();
2321 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002322 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002323 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002324
Roland Levillain19c54192016-11-04 13:44:09 +00002325 // The read barrier instrumentation of object ArrayGet instructions
2326 // does not support the HIntermediateAddress instruction.
2327 DCHECK(!((type == Primitive::kPrimNot) &&
2328 instruction->GetArray()->IsIntermediateAddress() &&
2329 kEmitCompilerReadBarrier));
2330
Roland Levillain44015862016-01-22 11:47:17 +00002331 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2332 // Object ArrayGet with Baker's read barrier case.
2333 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002334 // Note that a potential implicit null check is handled in the
2335 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2336 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2337 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002338 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002339 // General case.
2340 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002341 Register length;
2342 if (maybe_compressed_char_at) {
2343 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2344 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002345 if (instruction->GetArray()->IsIntermediateAddress()) {
2346 DCHECK_LT(count_offset, offset);
2347 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2348 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2349 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2350 } else {
2351 __ Ldr(length, HeapOperand(obj, count_offset));
2352 }
jessicahandojo05765752016-09-09 19:01:32 -07002353 codegen_->MaybeRecordImplicitNullCheck(instruction);
2354 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002355 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002356 if (maybe_compressed_char_at) {
2357 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002358 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2359 "Expecting 0=compressed, 1=uncompressed");
2360 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002361 __ Ldrb(Register(OutputCPURegister(instruction)),
2362 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2363 __ B(&done);
2364 __ Bind(&uncompressed_load);
2365 __ Ldrh(Register(OutputCPURegister(instruction)),
2366 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2367 __ Bind(&done);
2368 } else {
2369 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2370 source = HeapOperand(obj, offset);
2371 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002372 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002373 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002374 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002375 // We do not need to compute the intermediate address from the array: the
2376 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002377 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002378 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002379 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002380 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2381 }
2382 temp = obj;
2383 } else {
2384 __ Add(temp, obj, offset);
2385 }
jessicahandojo05765752016-09-09 19:01:32 -07002386 if (maybe_compressed_char_at) {
2387 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002388 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2389 "Expecting 0=compressed, 1=uncompressed");
2390 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002391 __ Ldrb(Register(OutputCPURegister(instruction)),
2392 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2393 __ B(&done);
2394 __ Bind(&uncompressed_load);
2395 __ Ldrh(Register(OutputCPURegister(instruction)),
2396 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2397 __ Bind(&done);
2398 } else {
2399 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2400 }
Roland Levillain44015862016-01-22 11:47:17 +00002401 }
jessicahandojo05765752016-09-09 19:01:32 -07002402 if (!maybe_compressed_char_at) {
2403 codegen_->Load(type, OutputCPURegister(instruction), source);
2404 codegen_->MaybeRecordImplicitNullCheck(instruction);
2405 }
Roland Levillain44015862016-01-22 11:47:17 +00002406
2407 if (type == Primitive::kPrimNot) {
2408 static_assert(
2409 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2410 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2411 Location obj_loc = locations->InAt(0);
2412 if (index.IsConstant()) {
2413 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2414 } else {
2415 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2416 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002417 }
Roland Levillain4d027112015-07-01 15:41:14 +01002418 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002419}
2420
Alexandre Rames5319def2014-10-23 10:03:10 +01002421void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2423 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002425}
2426
2427void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002428 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002429 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002430 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002431 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002432 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002433 // Mask out compression flag from String's array length.
2434 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002435 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002436 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002437}
2438
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002439void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002440 Primitive::Type value_type = instruction->GetComponentType();
2441
2442 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002443 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2444 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002445 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002446 LocationSummary::kCallOnSlowPath :
2447 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002448 locations->SetInAt(0, Location::RequiresRegister());
2449 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002450 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2451 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2452 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002453 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002454 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002455 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002456 }
2457}
2458
2459void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2460 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002461 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002462 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002463 bool needs_write_barrier =
2464 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002465
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002466 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002467 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002468 CPURegister source = value;
2469 Location index = locations->InAt(1);
2470 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2471 MemOperand destination = HeapOperand(array);
2472 MacroAssembler* masm = GetVIXLAssembler();
2473 BlockPoolsScope block_pools(masm);
2474
2475 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002476 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002477 if (index.IsConstant()) {
2478 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2479 destination = HeapOperand(array, offset);
2480 } else {
2481 UseScratchRegisterScope temps(masm);
2482 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002483 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002484 // We do not need to compute the intermediate address from the array: the
2485 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002486 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002487 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002488 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002489 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2490 }
2491 temp = array;
2492 } else {
2493 __ Add(temp, array, offset);
2494 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002495 destination = HeapOperand(temp,
2496 XRegisterFrom(index),
2497 LSL,
2498 Primitive::ComponentSizeShift(value_type));
2499 }
2500 codegen_->Store(value_type, value, destination);
2501 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002502 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002503 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002504 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002505 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002506 {
2507 // We use a block to end the scratch scope before the write barrier, thus
2508 // freeing the temporary registers so they can be used in `MarkGCCard`.
2509 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002510 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002511 if (index.IsConstant()) {
2512 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002513 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002514 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002515 destination = HeapOperand(temp,
2516 XRegisterFrom(index),
2517 LSL,
2518 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002519 }
2520
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002521 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2522 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2523 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2524
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002525 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002526 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2527 codegen_->AddSlowPath(slow_path);
2528 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002529 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002530 __ Cbnz(Register(value), &non_zero);
2531 if (!index.IsConstant()) {
2532 __ Add(temp, array, offset);
2533 }
2534 __ Str(wzr, destination);
2535 codegen_->MaybeRecordImplicitNullCheck(instruction);
2536 __ B(&done);
2537 __ Bind(&non_zero);
2538 }
2539
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002540 // Note that when Baker read barriers are enabled, the type
2541 // checks are performed without read barriers. This is fine,
2542 // even in the case where a class object is in the from-space
2543 // after the flip, as a comparison involving such a type would
2544 // not produce a false positive; it may of course produce a
2545 // false negative, in which case we would take the ArraySet
2546 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002547
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002548 Register temp2 = temps.AcquireSameSizeAs(array);
2549 // /* HeapReference<Class> */ temp = array->klass_
2550 __ Ldr(temp, HeapOperand(array, class_offset));
2551 codegen_->MaybeRecordImplicitNullCheck(instruction);
2552 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002553
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002554 // /* HeapReference<Class> */ temp = temp->component_type_
2555 __ Ldr(temp, HeapOperand(temp, component_offset));
2556 // /* HeapReference<Class> */ temp2 = value->klass_
2557 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2558 // If heap poisoning is enabled, no need to unpoison `temp`
2559 // nor `temp2`, as we are comparing two poisoned references.
2560 __ Cmp(temp, temp2);
2561 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002562
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002563 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2564 vixl::aarch64::Label do_put;
2565 __ B(eq, &do_put);
2566 // If heap poisoning is enabled, the `temp` reference has
2567 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002568 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2569
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002570 // /* HeapReference<Class> */ temp = temp->super_class_
2571 __ Ldr(temp, HeapOperand(temp, super_offset));
2572 // If heap poisoning is enabled, no need to unpoison
2573 // `temp`, as we are comparing against null below.
2574 __ Cbnz(temp, slow_path->GetEntryLabel());
2575 __ Bind(&do_put);
2576 } else {
2577 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002578 }
2579 }
2580
2581 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002582 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002583 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002584 __ Mov(temp2, value.W());
2585 GetAssembler()->PoisonHeapReference(temp2);
2586 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002587 }
2588
2589 if (!index.IsConstant()) {
2590 __ Add(temp, array, offset);
2591 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002592 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002593
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002594 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002595 codegen_->MaybeRecordImplicitNullCheck(instruction);
2596 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002597 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002598
2599 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2600
2601 if (done.IsLinked()) {
2602 __ Bind(&done);
2603 }
2604
2605 if (slow_path != nullptr) {
2606 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002607 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002608 }
2609}
2610
Alexandre Rames67555f72014-11-18 10:55:16 +00002611void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002612 RegisterSet caller_saves = RegisterSet::Empty();
2613 InvokeRuntimeCallingConvention calling_convention;
2614 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2615 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2616 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002617 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002618 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002619}
2620
2621void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002622 BoundsCheckSlowPathARM64* slow_path =
2623 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002624 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002625 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2626 __ B(slow_path->GetEntryLabel(), hs);
2627}
2628
Alexandre Rames67555f72014-11-18 10:55:16 +00002629void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2630 LocationSummary* locations =
2631 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2632 locations->SetInAt(0, Location::RequiresRegister());
2633 if (check->HasUses()) {
2634 locations->SetOut(Location::SameAsFirstInput());
2635 }
2636}
2637
2638void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2639 // We assume the class is not null.
2640 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2641 check->GetLoadClass(), check, check->GetDexPc(), true);
2642 codegen_->AddSlowPath(slow_path);
2643 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2644}
2645
Roland Levillain1a653882016-03-18 18:05:57 +00002646static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2647 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2648 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2649}
2650
2651void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2652 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2653 Location rhs_loc = instruction->GetLocations()->InAt(1);
2654 if (rhs_loc.IsConstant()) {
2655 // 0.0 is the only immediate that can be encoded directly in
2656 // an FCMP instruction.
2657 //
2658 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2659 // specify that in a floating-point comparison, positive zero
2660 // and negative zero are considered equal, so we can use the
2661 // literal 0.0 for both cases here.
2662 //
2663 // Note however that some methods (Float.equal, Float.compare,
2664 // Float.compareTo, Double.equal, Double.compare,
2665 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2666 // StrictMath.min) consider 0.0 to be (strictly) greater than
2667 // -0.0. So if we ever translate calls to these methods into a
2668 // HCompare instruction, we must handle the -0.0 case with
2669 // care here.
2670 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2671 __ Fcmp(lhs_reg, 0.0);
2672 } else {
2673 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2674 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002675}
2676
Serban Constantinescu02164b32014-11-13 14:05:07 +00002677void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002678 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002679 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2680 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002681 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002682 case Primitive::kPrimBoolean:
2683 case Primitive::kPrimByte:
2684 case Primitive::kPrimShort:
2685 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002686 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002687 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002688 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002689 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2691 break;
2692 }
2693 case Primitive::kPrimFloat:
2694 case Primitive::kPrimDouble: {
2695 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002696 locations->SetInAt(1,
2697 IsFloatingPointZeroConstant(compare->InputAt(1))
2698 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2699 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002700 locations->SetOut(Location::RequiresRegister());
2701 break;
2702 }
2703 default:
2704 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2705 }
2706}
2707
2708void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2709 Primitive::Type in_type = compare->InputAt(0)->GetType();
2710
2711 // 0 if: left == right
2712 // 1 if: left > right
2713 // -1 if: left < right
2714 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002715 case Primitive::kPrimBoolean:
2716 case Primitive::kPrimByte:
2717 case Primitive::kPrimShort:
2718 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002719 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002720 case Primitive::kPrimLong: {
2721 Register result = OutputRegister(compare);
2722 Register left = InputRegisterAt(compare, 0);
2723 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002724 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002725 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2726 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002727 break;
2728 }
2729 case Primitive::kPrimFloat:
2730 case Primitive::kPrimDouble: {
2731 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002732 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002733 __ Cset(result, ne);
2734 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002735 break;
2736 }
2737 default:
2738 LOG(FATAL) << "Unimplemented compare type " << in_type;
2739 }
2740}
2741
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002742void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002744
2745 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2746 locations->SetInAt(0, Location::RequiresFpuRegister());
2747 locations->SetInAt(1,
2748 IsFloatingPointZeroConstant(instruction->InputAt(1))
2749 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2750 : Location::RequiresFpuRegister());
2751 } else {
2752 // Integer cases.
2753 locations->SetInAt(0, Location::RequiresRegister());
2754 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2755 }
2756
David Brazdilb3e773e2016-01-26 11:28:37 +00002757 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002759 }
2760}
2761
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002762void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002763 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002764 return;
2765 }
2766
2767 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002768 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002769 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002770
Roland Levillain7f63c522015-07-13 15:54:55 +00002771 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002772 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002773 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002774 } else {
2775 // Integer cases.
2776 Register lhs = InputRegisterAt(instruction, 0);
2777 Operand rhs = InputOperandAt(instruction, 1);
2778 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002779 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002780 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002781}
2782
2783#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2784 M(Equal) \
2785 M(NotEqual) \
2786 M(LessThan) \
2787 M(LessThanOrEqual) \
2788 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002789 M(GreaterThanOrEqual) \
2790 M(Below) \
2791 M(BelowOrEqual) \
2792 M(Above) \
2793 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002794#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002795void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2796void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002797FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002798#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002799#undef FOR_EACH_CONDITION_INSTRUCTION
2800
Zheng Xuc6667102015-05-15 16:08:45 +08002801void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2802 DCHECK(instruction->IsDiv() || instruction->IsRem());
2803
2804 LocationSummary* locations = instruction->GetLocations();
2805 Location second = locations->InAt(1);
2806 DCHECK(second.IsConstant());
2807
2808 Register out = OutputRegister(instruction);
2809 Register dividend = InputRegisterAt(instruction, 0);
2810 int64_t imm = Int64FromConstant(second.GetConstant());
2811 DCHECK(imm == 1 || imm == -1);
2812
2813 if (instruction->IsRem()) {
2814 __ Mov(out, 0);
2815 } else {
2816 if (imm == 1) {
2817 __ Mov(out, dividend);
2818 } else {
2819 __ Neg(out, dividend);
2820 }
2821 }
2822}
2823
2824void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2825 DCHECK(instruction->IsDiv() || instruction->IsRem());
2826
2827 LocationSummary* locations = instruction->GetLocations();
2828 Location second = locations->InAt(1);
2829 DCHECK(second.IsConstant());
2830
2831 Register out = OutputRegister(instruction);
2832 Register dividend = InputRegisterAt(instruction, 0);
2833 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002834 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002835 int ctz_imm = CTZ(abs_imm);
2836
2837 UseScratchRegisterScope temps(GetVIXLAssembler());
2838 Register temp = temps.AcquireSameSizeAs(out);
2839
2840 if (instruction->IsDiv()) {
2841 __ Add(temp, dividend, abs_imm - 1);
2842 __ Cmp(dividend, 0);
2843 __ Csel(out, temp, dividend, lt);
2844 if (imm > 0) {
2845 __ Asr(out, out, ctz_imm);
2846 } else {
2847 __ Neg(out, Operand(out, ASR, ctz_imm));
2848 }
2849 } else {
2850 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2851 __ Asr(temp, dividend, bits - 1);
2852 __ Lsr(temp, temp, bits - ctz_imm);
2853 __ Add(out, dividend, temp);
2854 __ And(out, out, abs_imm - 1);
2855 __ Sub(out, out, temp);
2856 }
2857}
2858
2859void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2860 DCHECK(instruction->IsDiv() || instruction->IsRem());
2861
2862 LocationSummary* locations = instruction->GetLocations();
2863 Location second = locations->InAt(1);
2864 DCHECK(second.IsConstant());
2865
2866 Register out = OutputRegister(instruction);
2867 Register dividend = InputRegisterAt(instruction, 0);
2868 int64_t imm = Int64FromConstant(second.GetConstant());
2869
2870 Primitive::Type type = instruction->GetResultType();
2871 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2872
2873 int64_t magic;
2874 int shift;
2875 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2876
2877 UseScratchRegisterScope temps(GetVIXLAssembler());
2878 Register temp = temps.AcquireSameSizeAs(out);
2879
2880 // temp = get_high(dividend * magic)
2881 __ Mov(temp, magic);
2882 if (type == Primitive::kPrimLong) {
2883 __ Smulh(temp, dividend, temp);
2884 } else {
2885 __ Smull(temp.X(), dividend, temp);
2886 __ Lsr(temp.X(), temp.X(), 32);
2887 }
2888
2889 if (imm > 0 && magic < 0) {
2890 __ Add(temp, temp, dividend);
2891 } else if (imm < 0 && magic > 0) {
2892 __ Sub(temp, temp, dividend);
2893 }
2894
2895 if (shift != 0) {
2896 __ Asr(temp, temp, shift);
2897 }
2898
2899 if (instruction->IsDiv()) {
2900 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2901 } else {
2902 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2903 // TODO: Strength reduction for msub.
2904 Register temp_imm = temps.AcquireSameSizeAs(out);
2905 __ Mov(temp_imm, imm);
2906 __ Msub(out, temp, temp_imm, dividend);
2907 }
2908}
2909
2910void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2911 DCHECK(instruction->IsDiv() || instruction->IsRem());
2912 Primitive::Type type = instruction->GetResultType();
2913 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2914
2915 LocationSummary* locations = instruction->GetLocations();
2916 Register out = OutputRegister(instruction);
2917 Location second = locations->InAt(1);
2918
2919 if (second.IsConstant()) {
2920 int64_t imm = Int64FromConstant(second.GetConstant());
2921
2922 if (imm == 0) {
2923 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2924 } else if (imm == 1 || imm == -1) {
2925 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002926 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002927 DivRemByPowerOfTwo(instruction);
2928 } else {
2929 DCHECK(imm <= -2 || imm >= 2);
2930 GenerateDivRemWithAnyConstant(instruction);
2931 }
2932 } else {
2933 Register dividend = InputRegisterAt(instruction, 0);
2934 Register divisor = InputRegisterAt(instruction, 1);
2935 if (instruction->IsDiv()) {
2936 __ Sdiv(out, dividend, divisor);
2937 } else {
2938 UseScratchRegisterScope temps(GetVIXLAssembler());
2939 Register temp = temps.AcquireSameSizeAs(out);
2940 __ Sdiv(temp, dividend, divisor);
2941 __ Msub(out, temp, divisor, dividend);
2942 }
2943 }
2944}
2945
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002946void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2947 LocationSummary* locations =
2948 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2949 switch (div->GetResultType()) {
2950 case Primitive::kPrimInt:
2951 case Primitive::kPrimLong:
2952 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002953 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2955 break;
2956
2957 case Primitive::kPrimFloat:
2958 case Primitive::kPrimDouble:
2959 locations->SetInAt(0, Location::RequiresFpuRegister());
2960 locations->SetInAt(1, Location::RequiresFpuRegister());
2961 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2962 break;
2963
2964 default:
2965 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2966 }
2967}
2968
2969void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2970 Primitive::Type type = div->GetResultType();
2971 switch (type) {
2972 case Primitive::kPrimInt:
2973 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002974 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002975 break;
2976
2977 case Primitive::kPrimFloat:
2978 case Primitive::kPrimDouble:
2979 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2980 break;
2981
2982 default:
2983 LOG(FATAL) << "Unexpected div type " << type;
2984 }
2985}
2986
Alexandre Rames67555f72014-11-18 10:55:16 +00002987void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002988 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002989 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002990}
2991
2992void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2993 SlowPathCodeARM64* slow_path =
2994 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2995 codegen_->AddSlowPath(slow_path);
2996 Location value = instruction->GetLocations()->InAt(0);
2997
Alexandre Rames3e69f162014-12-10 10:36:50 +00002998 Primitive::Type type = instruction->GetType();
2999
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003000 if (!Primitive::IsIntegralType(type)) {
3001 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003002 return;
3003 }
3004
Alexandre Rames67555f72014-11-18 10:55:16 +00003005 if (value.IsConstant()) {
3006 int64_t divisor = Int64ConstantFrom(value);
3007 if (divisor == 0) {
3008 __ B(slow_path->GetEntryLabel());
3009 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003010 // A division by a non-null constant is valid. We don't need to perform
3011 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003012 }
3013 } else {
3014 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3015 }
3016}
3017
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003018void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3019 LocationSummary* locations =
3020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3021 locations->SetOut(Location::ConstantLocation(constant));
3022}
3023
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003024void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3025 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003026 // Will be generated at use site.
3027}
3028
Alexandre Rames5319def2014-10-23 10:03:10 +01003029void LocationsBuilderARM64::VisitExit(HExit* exit) {
3030 exit->SetLocations(nullptr);
3031}
3032
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003033void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003034}
3035
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003036void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3037 LocationSummary* locations =
3038 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3039 locations->SetOut(Location::ConstantLocation(constant));
3040}
3041
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003042void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003043 // Will be generated at use site.
3044}
3045
David Brazdilfc6a86a2015-06-26 10:33:45 +00003046void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003047 DCHECK(!successor->IsExitBlock());
3048 HBasicBlock* block = got->GetBlock();
3049 HInstruction* previous = got->GetPrevious();
3050 HLoopInformation* info = block->GetLoopInformation();
3051
David Brazdil46e2a392015-03-16 17:31:52 +00003052 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003053 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3054 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3055 return;
3056 }
3057 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3058 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3059 }
3060 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003061 __ B(codegen_->GetLabelOf(successor));
3062 }
3063}
3064
David Brazdilfc6a86a2015-06-26 10:33:45 +00003065void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3066 got->SetLocations(nullptr);
3067}
3068
3069void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3070 HandleGoto(got, got->GetSuccessor());
3071}
3072
3073void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3074 try_boundary->SetLocations(nullptr);
3075}
3076
3077void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3078 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3079 if (!successor->IsExitBlock()) {
3080 HandleGoto(try_boundary, successor);
3081 }
3082}
3083
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003084void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003085 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003086 vixl::aarch64::Label* true_target,
3087 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003088 // FP branching requires both targets to be explicit. If either of the targets
3089 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003090 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003091 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003092
David Brazdil0debae72015-11-12 18:37:00 +00003093 if (true_target == nullptr && false_target == nullptr) {
3094 // Nothing to do. The code always falls through.
3095 return;
3096 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003097 // Constant condition, statically compared against "true" (integer value 1).
3098 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003099 if (true_target != nullptr) {
3100 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003101 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003102 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003103 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003104 if (false_target != nullptr) {
3105 __ B(false_target);
3106 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003107 }
David Brazdil0debae72015-11-12 18:37:00 +00003108 return;
3109 }
3110
3111 // The following code generates these patterns:
3112 // (1) true_target == nullptr && false_target != nullptr
3113 // - opposite condition true => branch to false_target
3114 // (2) true_target != nullptr && false_target == nullptr
3115 // - condition true => branch to true_target
3116 // (3) true_target != nullptr && false_target != nullptr
3117 // - condition true => branch to true_target
3118 // - branch to false_target
3119 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003120 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003121 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003122 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003123 if (true_target == nullptr) {
3124 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3125 } else {
3126 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3127 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003128 } else {
3129 // The condition instruction has not been materialized, use its inputs as
3130 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003131 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003132
David Brazdil0debae72015-11-12 18:37:00 +00003133 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003134 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003135 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003136 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003137 IfCondition opposite_condition = condition->GetOppositeCondition();
3138 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003139 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003140 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003141 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003142 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003143 // Integer cases.
3144 Register lhs = InputRegisterAt(condition, 0);
3145 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003146
3147 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003148 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003149 if (true_target == nullptr) {
3150 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3151 non_fallthrough_target = false_target;
3152 } else {
3153 arm64_cond = ARM64Condition(condition->GetCondition());
3154 non_fallthrough_target = true_target;
3155 }
3156
Aart Bik086d27e2016-01-20 17:02:00 -08003157 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003158 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003159 switch (arm64_cond) {
3160 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003161 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003162 break;
3163 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003164 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003165 break;
3166 case lt:
3167 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003168 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003169 break;
3170 case ge:
3171 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003172 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003173 break;
3174 default:
3175 // Without the `static_cast` the compiler throws an error for
3176 // `-Werror=sign-promo`.
3177 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3178 }
3179 } else {
3180 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003181 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003182 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003183 }
3184 }
David Brazdil0debae72015-11-12 18:37:00 +00003185
3186 // If neither branch falls through (case 3), the conditional branch to `true_target`
3187 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3188 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003189 __ B(false_target);
3190 }
David Brazdil0debae72015-11-12 18:37:00 +00003191
3192 if (fallthrough_target.IsLinked()) {
3193 __ Bind(&fallthrough_target);
3194 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003195}
3196
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003197void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003199 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003200 locations->SetInAt(0, Location::RequiresRegister());
3201 }
3202}
3203
3204void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003205 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3206 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003207 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3208 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3209 true_target = nullptr;
3210 }
3211 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3212 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3213 false_target = nullptr;
3214 }
David Brazdil0debae72015-11-12 18:37:00 +00003215 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003216}
3217
3218void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3219 LocationSummary* locations = new (GetGraph()->GetArena())
3220 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003221 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003222 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003223 locations->SetInAt(0, Location::RequiresRegister());
3224 }
3225}
3226
3227void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003228 SlowPathCodeARM64* slow_path =
3229 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003230 GenerateTestAndBranch(deoptimize,
3231 /* condition_input_index */ 0,
3232 slow_path->GetEntryLabel(),
3233 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003234}
3235
David Brazdilc0b601b2016-02-08 14:20:45 +00003236static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3237 return condition->IsCondition() &&
3238 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3239}
3240
Alexandre Rames880f1192016-06-13 16:04:50 +01003241static inline Condition GetConditionForSelect(HCondition* condition) {
3242 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003243 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3244 : ARM64Condition(cond);
3245}
3246
David Brazdil74eb1b22015-12-14 11:44:01 +00003247void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3248 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003249 if (Primitive::IsFloatingPointType(select->GetType())) {
3250 locations->SetInAt(0, Location::RequiresFpuRegister());
3251 locations->SetInAt(1, Location::RequiresFpuRegister());
3252 locations->SetOut(Location::RequiresFpuRegister());
3253 } else {
3254 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3255 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3256 bool is_true_value_constant = cst_true_value != nullptr;
3257 bool is_false_value_constant = cst_false_value != nullptr;
3258 // Ask VIXL whether we should synthesize constants in registers.
3259 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3260 Operand true_op = is_true_value_constant ?
3261 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3262 Operand false_op = is_false_value_constant ?
3263 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3264 bool true_value_in_register = false;
3265 bool false_value_in_register = false;
3266 MacroAssembler::GetCselSynthesisInformation(
3267 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3268 true_value_in_register |= !is_true_value_constant;
3269 false_value_in_register |= !is_false_value_constant;
3270
3271 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3272 : Location::ConstantLocation(cst_true_value));
3273 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3274 : Location::ConstantLocation(cst_false_value));
3275 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003276 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003277
David Brazdil74eb1b22015-12-14 11:44:01 +00003278 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3279 locations->SetInAt(2, Location::RequiresRegister());
3280 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003281}
3282
3283void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003284 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003285 Condition csel_cond;
3286
3287 if (IsBooleanValueOrMaterializedCondition(cond)) {
3288 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003289 // Use the condition flags set by the previous instruction.
3290 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003291 } else {
3292 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003293 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003294 }
3295 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003296 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003297 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003298 } else {
3299 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003300 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003301 }
3302
Alexandre Rames880f1192016-06-13 16:04:50 +01003303 if (Primitive::IsFloatingPointType(select->GetType())) {
3304 __ Fcsel(OutputFPRegister(select),
3305 InputFPRegisterAt(select, 1),
3306 InputFPRegisterAt(select, 0),
3307 csel_cond);
3308 } else {
3309 __ Csel(OutputRegister(select),
3310 InputOperandAt(select, 1),
3311 InputOperandAt(select, 0),
3312 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003313 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003314}
3315
David Srbecky0cf44932015-12-09 14:09:59 +00003316void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3317 new (GetGraph()->GetArena()) LocationSummary(info);
3318}
3319
David Srbeckyd28f4a02016-03-14 17:14:24 +00003320void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3321 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003322}
3323
3324void CodeGeneratorARM64::GenerateNop() {
3325 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003326}
3327
Alexandre Rames5319def2014-10-23 10:03:10 +01003328void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003329 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003330}
3331
3332void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003333 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003334}
3335
3336void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003337 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003338}
3339
3340void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003341 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003342}
3343
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003344// Temp is used for read barrier.
3345static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3346 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003347 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003348 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3349 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3350 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3351 return 1;
3352 }
3353 return 0;
3354}
3355
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003356// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003357// interface pointer, one for loading the current interface.
3358// The other checks have one temp for loading the object's class.
3359static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3360 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3361 return 3;
3362 }
3363 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003364}
3365
Alexandre Rames67555f72014-11-18 10:55:16 +00003366void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003367 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003369 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003370 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003371 case TypeCheckKind::kExactCheck:
3372 case TypeCheckKind::kAbstractClassCheck:
3373 case TypeCheckKind::kClassHierarchyCheck:
3374 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003375 call_kind =
3376 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003377 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003378 break;
3379 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003380 case TypeCheckKind::kUnresolvedCheck:
3381 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003382 call_kind = LocationSummary::kCallOnSlowPath;
3383 break;
3384 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003385
Alexandre Rames67555f72014-11-18 10:55:16 +00003386 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003387 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003388 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003389 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003390 locations->SetInAt(0, Location::RequiresRegister());
3391 locations->SetInAt(1, Location::RequiresRegister());
3392 // The "out" register is used as a temporary, so it overlaps with the inputs.
3393 // Note that TypeCheckSlowPathARM64 uses this register too.
3394 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003395 // Add temps if necessary for read barriers.
3396 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003397}
3398
3399void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003400 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003401 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003402 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003403 Register obj = InputRegisterAt(instruction, 0);
3404 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003405 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003406 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003407 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3408 DCHECK_LE(num_temps, 1u);
3409 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003410 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3411 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3412 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3413 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003414
Scott Wakeling97c72b72016-06-24 16:19:36 +01003415 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003416 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003417
3418 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003419 // Avoid null check if we know `obj` is not null.
3420 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003421 __ Cbz(obj, &zero);
3422 }
3423
Roland Levillain44015862016-01-22 11:47:17 +00003424 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003425 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003426 // /* HeapReference<Class> */ out = obj->klass_
3427 GenerateReferenceLoadTwoRegisters(instruction,
3428 out_loc,
3429 obj_loc,
3430 class_offset,
3431 maybe_temp_loc,
3432 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003433 __ Cmp(out, cls);
3434 __ Cset(out, eq);
3435 if (zero.IsLinked()) {
3436 __ B(&done);
3437 }
3438 break;
3439 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003440
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003441 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003442 // /* HeapReference<Class> */ out = obj->klass_
3443 GenerateReferenceLoadTwoRegisters(instruction,
3444 out_loc,
3445 obj_loc,
3446 class_offset,
3447 maybe_temp_loc,
3448 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003449 // If the class is abstract, we eagerly fetch the super class of the
3450 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003451 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003452 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003454 GenerateReferenceLoadOneRegister(instruction,
3455 out_loc,
3456 super_offset,
3457 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003458 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 // If `out` is null, we use it for the result, and jump to `done`.
3460 __ Cbz(out, &done);
3461 __ Cmp(out, cls);
3462 __ B(ne, &loop);
3463 __ Mov(out, 1);
3464 if (zero.IsLinked()) {
3465 __ B(&done);
3466 }
3467 break;
3468 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003470 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003471 // /* HeapReference<Class> */ out = obj->klass_
3472 GenerateReferenceLoadTwoRegisters(instruction,
3473 out_loc,
3474 obj_loc,
3475 class_offset,
3476 maybe_temp_loc,
3477 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003478 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003479 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003480 __ Bind(&loop);
3481 __ Cmp(out, cls);
3482 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003483 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003484 GenerateReferenceLoadOneRegister(instruction,
3485 out_loc,
3486 super_offset,
3487 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003488 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003489 __ Cbnz(out, &loop);
3490 // If `out` is null, we use it for the result, and jump to `done`.
3491 __ B(&done);
3492 __ Bind(&success);
3493 __ Mov(out, 1);
3494 if (zero.IsLinked()) {
3495 __ B(&done);
3496 }
3497 break;
3498 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003500 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003501 // /* HeapReference<Class> */ out = obj->klass_
3502 GenerateReferenceLoadTwoRegisters(instruction,
3503 out_loc,
3504 obj_loc,
3505 class_offset,
3506 maybe_temp_loc,
3507 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003508 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003509 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003510 __ Cmp(out, cls);
3511 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003512 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003513 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003514 GenerateReferenceLoadOneRegister(instruction,
3515 out_loc,
3516 component_offset,
3517 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003518 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003519 // If `out` is null, we use it for the result, and jump to `done`.
3520 __ Cbz(out, &done);
3521 __ Ldrh(out, HeapOperand(out, primitive_offset));
3522 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3523 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003524 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003525 __ Mov(out, 1);
3526 __ B(&done);
3527 break;
3528 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003529
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003530 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003531 // No read barrier since the slow path will retry upon failure.
3532 // /* HeapReference<Class> */ out = obj->klass_
3533 GenerateReferenceLoadTwoRegisters(instruction,
3534 out_loc,
3535 obj_loc,
3536 class_offset,
3537 maybe_temp_loc,
3538 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003539 __ Cmp(out, cls);
3540 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003541 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3542 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003543 codegen_->AddSlowPath(slow_path);
3544 __ B(ne, slow_path->GetEntryLabel());
3545 __ Mov(out, 1);
3546 if (zero.IsLinked()) {
3547 __ B(&done);
3548 }
3549 break;
3550 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003551
Calin Juravle98893e12015-10-02 21:05:03 +01003552 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003553 case TypeCheckKind::kInterfaceCheck: {
3554 // Note that we indeed only call on slow path, but we always go
3555 // into the slow path for the unresolved and interface check
3556 // cases.
3557 //
3558 // We cannot directly call the InstanceofNonTrivial runtime
3559 // entry point without resorting to a type checking slow path
3560 // here (i.e. by calling InvokeRuntime directly), as it would
3561 // require to assign fixed registers for the inputs of this
3562 // HInstanceOf instruction (following the runtime calling
3563 // convention), which might be cluttered by the potential first
3564 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003565 //
3566 // TODO: Introduce a new runtime entry point taking the object
3567 // to test (instead of its class) as argument, and let it deal
3568 // with the read barrier issues. This will let us refactor this
3569 // case of the `switch` code as it was previously (with a direct
3570 // call to the runtime not using a type checking slow path).
3571 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003572 DCHECK(locations->OnlyCallsOnSlowPath());
3573 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3574 /* is_fatal */ false);
3575 codegen_->AddSlowPath(slow_path);
3576 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003577 if (zero.IsLinked()) {
3578 __ B(&done);
3579 }
3580 break;
3581 }
3582 }
3583
3584 if (zero.IsLinked()) {
3585 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003586 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003587 }
3588
3589 if (done.IsLinked()) {
3590 __ Bind(&done);
3591 }
3592
3593 if (slow_path != nullptr) {
3594 __ Bind(slow_path->GetExitLabel());
3595 }
3596}
3597
3598void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3599 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3600 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3601
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003602 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3603 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003604 case TypeCheckKind::kExactCheck:
3605 case TypeCheckKind::kAbstractClassCheck:
3606 case TypeCheckKind::kClassHierarchyCheck:
3607 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003608 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3609 LocationSummary::kCallOnSlowPath :
3610 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003611 break;
3612 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003613 case TypeCheckKind::kUnresolvedCheck:
3614 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003615 call_kind = LocationSummary::kCallOnSlowPath;
3616 break;
3617 }
3618
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3620 locations->SetInAt(0, Location::RequiresRegister());
3621 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003622 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3623 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003624}
3625
3626void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003627 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003628 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003629 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003630 Register obj = InputRegisterAt(instruction, 0);
3631 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003632 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3633 DCHECK_GE(num_temps, 1u);
3634 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003635 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003636 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3637 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003638 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003639 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3640 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3641 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3642 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3643 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3644 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3645 const uint32_t object_array_data_offset =
3646 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003647
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003648 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003649 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3650 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3651 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003652 if (!kEmitCompilerReadBarrier) {
3653 is_type_check_slow_path_fatal =
3654 (type_check_kind == TypeCheckKind::kExactCheck ||
3655 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3656 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3657 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3658 !instruction->CanThrowIntoCatchBlock();
3659 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003660 SlowPathCodeARM64* type_check_slow_path =
3661 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3662 is_type_check_slow_path_fatal);
3663 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003664
Scott Wakeling97c72b72016-06-24 16:19:36 +01003665 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003666 // Avoid null check if we know obj is not null.
3667 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003668 __ Cbz(obj, &done);
3669 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003670
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003671 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003672 case TypeCheckKind::kExactCheck:
3673 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003674 // /* HeapReference<Class> */ temp = obj->klass_
3675 GenerateReferenceLoadTwoRegisters(instruction,
3676 temp_loc,
3677 obj_loc,
3678 class_offset,
3679 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003680 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003681
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003682 __ Cmp(temp, cls);
3683 // Jump to slow path for throwing the exception or doing a
3684 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003685 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003686 break;
3687 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003688
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003689 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003690 // /* HeapReference<Class> */ temp = obj->klass_
3691 GenerateReferenceLoadTwoRegisters(instruction,
3692 temp_loc,
3693 obj_loc,
3694 class_offset,
3695 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003696 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003697
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003698 // If the class is abstract, we eagerly fetch the super class of the
3699 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003700 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003701 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003702 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003703 GenerateReferenceLoadOneRegister(instruction,
3704 temp_loc,
3705 super_offset,
3706 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003707 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003708
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003709 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3710 // exception.
3711 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3712 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003713 __ Cmp(temp, cls);
3714 __ B(ne, &loop);
3715 break;
3716 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003717
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003718 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003719 // /* HeapReference<Class> */ temp = obj->klass_
3720 GenerateReferenceLoadTwoRegisters(instruction,
3721 temp_loc,
3722 obj_loc,
3723 class_offset,
3724 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003725 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003726
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003727 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003728 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003729 __ Bind(&loop);
3730 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003731 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003732
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003733 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003734 GenerateReferenceLoadOneRegister(instruction,
3735 temp_loc,
3736 super_offset,
3737 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003738 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003739
3740 // If the class reference currently in `temp` is not null, jump
3741 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003742 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003743 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003744 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003745 break;
3746 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003747
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003748 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003749 // /* HeapReference<Class> */ temp = obj->klass_
3750 GenerateReferenceLoadTwoRegisters(instruction,
3751 temp_loc,
3752 obj_loc,
3753 class_offset,
3754 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003755 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003756
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003757 // Do an exact check.
3758 __ Cmp(temp, cls);
3759 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003760
3761 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003762 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003763 GenerateReferenceLoadOneRegister(instruction,
3764 temp_loc,
3765 component_offset,
3766 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003767 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003768
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003769 // If the component type is null, jump to the slow path to throw the exception.
3770 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3771 // Otherwise, the object is indeed an array. Further check that this component type is not a
3772 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003773 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3774 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003775 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003776 break;
3777 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003778
Calin Juravle98893e12015-10-02 21:05:03 +01003779 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003780 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003781 //
3782 // We cannot directly call the CheckCast runtime entry point
3783 // without resorting to a type checking slow path here (i.e. by
3784 // calling InvokeRuntime directly), as it would require to
3785 // assign fixed registers for the inputs of this HInstanceOf
3786 // instruction (following the runtime calling convention), which
3787 // might be cluttered by the potential first read barrier
3788 // emission at the beginning of this method.
3789 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003790 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003791 case TypeCheckKind::kInterfaceCheck: {
3792 // /* HeapReference<Class> */ temp = obj->klass_
3793 GenerateReferenceLoadTwoRegisters(instruction,
3794 temp_loc,
3795 obj_loc,
3796 class_offset,
3797 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003798 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003799
3800 // /* HeapReference<Class> */ temp = temp->iftable_
3801 GenerateReferenceLoadTwoRegisters(instruction,
3802 temp_loc,
3803 temp_loc,
3804 iftable_offset,
3805 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003806 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003807 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003808 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003809 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003810 vixl::aarch64::Label start_loop;
3811 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003812 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003813 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3814 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003815 // Go to next interface.
3816 __ Add(temp, temp, 2 * kHeapReferenceSize);
3817 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003818 // Compare the classes and continue the loop if they do not match.
3819 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3820 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003821 break;
3822 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003823 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003824 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003825
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003826 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003827}
3828
Alexandre Rames5319def2014-10-23 10:03:10 +01003829void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3830 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3831 locations->SetOut(Location::ConstantLocation(constant));
3832}
3833
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003834void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003835 // Will be generated at use site.
3836}
3837
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003838void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3839 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3840 locations->SetOut(Location::ConstantLocation(constant));
3841}
3842
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003843void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003844 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003845}
3846
Calin Juravle175dc732015-08-25 15:42:32 +01003847void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3848 // The trampoline uses the same calling convention as dex calling conventions,
3849 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3850 // the method_idx.
3851 HandleInvoke(invoke);
3852}
3853
3854void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3855 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3856}
3857
Alexandre Rames5319def2014-10-23 10:03:10 +01003858void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003859 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003860 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003861}
3862
Alexandre Rames67555f72014-11-18 10:55:16 +00003863void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3864 HandleInvoke(invoke);
3865}
3866
3867void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3868 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 LocationSummary* locations = invoke->GetLocations();
3870 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003871 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003872 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003873 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003874
3875 // The register ip1 is required to be used for the hidden argument in
3876 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003877 MacroAssembler* masm = GetVIXLAssembler();
3878 UseScratchRegisterScope scratch_scope(masm);
3879 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003880 scratch_scope.Exclude(ip1);
3881 __ Mov(ip1, invoke->GetDexMethodIndex());
3882
Alexandre Rames67555f72014-11-18 10:55:16 +00003883 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003884 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003885 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003886 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003887 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003889 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003890 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003891 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003892 // Instead of simply (possibly) unpoisoning `temp` here, we should
3893 // emit a read barrier for the previous class reference load.
3894 // However this is not required in practice, as this is an
3895 // intermediate/temporary reference and because the current
3896 // concurrent copying collector keeps the from-space memory
3897 // intact/accessible until the end of the marking phase (the
3898 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003899 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003900 __ Ldr(temp,
3901 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3902 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003903 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003904 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003905 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003906 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003907 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003908 // lr();
3909 __ Blr(lr);
3910 DCHECK(!codegen_->IsLeafMethod());
3911 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3912}
3913
3914void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003915 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3916 if (intrinsic.TryDispatch(invoke)) {
3917 return;
3918 }
3919
Alexandre Rames67555f72014-11-18 10:55:16 +00003920 HandleInvoke(invoke);
3921}
3922
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003923void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003924 // Explicit clinit checks triggered by static invokes must have been pruned by
3925 // art::PrepareForRegisterAllocation.
3926 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003927
Andreas Gampe878d58c2015-01-15 23:24:00 -08003928 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3929 if (intrinsic.TryDispatch(invoke)) {
3930 return;
3931 }
3932
Alexandre Rames67555f72014-11-18 10:55:16 +00003933 HandleInvoke(invoke);
3934}
3935
Andreas Gampe878d58c2015-01-15 23:24:00 -08003936static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3937 if (invoke->GetLocations()->Intrinsified()) {
3938 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3939 intrinsic.Dispatch(invoke);
3940 return true;
3941 }
3942 return false;
3943}
3944
Vladimir Markodc151b22015-10-15 18:02:30 +01003945HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3946 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003947 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003948 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003949 return desired_dispatch_info;
3950}
3951
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003952void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003953 // For better instruction scheduling we load the direct code pointer before the method pointer.
3954 bool direct_code_loaded = false;
3955 switch (invoke->GetCodePtrLocation()) {
3956 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3957 // LR = code address from literal pool with link-time patch.
3958 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3959 direct_code_loaded = true;
3960 break;
3961 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3962 // LR = invoke->GetDirectCodePtr();
3963 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3964 direct_code_loaded = true;
3965 break;
3966 default:
3967 break;
3968 }
3969
Andreas Gampe878d58c2015-01-15 23:24:00 -08003970 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003971 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3972 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003973 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3974 uint32_t offset =
3975 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003976 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003977 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003978 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003979 }
Vladimir Marko58155012015-08-19 12:49:41 +00003980 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003981 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003982 break;
3983 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3984 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003985 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003986 break;
3987 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3988 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003989 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003990 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3991 break;
3992 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3993 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003994 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003995 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003996 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003997 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003998 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003999 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004000 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004001 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004002 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004003 }
Vladimir Marko58155012015-08-19 12:49:41 +00004004 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004005 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004006 Register reg = XRegisterFrom(temp);
4007 Register method_reg;
4008 if (current_method.IsRegister()) {
4009 method_reg = XRegisterFrom(current_method);
4010 } else {
4011 DCHECK(invoke->GetLocations()->Intrinsified());
4012 DCHECK(!current_method.IsValid());
4013 method_reg = reg;
4014 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4015 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004016
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004017 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004018 __ Ldr(reg.X(),
4019 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004020 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004021 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004022 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4023 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004024 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4025 break;
4026 }
4027 }
4028
4029 switch (invoke->GetCodePtrLocation()) {
4030 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4031 __ Bl(&frame_entry_label_);
4032 break;
4033 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004034 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4035 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01004036 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
4037 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00004038 __ Bind(label);
4039 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00004040 break;
4041 }
4042 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4043 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4044 // LR prepared above for better instruction scheduling.
4045 DCHECK(direct_code_loaded);
4046 // lr()
4047 __ Blr(lr);
4048 break;
4049 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4050 // LR = callee_method->entry_point_from_quick_compiled_code_;
4051 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004052 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004053 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004054 // lr()
4055 __ Blr(lr);
4056 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004057 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004058
Andreas Gampe878d58c2015-01-15 23:24:00 -08004059 DCHECK(!IsLeafMethod());
4060}
4061
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004062void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004063 // Use the calling convention instead of the location of the receiver, as
4064 // intrinsics may have put the receiver in a different register. In the intrinsics
4065 // slow path, the arguments have been moved to the right place, so here we are
4066 // guaranteed that the receiver is the first register of the calling convention.
4067 InvokeDexCallingConvention calling_convention;
4068 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004069 Register temp = XRegisterFrom(temp_in);
4070 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4071 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4072 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004073 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004074
4075 BlockPoolsScope block_pools(GetVIXLAssembler());
4076
4077 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004078 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004079 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004080 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004081 // Instead of simply (possibly) unpoisoning `temp` here, we should
4082 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004083 // intermediate/temporary reference and because the current
4084 // concurrent copying collector keeps the from-space memory
4085 // intact/accessible until the end of the marking phase (the
4086 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004087 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4088 // temp = temp->GetMethodAt(method_offset);
4089 __ Ldr(temp, MemOperand(temp, method_offset));
4090 // lr = temp->GetEntryPoint();
4091 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4092 // lr();
4093 __ Blr(lr);
4094}
4095
Scott Wakeling97c72b72016-06-24 16:19:36 +01004096vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4097 const DexFile& dex_file,
4098 uint32_t string_index,
4099 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004100 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
4101}
4102
Scott Wakeling97c72b72016-06-24 16:19:36 +01004103vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4104 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004105 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004106 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004107 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004108}
4109
Scott Wakeling97c72b72016-06-24 16:19:36 +01004110vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4111 const DexFile& dex_file,
4112 uint32_t element_offset,
4113 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004114 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4115}
4116
Scott Wakeling97c72b72016-06-24 16:19:36 +01004117vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4118 const DexFile& dex_file,
4119 uint32_t offset_or_index,
4120 vixl::aarch64::Label* adrp_label,
4121 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004122 // Add a patch entry and return the label.
4123 patches->emplace_back(dex_file, offset_or_index);
4124 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004125 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004126 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4127 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4128 return label;
4129}
4130
Scott Wakeling97c72b72016-06-24 16:19:36 +01004131vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004132 const DexFile& dex_file, uint32_t string_index) {
4133 return boot_image_string_patches_.GetOrCreate(
4134 StringReference(&dex_file, string_index),
4135 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4136}
4137
Scott Wakeling97c72b72016-06-24 16:19:36 +01004138vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004139 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004140 return boot_image_type_patches_.GetOrCreate(
4141 TypeReference(&dex_file, type_index),
4142 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4143}
4144
Scott Wakeling97c72b72016-06-24 16:19:36 +01004145vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4146 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004147 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4148 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4149 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4150}
4151
Scott Wakeling97c72b72016-06-24 16:19:36 +01004152vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4153 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004154 return DeduplicateUint64Literal(address);
4155}
4156
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004157vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
4158 const DexFile& dex_file, uint32_t string_index) {
4159 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index), /* placeholder */ 0u);
4160 return jit_string_patches_.GetOrCreate(
4161 StringReference(&dex_file, string_index),
4162 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4163}
4164
Vladimir Markoaad75c62016-10-03 08:46:48 +00004165void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4166 vixl::aarch64::Register reg) {
4167 DCHECK(reg.IsX());
4168 SingleEmissionCheckScope guard(GetVIXLAssembler());
4169 __ Bind(fixup_label);
4170 __ adrp(reg, /* offset placeholder */ 0);
4171}
4172
4173void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4174 vixl::aarch64::Register out,
4175 vixl::aarch64::Register base) {
4176 DCHECK(out.IsX());
4177 DCHECK(base.IsX());
4178 SingleEmissionCheckScope guard(GetVIXLAssembler());
4179 __ Bind(fixup_label);
4180 __ add(out, base, Operand(/* offset placeholder */ 0));
4181}
4182
4183void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4184 vixl::aarch64::Register out,
4185 vixl::aarch64::Register base) {
4186 DCHECK(base.IsX());
4187 SingleEmissionCheckScope guard(GetVIXLAssembler());
4188 __ Bind(fixup_label);
4189 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4190}
4191
4192template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4193inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4194 const ArenaDeque<PcRelativePatchInfo>& infos,
4195 ArenaVector<LinkerPatch>* linker_patches) {
4196 for (const PcRelativePatchInfo& info : infos) {
4197 linker_patches->push_back(Factory(info.label.GetLocation(),
4198 &info.target_dex_file,
4199 info.pc_insn_label->GetLocation(),
4200 info.offset_or_index));
4201 }
4202}
4203
Vladimir Marko58155012015-08-19 12:49:41 +00004204void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4205 DCHECK(linker_patches->empty());
4206 size_t size =
4207 method_patches_.size() +
4208 call_patches_.size() +
4209 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004210 pc_relative_dex_cache_patches_.size() +
4211 boot_image_string_patches_.size() +
4212 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004213 boot_image_type_patches_.size() +
4214 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004215 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004216 linker_patches->reserve(size);
4217 for (const auto& entry : method_patches_) {
4218 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004219 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4220 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004221 target_method.dex_file,
4222 target_method.dex_method_index));
4223 }
4224 for (const auto& entry : call_patches_) {
4225 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004226 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4227 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004228 target_method.dex_file,
4229 target_method.dex_method_index));
4230 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004231 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4232 linker_patches->push_back(
4233 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004234 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004235 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004236 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004237 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004238 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004239 info.offset_or_index));
4240 }
4241 for (const auto& entry : boot_image_string_patches_) {
4242 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004243 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4244 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004245 target_string.dex_file,
4246 target_string.string_index));
4247 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004248 if (!GetCompilerOptions().IsBootImage()) {
4249 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4250 linker_patches);
4251 } else {
4252 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4253 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004254 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004255 for (const auto& entry : boot_image_type_patches_) {
4256 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004257 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4258 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004259 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004260 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004261 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004262 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4263 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004264 for (const auto& entry : boot_image_address_patches_) {
4265 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004266 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4267 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004268 }
4269}
4270
Scott Wakeling97c72b72016-06-24 16:19:36 +01004271vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004272 Uint32ToLiteralMap* map) {
4273 return map->GetOrCreate(
4274 value,
4275 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4276}
4277
Scott Wakeling97c72b72016-06-24 16:19:36 +01004278vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004279 return uint64_literals_.GetOrCreate(
4280 value,
4281 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004282}
4283
Scott Wakeling97c72b72016-06-24 16:19:36 +01004284vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004285 MethodReference target_method,
4286 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004287 return map->GetOrCreate(
4288 target_method,
4289 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004290}
4291
Scott Wakeling97c72b72016-06-24 16:19:36 +01004292vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004293 MethodReference target_method) {
4294 return DeduplicateMethodLiteral(target_method, &method_patches_);
4295}
4296
Scott Wakeling97c72b72016-06-24 16:19:36 +01004297vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004298 MethodReference target_method) {
4299 return DeduplicateMethodLiteral(target_method, &call_patches_);
4300}
4301
4302
Andreas Gampe878d58c2015-01-15 23:24:00 -08004303void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004304 // Explicit clinit checks triggered by static invokes must have been pruned by
4305 // art::PrepareForRegisterAllocation.
4306 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004307
Andreas Gampe878d58c2015-01-15 23:24:00 -08004308 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4309 return;
4310 }
4311
Alexandre Ramesd921d642015-04-16 15:07:16 +01004312 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004313 LocationSummary* locations = invoke->GetLocations();
4314 codegen_->GenerateStaticOrDirectCall(
4315 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004316 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004317}
4318
4319void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004320 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4321 return;
4322 }
4323
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004324 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004325 DCHECK(!codegen_->IsLeafMethod());
4326 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4327}
4328
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004329HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4330 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004331 switch (desired_class_load_kind) {
4332 case HLoadClass::LoadKind::kReferrersClass:
4333 break;
4334 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4335 DCHECK(!GetCompilerOptions().GetCompilePic());
4336 break;
4337 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4338 DCHECK(GetCompilerOptions().GetCompilePic());
4339 break;
4340 case HLoadClass::LoadKind::kBootImageAddress:
4341 break;
4342 case HLoadClass::LoadKind::kDexCacheAddress:
4343 DCHECK(Runtime::Current()->UseJitCompilation());
4344 break;
4345 case HLoadClass::LoadKind::kDexCachePcRelative:
4346 DCHECK(!Runtime::Current()->UseJitCompilation());
4347 break;
4348 case HLoadClass::LoadKind::kDexCacheViaMethod:
4349 break;
4350 }
4351 return desired_class_load_kind;
4352}
4353
Alexandre Rames67555f72014-11-18 10:55:16 +00004354void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004355 if (cls->NeedsAccessCheck()) {
4356 InvokeRuntimeCallingConvention calling_convention;
4357 CodeGenerator::CreateLoadClassLocationSummary(
4358 cls,
4359 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004360 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004361 /* code_generator_supports_read_barrier */ true);
4362 return;
4363 }
4364
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004365 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4366 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004367 ? LocationSummary::kCallOnSlowPath
4368 : LocationSummary::kNoCall;
4369 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004370 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004371 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004372 }
4373
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004374 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4375 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4376 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4377 locations->SetInAt(0, Location::RequiresRegister());
4378 }
4379 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004380}
4381
4382void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004383 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004384 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004385 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004386 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004387 return;
4388 }
4389
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004390 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004391 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004392
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004393 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4394 ? kWithoutReadBarrier
4395 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004396 bool generate_null_check = false;
4397 switch (cls->GetLoadKind()) {
4398 case HLoadClass::LoadKind::kReferrersClass: {
4399 DCHECK(!cls->CanCallRuntime());
4400 DCHECK(!cls->MustGenerateClinitCheck());
4401 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4402 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004403 GenerateGcRootFieldLoad(cls,
4404 out_loc,
4405 current_method,
4406 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004407 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004408 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004409 break;
4410 }
4411 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004412 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004413 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4414 cls->GetTypeIndex()));
4415 break;
4416 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004417 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004418 // Add ADRP with its PC-relative type patch.
4419 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004420 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004421 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004422 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004423 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004424 vixl::aarch64::Label* add_label =
4425 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004426 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004427 break;
4428 }
4429 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004430 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004431 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4432 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4433 break;
4434 }
4435 case HLoadClass::LoadKind::kDexCacheAddress: {
4436 DCHECK_NE(cls->GetAddress(), 0u);
4437 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4438 // that gives a 16KiB range. To try and reduce the number of literals if we load
4439 // multiple types, simply split the dex cache address to a 16KiB aligned base
4440 // loaded from a literal and the remaining offset embedded in the load.
4441 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4442 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4443 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4444 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4445 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4446 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4447 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004448 GenerateGcRootFieldLoad(cls,
4449 out_loc,
4450 out.X(),
4451 offset,
Roland Levillain00468f32016-10-27 18:02:48 +01004452 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004453 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004454 generate_null_check = !cls->IsInDexCache();
4455 break;
4456 }
4457 case HLoadClass::LoadKind::kDexCachePcRelative: {
4458 // Add ADRP with its PC-relative DexCache access patch.
4459 const DexFile& dex_file = cls->GetDexFile();
4460 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004461 vixl::aarch64::Label* adrp_label =
4462 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004463 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004464 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004465 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004466 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4467 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004468 GenerateGcRootFieldLoad(cls,
4469 out_loc,
4470 out.X(),
4471 /* offset placeholder */ 0,
4472 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004473 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004474 generate_null_check = !cls->IsInDexCache();
4475 break;
4476 }
4477 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4478 MemberOffset resolved_types_offset =
4479 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4480 // /* GcRoot<mirror::Class>[] */ out =
4481 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4482 Register current_method = InputRegisterAt(cls, 0);
4483 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4484 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004485 GenerateGcRootFieldLoad(cls,
4486 out_loc,
4487 out.X(),
Andreas Gampea5b09a62016-11-17 15:21:22 -08004488 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_),
Roland Levillain00468f32016-10-27 18:02:48 +01004489 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004490 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004491 generate_null_check = !cls->IsInDexCache();
4492 break;
4493 }
4494 }
4495
4496 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4497 DCHECK(cls->CanCallRuntime());
4498 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4499 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4500 codegen_->AddSlowPath(slow_path);
4501 if (generate_null_check) {
4502 __ Cbz(out, slow_path->GetEntryLabel());
4503 }
4504 if (cls->MustGenerateClinitCheck()) {
4505 GenerateClassInitializationCheck(slow_path, out);
4506 } else {
4507 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004508 }
4509 }
4510}
4511
David Brazdilcb1c0552015-08-04 16:22:25 +01004512static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004513 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004514}
4515
Alexandre Rames67555f72014-11-18 10:55:16 +00004516void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4517 LocationSummary* locations =
4518 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4519 locations->SetOut(Location::RequiresRegister());
4520}
4521
4522void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004523 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4524}
4525
4526void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4527 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4528}
4529
4530void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4531 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004532}
4533
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004534HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4535 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004536 switch (desired_string_load_kind) {
4537 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4538 DCHECK(!GetCompilerOptions().GetCompilePic());
4539 break;
4540 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4541 DCHECK(GetCompilerOptions().GetCompilePic());
4542 break;
4543 case HLoadString::LoadKind::kBootImageAddress:
4544 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004545 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004546 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004547 break;
4548 case HLoadString::LoadKind::kDexCacheViaMethod:
4549 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004550 case HLoadString::LoadKind::kJitTableAddress:
4551 DCHECK(Runtime::Current()->UseJitCompilation());
4552 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004553 }
4554 return desired_string_load_kind;
4555}
4556
Alexandre Rames67555f72014-11-18 10:55:16 +00004557void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004558 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004560 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004561 InvokeRuntimeCallingConvention calling_convention;
4562 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4563 } else {
4564 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004565 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4566 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4567 // Rely on the pResolveString and/or marking to save everything, including temps.
4568 RegisterSet caller_saves = RegisterSet::Empty();
4569 InvokeRuntimeCallingConvention calling_convention;
4570 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4571 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4572 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4573 Primitive::kPrimNot).GetCode());
4574 locations->SetCustomSlowPathCallerSaves(caller_saves);
4575 } else {
4576 // For non-Baker read barrier we have a temp-clobbering call.
4577 }
4578 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004579 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004580}
4581
4582void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004583 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004584 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004585
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004586 switch (load->GetLoadKind()) {
4587 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004588 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4589 load->GetStringIndex()));
4590 return; // No dex cache slow path.
4591 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004592 // Add ADRP with its PC-relative String patch.
4593 const DexFile& dex_file = load->GetDexFile();
4594 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004595 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004596 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004597 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004598 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004599 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004600 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004601 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004602 return; // No dex cache slow path.
4603 }
4604 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004605 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4606 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4607 return; // No dex cache slow path.
4608 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004609 case HLoadString::LoadKind::kBssEntry: {
4610 // Add ADRP with its PC-relative String .bss entry patch.
4611 const DexFile& dex_file = load->GetDexFile();
4612 uint32_t string_index = load->GetStringIndex();
4613 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004614 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4615 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004616 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004617 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004618 // Add LDR with its PC-relative String patch.
4619 vixl::aarch64::Label* ldr_label =
4620 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004621 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004622 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004623 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004624 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004625 /* offset placeholder */ 0u,
4626 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004627 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004628 SlowPathCodeARM64* slow_path =
4629 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004630 codegen_->AddSlowPath(slow_path);
4631 __ Cbz(out.X(), slow_path->GetEntryLabel());
4632 __ Bind(slow_path->GetExitLabel());
4633 return;
4634 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004635 case HLoadString::LoadKind::kJitTableAddress: {
4636 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
4637 load->GetStringIndex()));
4638 GenerateGcRootFieldLoad(load,
4639 out_loc,
4640 out.X(),
4641 /* offset */ 0,
4642 /* fixup_label */ nullptr,
4643 kCompilerReadBarrierOption);
4644 return;
4645 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004646 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004647 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004648 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004649
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004650 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004651 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004652 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004653 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4654 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4655 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004656}
4657
Alexandre Rames5319def2014-10-23 10:03:10 +01004658void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4659 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4660 locations->SetOut(Location::ConstantLocation(constant));
4661}
4662
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004663void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004664 // Will be generated at use site.
4665}
4666
Alexandre Rames67555f72014-11-18 10:55:16 +00004667void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4668 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004669 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004670 InvokeRuntimeCallingConvention calling_convention;
4671 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4672}
4673
4674void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004675 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4676 instruction,
4677 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004678 if (instruction->IsEnter()) {
4679 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4680 } else {
4681 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4682 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004683}
4684
Alexandre Rames42d641b2014-10-27 14:00:51 +00004685void LocationsBuilderARM64::VisitMul(HMul* mul) {
4686 LocationSummary* locations =
4687 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4688 switch (mul->GetResultType()) {
4689 case Primitive::kPrimInt:
4690 case Primitive::kPrimLong:
4691 locations->SetInAt(0, Location::RequiresRegister());
4692 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004694 break;
4695
4696 case Primitive::kPrimFloat:
4697 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004698 locations->SetInAt(0, Location::RequiresFpuRegister());
4699 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004700 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004701 break;
4702
4703 default:
4704 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4705 }
4706}
4707
4708void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4709 switch (mul->GetResultType()) {
4710 case Primitive::kPrimInt:
4711 case Primitive::kPrimLong:
4712 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4713 break;
4714
4715 case Primitive::kPrimFloat:
4716 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004717 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004718 break;
4719
4720 default:
4721 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4722 }
4723}
4724
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004725void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4726 LocationSummary* locations =
4727 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4728 switch (neg->GetResultType()) {
4729 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004730 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004731 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004733 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004734
4735 case Primitive::kPrimFloat:
4736 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004737 locations->SetInAt(0, Location::RequiresFpuRegister());
4738 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004739 break;
4740
4741 default:
4742 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4743 }
4744}
4745
4746void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4747 switch (neg->GetResultType()) {
4748 case Primitive::kPrimInt:
4749 case Primitive::kPrimLong:
4750 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4751 break;
4752
4753 case Primitive::kPrimFloat:
4754 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004755 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004756 break;
4757
4758 default:
4759 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4760 }
4761}
4762
4763void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4764 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004766 InvokeRuntimeCallingConvention calling_convention;
4767 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004768 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004769 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004770 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004771}
4772
4773void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4774 LocationSummary* locations = instruction->GetLocations();
4775 InvokeRuntimeCallingConvention calling_convention;
4776 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4777 DCHECK(type_index.Is(w0));
Andreas Gampea5b09a62016-11-17 15:21:22 -08004778 __ Mov(type_index, instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01004779 // Note: if heap poisoning is enabled, the entry point takes cares
4780 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004781 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004782 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004783}
4784
Alexandre Rames5319def2014-10-23 10:03:10 +01004785void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4786 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004788 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004789 if (instruction->IsStringAlloc()) {
4790 locations->AddTemp(LocationFrom(kArtMethodRegister));
4791 } else {
4792 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4793 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4794 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004795 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4796}
4797
4798void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004799 // Note: if heap poisoning is enabled, the entry point takes cares
4800 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004801 if (instruction->IsStringAlloc()) {
4802 // String is allocated through StringFactory. Call NewEmptyString entry point.
4803 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004804 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004805 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4806 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4807 __ Blr(lr);
4808 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4809 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004810 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004811 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4812 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004813}
4814
4815void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4816 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004817 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004819}
4820
4821void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004822 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004823 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004824 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004825 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004826 break;
4827
4828 default:
4829 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4830 }
4831}
4832
David Brazdil66d126e2015-04-03 16:02:44 +01004833void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4834 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4835 locations->SetInAt(0, Location::RequiresRegister());
4836 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4837}
4838
4839void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004840 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004841}
4842
Alexandre Rames5319def2014-10-23 10:03:10 +01004843void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004844 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4845 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004846}
4847
Calin Juravle2ae48182016-03-16 14:05:09 +00004848void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4849 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 return;
4851 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004852
Alexandre Ramesd921d642015-04-16 15:07:16 +01004853 BlockPoolsScope block_pools(GetVIXLAssembler());
4854 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004855 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004856 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004857}
4858
Calin Juravle2ae48182016-03-16 14:05:09 +00004859void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004860 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004861 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004862
4863 LocationSummary* locations = instruction->GetLocations();
4864 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004865
4866 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004867}
4868
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004869void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004870 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004871}
4872
Alexandre Rames67555f72014-11-18 10:55:16 +00004873void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4874 HandleBinaryOp(instruction);
4875}
4876
4877void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4878 HandleBinaryOp(instruction);
4879}
4880
Alexandre Rames3e69f162014-12-10 10:36:50 +00004881void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4882 LOG(FATAL) << "Unreachable";
4883}
4884
4885void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4886 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4887}
4888
Alexandre Rames5319def2014-10-23 10:03:10 +01004889void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4891 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4892 if (location.IsStackSlot()) {
4893 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4894 } else if (location.IsDoubleStackSlot()) {
4895 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4896 }
4897 locations->SetOut(location);
4898}
4899
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004900void InstructionCodeGeneratorARM64::VisitParameterValue(
4901 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004902 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004903}
4904
4905void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4906 LocationSummary* locations =
4907 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004908 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004909}
4910
4911void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4912 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4913 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004914}
4915
4916void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004918 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004919 locations->SetInAt(i, Location::Any());
4920 }
4921 locations->SetOut(Location::Any());
4922}
4923
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004924void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004925 LOG(FATAL) << "Unreachable";
4926}
4927
Serban Constantinescu02164b32014-11-13 14:05:07 +00004928void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004929 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004930 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004931 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4932 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004933 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4934
4935 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004936 case Primitive::kPrimInt:
4937 case Primitive::kPrimLong:
4938 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004939 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004940 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4941 break;
4942
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004943 case Primitive::kPrimFloat:
4944 case Primitive::kPrimDouble: {
4945 InvokeRuntimeCallingConvention calling_convention;
4946 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4947 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4948 locations->SetOut(calling_convention.GetReturnLocation(type));
4949
4950 break;
4951 }
4952
Serban Constantinescu02164b32014-11-13 14:05:07 +00004953 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004954 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004955 }
4956}
4957
4958void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4959 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004960
Serban Constantinescu02164b32014-11-13 14:05:07 +00004961 switch (type) {
4962 case Primitive::kPrimInt:
4963 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004964 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004965 break;
4966 }
4967
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004968 case Primitive::kPrimFloat:
4969 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004970 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4971 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004972 if (type == Primitive::kPrimFloat) {
4973 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4974 } else {
4975 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4976 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004977 break;
4978 }
4979
Serban Constantinescu02164b32014-11-13 14:05:07 +00004980 default:
4981 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004982 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004983 }
4984}
4985
Calin Juravle27df7582015-04-17 19:12:31 +01004986void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4987 memory_barrier->SetLocations(nullptr);
4988}
4989
4990void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004991 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004992}
4993
Alexandre Rames5319def2014-10-23 10:03:10 +01004994void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4996 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004997 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004998}
4999
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005000void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005001 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005002}
5003
5004void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5005 instruction->SetLocations(nullptr);
5006}
5007
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005008void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005009 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005010}
5011
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005012void LocationsBuilderARM64::VisitRor(HRor* ror) {
5013 HandleBinaryOp(ror);
5014}
5015
5016void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5017 HandleBinaryOp(ror);
5018}
5019
Serban Constantinescu02164b32014-11-13 14:05:07 +00005020void LocationsBuilderARM64::VisitShl(HShl* shl) {
5021 HandleShift(shl);
5022}
5023
5024void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5025 HandleShift(shl);
5026}
5027
5028void LocationsBuilderARM64::VisitShr(HShr* shr) {
5029 HandleShift(shr);
5030}
5031
5032void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5033 HandleShift(shr);
5034}
5035
Alexandre Rames5319def2014-10-23 10:03:10 +01005036void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005037 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005038}
5039
5040void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005041 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005042}
5043
Alexandre Rames67555f72014-11-18 10:55:16 +00005044void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005045 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005046}
5047
5048void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005049 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005050}
5051
5052void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005053 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005054}
5055
Alexandre Rames67555f72014-11-18 10:55:16 +00005056void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005057 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005058}
5059
Calin Juravlee460d1d2015-09-29 04:52:17 +01005060void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5061 HUnresolvedInstanceFieldGet* instruction) {
5062 FieldAccessCallingConventionARM64 calling_convention;
5063 codegen_->CreateUnresolvedFieldLocationSummary(
5064 instruction, instruction->GetFieldType(), calling_convention);
5065}
5066
5067void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5068 HUnresolvedInstanceFieldGet* instruction) {
5069 FieldAccessCallingConventionARM64 calling_convention;
5070 codegen_->GenerateUnresolvedFieldAccess(instruction,
5071 instruction->GetFieldType(),
5072 instruction->GetFieldIndex(),
5073 instruction->GetDexPc(),
5074 calling_convention);
5075}
5076
5077void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5078 HUnresolvedInstanceFieldSet* instruction) {
5079 FieldAccessCallingConventionARM64 calling_convention;
5080 codegen_->CreateUnresolvedFieldLocationSummary(
5081 instruction, instruction->GetFieldType(), calling_convention);
5082}
5083
5084void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5085 HUnresolvedInstanceFieldSet* instruction) {
5086 FieldAccessCallingConventionARM64 calling_convention;
5087 codegen_->GenerateUnresolvedFieldAccess(instruction,
5088 instruction->GetFieldType(),
5089 instruction->GetFieldIndex(),
5090 instruction->GetDexPc(),
5091 calling_convention);
5092}
5093
5094void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5095 HUnresolvedStaticFieldGet* instruction) {
5096 FieldAccessCallingConventionARM64 calling_convention;
5097 codegen_->CreateUnresolvedFieldLocationSummary(
5098 instruction, instruction->GetFieldType(), calling_convention);
5099}
5100
5101void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5102 HUnresolvedStaticFieldGet* instruction) {
5103 FieldAccessCallingConventionARM64 calling_convention;
5104 codegen_->GenerateUnresolvedFieldAccess(instruction,
5105 instruction->GetFieldType(),
5106 instruction->GetFieldIndex(),
5107 instruction->GetDexPc(),
5108 calling_convention);
5109}
5110
5111void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5112 HUnresolvedStaticFieldSet* instruction) {
5113 FieldAccessCallingConventionARM64 calling_convention;
5114 codegen_->CreateUnresolvedFieldLocationSummary(
5115 instruction, instruction->GetFieldType(), calling_convention);
5116}
5117
5118void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5119 HUnresolvedStaticFieldSet* instruction) {
5120 FieldAccessCallingConventionARM64 calling_convention;
5121 codegen_->GenerateUnresolvedFieldAccess(instruction,
5122 instruction->GetFieldType(),
5123 instruction->GetFieldIndex(),
5124 instruction->GetDexPc(),
5125 calling_convention);
5126}
5127
Alexandre Rames5319def2014-10-23 10:03:10 +01005128void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005129 LocationSummary* locations =
5130 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005131 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005132}
5133
5134void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005135 HBasicBlock* block = instruction->GetBlock();
5136 if (block->GetLoopInformation() != nullptr) {
5137 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5138 // The back edge will generate the suspend check.
5139 return;
5140 }
5141 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5142 // The goto will generate the suspend check.
5143 return;
5144 }
5145 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005146}
5147
Alexandre Rames67555f72014-11-18 10:55:16 +00005148void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5149 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005151 InvokeRuntimeCallingConvention calling_convention;
5152 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5153}
5154
5155void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005156 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005157 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005158}
5159
5160void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5161 LocationSummary* locations =
5162 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5163 Primitive::Type input_type = conversion->GetInputType();
5164 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005165 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005166 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5167 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5168 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5169 }
5170
Alexandre Rames542361f2015-01-29 16:57:31 +00005171 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005172 locations->SetInAt(0, Location::RequiresFpuRegister());
5173 } else {
5174 locations->SetInAt(0, Location::RequiresRegister());
5175 }
5176
Alexandre Rames542361f2015-01-29 16:57:31 +00005177 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005178 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5179 } else {
5180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5181 }
5182}
5183
5184void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5185 Primitive::Type result_type = conversion->GetResultType();
5186 Primitive::Type input_type = conversion->GetInputType();
5187
5188 DCHECK_NE(input_type, result_type);
5189
Alexandre Rames542361f2015-01-29 16:57:31 +00005190 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005191 int result_size = Primitive::ComponentSize(result_type);
5192 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005193 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005194 Register output = OutputRegister(conversion);
5195 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005196 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005197 // 'int' values are used directly as W registers, discarding the top
5198 // bits, so we don't need to sign-extend and can just perform a move.
5199 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5200 // top 32 bits of the target register. We theoretically could leave those
5201 // bits unchanged, but we would have to make sure that no code uses a
5202 // 32bit input value as a 64bit value assuming that the top 32 bits are
5203 // zero.
5204 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005205 } else if (result_type == Primitive::kPrimChar ||
5206 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5207 __ Ubfx(output,
5208 output.IsX() ? source.X() : source.W(),
5209 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005210 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005211 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005212 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005213 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005214 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005215 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005216 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5217 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005218 } else if (Primitive::IsFloatingPointType(result_type) &&
5219 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005220 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5221 } else {
5222 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5223 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005224 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005225}
Alexandre Rames67555f72014-11-18 10:55:16 +00005226
Serban Constantinescu02164b32014-11-13 14:05:07 +00005227void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5228 HandleShift(ushr);
5229}
5230
5231void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5232 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005233}
5234
5235void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5236 HandleBinaryOp(instruction);
5237}
5238
5239void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5240 HandleBinaryOp(instruction);
5241}
5242
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005243void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005244 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005245 LOG(FATAL) << "Unreachable";
5246}
5247
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005248void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005249 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005250 LOG(FATAL) << "Unreachable";
5251}
5252
Mark Mendellfe57faa2015-09-18 09:26:15 -04005253// Simple implementation of packed switch - generate cascaded compare/jumps.
5254void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5255 LocationSummary* locations =
5256 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5257 locations->SetInAt(0, Location::RequiresRegister());
5258}
5259
5260void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5261 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005262 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005263 Register value_reg = InputRegisterAt(switch_instr, 0);
5264 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5265
Zheng Xu3927c8b2015-11-18 17:46:25 +08005266 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005267 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005268 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5269 // make sure we don't emit it if the target may run out of range.
5270 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5271 // ranges and emit the tables only as required.
5272 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005273
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005274 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005275 // Current instruction id is an upper bound of the number of HIRs in the graph.
5276 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5277 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005278 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5279 Register temp = temps.AcquireW();
5280 __ Subs(temp, value_reg, Operand(lower_bound));
5281
Zheng Xu3927c8b2015-11-18 17:46:25 +08005282 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005283 // Jump to successors[0] if value == lower_bound.
5284 __ B(eq, codegen_->GetLabelOf(successors[0]));
5285 int32_t last_index = 0;
5286 for (; num_entries - last_index > 2; last_index += 2) {
5287 __ Subs(temp, temp, Operand(2));
5288 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5289 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5290 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5291 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5292 }
5293 if (num_entries - last_index == 2) {
5294 // The last missing case_value.
5295 __ Cmp(temp, Operand(1));
5296 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005297 }
5298
5299 // And the default for any other value.
5300 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5301 __ B(codegen_->GetLabelOf(default_block));
5302 }
5303 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005304 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005305
5306 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5307
5308 // Below instructions should use at most one blocked register. Since there are two blocked
5309 // registers, we are free to block one.
5310 Register temp_w = temps.AcquireW();
5311 Register index;
5312 // Remove the bias.
5313 if (lower_bound != 0) {
5314 index = temp_w;
5315 __ Sub(index, value_reg, Operand(lower_bound));
5316 } else {
5317 index = value_reg;
5318 }
5319
5320 // Jump to default block if index is out of the range.
5321 __ Cmp(index, Operand(num_entries));
5322 __ B(hs, codegen_->GetLabelOf(default_block));
5323
5324 // In current VIXL implementation, it won't require any blocked registers to encode the
5325 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5326 // register pressure.
5327 Register table_base = temps.AcquireX();
5328 // Load jump offset from the table.
5329 __ Adr(table_base, jump_table->GetTableStartLabel());
5330 Register jump_offset = temp_w;
5331 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5332
5333 // Jump to target block by branching to table_base(pc related) + offset.
5334 Register target_address = table_base;
5335 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5336 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005337 }
5338}
5339
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005340void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5341 HInstruction* instruction,
5342 Location out,
5343 uint32_t offset,
5344 Location maybe_temp,
5345 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005346 Primitive::Type type = Primitive::kPrimNot;
5347 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005348 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005349 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005350 Register temp_reg = RegisterFrom(maybe_temp, type);
5351 if (kUseBakerReadBarrier) {
5352 // Load with fast path based Baker's read barrier.
5353 // /* HeapReference<Object> */ out = *(out + offset)
5354 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5355 out,
5356 out_reg,
5357 offset,
5358 temp_reg,
5359 /* needs_null_check */ false,
5360 /* use_load_acquire */ false);
5361 } else {
5362 // Load with slow path based read barrier.
5363 // Save the value of `out` into `maybe_temp` before overwriting it
5364 // in the following move operation, as we will need it for the
5365 // read barrier below.
5366 __ Mov(temp_reg, out_reg);
5367 // /* HeapReference<Object> */ out = *(out + offset)
5368 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5369 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5370 }
5371 } else {
5372 // Plain load with no read barrier.
5373 // /* HeapReference<Object> */ out = *(out + offset)
5374 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5375 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5376 }
5377}
5378
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005379void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5380 HInstruction* instruction,
5381 Location out,
5382 Location obj,
5383 uint32_t offset,
5384 Location maybe_temp,
5385 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005386 Primitive::Type type = Primitive::kPrimNot;
5387 Register out_reg = RegisterFrom(out, type);
5388 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005389 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005390 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005391 if (kUseBakerReadBarrier) {
5392 // Load with fast path based Baker's read barrier.
5393 Register temp_reg = RegisterFrom(maybe_temp, type);
5394 // /* HeapReference<Object> */ out = *(obj + offset)
5395 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5396 out,
5397 obj_reg,
5398 offset,
5399 temp_reg,
5400 /* needs_null_check */ false,
5401 /* use_load_acquire */ false);
5402 } else {
5403 // Load with slow path based read barrier.
5404 // /* HeapReference<Object> */ out = *(obj + offset)
5405 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5406 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5407 }
5408 } else {
5409 // Plain load with no read barrier.
5410 // /* HeapReference<Object> */ out = *(obj + offset)
5411 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5412 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5413 }
5414}
5415
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005416void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5417 HInstruction* instruction,
5418 Location root,
5419 Register obj,
5420 uint32_t offset,
5421 vixl::aarch64::Label* fixup_label,
5422 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005423 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005424 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005425 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005426 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005427 if (kUseBakerReadBarrier) {
5428 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5429 // Baker's read barrier are used:
5430 //
5431 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005432 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5433 // if (temp != null) {
5434 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005435 // }
5436
5437 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005438 if (fixup_label == nullptr) {
5439 __ Ldr(root_reg, MemOperand(obj, offset));
5440 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005441 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005442 }
Roland Levillain44015862016-01-22 11:47:17 +00005443 static_assert(
5444 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5445 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5446 "have different sizes.");
5447 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5448 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5449 "have different sizes.");
5450
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005451 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005452
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005453 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5454 SlowPathCodeARM64* slow_path =
5455 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5456 root,
5457 LocationFrom(temp));
5458 codegen_->AddSlowPath(slow_path);
5459 const int32_t entry_point_offset =
5460 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5461 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5462 // Loading the entrypoint does not require a load acquire since it is only changed when
5463 // threads are suspended or running a checkpoint.
5464 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5465 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5466 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005467 __ Cbnz(temp, slow_path->GetEntryLabel());
5468 __ Bind(slow_path->GetExitLabel());
5469 } else {
5470 // GC root loaded through a slow path for read barriers other
5471 // than Baker's.
5472 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005473 if (fixup_label == nullptr) {
5474 __ Add(root_reg.X(), obj.X(), offset);
5475 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005476 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 }
Roland Levillain44015862016-01-22 11:47:17 +00005478 // /* mirror::Object* */ root = root->Read()
5479 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5480 }
5481 } else {
5482 // Plain GC root load with no read barrier.
5483 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005484 if (fixup_label == nullptr) {
5485 __ Ldr(root_reg, MemOperand(obj, offset));
5486 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005487 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005488 }
Roland Levillain44015862016-01-22 11:47:17 +00005489 // Note that GC roots are not affected by heap poisoning, thus we
5490 // do not have to unpoison `root_reg` here.
5491 }
5492}
5493
5494void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5495 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005496 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005497 uint32_t offset,
5498 Register temp,
5499 bool needs_null_check,
5500 bool use_load_acquire) {
5501 DCHECK(kEmitCompilerReadBarrier);
5502 DCHECK(kUseBakerReadBarrier);
5503
5504 // /* HeapReference<Object> */ ref = *(obj + offset)
5505 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005506 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005507 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5508 ref,
5509 obj,
5510 offset,
5511 no_index,
5512 no_scale_factor,
5513 temp,
5514 needs_null_check,
5515 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005516}
5517
5518void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5519 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005520 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005521 uint32_t data_offset,
5522 Location index,
5523 Register temp,
5524 bool needs_null_check) {
5525 DCHECK(kEmitCompilerReadBarrier);
5526 DCHECK(kUseBakerReadBarrier);
5527
5528 // Array cells are never volatile variables, therefore array loads
5529 // never use Load-Acquire instructions on ARM64.
5530 const bool use_load_acquire = false;
5531
Roland Levillainbfea3352016-06-23 13:48:47 +01005532 static_assert(
5533 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5534 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005535 // /* HeapReference<Object> */ ref =
5536 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005537 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5538 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5539 ref,
5540 obj,
5541 data_offset,
5542 index,
5543 scale_factor,
5544 temp,
5545 needs_null_check,
5546 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005547}
5548
5549void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5550 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005551 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005552 uint32_t offset,
5553 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005554 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005555 Register temp,
5556 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005557 bool use_load_acquire,
5558 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005559 DCHECK(kEmitCompilerReadBarrier);
5560 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005561 // If we are emitting an array load, we should not be using a
5562 // Load Acquire instruction. In other words:
5563 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5564 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005565
5566 MacroAssembler* masm = GetVIXLAssembler();
5567 UseScratchRegisterScope temps(masm);
5568
5569 // In slow path based read barriers, the read barrier call is
5570 // inserted after the original load. However, in fast path based
5571 // Baker's read barriers, we need to perform the load of
5572 // mirror::Object::monitor_ *before* the original reference load.
5573 // This load-load ordering is required by the read barrier.
5574 // The fast path/slow path (for Baker's algorithm) should look like:
5575 //
5576 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5577 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5578 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005579 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005580 // if (is_gray) {
5581 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5582 // }
5583 //
5584 // Note: the original implementation in ReadBarrier::Barrier is
5585 // slightly more complex as it performs additional checks that we do
5586 // not do here for performance reasons.
5587
5588 Primitive::Type type = Primitive::kPrimNot;
5589 Register ref_reg = RegisterFrom(ref, type);
5590 DCHECK(obj.IsW());
5591 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5592
5593 // /* int32_t */ monitor = obj->monitor_
5594 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5595 if (needs_null_check) {
5596 MaybeRecordImplicitNullCheck(instruction);
5597 }
5598 // /* LockWord */ lock_word = LockWord(monitor)
5599 static_assert(sizeof(LockWord) == sizeof(int32_t),
5600 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005601
Vladimir Marko877a0332016-07-11 19:30:56 +01005602 // Introduce a dependency on the lock_word including rb_state,
5603 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005604 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005605 // `obj` is unchanged by this operation, but its value now depends
5606 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005607 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005608
5609 // The actual reference load.
5610 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005611 // Load types involving an "index": ArrayGet,
5612 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5613 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005614 if (use_load_acquire) {
5615 // UnsafeGetObjectVolatile intrinsic case.
5616 // Register `index` is not an index in an object array, but an
5617 // offset to an object reference field within object `obj`.
5618 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5619 DCHECK(instruction->GetLocations()->Intrinsified());
5620 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5621 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005622 DCHECK_EQ(offset, 0u);
5623 DCHECK_EQ(scale_factor, 0u);
5624 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005625 // /* HeapReference<Object> */ ref = *(obj + index)
5626 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5627 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005628 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005629 // ArrayGet and UnsafeGetObject intrinsics cases.
5630 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5631 if (index.IsConstant()) {
5632 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5633 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5634 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005635 Register temp3 = temps.AcquireW();
5636 __ Add(temp3, obj, offset);
5637 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5638 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005639 }
Roland Levillain44015862016-01-22 11:47:17 +00005640 }
Roland Levillain44015862016-01-22 11:47:17 +00005641 } else {
5642 // /* HeapReference<Object> */ ref = *(obj + offset)
5643 MemOperand field = HeapOperand(obj, offset);
5644 if (use_load_acquire) {
5645 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5646 } else {
5647 Load(type, ref_reg, field);
5648 }
5649 }
5650
5651 // Object* ref = ref_addr->AsMirrorPtr()
5652 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5653
Vladimir Marko953437b2016-08-24 08:30:46 +00005654 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005655 SlowPathCodeARM64* slow_path;
5656 if (always_update_field) {
5657 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5658 // address of the form `obj + field_offset`, where `obj` is a
5659 // register and `field_offset` is a register. Thus `offset` and
5660 // `scale_factor` above are expected to be null in this code path.
5661 DCHECK_EQ(offset, 0u);
5662 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5663 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5664 instruction, ref, obj, /* field_offset */ index, temp);
5665 } else {
5666 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5667 }
Roland Levillain44015862016-01-22 11:47:17 +00005668 AddSlowPath(slow_path);
5669
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005670 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005671 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005672 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005673 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5674 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005675 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005676 __ Bind(slow_path->GetExitLabel());
5677}
5678
5679void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5680 Location out,
5681 Location ref,
5682 Location obj,
5683 uint32_t offset,
5684 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005685 DCHECK(kEmitCompilerReadBarrier);
5686
Roland Levillain44015862016-01-22 11:47:17 +00005687 // Insert a slow path based read barrier *after* the reference load.
5688 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005689 // If heap poisoning is enabled, the unpoisoning of the loaded
5690 // reference will be carried out by the runtime within the slow
5691 // path.
5692 //
5693 // Note that `ref` currently does not get unpoisoned (when heap
5694 // poisoning is enabled), which is alright as the `ref` argument is
5695 // not used by the artReadBarrierSlow entry point.
5696 //
5697 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5698 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5699 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5700 AddSlowPath(slow_path);
5701
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005702 __ B(slow_path->GetEntryLabel());
5703 __ Bind(slow_path->GetExitLabel());
5704}
5705
Roland Levillain44015862016-01-22 11:47:17 +00005706void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5707 Location out,
5708 Location ref,
5709 Location obj,
5710 uint32_t offset,
5711 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005712 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005713 // Baker's read barriers shall be handled by the fast path
5714 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5715 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005716 // If heap poisoning is enabled, unpoisoning will be taken care of
5717 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005718 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005719 } else if (kPoisonHeapReferences) {
5720 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5721 }
5722}
5723
Roland Levillain44015862016-01-22 11:47:17 +00005724void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5725 Location out,
5726 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005727 DCHECK(kEmitCompilerReadBarrier);
5728
Roland Levillain44015862016-01-22 11:47:17 +00005729 // Insert a slow path based read barrier *after* the GC root load.
5730 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005731 // Note that GC roots are not affected by heap poisoning, so we do
5732 // not need to do anything special for this here.
5733 SlowPathCodeARM64* slow_path =
5734 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5735 AddSlowPath(slow_path);
5736
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005737 __ B(slow_path->GetEntryLabel());
5738 __ Bind(slow_path->GetExitLabel());
5739}
5740
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005741void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5742 LocationSummary* locations =
5743 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5744 locations->SetInAt(0, Location::RequiresRegister());
5745 locations->SetOut(Location::RequiresRegister());
5746}
5747
5748void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5749 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005750 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005751 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005752 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005753 __ Ldr(XRegisterFrom(locations->Out()),
5754 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005755 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005756 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005757 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005758 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5759 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005760 __ Ldr(XRegisterFrom(locations->Out()),
5761 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005762 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005763}
5764
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005765void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5766 for (const auto& entry : jit_string_patches_) {
5767 const auto& it = jit_string_roots_.find(entry.first);
5768 DCHECK(it != jit_string_roots_.end());
5769 size_t index_in_table = it->second;
5770 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
5771 uint32_t literal_offset = literal->GetOffset();
5772 uintptr_t address =
5773 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5774 uint8_t* data = code + literal_offset;
5775 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5776 }
5777}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005778
Alexandre Rames67555f72014-11-18 10:55:16 +00005779#undef __
5780#undef QUICK_ENTRY_POINT
5781
Alexandre Rames5319def2014-10-23 10:03:10 +01005782} // namespace arm64
5783} // namespace art