blob: 8197787f18e4040d6aa4c759a7c17db6bc21ff42 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = at_->GetLocations();
285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
293 : kQuickInitializeType;
294 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100296 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100298 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000300
301 // Move the class to the desired location.
302 Location out = locations->Out();
303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 __ B(GetExitLabel());
311 }
312
Alexandre Rames9931f312015-06-19 14:47:01 +0100313 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
314
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
319 // The instruction where this slow path is happening.
320 // (Might be the load class or an initialization check).
321 HInstruction* const at_;
322
323 // The dex PC of `at_`.
324 const uint32_t dex_pc_;
325
326 // Whether to initialize the class.
327 const bool do_clinit_;
328
329 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
330};
331
Vladimir Markoaad75c62016-10-03 08:46:48 +0000332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100334 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
335 : SlowPathCodeARM64(instruction),
336 temp_(temp),
337 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 LocationSummary* locations = instruction_->GetLocations();
341 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
343
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100344 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
345 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
346 temps.Exclude(temp_);
347
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, locations);
350
351 InvokeRuntimeCallingConvention calling_convention;
352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
356 Primitive::Type type = instruction_->GetType();
357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
358
359 RestoreLiveRegisters(codegen, locations);
360
361 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
364 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
367 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
368 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100371 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100376 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 }
378
379 __ B(GetExitLabel());
380 }
381
382 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
383
384 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100385 const Register temp_;
386 vixl::aarch64::Label* adrp_label_;
387
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
389};
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 if (instruction_->CanThrowIntoCatchBlock()) {
399 // Live registers will be restored in the catch block if caught.
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
401 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000402 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
403 instruction_,
404 instruction_->GetDexPc(),
405 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800406 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100407 }
408
Alexandre Rames8158f282015-08-07 10:26:17 +0100409 bool IsFatal() const OVERRIDE { return true; }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
412
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
415};
416
417class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100419 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000420 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 if (successor_ == nullptr) {
428 __ B(GetReturnLabel());
429 } else {
430 __ B(arm64_codegen->GetLabelOf(successor_));
431 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 }
433
Scott Wakeling97c72b72016-06-24 16:19:36 +0100434 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 DCHECK(successor_ == nullptr);
436 return &return_label_;
437 }
438
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100439 HBasicBlock* GetSuccessor() const {
440 return successor_;
441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
444
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 // If not null, the block to branch to after the suspend check.
447 HBasicBlock* const successor_;
448
449 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100450 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100451
452 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
453};
454
Alexandre Rames67555f72014-11-18 10:55:16 +0000455class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000457 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000458 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100462 Location class_to_check = locations->InAt(1);
463 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
464 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 DCHECK(instruction_->IsCheckCast()
466 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100468 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000469
Alexandre Rames67555f72014-11-18 10:55:16 +0000470 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000472 if (!is_fatal_) {
473 SaveLiveRegisters(codegen, locations);
474 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475
476 // We're moving two locations to locations that could overlap, so we need a parallel
477 // move resolver.
478 InvokeRuntimeCallingConvention calling_convention;
479 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100480 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
481 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000482
483 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000484 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700485 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000486 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487 Primitive::Type ret_type = instruction_->GetType();
488 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
489 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
490 } else {
491 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000492 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800493 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000494 }
495
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000496 if (!is_fatal_) {
497 RestoreLiveRegisters(codegen, locations);
498 __ B(GetExitLabel());
499 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000500 }
501
Alexandre Rames9931f312015-06-19 14:47:01 +0100502 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100503 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100504
Alexandre Rames67555f72014-11-18 10:55:16 +0000505 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507
Alexandre Rames67555f72014-11-18 10:55:16 +0000508 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
509};
510
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700511class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
512 public:
Aart Bik42249c32016-01-07 15:33:50 -0800513 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000514 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700515
516 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800517 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700518 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000519 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000520 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700521 }
522
Alexandre Rames9931f312015-06-19 14:47:01 +0100523 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
524
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700526 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
527};
528
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100529class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
530 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
534 LocationSummary* locations = instruction_->GetLocations();
535 __ Bind(GetEntryLabel());
536 SaveLiveRegisters(codegen, locations);
537
538 InvokeRuntimeCallingConvention calling_convention;
539 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
540 parallel_move.AddMove(
541 locations->InAt(0),
542 LocationFrom(calling_convention.GetRegisterAt(0)),
543 Primitive::kPrimNot,
544 nullptr);
545 parallel_move.AddMove(
546 locations->InAt(1),
547 LocationFrom(calling_convention.GetRegisterAt(1)),
548 Primitive::kPrimInt,
549 nullptr);
550 parallel_move.AddMove(
551 locations->InAt(2),
552 LocationFrom(calling_convention.GetRegisterAt(2)),
553 Primitive::kPrimNot,
554 nullptr);
555 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
556
557 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000558 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100559 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
560 RestoreLiveRegisters(codegen, locations);
561 __ B(GetExitLabel());
562 }
563
564 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
565
566 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100567 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
568};
569
Zheng Xu3927c8b2015-11-18 17:46:25 +0800570void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
571 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000572 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800573
574 // We are about to use the assembler to place literals directly. Make sure we have enough
575 // underlying code buffer and we have generated the jump table with right size.
576 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
577 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
578
579 __ Bind(&table_start_);
580 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
581 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100582 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800583 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100584 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800585 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
586 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
587 Literal<int32_t> literal(jump_offset);
588 __ place(&literal);
589 }
590}
591
Roland Levillain44015862016-01-22 11:47:17 +0000592// Slow path marking an object during a read barrier.
593class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
594 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100595 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
596 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000597 DCHECK(kEmitCompilerReadBarrier);
598 }
599
600 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
601
602 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
603 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000604 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100605 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000606 DCHECK(instruction_->IsInstanceFieldGet() ||
607 instruction_->IsStaticFieldGet() ||
608 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100609 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000610 instruction_->IsLoadClass() ||
611 instruction_->IsLoadString() ||
612 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100613 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100614 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
615 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000616 << "Unexpected instruction in read barrier marking slow path: "
617 << instruction_->DebugName();
618
619 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100620 // No need to save live registers; it's taken care of by the
621 // entrypoint. Also, there is no need to update the stack mask,
622 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000623 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100624 DCHECK_NE(obj_.reg(), LR);
625 DCHECK_NE(obj_.reg(), WSP);
626 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100627 // IP0 is used internally by the ReadBarrierMarkRegX entry point
628 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700629 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100630 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
631 // "Compact" slow path, saving two moves.
632 //
633 // Instead of using the standard runtime calling convention (input
634 // and output in W0):
635 //
636 // W0 <- obj
637 // W0 <- ReadBarrierMark(W0)
638 // obj <- W0
639 //
640 // we just use rX (the register holding `obj`) as input and output
641 // of a dedicated entrypoint:
642 //
643 // rX <- ReadBarrierMarkRegX(rX)
644 //
645 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700646 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100647 // This runtime call does not require a stack map.
648 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000649 __ B(GetExitLabel());
650 }
651
652 private:
Roland Levillain44015862016-01-22 11:47:17 +0000653 const Location obj_;
654
655 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
656};
657
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000658// Slow path generating a read barrier for a heap reference.
659class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
660 public:
661 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
662 Location out,
663 Location ref,
664 Location obj,
665 uint32_t offset,
666 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000667 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000668 out_(out),
669 ref_(ref),
670 obj_(obj),
671 offset_(offset),
672 index_(index) {
673 DCHECK(kEmitCompilerReadBarrier);
674 // If `obj` is equal to `out` or `ref`, it means the initial object
675 // has been overwritten by (or after) the heap object reference load
676 // to be instrumented, e.g.:
677 //
678 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000679 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000680 //
681 // In that case, we have lost the information about the original
682 // object, and the emitted read barrier cannot work properly.
683 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
684 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
685 }
686
687 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
688 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
689 LocationSummary* locations = instruction_->GetLocations();
690 Primitive::Type type = Primitive::kPrimNot;
691 DCHECK(locations->CanCall());
692 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100693 DCHECK(instruction_->IsInstanceFieldGet() ||
694 instruction_->IsStaticFieldGet() ||
695 instruction_->IsArrayGet() ||
696 instruction_->IsInstanceOf() ||
697 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100698 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000699 << "Unexpected instruction in read barrier for heap reference slow path: "
700 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000701 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000702 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100703 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000704
705 __ Bind(GetEntryLabel());
706
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000707 SaveLiveRegisters(codegen, locations);
708
709 // We may have to change the index's value, but as `index_` is a
710 // constant member (like other "inputs" of this slow path),
711 // introduce a copy of it, `index`.
712 Location index = index_;
713 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100714 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000715 if (instruction_->IsArrayGet()) {
716 // Compute the actual memory offset and store it in `index`.
717 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
718 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
719 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
720 // We are about to change the value of `index_reg` (see the
721 // calls to vixl::MacroAssembler::Lsl and
722 // vixl::MacroAssembler::Mov below), but it has
723 // not been saved by the previous call to
724 // art::SlowPathCode::SaveLiveRegisters, as it is a
725 // callee-save register --
726 // art::SlowPathCode::SaveLiveRegisters does not consider
727 // callee-save registers, as it has been designed with the
728 // assumption that callee-save registers are supposed to be
729 // handled by the called function. So, as a callee-save
730 // register, `index_reg` _would_ eventually be saved onto
731 // the stack, but it would be too late: we would have
732 // changed its value earlier. Therefore, we manually save
733 // it here into another freely available register,
734 // `free_reg`, chosen of course among the caller-save
735 // registers (as a callee-save `free_reg` register would
736 // exhibit the same problem).
737 //
738 // Note we could have requested a temporary register from
739 // the register allocator instead; but we prefer not to, as
740 // this is a slow path, and we know we can find a
741 // caller-save register that is available.
742 Register free_reg = FindAvailableCallerSaveRegister(codegen);
743 __ Mov(free_reg.W(), index_reg);
744 index_reg = free_reg;
745 index = LocationFrom(index_reg);
746 } else {
747 // The initial register stored in `index_` has already been
748 // saved in the call to art::SlowPathCode::SaveLiveRegisters
749 // (as it is not a callee-save register), so we can freely
750 // use it.
751 }
752 // Shifting the index value contained in `index_reg` by the scale
753 // factor (2) cannot overflow in practice, as the runtime is
754 // unable to allocate object arrays with a size larger than
755 // 2^26 - 1 (that is, 2^28 - 4 bytes).
756 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
757 static_assert(
758 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
759 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
760 __ Add(index_reg, index_reg, Operand(offset_));
761 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100762 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
763 // intrinsics, `index_` is not shifted by a scale factor of 2
764 // (as in the case of ArrayGet), as it is actually an offset
765 // to an object field within an object.
766 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000767 DCHECK(instruction_->GetLocations()->Intrinsified());
768 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
769 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
770 << instruction_->AsInvoke()->GetIntrinsic();
771 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100772 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000773 }
774 }
775
776 // We're moving two or three locations to locations that could
777 // overlap, so we need a parallel move resolver.
778 InvokeRuntimeCallingConvention calling_convention;
779 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
780 parallel_move.AddMove(ref_,
781 LocationFrom(calling_convention.GetRegisterAt(0)),
782 type,
783 nullptr);
784 parallel_move.AddMove(obj_,
785 LocationFrom(calling_convention.GetRegisterAt(1)),
786 type,
787 nullptr);
788 if (index.IsValid()) {
789 parallel_move.AddMove(index,
790 LocationFrom(calling_convention.GetRegisterAt(2)),
791 Primitive::kPrimInt,
792 nullptr);
793 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
794 } else {
795 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
796 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
797 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000798 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000799 instruction_,
800 instruction_->GetDexPc(),
801 this);
802 CheckEntrypointTypes<
803 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
804 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
805
806 RestoreLiveRegisters(codegen, locations);
807
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000808 __ B(GetExitLabel());
809 }
810
811 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
812
813 private:
814 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100815 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
816 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000817 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
818 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
819 return Register(VIXLRegCodeFromART(i), kXRegSize);
820 }
821 }
822 // We shall never fail to find a free caller-save register, as
823 // there are more than two core caller-save registers on ARM64
824 // (meaning it is possible to find one which is different from
825 // `ref` and `obj`).
826 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
827 LOG(FATAL) << "Could not find a free register";
828 UNREACHABLE();
829 }
830
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000831 const Location out_;
832 const Location ref_;
833 const Location obj_;
834 const uint32_t offset_;
835 // An additional location containing an index to an array.
836 // Only used for HArrayGet and the UnsafeGetObject &
837 // UnsafeGetObjectVolatile intrinsics.
838 const Location index_;
839
840 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
841};
842
843// Slow path generating a read barrier for a GC root.
844class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
845 public:
846 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000847 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000848 DCHECK(kEmitCompilerReadBarrier);
849 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000850
851 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
852 LocationSummary* locations = instruction_->GetLocations();
853 Primitive::Type type = Primitive::kPrimNot;
854 DCHECK(locations->CanCall());
855 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000856 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
857 << "Unexpected instruction in read barrier for GC root slow path: "
858 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000859
860 __ Bind(GetEntryLabel());
861 SaveLiveRegisters(codegen, locations);
862
863 InvokeRuntimeCallingConvention calling_convention;
864 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
865 // The argument of the ReadBarrierForRootSlow is not a managed
866 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
867 // thus we need a 64-bit move here, and we cannot use
868 //
869 // arm64_codegen->MoveLocation(
870 // LocationFrom(calling_convention.GetRegisterAt(0)),
871 // root_,
872 // type);
873 //
874 // which would emit a 32-bit move, as `type` is a (32-bit wide)
875 // reference type (`Primitive::kPrimNot`).
876 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000877 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000878 instruction_,
879 instruction_->GetDexPc(),
880 this);
881 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
882 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
883
884 RestoreLiveRegisters(codegen, locations);
885 __ B(GetExitLabel());
886 }
887
888 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
889
890 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000891 const Location out_;
892 const Location root_;
893
894 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
895};
896
Alexandre Rames5319def2014-10-23 10:03:10 +0100897#undef __
898
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100899Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100900 Location next_location;
901 if (type == Primitive::kPrimVoid) {
902 LOG(FATAL) << "Unreachable type " << type;
903 }
904
Alexandre Rames542361f2015-01-29 16:57:31 +0000905 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100906 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
907 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000908 } else if (!Primitive::IsFloatingPointType(type) &&
909 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000910 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
911 } else {
912 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000913 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
914 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100915 }
916
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000917 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000918 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100919 return next_location;
920}
921
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100922Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100923 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100924}
925
Serban Constantinescu579885a2015-02-22 20:51:33 +0000926CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
927 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100928 const CompilerOptions& compiler_options,
929 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100930 : CodeGenerator(graph,
931 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000932 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000933 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100934 callee_saved_core_registers.GetList(),
935 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100936 compiler_options,
937 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100938 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800939 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100940 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000941 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000942 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100943 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000944 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000945 uint32_literals_(std::less<uint32_t>(),
946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100947 uint64_literals_(std::less<uint64_t>(),
948 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
949 method_patches_(MethodReferenceComparator(),
950 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
951 call_patches_(MethodReferenceComparator(),
952 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
953 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000954 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
955 boot_image_string_patches_(StringReferenceValueComparator(),
956 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
957 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100958 boot_image_type_patches_(TypeReferenceValueComparator(),
959 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
960 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000961 boot_image_address_patches_(std::less<uint32_t>(),
962 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000963 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000964 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000965}
Alexandre Rames5319def2014-10-23 10:03:10 +0100966
Alexandre Rames67555f72014-11-18 10:55:16 +0000967#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100968
Zheng Xu3927c8b2015-11-18 17:46:25 +0800969void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100970 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800971 jump_table->EmitTable(this);
972 }
973}
974
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000975void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800976 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000977 // Ensure we emit the literal pool.
978 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000979
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000980 CodeGenerator::Finalize(allocator);
981}
982
Zheng Xuad4450e2015-04-17 18:48:56 +0800983void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
984 // Note: There are 6 kinds of moves:
985 // 1. constant -> GPR/FPR (non-cycle)
986 // 2. constant -> stack (non-cycle)
987 // 3. GPR/FPR -> GPR/FPR
988 // 4. GPR/FPR -> stack
989 // 5. stack -> GPR/FPR
990 // 6. stack -> stack (non-cycle)
991 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
992 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
993 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
994 // dependency.
995 vixl_temps_.Open(GetVIXLAssembler());
996}
997
998void ParallelMoveResolverARM64::FinishEmitNativeCode() {
999 vixl_temps_.Close();
1000}
1001
1002Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1003 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1004 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1005 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1006 Location scratch = GetScratchLocation(kind);
1007 if (!scratch.Equals(Location::NoLocation())) {
1008 return scratch;
1009 }
1010 // Allocate from VIXL temp registers.
1011 if (kind == Location::kRegister) {
1012 scratch = LocationFrom(vixl_temps_.AcquireX());
1013 } else {
1014 DCHECK(kind == Location::kFpuRegister);
1015 scratch = LocationFrom(vixl_temps_.AcquireD());
1016 }
1017 AddScratchLocation(scratch);
1018 return scratch;
1019}
1020
1021void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1022 if (loc.IsRegister()) {
1023 vixl_temps_.Release(XRegisterFrom(loc));
1024 } else {
1025 DCHECK(loc.IsFpuRegister());
1026 vixl_temps_.Release(DRegisterFrom(loc));
1027 }
1028 RemoveScratchLocation(loc);
1029}
1030
Alexandre Rames3e69f162014-12-10 10:36:50 +00001031void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001032 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001033 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001034}
1035
Alexandre Rames5319def2014-10-23 10:03:10 +01001036void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001037 MacroAssembler* masm = GetVIXLAssembler();
1038 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001039 __ Bind(&frame_entry_label_);
1040
Serban Constantinescu02164b32014-11-13 14:05:07 +00001041 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1042 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001043 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001044 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001045 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001046 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001047 __ Ldr(wzr, MemOperand(temp, 0));
1048 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001049 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001050
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001051 if (!HasEmptyFrame()) {
1052 int frame_size = GetFrameSize();
1053 // Stack layout:
1054 // sp[frame_size - 8] : lr.
1055 // ... : other preserved core registers.
1056 // ... : other preserved fp registers.
1057 // ... : reserved frame space.
1058 // sp[0] : current method.
1059 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001061 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1062 frame_size - GetCoreSpillSize());
1063 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1064 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001065 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001066}
1067
1068void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001069 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001070 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001071 if (!HasEmptyFrame()) {
1072 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001073 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1074 frame_size - FrameEntrySpillSize());
1075 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1076 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001077 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001078 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001079 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001080 __ Ret();
1081 GetAssembler()->cfi().RestoreState();
1082 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001083}
1084
Scott Wakeling97c72b72016-06-24 16:19:36 +01001085CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001086 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001087 return CPURegList(CPURegister::kRegister, kXRegSize,
1088 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001089}
1090
Scott Wakeling97c72b72016-06-24 16:19:36 +01001091CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001092 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1093 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001094 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1095 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001096}
1097
Alexandre Rames5319def2014-10-23 10:03:10 +01001098void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1099 __ Bind(GetLabelOf(block));
1100}
1101
Calin Juravle175dc732015-08-25 15:42:32 +01001102void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1103 DCHECK(location.IsRegister());
1104 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1105}
1106
Calin Juravlee460d1d2015-09-29 04:52:17 +01001107void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1108 if (location.IsRegister()) {
1109 locations->AddTemp(location);
1110 } else {
1111 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1112 }
1113}
1114
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001115void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001116 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001117 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001118 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001119 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001120 if (value_can_be_null) {
1121 __ Cbz(value, &done);
1122 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001123 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001124 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001125 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001126 if (value_can_be_null) {
1127 __ Bind(&done);
1128 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001129}
1130
David Brazdil58282f42016-01-14 12:45:10 +00001131void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001132 // Blocked core registers:
1133 // lr : Runtime reserved.
1134 // tr : Runtime reserved.
1135 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1136 // ip1 : VIXL core temp.
1137 // ip0 : VIXL core temp.
1138 //
1139 // Blocked fp registers:
1140 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1142 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001143 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001144 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001145 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001146
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001147 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001148 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001149 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001150 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001151
David Brazdil58282f42016-01-14 12:45:10 +00001152 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001153 // Stubs do not save callee-save floating point registers. If the graph
1154 // is debuggable, we need to deal with these registers differently. For
1155 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001156 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1157 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001158 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001159 }
1160 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001161}
1162
Alexandre Rames3e69f162014-12-10 10:36:50 +00001163size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1164 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1165 __ Str(reg, MemOperand(sp, stack_index));
1166 return kArm64WordSize;
1167}
1168
1169size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1170 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1171 __ Ldr(reg, MemOperand(sp, stack_index));
1172 return kArm64WordSize;
1173}
1174
1175size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1176 FPRegister reg = FPRegister(reg_id, kDRegSize);
1177 __ Str(reg, MemOperand(sp, stack_index));
1178 return kArm64WordSize;
1179}
1180
1181size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1182 FPRegister reg = FPRegister(reg_id, kDRegSize);
1183 __ Ldr(reg, MemOperand(sp, stack_index));
1184 return kArm64WordSize;
1185}
1186
Alexandre Rames5319def2014-10-23 10:03:10 +01001187void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001188 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001189}
1190
1191void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001192 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001193}
1194
Alexandre Rames67555f72014-11-18 10:55:16 +00001195void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001196 if (constant->IsIntConstant()) {
1197 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1198 } else if (constant->IsLongConstant()) {
1199 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1200 } else if (constant->IsNullConstant()) {
1201 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001202 } else if (constant->IsFloatConstant()) {
1203 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1204 } else {
1205 DCHECK(constant->IsDoubleConstant());
1206 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1207 }
1208}
1209
Alexandre Rames3e69f162014-12-10 10:36:50 +00001210
1211static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1212 DCHECK(constant.IsConstant());
1213 HConstant* cst = constant.GetConstant();
1214 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001215 // Null is mapped to a core W register, which we associate with kPrimInt.
1216 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001217 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1218 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1219 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1220}
1221
Calin Juravlee460d1d2015-09-29 04:52:17 +01001222void CodeGeneratorARM64::MoveLocation(Location destination,
1223 Location source,
1224 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001225 if (source.Equals(destination)) {
1226 return;
1227 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001228
1229 // A valid move can always be inferred from the destination and source
1230 // locations. When moving from and to a register, the argument type can be
1231 // used to generate 32bit instead of 64bit moves. In debug mode we also
1232 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001234
1235 if (destination.IsRegister() || destination.IsFpuRegister()) {
1236 if (unspecified_type) {
1237 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1238 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001239 (src_cst != nullptr && (src_cst->IsIntConstant()
1240 || src_cst->IsFloatConstant()
1241 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001242 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001243 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001244 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 // If the source is a double stack slot or a 64bit constant, a 64bit
1246 // type is appropriate. Else the source is a register, and since the
1247 // type has not been specified, we chose a 64bit type to force a 64bit
1248 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001249 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001250 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001251 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1253 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1254 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001255 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1256 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1257 __ Ldr(dst, StackOperandFrom(source));
1258 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001260 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001262 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001263 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001264 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001265 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1267 ? Primitive::kPrimLong
1268 : Primitive::kPrimInt;
1269 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1270 }
1271 } else {
1272 DCHECK(source.IsFpuRegister());
1273 if (destination.IsRegister()) {
1274 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1275 ? Primitive::kPrimDouble
1276 : Primitive::kPrimFloat;
1277 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1278 } else {
1279 DCHECK(destination.IsFpuRegister());
1280 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 }
1282 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001283 } else { // The destination is not a register. It must be a stack slot.
1284 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1285 if (source.IsRegister() || source.IsFpuRegister()) {
1286 if (unspecified_type) {
1287 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001288 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001289 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001291 }
1292 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1294 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1295 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001296 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001297 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1298 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001299 UseScratchRegisterScope temps(GetVIXLAssembler());
1300 HConstant* src_cst = source.GetConstant();
1301 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001302 if (src_cst->IsZeroBitPattern()) {
1303 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001304 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001305 if (src_cst->IsIntConstant()) {
1306 temp = temps.AcquireW();
1307 } else if (src_cst->IsLongConstant()) {
1308 temp = temps.AcquireX();
1309 } else if (src_cst->IsFloatConstant()) {
1310 temp = temps.AcquireS();
1311 } else {
1312 DCHECK(src_cst->IsDoubleConstant());
1313 temp = temps.AcquireD();
1314 }
1315 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001316 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001317 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001318 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001320 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001321 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001322 // There is generally less pressure on FP registers.
1323 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001324 __ Ldr(temp, StackOperandFrom(source));
1325 __ Str(temp, StackOperandFrom(destination));
1326 }
1327 }
1328}
1329
1330void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001331 CPURegister dst,
1332 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001333 switch (type) {
1334 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001335 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001336 break;
1337 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001338 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 break;
1340 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001341 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001342 break;
1343 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001344 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001345 break;
1346 case Primitive::kPrimInt:
1347 case Primitive::kPrimNot:
1348 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001349 case Primitive::kPrimFloat:
1350 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001351 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001352 __ Ldr(dst, src);
1353 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001354 case Primitive::kPrimVoid:
1355 LOG(FATAL) << "Unreachable type " << type;
1356 }
1357}
1358
Calin Juravle77520bc2015-01-12 18:45:46 +00001359void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001360 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001361 const MemOperand& src,
1362 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001363 MacroAssembler* masm = GetVIXLAssembler();
1364 BlockPoolsScope block_pools(masm);
1365 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001366 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001367 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001368
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001369 DCHECK(!src.IsPreIndex());
1370 DCHECK(!src.IsPostIndex());
1371
1372 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001373 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001374 MemOperand base = MemOperand(temp_base);
1375 switch (type) {
1376 case Primitive::kPrimBoolean:
1377 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001378 if (needs_null_check) {
1379 MaybeRecordImplicitNullCheck(instruction);
1380 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001381 break;
1382 case Primitive::kPrimByte:
1383 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001384 if (needs_null_check) {
1385 MaybeRecordImplicitNullCheck(instruction);
1386 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001387 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1388 break;
1389 case Primitive::kPrimChar:
1390 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001391 if (needs_null_check) {
1392 MaybeRecordImplicitNullCheck(instruction);
1393 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001394 break;
1395 case Primitive::kPrimShort:
1396 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001397 if (needs_null_check) {
1398 MaybeRecordImplicitNullCheck(instruction);
1399 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001400 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1401 break;
1402 case Primitive::kPrimInt:
1403 case Primitive::kPrimNot:
1404 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001405 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001407 if (needs_null_check) {
1408 MaybeRecordImplicitNullCheck(instruction);
1409 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001410 break;
1411 case Primitive::kPrimFloat:
1412 case Primitive::kPrimDouble: {
1413 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001414 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001415
1416 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1417 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001418 if (needs_null_check) {
1419 MaybeRecordImplicitNullCheck(instruction);
1420 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421 __ Fmov(FPRegister(dst), temp);
1422 break;
1423 }
1424 case Primitive::kPrimVoid:
1425 LOG(FATAL) << "Unreachable type " << type;
1426 }
1427}
1428
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001429void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001430 CPURegister src,
1431 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001432 switch (type) {
1433 case Primitive::kPrimBoolean:
1434 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001435 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001436 break;
1437 case Primitive::kPrimChar:
1438 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001439 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001440 break;
1441 case Primitive::kPrimInt:
1442 case Primitive::kPrimNot:
1443 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001444 case Primitive::kPrimFloat:
1445 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001446 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001447 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001448 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001449 case Primitive::kPrimVoid:
1450 LOG(FATAL) << "Unreachable type " << type;
1451 }
1452}
1453
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001454void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1455 CPURegister src,
1456 const MemOperand& dst) {
1457 UseScratchRegisterScope temps(GetVIXLAssembler());
1458 Register temp_base = temps.AcquireX();
1459
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001460 DCHECK(!dst.IsPreIndex());
1461 DCHECK(!dst.IsPostIndex());
1462
1463 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001464 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001465 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001466 MemOperand base = MemOperand(temp_base);
1467 switch (type) {
1468 case Primitive::kPrimBoolean:
1469 case Primitive::kPrimByte:
1470 __ Stlrb(Register(src), base);
1471 break;
1472 case Primitive::kPrimChar:
1473 case Primitive::kPrimShort:
1474 __ Stlrh(Register(src), base);
1475 break;
1476 case Primitive::kPrimInt:
1477 case Primitive::kPrimNot:
1478 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001479 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001480 __ Stlr(Register(src), base);
1481 break;
1482 case Primitive::kPrimFloat:
1483 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001484 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001485 Register temp_src;
1486 if (src.IsZero()) {
1487 // The zero register is used to avoid synthesizing zero constants.
1488 temp_src = Register(src);
1489 } else {
1490 DCHECK(src.IsFPRegister());
1491 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1492 __ Fmov(temp_src, FPRegister(src));
1493 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001494
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001495 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001496 break;
1497 }
1498 case Primitive::kPrimVoid:
1499 LOG(FATAL) << "Unreachable type " << type;
1500 }
1501}
1502
Calin Juravle175dc732015-08-25 15:42:32 +01001503void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1504 HInstruction* instruction,
1505 uint32_t dex_pc,
1506 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001507 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001508 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001509 if (EntrypointRequiresStackMap(entrypoint)) {
1510 RecordPcInfo(instruction, dex_pc, slow_path);
1511 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001512}
1513
Roland Levillaindec8f632016-07-22 17:10:06 +01001514void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1515 HInstruction* instruction,
1516 SlowPathCode* slow_path) {
1517 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001518 GenerateInvokeRuntime(entry_point_offset);
1519}
1520
1521void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001522 BlockPoolsScope block_pools(GetVIXLAssembler());
1523 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1524 __ Blr(lr);
1525}
1526
Alexandre Rames67555f72014-11-18 10:55:16 +00001527void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001528 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001529 UseScratchRegisterScope temps(GetVIXLAssembler());
1530 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001531 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1532
Serban Constantinescu02164b32014-11-13 14:05:07 +00001533 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001534 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1535 __ Add(temp, class_reg, status_offset);
1536 __ Ldar(temp, HeapOperand(temp));
1537 __ Cmp(temp, mirror::Class::kStatusInitialized);
1538 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001539 __ Bind(slow_path->GetExitLabel());
1540}
Alexandre Rames5319def2014-10-23 10:03:10 +01001541
Roland Levillain44015862016-01-22 11:47:17 +00001542void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001543 BarrierType type = BarrierAll;
1544
1545 switch (kind) {
1546 case MemBarrierKind::kAnyAny:
1547 case MemBarrierKind::kAnyStore: {
1548 type = BarrierAll;
1549 break;
1550 }
1551 case MemBarrierKind::kLoadAny: {
1552 type = BarrierReads;
1553 break;
1554 }
1555 case MemBarrierKind::kStoreStore: {
1556 type = BarrierWrites;
1557 break;
1558 }
1559 default:
1560 LOG(FATAL) << "Unexpected memory barrier " << kind;
1561 }
1562 __ Dmb(InnerShareable, type);
1563}
1564
Serban Constantinescu02164b32014-11-13 14:05:07 +00001565void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1566 HBasicBlock* successor) {
1567 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001568 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1569 if (slow_path == nullptr) {
1570 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1571 instruction->SetSlowPath(slow_path);
1572 codegen_->AddSlowPath(slow_path);
1573 if (successor != nullptr) {
1574 DCHECK(successor->IsLoopHeader());
1575 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1576 }
1577 } else {
1578 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1579 }
1580
Serban Constantinescu02164b32014-11-13 14:05:07 +00001581 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1582 Register temp = temps.AcquireW();
1583
Andreas Gampe542451c2016-07-26 09:02:02 -07001584 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001585 if (successor == nullptr) {
1586 __ Cbnz(temp, slow_path->GetEntryLabel());
1587 __ Bind(slow_path->GetReturnLabel());
1588 } else {
1589 __ Cbz(temp, codegen_->GetLabelOf(successor));
1590 __ B(slow_path->GetEntryLabel());
1591 // slow_path will return to GetLabelOf(successor).
1592 }
1593}
1594
Alexandre Rames5319def2014-10-23 10:03:10 +01001595InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1596 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001597 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001598 assembler_(codegen->GetAssembler()),
1599 codegen_(codegen) {}
1600
1601#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001602 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001603
1604#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1605
1606enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001607 // Using a base helps identify when we hit such breakpoints.
1608 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001609#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1610 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1611#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1612};
1613
1614#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001615 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001616 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1617 } \
1618 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1620 locations->SetOut(Location::Any()); \
1621 }
1622 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1623#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1624
1625#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001626#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001627
Alexandre Rames67555f72014-11-18 10:55:16 +00001628void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001629 DCHECK_EQ(instr->InputCount(), 2U);
1630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1631 Primitive::Type type = instr->GetResultType();
1632 switch (type) {
1633 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001634 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001635 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001636 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001638 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001639
1640 case Primitive::kPrimFloat:
1641 case Primitive::kPrimDouble:
1642 locations->SetInAt(0, Location::RequiresFpuRegister());
1643 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001644 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001646
Alexandre Rames5319def2014-10-23 10:03:10 +01001647 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001648 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001649 }
1650}
1651
Alexandre Rames09a99962015-04-15 11:47:56 +01001652void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001653 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1654
1655 bool object_field_get_with_read_barrier =
1656 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001657 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001658 new (GetGraph()->GetArena()) LocationSummary(instruction,
1659 object_field_get_with_read_barrier ?
1660 LocationSummary::kCallOnSlowPath :
1661 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001662 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001663 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001664 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001665 locations->SetInAt(0, Location::RequiresRegister());
1666 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1667 locations->SetOut(Location::RequiresFpuRegister());
1668 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001669 // The output overlaps for an object field get when read barriers
1670 // are enabled: we do not want the load to overwrite the object's
1671 // location, as we need it to emit the read barrier.
1672 locations->SetOut(
1673 Location::RequiresRegister(),
1674 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001675 }
1676}
1677
1678void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1679 const FieldInfo& field_info) {
1680 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001681 LocationSummary* locations = instruction->GetLocations();
1682 Location base_loc = locations->InAt(0);
1683 Location out = locations->Out();
1684 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001685 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001686 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001687 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001688
Roland Levillain44015862016-01-22 11:47:17 +00001689 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1690 // Object FieldGet with Baker's read barrier case.
1691 MacroAssembler* masm = GetVIXLAssembler();
1692 UseScratchRegisterScope temps(masm);
1693 // /* HeapReference<Object> */ out = *(base + offset)
1694 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1695 Register temp = temps.AcquireW();
1696 // Note that potential implicit null checks are handled in this
1697 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1698 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1699 instruction,
1700 out,
1701 base,
1702 offset,
1703 temp,
1704 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001705 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001706 } else {
1707 // General case.
1708 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001709 // Note that a potential implicit null check is handled in this
1710 // CodeGeneratorARM64::LoadAcquire call.
1711 // NB: LoadAcquire will record the pc info if needed.
1712 codegen_->LoadAcquire(
1713 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001714 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001715 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001716 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001717 }
Roland Levillain44015862016-01-22 11:47:17 +00001718 if (field_type == Primitive::kPrimNot) {
1719 // If read barriers are enabled, emit read barriers other than
1720 // Baker's using a slow path (and also unpoison the loaded
1721 // reference, if heap poisoning is enabled).
1722 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1723 }
Roland Levillain4d027112015-07-01 15:41:14 +01001724 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001725}
1726
1727void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1728 LocationSummary* locations =
1729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1730 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001731 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1732 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1733 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001734 locations->SetInAt(1, Location::RequiresFpuRegister());
1735 } else {
1736 locations->SetInAt(1, Location::RequiresRegister());
1737 }
1738}
1739
1740void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001741 const FieldInfo& field_info,
1742 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001743 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001744 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001745
1746 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001747 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001748 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001749 Offset offset = field_info.GetFieldOffset();
1750 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001751
Roland Levillain4d027112015-07-01 15:41:14 +01001752 {
1753 // We use a block to end the scratch scope before the write barrier, thus
1754 // freeing the temporary registers so they can be used in `MarkGCCard`.
1755 UseScratchRegisterScope temps(GetVIXLAssembler());
1756
1757 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1758 DCHECK(value.IsW());
1759 Register temp = temps.AcquireW();
1760 __ Mov(temp, value.W());
1761 GetAssembler()->PoisonHeapReference(temp.W());
1762 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001763 }
Roland Levillain4d027112015-07-01 15:41:14 +01001764
1765 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001766 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001768 } else {
1769 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1770 codegen_->MaybeRecordImplicitNullCheck(instruction);
1771 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001772 }
1773
1774 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001775 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001776 }
1777}
1778
Alexandre Rames67555f72014-11-18 10:55:16 +00001779void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001780 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001781
1782 switch (type) {
1783 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001784 case Primitive::kPrimLong: {
1785 Register dst = OutputRegister(instr);
1786 Register lhs = InputRegisterAt(instr, 0);
1787 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001788 if (instr->IsAdd()) {
1789 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001790 } else if (instr->IsAnd()) {
1791 __ And(dst, lhs, rhs);
1792 } else if (instr->IsOr()) {
1793 __ Orr(dst, lhs, rhs);
1794 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001795 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001796 } else if (instr->IsRor()) {
1797 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001798 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001799 __ Ror(dst, lhs, shift);
1800 } else {
1801 // Ensure shift distance is in the same size register as the result. If
1802 // we are rotating a long and the shift comes in a w register originally,
1803 // we don't need to sxtw for use as an x since the shift distances are
1804 // all & reg_bits - 1.
1805 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1806 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001807 } else {
1808 DCHECK(instr->IsXor());
1809 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001810 }
1811 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001812 }
1813 case Primitive::kPrimFloat:
1814 case Primitive::kPrimDouble: {
1815 FPRegister dst = OutputFPRegister(instr);
1816 FPRegister lhs = InputFPRegisterAt(instr, 0);
1817 FPRegister rhs = InputFPRegisterAt(instr, 1);
1818 if (instr->IsAdd()) {
1819 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001820 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001821 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001822 } else {
1823 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001824 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001825 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001826 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001827 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001828 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001829 }
1830}
1831
Serban Constantinescu02164b32014-11-13 14:05:07 +00001832void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1833 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1834
1835 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1836 Primitive::Type type = instr->GetResultType();
1837 switch (type) {
1838 case Primitive::kPrimInt:
1839 case Primitive::kPrimLong: {
1840 locations->SetInAt(0, Location::RequiresRegister());
1841 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1842 locations->SetOut(Location::RequiresRegister());
1843 break;
1844 }
1845 default:
1846 LOG(FATAL) << "Unexpected shift type " << type;
1847 }
1848}
1849
1850void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1851 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1852
1853 Primitive::Type type = instr->GetType();
1854 switch (type) {
1855 case Primitive::kPrimInt:
1856 case Primitive::kPrimLong: {
1857 Register dst = OutputRegister(instr);
1858 Register lhs = InputRegisterAt(instr, 0);
1859 Operand rhs = InputOperandAt(instr, 1);
1860 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001861 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001862 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001863 if (instr->IsShl()) {
1864 __ Lsl(dst, lhs, shift_value);
1865 } else if (instr->IsShr()) {
1866 __ Asr(dst, lhs, shift_value);
1867 } else {
1868 __ Lsr(dst, lhs, shift_value);
1869 }
1870 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001871 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001872
1873 if (instr->IsShl()) {
1874 __ Lsl(dst, lhs, rhs_reg);
1875 } else if (instr->IsShr()) {
1876 __ Asr(dst, lhs, rhs_reg);
1877 } else {
1878 __ Lsr(dst, lhs, rhs_reg);
1879 }
1880 }
1881 break;
1882 }
1883 default:
1884 LOG(FATAL) << "Unexpected shift operation type " << type;
1885 }
1886}
1887
Alexandre Rames5319def2014-10-23 10:03:10 +01001888void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001889 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001890}
1891
1892void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001893 HandleBinaryOp(instruction);
1894}
1895
1896void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1897 HandleBinaryOp(instruction);
1898}
1899
1900void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1901 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001902}
1903
Artem Serov7fc63502016-02-09 17:15:29 +00001904void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001905 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1906 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1907 locations->SetInAt(0, Location::RequiresRegister());
1908 // There is no immediate variant of negated bitwise instructions in AArch64.
1909 locations->SetInAt(1, Location::RequiresRegister());
1910 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1911}
1912
Artem Serov7fc63502016-02-09 17:15:29 +00001913void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001914 Register dst = OutputRegister(instr);
1915 Register lhs = InputRegisterAt(instr, 0);
1916 Register rhs = InputRegisterAt(instr, 1);
1917
1918 switch (instr->GetOpKind()) {
1919 case HInstruction::kAnd:
1920 __ Bic(dst, lhs, rhs);
1921 break;
1922 case HInstruction::kOr:
1923 __ Orn(dst, lhs, rhs);
1924 break;
1925 case HInstruction::kXor:
1926 __ Eon(dst, lhs, rhs);
1927 break;
1928 default:
1929 LOG(FATAL) << "Unreachable";
1930 }
1931}
1932
Alexandre Rames8626b742015-11-25 16:28:08 +00001933void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1934 HArm64DataProcWithShifterOp* instruction) {
1935 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1936 instruction->GetType() == Primitive::kPrimLong);
1937 LocationSummary* locations =
1938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1939 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1940 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1941 } else {
1942 locations->SetInAt(0, Location::RequiresRegister());
1943 }
1944 locations->SetInAt(1, Location::RequiresRegister());
1945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1946}
1947
1948void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1949 HArm64DataProcWithShifterOp* instruction) {
1950 Primitive::Type type = instruction->GetType();
1951 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1952 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1953 Register out = OutputRegister(instruction);
1954 Register left;
1955 if (kind != HInstruction::kNeg) {
1956 left = InputRegisterAt(instruction, 0);
1957 }
1958 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1959 // shifter operand operation, the IR generating `right_reg` (input to the type
1960 // conversion) can have a different type from the current instruction's type,
1961 // so we manually indicate the type.
1962 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001963 int64_t shift_amount = instruction->GetShiftAmount() &
1964 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001965
1966 Operand right_operand(0);
1967
1968 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1969 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1970 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1971 } else {
1972 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1973 }
1974
1975 // Logical binary operations do not support extension operations in the
1976 // operand. Note that VIXL would still manage if it was passed by generating
1977 // the extension as a separate instruction.
1978 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1979 DCHECK(!right_operand.IsExtendedRegister() ||
1980 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1981 kind != HInstruction::kNeg));
1982 switch (kind) {
1983 case HInstruction::kAdd:
1984 __ Add(out, left, right_operand);
1985 break;
1986 case HInstruction::kAnd:
1987 __ And(out, left, right_operand);
1988 break;
1989 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001990 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001991 __ Neg(out, right_operand);
1992 break;
1993 case HInstruction::kOr:
1994 __ Orr(out, left, right_operand);
1995 break;
1996 case HInstruction::kSub:
1997 __ Sub(out, left, right_operand);
1998 break;
1999 case HInstruction::kXor:
2000 __ Eor(out, left, right_operand);
2001 break;
2002 default:
2003 LOG(FATAL) << "Unexpected operation kind: " << kind;
2004 UNREACHABLE();
2005 }
2006}
2007
Artem Serov328429f2016-07-06 16:23:04 +01002008void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002009 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2010 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2013 locations->SetInAt(0, Location::RequiresRegister());
2014 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2015 locations->SetOut(Location::RequiresRegister());
2016}
2017
Roland Levillain4a3aa572016-08-15 13:17:06 +00002018void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
2019 HIntermediateAddress* instruction) {
2020 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2021 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002022 __ Add(OutputRegister(instruction),
2023 InputRegisterAt(instruction, 0),
2024 Operand(InputOperandAt(instruction, 1)));
2025}
2026
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002027void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002028 LocationSummary* locations =
2029 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002030 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2031 if (instr->GetOpKind() == HInstruction::kSub &&
2032 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002033 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002034 // Don't allocate register for Mneg instruction.
2035 } else {
2036 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2037 Location::RequiresRegister());
2038 }
2039 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2040 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2042}
2043
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002044void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002045 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002046 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2047 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002048
2049 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2050 // This fixup should be carried out for all multiply-accumulate instructions:
2051 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2052 if (instr->GetType() == Primitive::kPrimLong &&
2053 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2054 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002055 vixl::aarch64::Instruction* prev =
2056 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002057 if (prev->IsLoadOrStore()) {
2058 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002059 vixl::aarch64::CodeBufferCheckScope scope(masm,
2060 kInstructionSize,
2061 vixl::aarch64::CodeBufferCheckScope::kCheck,
2062 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002063 __ nop();
2064 }
2065 }
2066
2067 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002068 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002069 __ Madd(res, mul_left, mul_right, accumulator);
2070 } else {
2071 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002072 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002073 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002074 __ Mneg(res, mul_left, mul_right);
2075 } else {
2076 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2077 __ Msub(res, mul_left, mul_right, accumulator);
2078 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002079 }
2080}
2081
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002082void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002083 bool object_array_get_with_read_barrier =
2084 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002085 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002086 new (GetGraph()->GetArena()) LocationSummary(instruction,
2087 object_array_get_with_read_barrier ?
2088 LocationSummary::kCallOnSlowPath :
2089 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002090 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002091 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002092 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002093 locations->SetInAt(0, Location::RequiresRegister());
2094 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002095 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2096 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2097 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002098 // The output overlaps in the case of an object array get with
2099 // read barriers enabled: we do not want the move to overwrite the
2100 // array's location, as we need it to emit the read barrier.
2101 locations->SetOut(
2102 Location::RequiresRegister(),
2103 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002104 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002105}
2106
2107void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002108 Primitive::Type type = instruction->GetType();
2109 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002110 LocationSummary* locations = instruction->GetLocations();
2111 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002112 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002113 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002114 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2115 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002116 MacroAssembler* masm = GetVIXLAssembler();
2117 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002118 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002119 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002120
Roland Levillain44015862016-01-22 11:47:17 +00002121 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2122 // Object ArrayGet with Baker's read barrier case.
2123 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002124 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2125 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002126 // Note that a potential implicit null check is handled in the
2127 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2128 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2129 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002130 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002131 // General case.
2132 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002133 Register length;
2134 if (maybe_compressed_char_at) {
2135 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2136 length = temps.AcquireW();
2137 __ Ldr(length, HeapOperand(obj, count_offset));
2138 codegen_->MaybeRecordImplicitNullCheck(instruction);
2139 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002140 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002141 if (maybe_compressed_char_at) {
2142 vixl::aarch64::Label uncompressed_load, done;
2143 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2144 __ Ldrb(Register(OutputCPURegister(instruction)),
2145 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2146 __ B(&done);
2147 __ Bind(&uncompressed_load);
2148 __ Ldrh(Register(OutputCPURegister(instruction)),
2149 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2150 __ Bind(&done);
2151 } else {
2152 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2153 source = HeapOperand(obj, offset);
2154 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002155 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002156 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002157 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002158 // The read barrier instrumentation does not support the
2159 // HIntermediateAddress instruction yet.
2160 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002161 // We do not need to compute the intermediate address from the array: the
2162 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002163 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002164 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002165 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002166 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2167 }
2168 temp = obj;
2169 } else {
2170 __ Add(temp, obj, offset);
2171 }
jessicahandojo05765752016-09-09 19:01:32 -07002172 if (maybe_compressed_char_at) {
2173 vixl::aarch64::Label uncompressed_load, done;
2174 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2175 __ Ldrb(Register(OutputCPURegister(instruction)),
2176 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2177 __ B(&done);
2178 __ Bind(&uncompressed_load);
2179 __ Ldrh(Register(OutputCPURegister(instruction)),
2180 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2181 __ Bind(&done);
2182 } else {
2183 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2184 }
Roland Levillain44015862016-01-22 11:47:17 +00002185 }
jessicahandojo05765752016-09-09 19:01:32 -07002186 if (!maybe_compressed_char_at) {
2187 codegen_->Load(type, OutputCPURegister(instruction), source);
2188 codegen_->MaybeRecordImplicitNullCheck(instruction);
2189 }
Roland Levillain44015862016-01-22 11:47:17 +00002190
2191 if (type == Primitive::kPrimNot) {
2192 static_assert(
2193 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2194 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2195 Location obj_loc = locations->InAt(0);
2196 if (index.IsConstant()) {
2197 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2198 } else {
2199 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2200 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002201 }
Roland Levillain4d027112015-07-01 15:41:14 +01002202 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002203}
2204
Alexandre Rames5319def2014-10-23 10:03:10 +01002205void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2207 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002209}
2210
2211void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002212 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002213 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002214 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002215 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002216 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002217 // Mask out compression flag from String's array length.
2218 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2219 __ And(out.W(), out.W(), Operand(static_cast<int32_t>(INT32_MAX)));
2220 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002221}
2222
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002223void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002224 Primitive::Type value_type = instruction->GetComponentType();
2225
2226 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2228 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002229 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002230 LocationSummary::kCallOnSlowPath :
2231 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002232 locations->SetInAt(0, Location::RequiresRegister());
2233 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002234 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2235 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2236 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002237 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002238 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002239 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002240 }
2241}
2242
2243void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2244 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002245 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002246 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002247 bool needs_write_barrier =
2248 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002249
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002250 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002251 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002252 CPURegister source = value;
2253 Location index = locations->InAt(1);
2254 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2255 MemOperand destination = HeapOperand(array);
2256 MacroAssembler* masm = GetVIXLAssembler();
2257 BlockPoolsScope block_pools(masm);
2258
2259 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002260 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002261 if (index.IsConstant()) {
2262 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2263 destination = HeapOperand(array, offset);
2264 } else {
2265 UseScratchRegisterScope temps(masm);
2266 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002267 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002268 // The read barrier instrumentation does not support the
2269 // HIntermediateAddress instruction yet.
2270 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002271 // We do not need to compute the intermediate address from the array: the
2272 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002273 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002274 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002275 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002276 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2277 }
2278 temp = array;
2279 } else {
2280 __ Add(temp, array, offset);
2281 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002282 destination = HeapOperand(temp,
2283 XRegisterFrom(index),
2284 LSL,
2285 Primitive::ComponentSizeShift(value_type));
2286 }
2287 codegen_->Store(value_type, value, destination);
2288 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002289 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002290 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002291 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002292 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002293 {
2294 // We use a block to end the scratch scope before the write barrier, thus
2295 // freeing the temporary registers so they can be used in `MarkGCCard`.
2296 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002297 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002298 if (index.IsConstant()) {
2299 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002300 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002301 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002302 destination = HeapOperand(temp,
2303 XRegisterFrom(index),
2304 LSL,
2305 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002306 }
2307
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002308 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2309 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2310 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2311
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002312 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002313 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2314 codegen_->AddSlowPath(slow_path);
2315 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002316 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002317 __ Cbnz(Register(value), &non_zero);
2318 if (!index.IsConstant()) {
2319 __ Add(temp, array, offset);
2320 }
2321 __ Str(wzr, destination);
2322 codegen_->MaybeRecordImplicitNullCheck(instruction);
2323 __ B(&done);
2324 __ Bind(&non_zero);
2325 }
2326
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002327 // Note that when Baker read barriers are enabled, the type
2328 // checks are performed without read barriers. This is fine,
2329 // even in the case where a class object is in the from-space
2330 // after the flip, as a comparison involving such a type would
2331 // not produce a false positive; it may of course produce a
2332 // false negative, in which case we would take the ArraySet
2333 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002334
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002335 Register temp2 = temps.AcquireSameSizeAs(array);
2336 // /* HeapReference<Class> */ temp = array->klass_
2337 __ Ldr(temp, HeapOperand(array, class_offset));
2338 codegen_->MaybeRecordImplicitNullCheck(instruction);
2339 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002340
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002341 // /* HeapReference<Class> */ temp = temp->component_type_
2342 __ Ldr(temp, HeapOperand(temp, component_offset));
2343 // /* HeapReference<Class> */ temp2 = value->klass_
2344 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2345 // If heap poisoning is enabled, no need to unpoison `temp`
2346 // nor `temp2`, as we are comparing two poisoned references.
2347 __ Cmp(temp, temp2);
2348 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002349
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002350 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2351 vixl::aarch64::Label do_put;
2352 __ B(eq, &do_put);
2353 // If heap poisoning is enabled, the `temp` reference has
2354 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002355 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2356
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002357 // /* HeapReference<Class> */ temp = temp->super_class_
2358 __ Ldr(temp, HeapOperand(temp, super_offset));
2359 // If heap poisoning is enabled, no need to unpoison
2360 // `temp`, as we are comparing against null below.
2361 __ Cbnz(temp, slow_path->GetEntryLabel());
2362 __ Bind(&do_put);
2363 } else {
2364 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002365 }
2366 }
2367
2368 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002369 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002370 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002371 __ Mov(temp2, value.W());
2372 GetAssembler()->PoisonHeapReference(temp2);
2373 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002374 }
2375
2376 if (!index.IsConstant()) {
2377 __ Add(temp, array, offset);
2378 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002379 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002380
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002381 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002382 codegen_->MaybeRecordImplicitNullCheck(instruction);
2383 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002384 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002385
2386 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2387
2388 if (done.IsLinked()) {
2389 __ Bind(&done);
2390 }
2391
2392 if (slow_path != nullptr) {
2393 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002394 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002395 }
2396}
2397
Alexandre Rames67555f72014-11-18 10:55:16 +00002398void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002399 RegisterSet caller_saves = RegisterSet::Empty();
2400 InvokeRuntimeCallingConvention calling_convention;
2401 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2402 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2403 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002404 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002405 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002406}
2407
2408void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002409 BoundsCheckSlowPathARM64* slow_path =
2410 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002411 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002412 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2413 __ B(slow_path->GetEntryLabel(), hs);
2414}
2415
Alexandre Rames67555f72014-11-18 10:55:16 +00002416void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2417 LocationSummary* locations =
2418 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2419 locations->SetInAt(0, Location::RequiresRegister());
2420 if (check->HasUses()) {
2421 locations->SetOut(Location::SameAsFirstInput());
2422 }
2423}
2424
2425void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2426 // We assume the class is not null.
2427 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2428 check->GetLoadClass(), check, check->GetDexPc(), true);
2429 codegen_->AddSlowPath(slow_path);
2430 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2431}
2432
Roland Levillain1a653882016-03-18 18:05:57 +00002433static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2434 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2435 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2436}
2437
2438void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2439 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2440 Location rhs_loc = instruction->GetLocations()->InAt(1);
2441 if (rhs_loc.IsConstant()) {
2442 // 0.0 is the only immediate that can be encoded directly in
2443 // an FCMP instruction.
2444 //
2445 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2446 // specify that in a floating-point comparison, positive zero
2447 // and negative zero are considered equal, so we can use the
2448 // literal 0.0 for both cases here.
2449 //
2450 // Note however that some methods (Float.equal, Float.compare,
2451 // Float.compareTo, Double.equal, Double.compare,
2452 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2453 // StrictMath.min) consider 0.0 to be (strictly) greater than
2454 // -0.0. So if we ever translate calls to these methods into a
2455 // HCompare instruction, we must handle the -0.0 case with
2456 // care here.
2457 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2458 __ Fcmp(lhs_reg, 0.0);
2459 } else {
2460 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2461 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002462}
2463
Serban Constantinescu02164b32014-11-13 14:05:07 +00002464void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002465 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002466 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2467 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002469 case Primitive::kPrimBoolean:
2470 case Primitive::kPrimByte:
2471 case Primitive::kPrimShort:
2472 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002473 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002474 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002475 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002476 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2478 break;
2479 }
2480 case Primitive::kPrimFloat:
2481 case Primitive::kPrimDouble: {
2482 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002483 locations->SetInAt(1,
2484 IsFloatingPointZeroConstant(compare->InputAt(1))
2485 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2486 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002487 locations->SetOut(Location::RequiresRegister());
2488 break;
2489 }
2490 default:
2491 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2492 }
2493}
2494
2495void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2496 Primitive::Type in_type = compare->InputAt(0)->GetType();
2497
2498 // 0 if: left == right
2499 // 1 if: left > right
2500 // -1 if: left < right
2501 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002502 case Primitive::kPrimBoolean:
2503 case Primitive::kPrimByte:
2504 case Primitive::kPrimShort:
2505 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002506 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002507 case Primitive::kPrimLong: {
2508 Register result = OutputRegister(compare);
2509 Register left = InputRegisterAt(compare, 0);
2510 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002511 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002512 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2513 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002514 break;
2515 }
2516 case Primitive::kPrimFloat:
2517 case Primitive::kPrimDouble: {
2518 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002519 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002520 __ Cset(result, ne);
2521 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002522 break;
2523 }
2524 default:
2525 LOG(FATAL) << "Unimplemented compare type " << in_type;
2526 }
2527}
2528
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002529void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002531
2532 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2533 locations->SetInAt(0, Location::RequiresFpuRegister());
2534 locations->SetInAt(1,
2535 IsFloatingPointZeroConstant(instruction->InputAt(1))
2536 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2537 : Location::RequiresFpuRegister());
2538 } else {
2539 // Integer cases.
2540 locations->SetInAt(0, Location::RequiresRegister());
2541 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2542 }
2543
David Brazdilb3e773e2016-01-26 11:28:37 +00002544 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002546 }
2547}
2548
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002549void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002550 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002551 return;
2552 }
2553
2554 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002555 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002556 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002557
Roland Levillain7f63c522015-07-13 15:54:55 +00002558 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002559 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002560 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002561 } else {
2562 // Integer cases.
2563 Register lhs = InputRegisterAt(instruction, 0);
2564 Operand rhs = InputOperandAt(instruction, 1);
2565 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002566 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002567 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002568}
2569
2570#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2571 M(Equal) \
2572 M(NotEqual) \
2573 M(LessThan) \
2574 M(LessThanOrEqual) \
2575 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002576 M(GreaterThanOrEqual) \
2577 M(Below) \
2578 M(BelowOrEqual) \
2579 M(Above) \
2580 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002581#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002582void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2583void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002584FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002585#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002586#undef FOR_EACH_CONDITION_INSTRUCTION
2587
Zheng Xuc6667102015-05-15 16:08:45 +08002588void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2589 DCHECK(instruction->IsDiv() || instruction->IsRem());
2590
2591 LocationSummary* locations = instruction->GetLocations();
2592 Location second = locations->InAt(1);
2593 DCHECK(second.IsConstant());
2594
2595 Register out = OutputRegister(instruction);
2596 Register dividend = InputRegisterAt(instruction, 0);
2597 int64_t imm = Int64FromConstant(second.GetConstant());
2598 DCHECK(imm == 1 || imm == -1);
2599
2600 if (instruction->IsRem()) {
2601 __ Mov(out, 0);
2602 } else {
2603 if (imm == 1) {
2604 __ Mov(out, dividend);
2605 } else {
2606 __ Neg(out, dividend);
2607 }
2608 }
2609}
2610
2611void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2612 DCHECK(instruction->IsDiv() || instruction->IsRem());
2613
2614 LocationSummary* locations = instruction->GetLocations();
2615 Location second = locations->InAt(1);
2616 DCHECK(second.IsConstant());
2617
2618 Register out = OutputRegister(instruction);
2619 Register dividend = InputRegisterAt(instruction, 0);
2620 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002621 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002622 int ctz_imm = CTZ(abs_imm);
2623
2624 UseScratchRegisterScope temps(GetVIXLAssembler());
2625 Register temp = temps.AcquireSameSizeAs(out);
2626
2627 if (instruction->IsDiv()) {
2628 __ Add(temp, dividend, abs_imm - 1);
2629 __ Cmp(dividend, 0);
2630 __ Csel(out, temp, dividend, lt);
2631 if (imm > 0) {
2632 __ Asr(out, out, ctz_imm);
2633 } else {
2634 __ Neg(out, Operand(out, ASR, ctz_imm));
2635 }
2636 } else {
2637 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2638 __ Asr(temp, dividend, bits - 1);
2639 __ Lsr(temp, temp, bits - ctz_imm);
2640 __ Add(out, dividend, temp);
2641 __ And(out, out, abs_imm - 1);
2642 __ Sub(out, out, temp);
2643 }
2644}
2645
2646void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2647 DCHECK(instruction->IsDiv() || instruction->IsRem());
2648
2649 LocationSummary* locations = instruction->GetLocations();
2650 Location second = locations->InAt(1);
2651 DCHECK(second.IsConstant());
2652
2653 Register out = OutputRegister(instruction);
2654 Register dividend = InputRegisterAt(instruction, 0);
2655 int64_t imm = Int64FromConstant(second.GetConstant());
2656
2657 Primitive::Type type = instruction->GetResultType();
2658 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2659
2660 int64_t magic;
2661 int shift;
2662 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2663
2664 UseScratchRegisterScope temps(GetVIXLAssembler());
2665 Register temp = temps.AcquireSameSizeAs(out);
2666
2667 // temp = get_high(dividend * magic)
2668 __ Mov(temp, magic);
2669 if (type == Primitive::kPrimLong) {
2670 __ Smulh(temp, dividend, temp);
2671 } else {
2672 __ Smull(temp.X(), dividend, temp);
2673 __ Lsr(temp.X(), temp.X(), 32);
2674 }
2675
2676 if (imm > 0 && magic < 0) {
2677 __ Add(temp, temp, dividend);
2678 } else if (imm < 0 && magic > 0) {
2679 __ Sub(temp, temp, dividend);
2680 }
2681
2682 if (shift != 0) {
2683 __ Asr(temp, temp, shift);
2684 }
2685
2686 if (instruction->IsDiv()) {
2687 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2688 } else {
2689 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2690 // TODO: Strength reduction for msub.
2691 Register temp_imm = temps.AcquireSameSizeAs(out);
2692 __ Mov(temp_imm, imm);
2693 __ Msub(out, temp, temp_imm, dividend);
2694 }
2695}
2696
2697void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2698 DCHECK(instruction->IsDiv() || instruction->IsRem());
2699 Primitive::Type type = instruction->GetResultType();
2700 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2701
2702 LocationSummary* locations = instruction->GetLocations();
2703 Register out = OutputRegister(instruction);
2704 Location second = locations->InAt(1);
2705
2706 if (second.IsConstant()) {
2707 int64_t imm = Int64FromConstant(second.GetConstant());
2708
2709 if (imm == 0) {
2710 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2711 } else if (imm == 1 || imm == -1) {
2712 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002713 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002714 DivRemByPowerOfTwo(instruction);
2715 } else {
2716 DCHECK(imm <= -2 || imm >= 2);
2717 GenerateDivRemWithAnyConstant(instruction);
2718 }
2719 } else {
2720 Register dividend = InputRegisterAt(instruction, 0);
2721 Register divisor = InputRegisterAt(instruction, 1);
2722 if (instruction->IsDiv()) {
2723 __ Sdiv(out, dividend, divisor);
2724 } else {
2725 UseScratchRegisterScope temps(GetVIXLAssembler());
2726 Register temp = temps.AcquireSameSizeAs(out);
2727 __ Sdiv(temp, dividend, divisor);
2728 __ Msub(out, temp, divisor, dividend);
2729 }
2730 }
2731}
2732
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002733void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2734 LocationSummary* locations =
2735 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2736 switch (div->GetResultType()) {
2737 case Primitive::kPrimInt:
2738 case Primitive::kPrimLong:
2739 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002740 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2742 break;
2743
2744 case Primitive::kPrimFloat:
2745 case Primitive::kPrimDouble:
2746 locations->SetInAt(0, Location::RequiresFpuRegister());
2747 locations->SetInAt(1, Location::RequiresFpuRegister());
2748 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2749 break;
2750
2751 default:
2752 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2753 }
2754}
2755
2756void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2757 Primitive::Type type = div->GetResultType();
2758 switch (type) {
2759 case Primitive::kPrimInt:
2760 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002761 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002762 break;
2763
2764 case Primitive::kPrimFloat:
2765 case Primitive::kPrimDouble:
2766 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2767 break;
2768
2769 default:
2770 LOG(FATAL) << "Unexpected div type " << type;
2771 }
2772}
2773
Alexandre Rames67555f72014-11-18 10:55:16 +00002774void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002775 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002776 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002777}
2778
2779void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2780 SlowPathCodeARM64* slow_path =
2781 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2782 codegen_->AddSlowPath(slow_path);
2783 Location value = instruction->GetLocations()->InAt(0);
2784
Alexandre Rames3e69f162014-12-10 10:36:50 +00002785 Primitive::Type type = instruction->GetType();
2786
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002787 if (!Primitive::IsIntegralType(type)) {
2788 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002789 return;
2790 }
2791
Alexandre Rames67555f72014-11-18 10:55:16 +00002792 if (value.IsConstant()) {
2793 int64_t divisor = Int64ConstantFrom(value);
2794 if (divisor == 0) {
2795 __ B(slow_path->GetEntryLabel());
2796 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002797 // A division by a non-null constant is valid. We don't need to perform
2798 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002799 }
2800 } else {
2801 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2802 }
2803}
2804
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002805void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2806 LocationSummary* locations =
2807 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2808 locations->SetOut(Location::ConstantLocation(constant));
2809}
2810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002811void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2812 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002813 // Will be generated at use site.
2814}
2815
Alexandre Rames5319def2014-10-23 10:03:10 +01002816void LocationsBuilderARM64::VisitExit(HExit* exit) {
2817 exit->SetLocations(nullptr);
2818}
2819
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002820void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002821}
2822
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002823void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2824 LocationSummary* locations =
2825 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2826 locations->SetOut(Location::ConstantLocation(constant));
2827}
2828
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002829void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002830 // Will be generated at use site.
2831}
2832
David Brazdilfc6a86a2015-06-26 10:33:45 +00002833void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002834 DCHECK(!successor->IsExitBlock());
2835 HBasicBlock* block = got->GetBlock();
2836 HInstruction* previous = got->GetPrevious();
2837 HLoopInformation* info = block->GetLoopInformation();
2838
David Brazdil46e2a392015-03-16 17:31:52 +00002839 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002840 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2841 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2842 return;
2843 }
2844 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2845 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2846 }
2847 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002848 __ B(codegen_->GetLabelOf(successor));
2849 }
2850}
2851
David Brazdilfc6a86a2015-06-26 10:33:45 +00002852void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2853 got->SetLocations(nullptr);
2854}
2855
2856void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2857 HandleGoto(got, got->GetSuccessor());
2858}
2859
2860void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2861 try_boundary->SetLocations(nullptr);
2862}
2863
2864void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2865 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2866 if (!successor->IsExitBlock()) {
2867 HandleGoto(try_boundary, successor);
2868 }
2869}
2870
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002871void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002872 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002873 vixl::aarch64::Label* true_target,
2874 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002875 // FP branching requires both targets to be explicit. If either of the targets
2876 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002877 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002878 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002879
David Brazdil0debae72015-11-12 18:37:00 +00002880 if (true_target == nullptr && false_target == nullptr) {
2881 // Nothing to do. The code always falls through.
2882 return;
2883 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002884 // Constant condition, statically compared against "true" (integer value 1).
2885 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002886 if (true_target != nullptr) {
2887 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002888 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002889 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002890 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002891 if (false_target != nullptr) {
2892 __ B(false_target);
2893 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002894 }
David Brazdil0debae72015-11-12 18:37:00 +00002895 return;
2896 }
2897
2898 // The following code generates these patterns:
2899 // (1) true_target == nullptr && false_target != nullptr
2900 // - opposite condition true => branch to false_target
2901 // (2) true_target != nullptr && false_target == nullptr
2902 // - condition true => branch to true_target
2903 // (3) true_target != nullptr && false_target != nullptr
2904 // - condition true => branch to true_target
2905 // - branch to false_target
2906 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002907 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002908 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002909 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002910 if (true_target == nullptr) {
2911 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2912 } else {
2913 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2914 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002915 } else {
2916 // The condition instruction has not been materialized, use its inputs as
2917 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002918 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002919
David Brazdil0debae72015-11-12 18:37:00 +00002920 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002921 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002922 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002923 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002924 IfCondition opposite_condition = condition->GetOppositeCondition();
2925 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002926 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002927 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002928 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002929 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002930 // Integer cases.
2931 Register lhs = InputRegisterAt(condition, 0);
2932 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002933
2934 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002935 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002936 if (true_target == nullptr) {
2937 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2938 non_fallthrough_target = false_target;
2939 } else {
2940 arm64_cond = ARM64Condition(condition->GetCondition());
2941 non_fallthrough_target = true_target;
2942 }
2943
Aart Bik086d27e2016-01-20 17:02:00 -08002944 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002945 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002946 switch (arm64_cond) {
2947 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002948 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002949 break;
2950 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002951 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002952 break;
2953 case lt:
2954 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002955 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002956 break;
2957 case ge:
2958 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002959 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002960 break;
2961 default:
2962 // Without the `static_cast` the compiler throws an error for
2963 // `-Werror=sign-promo`.
2964 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2965 }
2966 } else {
2967 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002968 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002969 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002970 }
2971 }
David Brazdil0debae72015-11-12 18:37:00 +00002972
2973 // If neither branch falls through (case 3), the conditional branch to `true_target`
2974 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2975 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002976 __ B(false_target);
2977 }
David Brazdil0debae72015-11-12 18:37:00 +00002978
2979 if (fallthrough_target.IsLinked()) {
2980 __ Bind(&fallthrough_target);
2981 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002982}
2983
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002984void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002986 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002987 locations->SetInAt(0, Location::RequiresRegister());
2988 }
2989}
2990
2991void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002992 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2993 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002994 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2995 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2996 true_target = nullptr;
2997 }
2998 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2999 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3000 false_target = nullptr;
3001 }
David Brazdil0debae72015-11-12 18:37:00 +00003002 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003003}
3004
3005void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3006 LocationSummary* locations = new (GetGraph()->GetArena())
3007 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003008 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003009 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003010 locations->SetInAt(0, Location::RequiresRegister());
3011 }
3012}
3013
3014void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003015 SlowPathCodeARM64* slow_path =
3016 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003017 GenerateTestAndBranch(deoptimize,
3018 /* condition_input_index */ 0,
3019 slow_path->GetEntryLabel(),
3020 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003021}
3022
David Brazdilc0b601b2016-02-08 14:20:45 +00003023static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3024 return condition->IsCondition() &&
3025 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3026}
3027
Alexandre Rames880f1192016-06-13 16:04:50 +01003028static inline Condition GetConditionForSelect(HCondition* condition) {
3029 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003030 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3031 : ARM64Condition(cond);
3032}
3033
David Brazdil74eb1b22015-12-14 11:44:01 +00003034void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3035 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003036 if (Primitive::IsFloatingPointType(select->GetType())) {
3037 locations->SetInAt(0, Location::RequiresFpuRegister());
3038 locations->SetInAt(1, Location::RequiresFpuRegister());
3039 locations->SetOut(Location::RequiresFpuRegister());
3040 } else {
3041 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3042 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3043 bool is_true_value_constant = cst_true_value != nullptr;
3044 bool is_false_value_constant = cst_false_value != nullptr;
3045 // Ask VIXL whether we should synthesize constants in registers.
3046 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3047 Operand true_op = is_true_value_constant ?
3048 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3049 Operand false_op = is_false_value_constant ?
3050 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3051 bool true_value_in_register = false;
3052 bool false_value_in_register = false;
3053 MacroAssembler::GetCselSynthesisInformation(
3054 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3055 true_value_in_register |= !is_true_value_constant;
3056 false_value_in_register |= !is_false_value_constant;
3057
3058 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3059 : Location::ConstantLocation(cst_true_value));
3060 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3061 : Location::ConstantLocation(cst_false_value));
3062 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003063 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003064
David Brazdil74eb1b22015-12-14 11:44:01 +00003065 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3066 locations->SetInAt(2, Location::RequiresRegister());
3067 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003068}
3069
3070void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003071 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003072 Condition csel_cond;
3073
3074 if (IsBooleanValueOrMaterializedCondition(cond)) {
3075 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003076 // Use the condition flags set by the previous instruction.
3077 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003078 } else {
3079 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003080 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003081 }
3082 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003083 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003084 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003085 } else {
3086 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003087 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003088 }
3089
Alexandre Rames880f1192016-06-13 16:04:50 +01003090 if (Primitive::IsFloatingPointType(select->GetType())) {
3091 __ Fcsel(OutputFPRegister(select),
3092 InputFPRegisterAt(select, 1),
3093 InputFPRegisterAt(select, 0),
3094 csel_cond);
3095 } else {
3096 __ Csel(OutputRegister(select),
3097 InputOperandAt(select, 1),
3098 InputOperandAt(select, 0),
3099 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003100 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003101}
3102
David Srbecky0cf44932015-12-09 14:09:59 +00003103void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3104 new (GetGraph()->GetArena()) LocationSummary(info);
3105}
3106
David Srbeckyd28f4a02016-03-14 17:14:24 +00003107void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3108 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003109}
3110
3111void CodeGeneratorARM64::GenerateNop() {
3112 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003113}
3114
Alexandre Rames5319def2014-10-23 10:03:10 +01003115void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003116 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003117}
3118
3119void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003120 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003121}
3122
3123void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003124 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003125}
3126
3127void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003128 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003129}
3130
Roland Levillain44015862016-01-22 11:47:17 +00003131static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3132 return kEmitCompilerReadBarrier &&
3133 (kUseBakerReadBarrier ||
3134 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3135 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3136 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3137}
3138
Alexandre Rames67555f72014-11-18 10:55:16 +00003139void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003140 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003141 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003142 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003143 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003144 case TypeCheckKind::kExactCheck:
3145 case TypeCheckKind::kAbstractClassCheck:
3146 case TypeCheckKind::kClassHierarchyCheck:
3147 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003148 call_kind =
3149 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003150 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003151 break;
3152 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003153 case TypeCheckKind::kUnresolvedCheck:
3154 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003155 call_kind = LocationSummary::kCallOnSlowPath;
3156 break;
3157 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003158
Alexandre Rames67555f72014-11-18 10:55:16 +00003159 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003160 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003161 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003162 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003163 locations->SetInAt(0, Location::RequiresRegister());
3164 locations->SetInAt(1, Location::RequiresRegister());
3165 // The "out" register is used as a temporary, so it overlaps with the inputs.
3166 // Note that TypeCheckSlowPathARM64 uses this register too.
3167 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3168 // When read barriers are enabled, we need a temporary register for
3169 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003170 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003171 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003172 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003173}
3174
3175void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003176 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003177 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003178 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003179 Register obj = InputRegisterAt(instruction, 0);
3180 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003181 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003182 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003183 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3184 locations->GetTemp(0) :
3185 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003186 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3187 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3188 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3189 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003190
Scott Wakeling97c72b72016-06-24 16:19:36 +01003191 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003192 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003193
3194 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003195 // Avoid null check if we know `obj` is not null.
3196 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003197 __ Cbz(obj, &zero);
3198 }
3199
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003200 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003201 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003202
Roland Levillain44015862016-01-22 11:47:17 +00003203 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003204 case TypeCheckKind::kExactCheck: {
3205 __ Cmp(out, cls);
3206 __ Cset(out, eq);
3207 if (zero.IsLinked()) {
3208 __ B(&done);
3209 }
3210 break;
3211 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003213 case TypeCheckKind::kAbstractClassCheck: {
3214 // If the class is abstract, we eagerly fetch the super class of the
3215 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003216 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003217 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003218 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003219 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003220 // If `out` is null, we use it for the result, and jump to `done`.
3221 __ Cbz(out, &done);
3222 __ Cmp(out, cls);
3223 __ B(ne, &loop);
3224 __ Mov(out, 1);
3225 if (zero.IsLinked()) {
3226 __ B(&done);
3227 }
3228 break;
3229 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003230
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003231 case TypeCheckKind::kClassHierarchyCheck: {
3232 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003233 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003234 __ Bind(&loop);
3235 __ Cmp(out, cls);
3236 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003237 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003238 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003239 __ Cbnz(out, &loop);
3240 // If `out` is null, we use it for the result, and jump to `done`.
3241 __ B(&done);
3242 __ Bind(&success);
3243 __ Mov(out, 1);
3244 if (zero.IsLinked()) {
3245 __ B(&done);
3246 }
3247 break;
3248 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003249
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003250 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003251 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003252 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003253 __ Cmp(out, cls);
3254 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003255 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003256 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003257 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003258 // If `out` is null, we use it for the result, and jump to `done`.
3259 __ Cbz(out, &done);
3260 __ Ldrh(out, HeapOperand(out, primitive_offset));
3261 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3262 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003263 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003264 __ Mov(out, 1);
3265 __ B(&done);
3266 break;
3267 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003268
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003269 case TypeCheckKind::kArrayCheck: {
3270 __ Cmp(out, cls);
3271 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003272 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3273 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003274 codegen_->AddSlowPath(slow_path);
3275 __ B(ne, slow_path->GetEntryLabel());
3276 __ Mov(out, 1);
3277 if (zero.IsLinked()) {
3278 __ B(&done);
3279 }
3280 break;
3281 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003282
Calin Juravle98893e12015-10-02 21:05:03 +01003283 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003284 case TypeCheckKind::kInterfaceCheck: {
3285 // Note that we indeed only call on slow path, but we always go
3286 // into the slow path for the unresolved and interface check
3287 // cases.
3288 //
3289 // We cannot directly call the InstanceofNonTrivial runtime
3290 // entry point without resorting to a type checking slow path
3291 // here (i.e. by calling InvokeRuntime directly), as it would
3292 // require to assign fixed registers for the inputs of this
3293 // HInstanceOf instruction (following the runtime calling
3294 // convention), which might be cluttered by the potential first
3295 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003296 //
3297 // TODO: Introduce a new runtime entry point taking the object
3298 // to test (instead of its class) as argument, and let it deal
3299 // with the read barrier issues. This will let us refactor this
3300 // case of the `switch` code as it was previously (with a direct
3301 // call to the runtime not using a type checking slow path).
3302 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303 DCHECK(locations->OnlyCallsOnSlowPath());
3304 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3305 /* is_fatal */ false);
3306 codegen_->AddSlowPath(slow_path);
3307 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003308 if (zero.IsLinked()) {
3309 __ B(&done);
3310 }
3311 break;
3312 }
3313 }
3314
3315 if (zero.IsLinked()) {
3316 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003317 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003318 }
3319
3320 if (done.IsLinked()) {
3321 __ Bind(&done);
3322 }
3323
3324 if (slow_path != nullptr) {
3325 __ Bind(slow_path->GetExitLabel());
3326 }
3327}
3328
3329void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3330 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3331 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3332
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003333 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3334 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003335 case TypeCheckKind::kExactCheck:
3336 case TypeCheckKind::kAbstractClassCheck:
3337 case TypeCheckKind::kClassHierarchyCheck:
3338 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003339 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3340 LocationSummary::kCallOnSlowPath :
3341 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003342 break;
3343 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003344 case TypeCheckKind::kUnresolvedCheck:
3345 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003346 call_kind = LocationSummary::kCallOnSlowPath;
3347 break;
3348 }
3349
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3351 locations->SetInAt(0, Location::RequiresRegister());
3352 locations->SetInAt(1, Location::RequiresRegister());
3353 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3354 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003355 // When read barriers are enabled, we need an additional temporary
3356 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003357 if (TypeCheckNeedsATemporary(type_check_kind)) {
3358 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003359 }
3360}
3361
3362void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003363 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003364 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003365 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003366 Register obj = InputRegisterAt(instruction, 0);
3367 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003369 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3370 locations->GetTemp(1) :
3371 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003372 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003373 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3374 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3375 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3376 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003377
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378 bool is_type_check_slow_path_fatal =
3379 (type_check_kind == TypeCheckKind::kExactCheck ||
3380 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3381 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3382 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3383 !instruction->CanThrowIntoCatchBlock();
3384 SlowPathCodeARM64* type_check_slow_path =
3385 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3386 is_type_check_slow_path_fatal);
3387 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003388
Scott Wakeling97c72b72016-06-24 16:19:36 +01003389 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003390 // Avoid null check if we know obj is not null.
3391 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003392 __ Cbz(obj, &done);
3393 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003394
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003395 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003396 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003397
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003398 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003399 case TypeCheckKind::kExactCheck:
3400 case TypeCheckKind::kArrayCheck: {
3401 __ Cmp(temp, cls);
3402 // Jump to slow path for throwing the exception or doing a
3403 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003404 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003405 break;
3406 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003407
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003408 case TypeCheckKind::kAbstractClassCheck: {
3409 // If the class is abstract, we eagerly fetch the super class of the
3410 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003411 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003412 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003414 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415
3416 // If the class reference currently in `temp` is not null, jump
3417 // to the `compare_classes` label to compare it with the checked
3418 // class.
3419 __ Cbnz(temp, &compare_classes);
3420 // Otherwise, jump to the slow path to throw the exception.
3421 //
3422 // But before, move back the object's class into `temp` before
3423 // going into the slow path, as it has been overwritten in the
3424 // meantime.
3425 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003426 GenerateReferenceLoadTwoRegisters(
3427 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003428 __ B(type_check_slow_path->GetEntryLabel());
3429
3430 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 __ Cmp(temp, cls);
3432 __ B(ne, &loop);
3433 break;
3434 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003435
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003436 case TypeCheckKind::kClassHierarchyCheck: {
3437 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003438 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003439 __ Bind(&loop);
3440 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003441 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003442
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003443 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003444 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003445
3446 // If the class reference currently in `temp` is not null, jump
3447 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003448 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003449 // Otherwise, jump to the slow path to throw the exception.
3450 //
3451 // But before, move back the object's class into `temp` before
3452 // going into the slow path, as it has been overwritten in the
3453 // meantime.
3454 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003455 GenerateReferenceLoadTwoRegisters(
3456 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003457 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003458 break;
3459 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003460
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003461 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003462 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003463 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003464 __ Cmp(temp, cls);
3465 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003466
3467 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003468 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003469 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003470
3471 // If the component type is not null (i.e. the object is indeed
3472 // an array), jump to label `check_non_primitive_component_type`
3473 // to further check that this component type is not a primitive
3474 // type.
3475 __ Cbnz(temp, &check_non_primitive_component_type);
3476 // Otherwise, jump to the slow path to throw the exception.
3477 //
3478 // But before, move back the object's class into `temp` before
3479 // going into the slow path, as it has been overwritten in the
3480 // meantime.
3481 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003482 GenerateReferenceLoadTwoRegisters(
3483 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003484 __ B(type_check_slow_path->GetEntryLabel());
3485
3486 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003487 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3488 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003489 __ Cbz(temp, &done);
3490 // Same comment as above regarding `temp` and the slow path.
3491 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003492 GenerateReferenceLoadTwoRegisters(
3493 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003494 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 break;
3496 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003497
Calin Juravle98893e12015-10-02 21:05:03 +01003498 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003499 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003500 // We always go into the type check slow path for the unresolved
3501 // and interface check cases.
3502 //
3503 // We cannot directly call the CheckCast runtime entry point
3504 // without resorting to a type checking slow path here (i.e. by
3505 // calling InvokeRuntime directly), as it would require to
3506 // assign fixed registers for the inputs of this HInstanceOf
3507 // instruction (following the runtime calling convention), which
3508 // might be cluttered by the potential first read barrier
3509 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003510 //
3511 // TODO: Introduce a new runtime entry point taking the object
3512 // to test (instead of its class) as argument, and let it deal
3513 // with the read barrier issues. This will let us refactor this
3514 // case of the `switch` code as it was previously (with a direct
3515 // call to the runtime not using a type checking slow path).
3516 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003517 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003518 break;
3519 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003520 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003521
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003522 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003523}
3524
Alexandre Rames5319def2014-10-23 10:03:10 +01003525void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3527 locations->SetOut(Location::ConstantLocation(constant));
3528}
3529
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003530void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003531 // Will be generated at use site.
3532}
3533
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003534void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3536 locations->SetOut(Location::ConstantLocation(constant));
3537}
3538
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003539void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003540 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003541}
3542
Calin Juravle175dc732015-08-25 15:42:32 +01003543void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3544 // The trampoline uses the same calling convention as dex calling conventions,
3545 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3546 // the method_idx.
3547 HandleInvoke(invoke);
3548}
3549
3550void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3551 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3552}
3553
Alexandre Rames5319def2014-10-23 10:03:10 +01003554void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003555 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003556 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003557}
3558
Alexandre Rames67555f72014-11-18 10:55:16 +00003559void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3560 HandleInvoke(invoke);
3561}
3562
3563void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3564 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003565 LocationSummary* locations = invoke->GetLocations();
3566 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003567 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003568 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003569 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003570
3571 // The register ip1 is required to be used for the hidden argument in
3572 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003573 MacroAssembler* masm = GetVIXLAssembler();
3574 UseScratchRegisterScope scratch_scope(masm);
3575 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003576 scratch_scope.Exclude(ip1);
3577 __ Mov(ip1, invoke->GetDexMethodIndex());
3578
Alexandre Rames67555f72014-11-18 10:55:16 +00003579 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003580 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003581 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003582 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003583 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003584 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003585 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003586 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003587 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003588 // Instead of simply (possibly) unpoisoning `temp` here, we should
3589 // emit a read barrier for the previous class reference load.
3590 // However this is not required in practice, as this is an
3591 // intermediate/temporary reference and because the current
3592 // concurrent copying collector keeps the from-space memory
3593 // intact/accessible until the end of the marking phase (the
3594 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003595 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003596 __ Ldr(temp,
3597 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3598 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003599 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003600 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003601 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003602 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003603 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003604 // lr();
3605 __ Blr(lr);
3606 DCHECK(!codegen_->IsLeafMethod());
3607 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3608}
3609
3610void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003611 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3612 if (intrinsic.TryDispatch(invoke)) {
3613 return;
3614 }
3615
Alexandre Rames67555f72014-11-18 10:55:16 +00003616 HandleInvoke(invoke);
3617}
3618
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003619void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003620 // Explicit clinit checks triggered by static invokes must have been pruned by
3621 // art::PrepareForRegisterAllocation.
3622 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003623
Andreas Gampe878d58c2015-01-15 23:24:00 -08003624 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3625 if (intrinsic.TryDispatch(invoke)) {
3626 return;
3627 }
3628
Alexandre Rames67555f72014-11-18 10:55:16 +00003629 HandleInvoke(invoke);
3630}
3631
Andreas Gampe878d58c2015-01-15 23:24:00 -08003632static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3633 if (invoke->GetLocations()->Intrinsified()) {
3634 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3635 intrinsic.Dispatch(invoke);
3636 return true;
3637 }
3638 return false;
3639}
3640
Vladimir Markodc151b22015-10-15 18:02:30 +01003641HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3642 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003643 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003644 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003645 return desired_dispatch_info;
3646}
3647
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003648void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003649 // For better instruction scheduling we load the direct code pointer before the method pointer.
3650 bool direct_code_loaded = false;
3651 switch (invoke->GetCodePtrLocation()) {
3652 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3653 // LR = code address from literal pool with link-time patch.
3654 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3655 direct_code_loaded = true;
3656 break;
3657 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3658 // LR = invoke->GetDirectCodePtr();
3659 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3660 direct_code_loaded = true;
3661 break;
3662 default:
3663 break;
3664 }
3665
Andreas Gampe878d58c2015-01-15 23:24:00 -08003666 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003667 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3668 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003669 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3670 uint32_t offset =
3671 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003672 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003673 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003674 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003675 }
Vladimir Marko58155012015-08-19 12:49:41 +00003676 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003677 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003678 break;
3679 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3680 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003681 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003682 break;
3683 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3684 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003685 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003686 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3687 break;
3688 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3689 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003690 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003691 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003692 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003693 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003694 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003695 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003696 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003697 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003698 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003699 }
Vladimir Marko58155012015-08-19 12:49:41 +00003700 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003701 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003702 Register reg = XRegisterFrom(temp);
3703 Register method_reg;
3704 if (current_method.IsRegister()) {
3705 method_reg = XRegisterFrom(current_method);
3706 } else {
3707 DCHECK(invoke->GetLocations()->Intrinsified());
3708 DCHECK(!current_method.IsValid());
3709 method_reg = reg;
3710 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3711 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003712
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003713 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003714 __ Ldr(reg.X(),
3715 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003716 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003717 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003718 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3719 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003720 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3721 break;
3722 }
3723 }
3724
3725 switch (invoke->GetCodePtrLocation()) {
3726 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3727 __ Bl(&frame_entry_label_);
3728 break;
3729 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00003730 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3731 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01003732 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3733 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003734 __ Bind(label);
3735 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003736 break;
3737 }
3738 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3739 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3740 // LR prepared above for better instruction scheduling.
3741 DCHECK(direct_code_loaded);
3742 // lr()
3743 __ Blr(lr);
3744 break;
3745 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3746 // LR = callee_method->entry_point_from_quick_compiled_code_;
3747 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003748 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003749 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003750 // lr()
3751 __ Blr(lr);
3752 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003753 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003754
Andreas Gampe878d58c2015-01-15 23:24:00 -08003755 DCHECK(!IsLeafMethod());
3756}
3757
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003758void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003759 // Use the calling convention instead of the location of the receiver, as
3760 // intrinsics may have put the receiver in a different register. In the intrinsics
3761 // slow path, the arguments have been moved to the right place, so here we are
3762 // guaranteed that the receiver is the first register of the calling convention.
3763 InvokeDexCallingConvention calling_convention;
3764 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003765 Register temp = XRegisterFrom(temp_in);
3766 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3767 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3768 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003769 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003770
3771 BlockPoolsScope block_pools(GetVIXLAssembler());
3772
3773 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003774 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003775 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003776 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003777 // Instead of simply (possibly) unpoisoning `temp` here, we should
3778 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003779 // intermediate/temporary reference and because the current
3780 // concurrent copying collector keeps the from-space memory
3781 // intact/accessible until the end of the marking phase (the
3782 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003783 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3784 // temp = temp->GetMethodAt(method_offset);
3785 __ Ldr(temp, MemOperand(temp, method_offset));
3786 // lr = temp->GetEntryPoint();
3787 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3788 // lr();
3789 __ Blr(lr);
3790}
3791
Scott Wakeling97c72b72016-06-24 16:19:36 +01003792vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3793 const DexFile& dex_file,
3794 uint32_t string_index,
3795 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003796 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3797}
3798
Scott Wakeling97c72b72016-06-24 16:19:36 +01003799vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3800 const DexFile& dex_file,
3801 uint32_t type_index,
3802 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003803 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3804}
3805
Scott Wakeling97c72b72016-06-24 16:19:36 +01003806vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3807 const DexFile& dex_file,
3808 uint32_t element_offset,
3809 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003810 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3811}
3812
Scott Wakeling97c72b72016-06-24 16:19:36 +01003813vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3814 const DexFile& dex_file,
3815 uint32_t offset_or_index,
3816 vixl::aarch64::Label* adrp_label,
3817 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003818 // Add a patch entry and return the label.
3819 patches->emplace_back(dex_file, offset_or_index);
3820 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003821 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003822 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3823 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3824 return label;
3825}
3826
Scott Wakeling97c72b72016-06-24 16:19:36 +01003827vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003828 const DexFile& dex_file, uint32_t string_index) {
3829 return boot_image_string_patches_.GetOrCreate(
3830 StringReference(&dex_file, string_index),
3831 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3832}
3833
Scott Wakeling97c72b72016-06-24 16:19:36 +01003834vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003835 const DexFile& dex_file, uint32_t type_index) {
3836 return boot_image_type_patches_.GetOrCreate(
3837 TypeReference(&dex_file, type_index),
3838 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3839}
3840
Scott Wakeling97c72b72016-06-24 16:19:36 +01003841vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3842 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003843 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3844 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3845 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3846}
3847
Scott Wakeling97c72b72016-06-24 16:19:36 +01003848vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3849 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003850 return DeduplicateUint64Literal(address);
3851}
3852
Vladimir Markoaad75c62016-10-03 08:46:48 +00003853void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
3854 vixl::aarch64::Register reg) {
3855 DCHECK(reg.IsX());
3856 SingleEmissionCheckScope guard(GetVIXLAssembler());
3857 __ Bind(fixup_label);
3858 __ adrp(reg, /* offset placeholder */ 0);
3859}
3860
3861void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
3862 vixl::aarch64::Register out,
3863 vixl::aarch64::Register base) {
3864 DCHECK(out.IsX());
3865 DCHECK(base.IsX());
3866 SingleEmissionCheckScope guard(GetVIXLAssembler());
3867 __ Bind(fixup_label);
3868 __ add(out, base, Operand(/* offset placeholder */ 0));
3869}
3870
3871void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
3872 vixl::aarch64::Register out,
3873 vixl::aarch64::Register base) {
3874 DCHECK(base.IsX());
3875 SingleEmissionCheckScope guard(GetVIXLAssembler());
3876 __ Bind(fixup_label);
3877 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
3878}
3879
3880template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
3881inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
3882 const ArenaDeque<PcRelativePatchInfo>& infos,
3883 ArenaVector<LinkerPatch>* linker_patches) {
3884 for (const PcRelativePatchInfo& info : infos) {
3885 linker_patches->push_back(Factory(info.label.GetLocation(),
3886 &info.target_dex_file,
3887 info.pc_insn_label->GetLocation(),
3888 info.offset_or_index));
3889 }
3890}
3891
Vladimir Marko58155012015-08-19 12:49:41 +00003892void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3893 DCHECK(linker_patches->empty());
3894 size_t size =
3895 method_patches_.size() +
3896 call_patches_.size() +
3897 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003898 pc_relative_dex_cache_patches_.size() +
3899 boot_image_string_patches_.size() +
3900 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003901 boot_image_type_patches_.size() +
3902 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003903 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003904 linker_patches->reserve(size);
3905 for (const auto& entry : method_patches_) {
3906 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003907 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3908 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003909 target_method.dex_file,
3910 target_method.dex_method_index));
3911 }
3912 for (const auto& entry : call_patches_) {
3913 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003914 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3915 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003916 target_method.dex_file,
3917 target_method.dex_method_index));
3918 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003919 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3920 linker_patches->push_back(
3921 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00003922 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003923 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003924 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003925 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003926 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003927 info.offset_or_index));
3928 }
3929 for (const auto& entry : boot_image_string_patches_) {
3930 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003931 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3932 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003933 target_string.dex_file,
3934 target_string.string_index));
3935 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003936 if (!GetCompilerOptions().IsBootImage()) {
3937 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
3938 linker_patches);
3939 } else {
3940 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
3941 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003942 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003943 for (const auto& entry : boot_image_type_patches_) {
3944 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003945 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3946 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003947 target_type.dex_file,
3948 target_type.type_index));
3949 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003950 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
3951 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003952 for (const auto& entry : boot_image_address_patches_) {
3953 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003954 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3955 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003956 }
3957}
3958
Scott Wakeling97c72b72016-06-24 16:19:36 +01003959vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003960 Uint32ToLiteralMap* map) {
3961 return map->GetOrCreate(
3962 value,
3963 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3964}
3965
Scott Wakeling97c72b72016-06-24 16:19:36 +01003966vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003967 return uint64_literals_.GetOrCreate(
3968 value,
3969 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003970}
3971
Scott Wakeling97c72b72016-06-24 16:19:36 +01003972vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003973 MethodReference target_method,
3974 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003975 return map->GetOrCreate(
3976 target_method,
3977 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003978}
3979
Scott Wakeling97c72b72016-06-24 16:19:36 +01003980vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003981 MethodReference target_method) {
3982 return DeduplicateMethodLiteral(target_method, &method_patches_);
3983}
3984
Scott Wakeling97c72b72016-06-24 16:19:36 +01003985vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003986 MethodReference target_method) {
3987 return DeduplicateMethodLiteral(target_method, &call_patches_);
3988}
3989
3990
Andreas Gampe878d58c2015-01-15 23:24:00 -08003991void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003992 // Explicit clinit checks triggered by static invokes must have been pruned by
3993 // art::PrepareForRegisterAllocation.
3994 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003995
Andreas Gampe878d58c2015-01-15 23:24:00 -08003996 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3997 return;
3998 }
3999
Alexandre Ramesd921d642015-04-16 15:07:16 +01004000 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004001 LocationSummary* locations = invoke->GetLocations();
4002 codegen_->GenerateStaticOrDirectCall(
4003 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004004 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004005}
4006
4007void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004008 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4009 return;
4010 }
4011
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004012 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004013 DCHECK(!codegen_->IsLeafMethod());
4014 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4015}
4016
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004017HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4018 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004019 switch (desired_class_load_kind) {
4020 case HLoadClass::LoadKind::kReferrersClass:
4021 break;
4022 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4023 DCHECK(!GetCompilerOptions().GetCompilePic());
4024 break;
4025 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4026 DCHECK(GetCompilerOptions().GetCompilePic());
4027 break;
4028 case HLoadClass::LoadKind::kBootImageAddress:
4029 break;
4030 case HLoadClass::LoadKind::kDexCacheAddress:
4031 DCHECK(Runtime::Current()->UseJitCompilation());
4032 break;
4033 case HLoadClass::LoadKind::kDexCachePcRelative:
4034 DCHECK(!Runtime::Current()->UseJitCompilation());
4035 break;
4036 case HLoadClass::LoadKind::kDexCacheViaMethod:
4037 break;
4038 }
4039 return desired_class_load_kind;
4040}
4041
Alexandre Rames67555f72014-11-18 10:55:16 +00004042void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004043 if (cls->NeedsAccessCheck()) {
4044 InvokeRuntimeCallingConvention calling_convention;
4045 CodeGenerator::CreateLoadClassLocationSummary(
4046 cls,
4047 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004048 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004049 /* code_generator_supports_read_barrier */ true);
4050 return;
4051 }
4052
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004053 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4054 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004055 ? LocationSummary::kCallOnSlowPath
4056 : LocationSummary::kNoCall;
4057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004058 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004059 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004060 }
4061
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004062 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4063 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4064 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4065 locations->SetInAt(0, Location::RequiresRegister());
4066 }
4067 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004068}
4069
4070void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004071 if (cls->NeedsAccessCheck()) {
4072 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004073 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004074 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004075 return;
4076 }
4077
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004078 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004079 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004080
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004081 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004082 bool generate_null_check = false;
4083 switch (cls->GetLoadKind()) {
4084 case HLoadClass::LoadKind::kReferrersClass: {
4085 DCHECK(!cls->CanCallRuntime());
4086 DCHECK(!cls->MustGenerateClinitCheck());
4087 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4088 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004089 GenerateGcRootFieldLoad(cls,
4090 out_loc,
4091 current_method,
4092 ArtMethod::DeclaringClassOffset().Int32Value(),
4093 /*fixup_label*/ nullptr,
4094 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004095 break;
4096 }
4097 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004098 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004099 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4100 cls->GetTypeIndex()));
4101 break;
4102 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004103 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004104 // Add ADRP with its PC-relative type patch.
4105 const DexFile& dex_file = cls->GetDexFile();
4106 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004107 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004108 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004109 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004110 vixl::aarch64::Label* add_label =
4111 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004112 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004113 break;
4114 }
4115 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004116 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004117 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4118 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4119 break;
4120 }
4121 case HLoadClass::LoadKind::kDexCacheAddress: {
4122 DCHECK_NE(cls->GetAddress(), 0u);
4123 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4124 // that gives a 16KiB range. To try and reduce the number of literals if we load
4125 // multiple types, simply split the dex cache address to a 16KiB aligned base
4126 // loaded from a literal and the remaining offset embedded in the load.
4127 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4128 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4129 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4130 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4131 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4132 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4133 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004134 GenerateGcRootFieldLoad(cls,
4135 out_loc,
4136 out.X(),
4137 offset,
4138 /*fixup_label*/ nullptr,
4139 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004140 generate_null_check = !cls->IsInDexCache();
4141 break;
4142 }
4143 case HLoadClass::LoadKind::kDexCachePcRelative: {
4144 // Add ADRP with its PC-relative DexCache access patch.
4145 const DexFile& dex_file = cls->GetDexFile();
4146 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004147 vixl::aarch64::Label* adrp_label =
4148 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004149 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004150 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004151 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004152 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4153 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004154 GenerateGcRootFieldLoad(cls,
4155 out_loc,
4156 out.X(),
4157 /* offset placeholder */ 0,
4158 ldr_label,
4159 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004160 generate_null_check = !cls->IsInDexCache();
4161 break;
4162 }
4163 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4164 MemberOffset resolved_types_offset =
4165 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4166 // /* GcRoot<mirror::Class>[] */ out =
4167 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4168 Register current_method = InputRegisterAt(cls, 0);
4169 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4170 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004171 GenerateGcRootFieldLoad(cls,
4172 out_loc,
4173 out.X(),
4174 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4175 /*fixup_label*/ nullptr,
4176 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004177 generate_null_check = !cls->IsInDexCache();
4178 break;
4179 }
4180 }
4181
4182 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4183 DCHECK(cls->CanCallRuntime());
4184 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4185 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4186 codegen_->AddSlowPath(slow_path);
4187 if (generate_null_check) {
4188 __ Cbz(out, slow_path->GetEntryLabel());
4189 }
4190 if (cls->MustGenerateClinitCheck()) {
4191 GenerateClassInitializationCheck(slow_path, out);
4192 } else {
4193 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004194 }
4195 }
4196}
4197
David Brazdilcb1c0552015-08-04 16:22:25 +01004198static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004199 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004200}
4201
Alexandre Rames67555f72014-11-18 10:55:16 +00004202void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4203 LocationSummary* locations =
4204 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4205 locations->SetOut(Location::RequiresRegister());
4206}
4207
4208void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004209 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4210}
4211
4212void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4213 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4214}
4215
4216void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4217 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004218}
4219
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004220HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4221 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004222 switch (desired_string_load_kind) {
4223 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4224 DCHECK(!GetCompilerOptions().GetCompilePic());
4225 break;
4226 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4227 DCHECK(GetCompilerOptions().GetCompilePic());
4228 break;
4229 case HLoadString::LoadKind::kBootImageAddress:
4230 break;
4231 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004232 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004233 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004234 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004235 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004236 break;
4237 case HLoadString::LoadKind::kDexCacheViaMethod:
4238 break;
4239 }
4240 return desired_string_load_kind;
4241}
4242
Alexandre Rames67555f72014-11-18 10:55:16 +00004243void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004244 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00004245 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4246 ? LocationSummary::kCallOnMainOnly
4247 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004248 : LocationSummary::kNoCall;
4249 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004250 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004251 InvokeRuntimeCallingConvention calling_convention;
4252 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4253 } else {
4254 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004255 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4256 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4257 // Rely on the pResolveString and/or marking to save everything, including temps.
4258 RegisterSet caller_saves = RegisterSet::Empty();
4259 InvokeRuntimeCallingConvention calling_convention;
4260 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4261 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4262 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4263 Primitive::kPrimNot).GetCode());
4264 locations->SetCustomSlowPathCallerSaves(caller_saves);
4265 } else {
4266 // For non-Baker read barrier we have a temp-clobbering call.
4267 }
4268 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004269 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004270}
4271
4272void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004273 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004274
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004275 switch (load->GetLoadKind()) {
4276 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004277 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4278 load->GetStringIndex()));
4279 return; // No dex cache slow path.
4280 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004281 // Add ADRP with its PC-relative String patch.
4282 const DexFile& dex_file = load->GetDexFile();
4283 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004284 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004285 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004286 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004287 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004288 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004289 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004290 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004291 return; // No dex cache slow path.
4292 }
4293 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004294 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4295 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4296 return; // No dex cache slow path.
4297 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004298 case HLoadString::LoadKind::kBssEntry: {
4299 // Add ADRP with its PC-relative String .bss entry patch.
4300 const DexFile& dex_file = load->GetDexFile();
4301 uint32_t string_index = load->GetStringIndex();
4302 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004303 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4304 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004305 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004306 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004307 // Add LDR with its PC-relative String patch.
4308 vixl::aarch64::Label* ldr_label =
4309 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4310 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4311 GenerateGcRootFieldLoad(load,
4312 load->GetLocations()->Out(),
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004313 temp,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004314 /* placeholder */ 0u,
4315 ldr_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004316 SlowPathCodeARM64* slow_path =
4317 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004318 codegen_->AddSlowPath(slow_path);
4319 __ Cbz(out.X(), slow_path->GetEntryLabel());
4320 __ Bind(slow_path->GetExitLabel());
4321 return;
4322 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004323 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004324 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004325 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004326
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004327 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004328 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004329 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004330 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4331 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4332 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004333}
4334
Alexandre Rames5319def2014-10-23 10:03:10 +01004335void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4337 locations->SetOut(Location::ConstantLocation(constant));
4338}
4339
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004340void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004341 // Will be generated at use site.
4342}
4343
Alexandre Rames67555f72014-11-18 10:55:16 +00004344void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4345 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004346 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004347 InvokeRuntimeCallingConvention calling_convention;
4348 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4349}
4350
4351void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004352 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4353 instruction,
4354 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004355 if (instruction->IsEnter()) {
4356 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4357 } else {
4358 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4359 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004360}
4361
Alexandre Rames42d641b2014-10-27 14:00:51 +00004362void LocationsBuilderARM64::VisitMul(HMul* mul) {
4363 LocationSummary* locations =
4364 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4365 switch (mul->GetResultType()) {
4366 case Primitive::kPrimInt:
4367 case Primitive::kPrimLong:
4368 locations->SetInAt(0, Location::RequiresRegister());
4369 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004370 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004371 break;
4372
4373 case Primitive::kPrimFloat:
4374 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004375 locations->SetInAt(0, Location::RequiresFpuRegister());
4376 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004377 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004378 break;
4379
4380 default:
4381 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4382 }
4383}
4384
4385void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4386 switch (mul->GetResultType()) {
4387 case Primitive::kPrimInt:
4388 case Primitive::kPrimLong:
4389 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4390 break;
4391
4392 case Primitive::kPrimFloat:
4393 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004394 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004395 break;
4396
4397 default:
4398 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4399 }
4400}
4401
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004402void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4403 LocationSummary* locations =
4404 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4405 switch (neg->GetResultType()) {
4406 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004407 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004408 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004410 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004411
4412 case Primitive::kPrimFloat:
4413 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004414 locations->SetInAt(0, Location::RequiresFpuRegister());
4415 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004416 break;
4417
4418 default:
4419 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4420 }
4421}
4422
4423void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4424 switch (neg->GetResultType()) {
4425 case Primitive::kPrimInt:
4426 case Primitive::kPrimLong:
4427 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4428 break;
4429
4430 case Primitive::kPrimFloat:
4431 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004432 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004433 break;
4434
4435 default:
4436 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4437 }
4438}
4439
4440void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4441 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004442 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004443 InvokeRuntimeCallingConvention calling_convention;
4444 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004445 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004446 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004447 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004448}
4449
4450void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4451 LocationSummary* locations = instruction->GetLocations();
4452 InvokeRuntimeCallingConvention calling_convention;
4453 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4454 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004455 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004456 // Note: if heap poisoning is enabled, the entry point takes cares
4457 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004458 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004459 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004460}
4461
Alexandre Rames5319def2014-10-23 10:03:10 +01004462void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4463 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004464 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004465 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004466 if (instruction->IsStringAlloc()) {
4467 locations->AddTemp(LocationFrom(kArtMethodRegister));
4468 } else {
4469 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4470 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4471 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004472 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4473}
4474
4475void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004476 // Note: if heap poisoning is enabled, the entry point takes cares
4477 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004478 if (instruction->IsStringAlloc()) {
4479 // String is allocated through StringFactory. Call NewEmptyString entry point.
4480 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004481 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004482 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4483 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4484 __ Blr(lr);
4485 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4486 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004487 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004488 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4489 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004490}
4491
4492void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004494 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004496}
4497
4498void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004499 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004500 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004501 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004502 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004503 break;
4504
4505 default:
4506 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4507 }
4508}
4509
David Brazdil66d126e2015-04-03 16:02:44 +01004510void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4512 locations->SetInAt(0, Location::RequiresRegister());
4513 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4514}
4515
4516void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004517 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004518}
4519
Alexandre Rames5319def2014-10-23 10:03:10 +01004520void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004521 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4522 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004523}
4524
Calin Juravle2ae48182016-03-16 14:05:09 +00004525void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4526 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004527 return;
4528 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004529
Alexandre Ramesd921d642015-04-16 15:07:16 +01004530 BlockPoolsScope block_pools(GetVIXLAssembler());
4531 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004532 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004533 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004534}
4535
Calin Juravle2ae48182016-03-16 14:05:09 +00004536void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004537 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004538 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004539
4540 LocationSummary* locations = instruction->GetLocations();
4541 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004542
4543 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004544}
4545
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004546void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004547 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004548}
4549
Alexandre Rames67555f72014-11-18 10:55:16 +00004550void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4551 HandleBinaryOp(instruction);
4552}
4553
4554void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4555 HandleBinaryOp(instruction);
4556}
4557
Alexandre Rames3e69f162014-12-10 10:36:50 +00004558void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4559 LOG(FATAL) << "Unreachable";
4560}
4561
4562void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4563 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4564}
4565
Alexandre Rames5319def2014-10-23 10:03:10 +01004566void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4568 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4569 if (location.IsStackSlot()) {
4570 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4571 } else if (location.IsDoubleStackSlot()) {
4572 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4573 }
4574 locations->SetOut(location);
4575}
4576
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004577void InstructionCodeGeneratorARM64::VisitParameterValue(
4578 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004579 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004580}
4581
4582void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4583 LocationSummary* locations =
4584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004585 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004586}
4587
4588void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4589 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4590 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004591}
4592
4593void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004595 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004596 locations->SetInAt(i, Location::Any());
4597 }
4598 locations->SetOut(Location::Any());
4599}
4600
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004601void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004602 LOG(FATAL) << "Unreachable";
4603}
4604
Serban Constantinescu02164b32014-11-13 14:05:07 +00004605void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004606 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004607 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004608 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4609 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004610 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4611
4612 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004613 case Primitive::kPrimInt:
4614 case Primitive::kPrimLong:
4615 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004616 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004617 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4618 break;
4619
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004620 case Primitive::kPrimFloat:
4621 case Primitive::kPrimDouble: {
4622 InvokeRuntimeCallingConvention calling_convention;
4623 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4624 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4625 locations->SetOut(calling_convention.GetReturnLocation(type));
4626
4627 break;
4628 }
4629
Serban Constantinescu02164b32014-11-13 14:05:07 +00004630 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004631 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004632 }
4633}
4634
4635void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4636 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004637
Serban Constantinescu02164b32014-11-13 14:05:07 +00004638 switch (type) {
4639 case Primitive::kPrimInt:
4640 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004641 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004642 break;
4643 }
4644
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004645 case Primitive::kPrimFloat:
4646 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004647 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4648 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004649 if (type == Primitive::kPrimFloat) {
4650 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4651 } else {
4652 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4653 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004654 break;
4655 }
4656
Serban Constantinescu02164b32014-11-13 14:05:07 +00004657 default:
4658 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004659 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004660 }
4661}
4662
Calin Juravle27df7582015-04-17 19:12:31 +01004663void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4664 memory_barrier->SetLocations(nullptr);
4665}
4666
4667void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004668 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004669}
4670
Alexandre Rames5319def2014-10-23 10:03:10 +01004671void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4673 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004674 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004675}
4676
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004677void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004678 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004679}
4680
4681void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4682 instruction->SetLocations(nullptr);
4683}
4684
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004685void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004686 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004687}
4688
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004689void LocationsBuilderARM64::VisitRor(HRor* ror) {
4690 HandleBinaryOp(ror);
4691}
4692
4693void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4694 HandleBinaryOp(ror);
4695}
4696
Serban Constantinescu02164b32014-11-13 14:05:07 +00004697void LocationsBuilderARM64::VisitShl(HShl* shl) {
4698 HandleShift(shl);
4699}
4700
4701void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4702 HandleShift(shl);
4703}
4704
4705void LocationsBuilderARM64::VisitShr(HShr* shr) {
4706 HandleShift(shr);
4707}
4708
4709void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4710 HandleShift(shr);
4711}
4712
Alexandre Rames5319def2014-10-23 10:03:10 +01004713void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004714 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004715}
4716
4717void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004718 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004719}
4720
Alexandre Rames67555f72014-11-18 10:55:16 +00004721void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004722 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004723}
4724
4725void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004726 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004727}
4728
4729void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004730 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004731}
4732
Alexandre Rames67555f72014-11-18 10:55:16 +00004733void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004734 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004735}
4736
Calin Juravlee460d1d2015-09-29 04:52:17 +01004737void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4738 HUnresolvedInstanceFieldGet* instruction) {
4739 FieldAccessCallingConventionARM64 calling_convention;
4740 codegen_->CreateUnresolvedFieldLocationSummary(
4741 instruction, instruction->GetFieldType(), calling_convention);
4742}
4743
4744void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4745 HUnresolvedInstanceFieldGet* instruction) {
4746 FieldAccessCallingConventionARM64 calling_convention;
4747 codegen_->GenerateUnresolvedFieldAccess(instruction,
4748 instruction->GetFieldType(),
4749 instruction->GetFieldIndex(),
4750 instruction->GetDexPc(),
4751 calling_convention);
4752}
4753
4754void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4755 HUnresolvedInstanceFieldSet* instruction) {
4756 FieldAccessCallingConventionARM64 calling_convention;
4757 codegen_->CreateUnresolvedFieldLocationSummary(
4758 instruction, instruction->GetFieldType(), calling_convention);
4759}
4760
4761void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4762 HUnresolvedInstanceFieldSet* instruction) {
4763 FieldAccessCallingConventionARM64 calling_convention;
4764 codegen_->GenerateUnresolvedFieldAccess(instruction,
4765 instruction->GetFieldType(),
4766 instruction->GetFieldIndex(),
4767 instruction->GetDexPc(),
4768 calling_convention);
4769}
4770
4771void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4772 HUnresolvedStaticFieldGet* instruction) {
4773 FieldAccessCallingConventionARM64 calling_convention;
4774 codegen_->CreateUnresolvedFieldLocationSummary(
4775 instruction, instruction->GetFieldType(), calling_convention);
4776}
4777
4778void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4779 HUnresolvedStaticFieldGet* instruction) {
4780 FieldAccessCallingConventionARM64 calling_convention;
4781 codegen_->GenerateUnresolvedFieldAccess(instruction,
4782 instruction->GetFieldType(),
4783 instruction->GetFieldIndex(),
4784 instruction->GetDexPc(),
4785 calling_convention);
4786}
4787
4788void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4789 HUnresolvedStaticFieldSet* instruction) {
4790 FieldAccessCallingConventionARM64 calling_convention;
4791 codegen_->CreateUnresolvedFieldLocationSummary(
4792 instruction, instruction->GetFieldType(), calling_convention);
4793}
4794
4795void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4796 HUnresolvedStaticFieldSet* instruction) {
4797 FieldAccessCallingConventionARM64 calling_convention;
4798 codegen_->GenerateUnresolvedFieldAccess(instruction,
4799 instruction->GetFieldType(),
4800 instruction->GetFieldIndex(),
4801 instruction->GetDexPc(),
4802 calling_convention);
4803}
4804
Alexandre Rames5319def2014-10-23 10:03:10 +01004805void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004806 LocationSummary* locations =
4807 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004808 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004809}
4810
4811void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004812 HBasicBlock* block = instruction->GetBlock();
4813 if (block->GetLoopInformation() != nullptr) {
4814 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4815 // The back edge will generate the suspend check.
4816 return;
4817 }
4818 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4819 // The goto will generate the suspend check.
4820 return;
4821 }
4822 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004823}
4824
Alexandre Rames67555f72014-11-18 10:55:16 +00004825void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4826 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004827 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004828 InvokeRuntimeCallingConvention calling_convention;
4829 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4830}
4831
4832void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004833 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004834 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004835}
4836
4837void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4838 LocationSummary* locations =
4839 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4840 Primitive::Type input_type = conversion->GetInputType();
4841 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004842 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004843 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4844 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4845 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4846 }
4847
Alexandre Rames542361f2015-01-29 16:57:31 +00004848 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004849 locations->SetInAt(0, Location::RequiresFpuRegister());
4850 } else {
4851 locations->SetInAt(0, Location::RequiresRegister());
4852 }
4853
Alexandre Rames542361f2015-01-29 16:57:31 +00004854 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004855 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4856 } else {
4857 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4858 }
4859}
4860
4861void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4862 Primitive::Type result_type = conversion->GetResultType();
4863 Primitive::Type input_type = conversion->GetInputType();
4864
4865 DCHECK_NE(input_type, result_type);
4866
Alexandre Rames542361f2015-01-29 16:57:31 +00004867 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004868 int result_size = Primitive::ComponentSize(result_type);
4869 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004870 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004871 Register output = OutputRegister(conversion);
4872 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004873 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004874 // 'int' values are used directly as W registers, discarding the top
4875 // bits, so we don't need to sign-extend and can just perform a move.
4876 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4877 // top 32 bits of the target register. We theoretically could leave those
4878 // bits unchanged, but we would have to make sure that no code uses a
4879 // 32bit input value as a 64bit value assuming that the top 32 bits are
4880 // zero.
4881 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004882 } else if (result_type == Primitive::kPrimChar ||
4883 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4884 __ Ubfx(output,
4885 output.IsX() ? source.X() : source.W(),
4886 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004887 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004888 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004889 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004890 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004891 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004892 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004893 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4894 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004895 } else if (Primitive::IsFloatingPointType(result_type) &&
4896 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004897 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4898 } else {
4899 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4900 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004901 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004902}
Alexandre Rames67555f72014-11-18 10:55:16 +00004903
Serban Constantinescu02164b32014-11-13 14:05:07 +00004904void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4905 HandleShift(ushr);
4906}
4907
4908void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4909 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004910}
4911
4912void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4913 HandleBinaryOp(instruction);
4914}
4915
4916void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4917 HandleBinaryOp(instruction);
4918}
4919
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004920void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004921 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004922 LOG(FATAL) << "Unreachable";
4923}
4924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004925void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004926 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004927 LOG(FATAL) << "Unreachable";
4928}
4929
Mark Mendellfe57faa2015-09-18 09:26:15 -04004930// Simple implementation of packed switch - generate cascaded compare/jumps.
4931void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4932 LocationSummary* locations =
4933 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4934 locations->SetInAt(0, Location::RequiresRegister());
4935}
4936
4937void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4938 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004939 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004940 Register value_reg = InputRegisterAt(switch_instr, 0);
4941 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4942
Zheng Xu3927c8b2015-11-18 17:46:25 +08004943 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004944 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004945 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4946 // make sure we don't emit it if the target may run out of range.
4947 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4948 // ranges and emit the tables only as required.
4949 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004950
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004951 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004952 // Current instruction id is an upper bound of the number of HIRs in the graph.
4953 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4954 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004955 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4956 Register temp = temps.AcquireW();
4957 __ Subs(temp, value_reg, Operand(lower_bound));
4958
Zheng Xu3927c8b2015-11-18 17:46:25 +08004959 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004960 // Jump to successors[0] if value == lower_bound.
4961 __ B(eq, codegen_->GetLabelOf(successors[0]));
4962 int32_t last_index = 0;
4963 for (; num_entries - last_index > 2; last_index += 2) {
4964 __ Subs(temp, temp, Operand(2));
4965 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4966 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4967 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4968 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4969 }
4970 if (num_entries - last_index == 2) {
4971 // The last missing case_value.
4972 __ Cmp(temp, Operand(1));
4973 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004974 }
4975
4976 // And the default for any other value.
4977 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4978 __ B(codegen_->GetLabelOf(default_block));
4979 }
4980 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004981 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004982
4983 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4984
4985 // Below instructions should use at most one blocked register. Since there are two blocked
4986 // registers, we are free to block one.
4987 Register temp_w = temps.AcquireW();
4988 Register index;
4989 // Remove the bias.
4990 if (lower_bound != 0) {
4991 index = temp_w;
4992 __ Sub(index, value_reg, Operand(lower_bound));
4993 } else {
4994 index = value_reg;
4995 }
4996
4997 // Jump to default block if index is out of the range.
4998 __ Cmp(index, Operand(num_entries));
4999 __ B(hs, codegen_->GetLabelOf(default_block));
5000
5001 // In current VIXL implementation, it won't require any blocked registers to encode the
5002 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5003 // register pressure.
5004 Register table_base = temps.AcquireX();
5005 // Load jump offset from the table.
5006 __ Adr(table_base, jump_table->GetTableStartLabel());
5007 Register jump_offset = temp_w;
5008 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5009
5010 // Jump to target block by branching to table_base(pc related) + offset.
5011 Register target_address = table_base;
5012 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5013 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005014 }
5015}
5016
Roland Levillain44015862016-01-22 11:47:17 +00005017void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5018 Location out,
5019 uint32_t offset,
5020 Location maybe_temp) {
5021 Primitive::Type type = Primitive::kPrimNot;
5022 Register out_reg = RegisterFrom(out, type);
5023 if (kEmitCompilerReadBarrier) {
5024 Register temp_reg = RegisterFrom(maybe_temp, type);
5025 if (kUseBakerReadBarrier) {
5026 // Load with fast path based Baker's read barrier.
5027 // /* HeapReference<Object> */ out = *(out + offset)
5028 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5029 out,
5030 out_reg,
5031 offset,
5032 temp_reg,
5033 /* needs_null_check */ false,
5034 /* use_load_acquire */ false);
5035 } else {
5036 // Load with slow path based read barrier.
5037 // Save the value of `out` into `maybe_temp` before overwriting it
5038 // in the following move operation, as we will need it for the
5039 // read barrier below.
5040 __ Mov(temp_reg, out_reg);
5041 // /* HeapReference<Object> */ out = *(out + offset)
5042 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5043 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5044 }
5045 } else {
5046 // Plain load with no read barrier.
5047 // /* HeapReference<Object> */ out = *(out + offset)
5048 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5049 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5050 }
5051}
5052
5053void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5054 Location out,
5055 Location obj,
5056 uint32_t offset,
5057 Location maybe_temp) {
5058 Primitive::Type type = Primitive::kPrimNot;
5059 Register out_reg = RegisterFrom(out, type);
5060 Register obj_reg = RegisterFrom(obj, type);
5061 if (kEmitCompilerReadBarrier) {
5062 if (kUseBakerReadBarrier) {
5063 // Load with fast path based Baker's read barrier.
5064 Register temp_reg = RegisterFrom(maybe_temp, type);
5065 // /* HeapReference<Object> */ out = *(obj + offset)
5066 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5067 out,
5068 obj_reg,
5069 offset,
5070 temp_reg,
5071 /* needs_null_check */ false,
5072 /* use_load_acquire */ false);
5073 } else {
5074 // Load with slow path based read barrier.
5075 // /* HeapReference<Object> */ out = *(obj + offset)
5076 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5077 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5078 }
5079 } else {
5080 // Plain load with no read barrier.
5081 // /* HeapReference<Object> */ out = *(obj + offset)
5082 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5083 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5084 }
5085}
5086
5087void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5088 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005089 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005091 vixl::aarch64::Label* fixup_label,
5092 bool requires_read_barrier) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005093 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005094 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005095 if (requires_read_barrier) {
5096 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005097 if (kUseBakerReadBarrier) {
5098 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5099 // Baker's read barrier are used:
5100 //
5101 // root = obj.field;
5102 // if (Thread::Current()->GetIsGcMarking()) {
5103 // root = ReadBarrier::Mark(root)
5104 // }
5105
5106 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005107 if (fixup_label == nullptr) {
5108 __ Ldr(root_reg, MemOperand(obj, offset));
5109 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005110 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005111 }
Roland Levillain44015862016-01-22 11:47:17 +00005112 static_assert(
5113 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5114 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5115 "have different sizes.");
5116 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5117 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5118 "have different sizes.");
5119
Vladimir Marko953437b2016-08-24 08:30:46 +00005120 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005121 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005122 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005123 codegen_->AddSlowPath(slow_path);
5124
5125 MacroAssembler* masm = GetVIXLAssembler();
5126 UseScratchRegisterScope temps(masm);
5127 Register temp = temps.AcquireW();
5128 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005129 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005130 __ Cbnz(temp, slow_path->GetEntryLabel());
5131 __ Bind(slow_path->GetExitLabel());
5132 } else {
5133 // GC root loaded through a slow path for read barriers other
5134 // than Baker's.
5135 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005136 if (fixup_label == nullptr) {
5137 __ Add(root_reg.X(), obj.X(), offset);
5138 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005139 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005140 }
Roland Levillain44015862016-01-22 11:47:17 +00005141 // /* mirror::Object* */ root = root->Read()
5142 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5143 }
5144 } else {
5145 // Plain GC root load with no read barrier.
5146 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005147 if (fixup_label == nullptr) {
5148 __ Ldr(root_reg, MemOperand(obj, offset));
5149 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005150 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005151 }
Roland Levillain44015862016-01-22 11:47:17 +00005152 // Note that GC roots are not affected by heap poisoning, thus we
5153 // do not have to unpoison `root_reg` here.
5154 }
5155}
5156
5157void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5158 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005159 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005160 uint32_t offset,
5161 Register temp,
5162 bool needs_null_check,
5163 bool use_load_acquire) {
5164 DCHECK(kEmitCompilerReadBarrier);
5165 DCHECK(kUseBakerReadBarrier);
5166
5167 // /* HeapReference<Object> */ ref = *(obj + offset)
5168 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005169 size_t no_scale_factor = 0U;
5170 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5171 ref,
5172 obj,
5173 offset,
5174 no_index,
5175 no_scale_factor,
5176 temp,
5177 needs_null_check,
5178 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005179}
5180
5181void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5182 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005183 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005184 uint32_t data_offset,
5185 Location index,
5186 Register temp,
5187 bool needs_null_check) {
5188 DCHECK(kEmitCompilerReadBarrier);
5189 DCHECK(kUseBakerReadBarrier);
5190
5191 // Array cells are never volatile variables, therefore array loads
5192 // never use Load-Acquire instructions on ARM64.
5193 const bool use_load_acquire = false;
5194
Roland Levillainbfea3352016-06-23 13:48:47 +01005195 static_assert(
5196 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5197 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005198 // /* HeapReference<Object> */ ref =
5199 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005200 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5201 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5202 ref,
5203 obj,
5204 data_offset,
5205 index,
5206 scale_factor,
5207 temp,
5208 needs_null_check,
5209 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005210}
5211
5212void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5213 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005214 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005215 uint32_t offset,
5216 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005217 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005218 Register temp,
5219 bool needs_null_check,
5220 bool use_load_acquire) {
5221 DCHECK(kEmitCompilerReadBarrier);
5222 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005223 // If we are emitting an array load, we should not be using a
5224 // Load Acquire instruction. In other words:
5225 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5226 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005227
5228 MacroAssembler* masm = GetVIXLAssembler();
5229 UseScratchRegisterScope temps(masm);
5230
5231 // In slow path based read barriers, the read barrier call is
5232 // inserted after the original load. However, in fast path based
5233 // Baker's read barriers, we need to perform the load of
5234 // mirror::Object::monitor_ *before* the original reference load.
5235 // This load-load ordering is required by the read barrier.
5236 // The fast path/slow path (for Baker's algorithm) should look like:
5237 //
5238 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5239 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5240 // HeapReference<Object> ref = *src; // Original reference load.
5241 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5242 // if (is_gray) {
5243 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5244 // }
5245 //
5246 // Note: the original implementation in ReadBarrier::Barrier is
5247 // slightly more complex as it performs additional checks that we do
5248 // not do here for performance reasons.
5249
5250 Primitive::Type type = Primitive::kPrimNot;
5251 Register ref_reg = RegisterFrom(ref, type);
5252 DCHECK(obj.IsW());
5253 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5254
5255 // /* int32_t */ monitor = obj->monitor_
5256 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5257 if (needs_null_check) {
5258 MaybeRecordImplicitNullCheck(instruction);
5259 }
5260 // /* LockWord */ lock_word = LockWord(monitor)
5261 static_assert(sizeof(LockWord) == sizeof(int32_t),
5262 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005263
Vladimir Marko877a0332016-07-11 19:30:56 +01005264 // Introduce a dependency on the lock_word including rb_state,
5265 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005266 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005267 // `obj` is unchanged by this operation, but its value now depends
5268 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005269 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005270
5271 // The actual reference load.
5272 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005273 // Load types involving an "index".
5274 if (use_load_acquire) {
5275 // UnsafeGetObjectVolatile intrinsic case.
5276 // Register `index` is not an index in an object array, but an
5277 // offset to an object reference field within object `obj`.
5278 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5279 DCHECK(instruction->GetLocations()->Intrinsified());
5280 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5281 << instruction->AsInvoke()->GetIntrinsic();
5282 DCHECK_EQ(offset, 0U);
5283 DCHECK_EQ(scale_factor, 0U);
5284 DCHECK_EQ(needs_null_check, 0U);
5285 // /* HeapReference<Object> */ ref = *(obj + index)
5286 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5287 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005288 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005289 // ArrayGet and UnsafeGetObject intrinsics cases.
5290 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5291 if (index.IsConstant()) {
5292 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5293 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5294 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005295 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005296 __ Add(temp2, obj, offset);
5297 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5298 temps.Release(temp2);
5299 }
Roland Levillain44015862016-01-22 11:47:17 +00005300 }
Roland Levillain44015862016-01-22 11:47:17 +00005301 } else {
5302 // /* HeapReference<Object> */ ref = *(obj + offset)
5303 MemOperand field = HeapOperand(obj, offset);
5304 if (use_load_acquire) {
5305 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5306 } else {
5307 Load(type, ref_reg, field);
5308 }
5309 }
5310
5311 // Object* ref = ref_addr->AsMirrorPtr()
5312 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5313
Vladimir Marko953437b2016-08-24 08:30:46 +00005314 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005315 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005316 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005317 AddSlowPath(slow_path);
5318
5319 // if (rb_state == ReadBarrier::gray_ptr_)
5320 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005321 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5322 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5323 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5324 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5325 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005326 __ Bind(slow_path->GetExitLabel());
5327}
5328
5329void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5330 Location out,
5331 Location ref,
5332 Location obj,
5333 uint32_t offset,
5334 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005335 DCHECK(kEmitCompilerReadBarrier);
5336
Roland Levillain44015862016-01-22 11:47:17 +00005337 // Insert a slow path based read barrier *after* the reference load.
5338 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005339 // If heap poisoning is enabled, the unpoisoning of the loaded
5340 // reference will be carried out by the runtime within the slow
5341 // path.
5342 //
5343 // Note that `ref` currently does not get unpoisoned (when heap
5344 // poisoning is enabled), which is alright as the `ref` argument is
5345 // not used by the artReadBarrierSlow entry point.
5346 //
5347 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5348 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5349 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5350 AddSlowPath(slow_path);
5351
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005352 __ B(slow_path->GetEntryLabel());
5353 __ Bind(slow_path->GetExitLabel());
5354}
5355
Roland Levillain44015862016-01-22 11:47:17 +00005356void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5357 Location out,
5358 Location ref,
5359 Location obj,
5360 uint32_t offset,
5361 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005362 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005363 // Baker's read barriers shall be handled by the fast path
5364 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5365 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005366 // If heap poisoning is enabled, unpoisoning will be taken care of
5367 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005368 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005369 } else if (kPoisonHeapReferences) {
5370 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5371 }
5372}
5373
Roland Levillain44015862016-01-22 11:47:17 +00005374void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5375 Location out,
5376 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005377 DCHECK(kEmitCompilerReadBarrier);
5378
Roland Levillain44015862016-01-22 11:47:17 +00005379 // Insert a slow path based read barrier *after* the GC root load.
5380 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005381 // Note that GC roots are not affected by heap poisoning, so we do
5382 // not need to do anything special for this here.
5383 SlowPathCodeARM64* slow_path =
5384 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5385 AddSlowPath(slow_path);
5386
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005387 __ B(slow_path->GetEntryLabel());
5388 __ Bind(slow_path->GetExitLabel());
5389}
5390
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005391void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5392 LocationSummary* locations =
5393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5394 locations->SetInAt(0, Location::RequiresRegister());
5395 locations->SetOut(Location::RequiresRegister());
5396}
5397
5398void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5399 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005400 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005401 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005402 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005403 __ Ldr(XRegisterFrom(locations->Out()),
5404 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005405 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005406 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005407 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005408 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5409 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005410 __ Ldr(XRegisterFrom(locations->Out()),
5411 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005412 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005413}
5414
5415
5416
Alexandre Rames67555f72014-11-18 10:55:16 +00005417#undef __
5418#undef QUICK_ENTRY_POINT
5419
Alexandre Rames5319def2014-10-23 10:03:10 +01005420} // namespace arm64
5421} // namespace art