blob: 4f7f36bb5af665861ddb67c1c8dcd56b6f58640f [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
Alexandre Rames5319def2014-10-23 10:03:10 +0100332class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000334 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100335
Alexandre Rames67555f72014-11-18 10:55:16 +0000336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100338 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000339 if (instruction_->CanThrowIntoCatchBlock()) {
340 // Live registers will be restored in the catch block if caught.
341 SaveLiveRegisters(codegen, instruction_->GetLocations());
342 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000343 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
344 instruction_,
345 instruction_->GetDexPc(),
346 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100348 }
349
Alexandre Rames8158f282015-08-07 10:26:17 +0100350 bool IsFatal() const OVERRIDE { return true; }
351
Alexandre Rames9931f312015-06-19 14:47:01 +0100352 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
353
Alexandre Rames5319def2014-10-23 10:03:10 +0100354 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
356};
357
358class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
359 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100360 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000361 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
364 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100365 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000366 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800367 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000368 if (successor_ == nullptr) {
369 __ B(GetReturnLabel());
370 } else {
371 __ B(arm64_codegen->GetLabelOf(successor_));
372 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100373 }
374
Scott Wakeling97c72b72016-06-24 16:19:36 +0100375 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 DCHECK(successor_ == nullptr);
377 return &return_label_;
378 }
379
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100380 HBasicBlock* GetSuccessor() const {
381 return successor_;
382 }
383
Alexandre Rames9931f312015-06-19 14:47:01 +0100384 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
385
Alexandre Rames5319def2014-10-23 10:03:10 +0100386 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 // If not null, the block to branch to after the suspend check.
388 HBasicBlock* const successor_;
389
390 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100391 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100392
393 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
394};
395
Alexandre Rames67555f72014-11-18 10:55:16 +0000396class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
397 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000398 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000399 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000402 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100403 Location class_to_check = locations->InAt(1);
404 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
405 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000406 DCHECK(instruction_->IsCheckCast()
407 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
408 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100409 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000410
Alexandre Rames67555f72014-11-18 10:55:16 +0000411 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000412
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000413 if (!is_fatal_) {
414 SaveLiveRegisters(codegen, locations);
415 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000416
417 // We're moving two locations to locations that could overlap, so we need a parallel
418 // move resolver.
419 InvokeRuntimeCallingConvention calling_convention;
420 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100421 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
422 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
424 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700426 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000427 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000428 Primitive::Type ret_type = instruction_->GetType();
429 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
430 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
431 } else {
432 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000433 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800434 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000435 }
436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000437 if (!is_fatal_) {
438 RestoreLiveRegisters(codegen, locations);
439 __ B(GetExitLabel());
440 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100444 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100445
Alexandre Rames67555f72014-11-18 10:55:16 +0000446 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000447 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000448
Alexandre Rames67555f72014-11-18 10:55:16 +0000449 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
450};
451
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700452class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
453 public:
Aart Bik42249c32016-01-07 15:33:50 -0800454 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000455 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700456
457 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800458 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700459 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000460 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000461 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700462 }
463
Alexandre Rames9931f312015-06-19 14:47:01 +0100464 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
465
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700466 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700467 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
468};
469
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100470class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
471 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000472 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100473
474 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
475 LocationSummary* locations = instruction_->GetLocations();
476 __ Bind(GetEntryLabel());
477 SaveLiveRegisters(codegen, locations);
478
479 InvokeRuntimeCallingConvention calling_convention;
480 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
481 parallel_move.AddMove(
482 locations->InAt(0),
483 LocationFrom(calling_convention.GetRegisterAt(0)),
484 Primitive::kPrimNot,
485 nullptr);
486 parallel_move.AddMove(
487 locations->InAt(1),
488 LocationFrom(calling_convention.GetRegisterAt(1)),
489 Primitive::kPrimInt,
490 nullptr);
491 parallel_move.AddMove(
492 locations->InAt(2),
493 LocationFrom(calling_convention.GetRegisterAt(2)),
494 Primitive::kPrimNot,
495 nullptr);
496 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
497
498 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000499 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100500 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
501 RestoreLiveRegisters(codegen, locations);
502 __ B(GetExitLabel());
503 }
504
505 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
506
507 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100508 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
509};
510
Zheng Xu3927c8b2015-11-18 17:46:25 +0800511void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
512 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000513 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800514
515 // We are about to use the assembler to place literals directly. Make sure we have enough
516 // underlying code buffer and we have generated the jump table with right size.
517 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
518 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
519
520 __ Bind(&table_start_);
521 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
522 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100523 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800524 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100525 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800526 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
527 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
528 Literal<int32_t> literal(jump_offset);
529 __ place(&literal);
530 }
531}
532
Roland Levillain44015862016-01-22 11:47:17 +0000533// Slow path marking an object during a read barrier.
534class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
535 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100536 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
537 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000538 DCHECK(kEmitCompilerReadBarrier);
539 }
540
541 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
542
543 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
544 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000545 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100546 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000547 DCHECK(instruction_->IsInstanceFieldGet() ||
548 instruction_->IsStaticFieldGet() ||
549 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100550 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000551 instruction_->IsLoadClass() ||
552 instruction_->IsLoadString() ||
553 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100554 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100555 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
556 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000557 << "Unexpected instruction in read barrier marking slow path: "
558 << instruction_->DebugName();
559
560 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100561 // No need to save live registers; it's taken care of by the
562 // entrypoint. Also, there is no need to update the stack mask,
563 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000564 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100565 DCHECK_NE(obj_.reg(), LR);
566 DCHECK_NE(obj_.reg(), WSP);
567 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100568 // IP0 is used internally by the ReadBarrierMarkRegX entry point
569 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700570 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100571 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
572 // "Compact" slow path, saving two moves.
573 //
574 // Instead of using the standard runtime calling convention (input
575 // and output in W0):
576 //
577 // W0 <- obj
578 // W0 <- ReadBarrierMark(W0)
579 // obj <- W0
580 //
581 // we just use rX (the register holding `obj`) as input and output
582 // of a dedicated entrypoint:
583 //
584 // rX <- ReadBarrierMarkRegX(rX)
585 //
586 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700587 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100588 // This runtime call does not require a stack map.
589 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000590 __ B(GetExitLabel());
591 }
592
593 private:
Roland Levillain44015862016-01-22 11:47:17 +0000594 const Location obj_;
595
596 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
597};
598
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000599// Slow path generating a read barrier for a heap reference.
600class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
601 public:
602 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
603 Location out,
604 Location ref,
605 Location obj,
606 uint32_t offset,
607 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000608 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000609 out_(out),
610 ref_(ref),
611 obj_(obj),
612 offset_(offset),
613 index_(index) {
614 DCHECK(kEmitCompilerReadBarrier);
615 // If `obj` is equal to `out` or `ref`, it means the initial object
616 // has been overwritten by (or after) the heap object reference load
617 // to be instrumented, e.g.:
618 //
619 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000620 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000621 //
622 // In that case, we have lost the information about the original
623 // object, and the emitted read barrier cannot work properly.
624 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
625 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
626 }
627
628 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
629 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
630 LocationSummary* locations = instruction_->GetLocations();
631 Primitive::Type type = Primitive::kPrimNot;
632 DCHECK(locations->CanCall());
633 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100634 DCHECK(instruction_->IsInstanceFieldGet() ||
635 instruction_->IsStaticFieldGet() ||
636 instruction_->IsArrayGet() ||
637 instruction_->IsInstanceOf() ||
638 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100639 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000640 << "Unexpected instruction in read barrier for heap reference slow path: "
641 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000642 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000643 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100644 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000645
646 __ Bind(GetEntryLabel());
647
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000648 SaveLiveRegisters(codegen, locations);
649
650 // We may have to change the index's value, but as `index_` is a
651 // constant member (like other "inputs" of this slow path),
652 // introduce a copy of it, `index`.
653 Location index = index_;
654 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100655 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000656 if (instruction_->IsArrayGet()) {
657 // Compute the actual memory offset and store it in `index`.
658 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
659 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
660 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
661 // We are about to change the value of `index_reg` (see the
662 // calls to vixl::MacroAssembler::Lsl and
663 // vixl::MacroAssembler::Mov below), but it has
664 // not been saved by the previous call to
665 // art::SlowPathCode::SaveLiveRegisters, as it is a
666 // callee-save register --
667 // art::SlowPathCode::SaveLiveRegisters does not consider
668 // callee-save registers, as it has been designed with the
669 // assumption that callee-save registers are supposed to be
670 // handled by the called function. So, as a callee-save
671 // register, `index_reg` _would_ eventually be saved onto
672 // the stack, but it would be too late: we would have
673 // changed its value earlier. Therefore, we manually save
674 // it here into another freely available register,
675 // `free_reg`, chosen of course among the caller-save
676 // registers (as a callee-save `free_reg` register would
677 // exhibit the same problem).
678 //
679 // Note we could have requested a temporary register from
680 // the register allocator instead; but we prefer not to, as
681 // this is a slow path, and we know we can find a
682 // caller-save register that is available.
683 Register free_reg = FindAvailableCallerSaveRegister(codegen);
684 __ Mov(free_reg.W(), index_reg);
685 index_reg = free_reg;
686 index = LocationFrom(index_reg);
687 } else {
688 // The initial register stored in `index_` has already been
689 // saved in the call to art::SlowPathCode::SaveLiveRegisters
690 // (as it is not a callee-save register), so we can freely
691 // use it.
692 }
693 // Shifting the index value contained in `index_reg` by the scale
694 // factor (2) cannot overflow in practice, as the runtime is
695 // unable to allocate object arrays with a size larger than
696 // 2^26 - 1 (that is, 2^28 - 4 bytes).
697 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
698 static_assert(
699 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
700 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
701 __ Add(index_reg, index_reg, Operand(offset_));
702 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100703 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
704 // intrinsics, `index_` is not shifted by a scale factor of 2
705 // (as in the case of ArrayGet), as it is actually an offset
706 // to an object field within an object.
707 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000708 DCHECK(instruction_->GetLocations()->Intrinsified());
709 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
710 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
711 << instruction_->AsInvoke()->GetIntrinsic();
712 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100713 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000714 }
715 }
716
717 // We're moving two or three locations to locations that could
718 // overlap, so we need a parallel move resolver.
719 InvokeRuntimeCallingConvention calling_convention;
720 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
721 parallel_move.AddMove(ref_,
722 LocationFrom(calling_convention.GetRegisterAt(0)),
723 type,
724 nullptr);
725 parallel_move.AddMove(obj_,
726 LocationFrom(calling_convention.GetRegisterAt(1)),
727 type,
728 nullptr);
729 if (index.IsValid()) {
730 parallel_move.AddMove(index,
731 LocationFrom(calling_convention.GetRegisterAt(2)),
732 Primitive::kPrimInt,
733 nullptr);
734 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
735 } else {
736 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
737 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
738 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000739 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000740 instruction_,
741 instruction_->GetDexPc(),
742 this);
743 CheckEntrypointTypes<
744 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
745 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
746
747 RestoreLiveRegisters(codegen, locations);
748
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000749 __ B(GetExitLabel());
750 }
751
752 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
753
754 private:
755 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100756 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
757 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000758 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
759 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
760 return Register(VIXLRegCodeFromART(i), kXRegSize);
761 }
762 }
763 // We shall never fail to find a free caller-save register, as
764 // there are more than two core caller-save registers on ARM64
765 // (meaning it is possible to find one which is different from
766 // `ref` and `obj`).
767 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
768 LOG(FATAL) << "Could not find a free register";
769 UNREACHABLE();
770 }
771
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000772 const Location out_;
773 const Location ref_;
774 const Location obj_;
775 const uint32_t offset_;
776 // An additional location containing an index to an array.
777 // Only used for HArrayGet and the UnsafeGetObject &
778 // UnsafeGetObjectVolatile intrinsics.
779 const Location index_;
780
781 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
782};
783
784// Slow path generating a read barrier for a GC root.
785class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
786 public:
787 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000788 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000789 DCHECK(kEmitCompilerReadBarrier);
790 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000791
792 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
793 LocationSummary* locations = instruction_->GetLocations();
794 Primitive::Type type = Primitive::kPrimNot;
795 DCHECK(locations->CanCall());
796 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000797 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
798 << "Unexpected instruction in read barrier for GC root slow path: "
799 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000800
801 __ Bind(GetEntryLabel());
802 SaveLiveRegisters(codegen, locations);
803
804 InvokeRuntimeCallingConvention calling_convention;
805 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
806 // The argument of the ReadBarrierForRootSlow is not a managed
807 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
808 // thus we need a 64-bit move here, and we cannot use
809 //
810 // arm64_codegen->MoveLocation(
811 // LocationFrom(calling_convention.GetRegisterAt(0)),
812 // root_,
813 // type);
814 //
815 // which would emit a 32-bit move, as `type` is a (32-bit wide)
816 // reference type (`Primitive::kPrimNot`).
817 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000818 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000819 instruction_,
820 instruction_->GetDexPc(),
821 this);
822 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
823 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
824
825 RestoreLiveRegisters(codegen, locations);
826 __ B(GetExitLabel());
827 }
828
829 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
830
831 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000832 const Location out_;
833 const Location root_;
834
835 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
836};
837
Alexandre Rames5319def2014-10-23 10:03:10 +0100838#undef __
839
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100840Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100841 Location next_location;
842 if (type == Primitive::kPrimVoid) {
843 LOG(FATAL) << "Unreachable type " << type;
844 }
845
Alexandre Rames542361f2015-01-29 16:57:31 +0000846 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100847 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
848 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000849 } else if (!Primitive::IsFloatingPointType(type) &&
850 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000851 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
852 } else {
853 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000854 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
855 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100856 }
857
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000858 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000859 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100860 return next_location;
861}
862
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100863Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100864 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100865}
866
Serban Constantinescu579885a2015-02-22 20:51:33 +0000867CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
868 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100869 const CompilerOptions& compiler_options,
870 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100871 : CodeGenerator(graph,
872 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000873 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000874 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100875 callee_saved_core_registers.GetList(),
876 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100877 compiler_options,
878 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100879 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800880 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100881 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000882 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000883 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100884 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000885 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000886 uint32_literals_(std::less<uint32_t>(),
887 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100888 uint64_literals_(std::less<uint64_t>(),
889 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
890 method_patches_(MethodReferenceComparator(),
891 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
892 call_patches_(MethodReferenceComparator(),
893 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
894 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000895 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
896 boot_image_string_patches_(StringReferenceValueComparator(),
897 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
898 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100899 boot_image_type_patches_(TypeReferenceValueComparator(),
900 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
901 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000902 boot_image_address_patches_(std::less<uint32_t>(),
903 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000904 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000905 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000906}
Alexandre Rames5319def2014-10-23 10:03:10 +0100907
Alexandre Rames67555f72014-11-18 10:55:16 +0000908#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100909
Zheng Xu3927c8b2015-11-18 17:46:25 +0800910void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100911 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800912 jump_table->EmitTable(this);
913 }
914}
915
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000916void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800917 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000918 // Ensure we emit the literal pool.
919 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000920
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000921 CodeGenerator::Finalize(allocator);
922}
923
Zheng Xuad4450e2015-04-17 18:48:56 +0800924void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
925 // Note: There are 6 kinds of moves:
926 // 1. constant -> GPR/FPR (non-cycle)
927 // 2. constant -> stack (non-cycle)
928 // 3. GPR/FPR -> GPR/FPR
929 // 4. GPR/FPR -> stack
930 // 5. stack -> GPR/FPR
931 // 6. stack -> stack (non-cycle)
932 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
933 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
934 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
935 // dependency.
936 vixl_temps_.Open(GetVIXLAssembler());
937}
938
939void ParallelMoveResolverARM64::FinishEmitNativeCode() {
940 vixl_temps_.Close();
941}
942
943Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
944 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
945 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
946 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
947 Location scratch = GetScratchLocation(kind);
948 if (!scratch.Equals(Location::NoLocation())) {
949 return scratch;
950 }
951 // Allocate from VIXL temp registers.
952 if (kind == Location::kRegister) {
953 scratch = LocationFrom(vixl_temps_.AcquireX());
954 } else {
955 DCHECK(kind == Location::kFpuRegister);
956 scratch = LocationFrom(vixl_temps_.AcquireD());
957 }
958 AddScratchLocation(scratch);
959 return scratch;
960}
961
962void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
963 if (loc.IsRegister()) {
964 vixl_temps_.Release(XRegisterFrom(loc));
965 } else {
966 DCHECK(loc.IsFpuRegister());
967 vixl_temps_.Release(DRegisterFrom(loc));
968 }
969 RemoveScratchLocation(loc);
970}
971
Alexandre Rames3e69f162014-12-10 10:36:50 +0000972void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100973 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100974 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000975}
976
Alexandre Rames5319def2014-10-23 10:03:10 +0100977void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100978 MacroAssembler* masm = GetVIXLAssembler();
979 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000980 __ Bind(&frame_entry_label_);
981
Serban Constantinescu02164b32014-11-13 14:05:07 +0000982 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
983 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100984 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000985 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000986 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000987 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000988 __ Ldr(wzr, MemOperand(temp, 0));
989 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000990 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100991
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000992 if (!HasEmptyFrame()) {
993 int frame_size = GetFrameSize();
994 // Stack layout:
995 // sp[frame_size - 8] : lr.
996 // ... : other preserved core registers.
997 // ... : other preserved fp registers.
998 // ... : reserved frame space.
999 // sp[0] : current method.
1000 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001001 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001002 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1003 frame_size - GetCoreSpillSize());
1004 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1005 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001006 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001007}
1008
1009void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001010 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001011 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001012 if (!HasEmptyFrame()) {
1013 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001014 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1015 frame_size - FrameEntrySpillSize());
1016 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1017 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001018 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001019 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001020 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001021 __ Ret();
1022 GetAssembler()->cfi().RestoreState();
1023 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001024}
1025
Scott Wakeling97c72b72016-06-24 16:19:36 +01001026CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001027 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001028 return CPURegList(CPURegister::kRegister, kXRegSize,
1029 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001030}
1031
Scott Wakeling97c72b72016-06-24 16:19:36 +01001032CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001033 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1034 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001035 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1036 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001037}
1038
Alexandre Rames5319def2014-10-23 10:03:10 +01001039void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1040 __ Bind(GetLabelOf(block));
1041}
1042
Calin Juravle175dc732015-08-25 15:42:32 +01001043void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1044 DCHECK(location.IsRegister());
1045 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1046}
1047
Calin Juravlee460d1d2015-09-29 04:52:17 +01001048void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1049 if (location.IsRegister()) {
1050 locations->AddTemp(location);
1051 } else {
1052 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1053 }
1054}
1055
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001056void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001057 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001058 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001059 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001060 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001061 if (value_can_be_null) {
1062 __ Cbz(value, &done);
1063 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001064 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001065 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001066 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001067 if (value_can_be_null) {
1068 __ Bind(&done);
1069 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001070}
1071
David Brazdil58282f42016-01-14 12:45:10 +00001072void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001073 // Blocked core registers:
1074 // lr : Runtime reserved.
1075 // tr : Runtime reserved.
1076 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1077 // ip1 : VIXL core temp.
1078 // ip0 : VIXL core temp.
1079 //
1080 // Blocked fp registers:
1081 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001082 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1083 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001084 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001085 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001086 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001087
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001088 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001089 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001090 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001091 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001092
David Brazdil58282f42016-01-14 12:45:10 +00001093 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001094 // Stubs do not save callee-save floating point registers. If the graph
1095 // is debuggable, we need to deal with these registers differently. For
1096 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001097 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1098 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001099 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001100 }
1101 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001102}
1103
Alexandre Rames3e69f162014-12-10 10:36:50 +00001104size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1105 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1106 __ Str(reg, MemOperand(sp, stack_index));
1107 return kArm64WordSize;
1108}
1109
1110size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1111 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1112 __ Ldr(reg, MemOperand(sp, stack_index));
1113 return kArm64WordSize;
1114}
1115
1116size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1117 FPRegister reg = FPRegister(reg_id, kDRegSize);
1118 __ Str(reg, MemOperand(sp, stack_index));
1119 return kArm64WordSize;
1120}
1121
1122size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1123 FPRegister reg = FPRegister(reg_id, kDRegSize);
1124 __ Ldr(reg, MemOperand(sp, stack_index));
1125 return kArm64WordSize;
1126}
1127
Alexandre Rames5319def2014-10-23 10:03:10 +01001128void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001129 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001130}
1131
1132void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001133 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001134}
1135
Alexandre Rames67555f72014-11-18 10:55:16 +00001136void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001137 if (constant->IsIntConstant()) {
1138 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1139 } else if (constant->IsLongConstant()) {
1140 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1141 } else if (constant->IsNullConstant()) {
1142 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001143 } else if (constant->IsFloatConstant()) {
1144 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1145 } else {
1146 DCHECK(constant->IsDoubleConstant());
1147 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1148 }
1149}
1150
Alexandre Rames3e69f162014-12-10 10:36:50 +00001151
1152static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1153 DCHECK(constant.IsConstant());
1154 HConstant* cst = constant.GetConstant();
1155 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001156 // Null is mapped to a core W register, which we associate with kPrimInt.
1157 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001158 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1159 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1160 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1161}
1162
Calin Juravlee460d1d2015-09-29 04:52:17 +01001163void CodeGeneratorARM64::MoveLocation(Location destination,
1164 Location source,
1165 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001166 if (source.Equals(destination)) {
1167 return;
1168 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001169
1170 // A valid move can always be inferred from the destination and source
1171 // locations. When moving from and to a register, the argument type can be
1172 // used to generate 32bit instead of 64bit moves. In debug mode we also
1173 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001174 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001175
1176 if (destination.IsRegister() || destination.IsFpuRegister()) {
1177 if (unspecified_type) {
1178 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1179 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001180 (src_cst != nullptr && (src_cst->IsIntConstant()
1181 || src_cst->IsFloatConstant()
1182 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001183 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001184 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001185 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001186 // If the source is a double stack slot or a 64bit constant, a 64bit
1187 // type is appropriate. Else the source is a register, and since the
1188 // type has not been specified, we chose a 64bit type to force a 64bit
1189 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001190 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001191 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001192 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001193 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1194 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1195 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001196 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1197 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1198 __ Ldr(dst, StackOperandFrom(source));
1199 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001201 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001202 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001203 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001204 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001205 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001206 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1208 ? Primitive::kPrimLong
1209 : Primitive::kPrimInt;
1210 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1211 }
1212 } else {
1213 DCHECK(source.IsFpuRegister());
1214 if (destination.IsRegister()) {
1215 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1216 ? Primitive::kPrimDouble
1217 : Primitive::kPrimFloat;
1218 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1219 } else {
1220 DCHECK(destination.IsFpuRegister());
1221 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001222 }
1223 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001224 } else { // The destination is not a register. It must be a stack slot.
1225 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1226 if (source.IsRegister() || source.IsFpuRegister()) {
1227 if (unspecified_type) {
1228 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001229 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001232 }
1233 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1235 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1236 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001237 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001238 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1239 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 UseScratchRegisterScope temps(GetVIXLAssembler());
1241 HConstant* src_cst = source.GetConstant();
1242 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001243 if (src_cst->IsZeroBitPattern()) {
1244 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001246 if (src_cst->IsIntConstant()) {
1247 temp = temps.AcquireW();
1248 } else if (src_cst->IsLongConstant()) {
1249 temp = temps.AcquireX();
1250 } else if (src_cst->IsFloatConstant()) {
1251 temp = temps.AcquireS();
1252 } else {
1253 DCHECK(src_cst->IsDoubleConstant());
1254 temp = temps.AcquireD();
1255 }
1256 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001257 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001258 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001259 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001260 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001262 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001263 // There is generally less pressure on FP registers.
1264 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001265 __ Ldr(temp, StackOperandFrom(source));
1266 __ Str(temp, StackOperandFrom(destination));
1267 }
1268 }
1269}
1270
1271void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001272 CPURegister dst,
1273 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001274 switch (type) {
1275 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001276 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001277 break;
1278 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001279 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001280 break;
1281 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001282 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001283 break;
1284 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001285 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001286 break;
1287 case Primitive::kPrimInt:
1288 case Primitive::kPrimNot:
1289 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001290 case Primitive::kPrimFloat:
1291 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001292 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001293 __ Ldr(dst, src);
1294 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001295 case Primitive::kPrimVoid:
1296 LOG(FATAL) << "Unreachable type " << type;
1297 }
1298}
1299
Calin Juravle77520bc2015-01-12 18:45:46 +00001300void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001301 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001302 const MemOperand& src,
1303 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001304 MacroAssembler* masm = GetVIXLAssembler();
1305 BlockPoolsScope block_pools(masm);
1306 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001307 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001308 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001309
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001310 DCHECK(!src.IsPreIndex());
1311 DCHECK(!src.IsPostIndex());
1312
1313 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001314 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001315 MemOperand base = MemOperand(temp_base);
1316 switch (type) {
1317 case Primitive::kPrimBoolean:
1318 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001319 if (needs_null_check) {
1320 MaybeRecordImplicitNullCheck(instruction);
1321 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001322 break;
1323 case Primitive::kPrimByte:
1324 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001325 if (needs_null_check) {
1326 MaybeRecordImplicitNullCheck(instruction);
1327 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001328 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1329 break;
1330 case Primitive::kPrimChar:
1331 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001332 if (needs_null_check) {
1333 MaybeRecordImplicitNullCheck(instruction);
1334 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001335 break;
1336 case Primitive::kPrimShort:
1337 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001338 if (needs_null_check) {
1339 MaybeRecordImplicitNullCheck(instruction);
1340 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001341 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1342 break;
1343 case Primitive::kPrimInt:
1344 case Primitive::kPrimNot:
1345 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001346 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001347 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001348 if (needs_null_check) {
1349 MaybeRecordImplicitNullCheck(instruction);
1350 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001351 break;
1352 case Primitive::kPrimFloat:
1353 case Primitive::kPrimDouble: {
1354 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001355 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001356
1357 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1358 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001359 if (needs_null_check) {
1360 MaybeRecordImplicitNullCheck(instruction);
1361 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001362 __ Fmov(FPRegister(dst), temp);
1363 break;
1364 }
1365 case Primitive::kPrimVoid:
1366 LOG(FATAL) << "Unreachable type " << type;
1367 }
1368}
1369
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001370void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001371 CPURegister src,
1372 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001373 switch (type) {
1374 case Primitive::kPrimBoolean:
1375 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001376 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001377 break;
1378 case Primitive::kPrimChar:
1379 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001380 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001381 break;
1382 case Primitive::kPrimInt:
1383 case Primitive::kPrimNot:
1384 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001387 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001388 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001389 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001390 case Primitive::kPrimVoid:
1391 LOG(FATAL) << "Unreachable type " << type;
1392 }
1393}
1394
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001395void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1396 CPURegister src,
1397 const MemOperand& dst) {
1398 UseScratchRegisterScope temps(GetVIXLAssembler());
1399 Register temp_base = temps.AcquireX();
1400
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001401 DCHECK(!dst.IsPreIndex());
1402 DCHECK(!dst.IsPostIndex());
1403
1404 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001405 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001406 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001407 MemOperand base = MemOperand(temp_base);
1408 switch (type) {
1409 case Primitive::kPrimBoolean:
1410 case Primitive::kPrimByte:
1411 __ Stlrb(Register(src), base);
1412 break;
1413 case Primitive::kPrimChar:
1414 case Primitive::kPrimShort:
1415 __ Stlrh(Register(src), base);
1416 break;
1417 case Primitive::kPrimInt:
1418 case Primitive::kPrimNot:
1419 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001420 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421 __ Stlr(Register(src), base);
1422 break;
1423 case Primitive::kPrimFloat:
1424 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001425 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001426 Register temp_src;
1427 if (src.IsZero()) {
1428 // The zero register is used to avoid synthesizing zero constants.
1429 temp_src = Register(src);
1430 } else {
1431 DCHECK(src.IsFPRegister());
1432 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1433 __ Fmov(temp_src, FPRegister(src));
1434 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001435
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001436 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001437 break;
1438 }
1439 case Primitive::kPrimVoid:
1440 LOG(FATAL) << "Unreachable type " << type;
1441 }
1442}
1443
Calin Juravle175dc732015-08-25 15:42:32 +01001444void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1445 HInstruction* instruction,
1446 uint32_t dex_pc,
1447 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001448 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001449 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001450 if (EntrypointRequiresStackMap(entrypoint)) {
1451 RecordPcInfo(instruction, dex_pc, slow_path);
1452 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001453}
1454
Roland Levillaindec8f632016-07-22 17:10:06 +01001455void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1456 HInstruction* instruction,
1457 SlowPathCode* slow_path) {
1458 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001459 GenerateInvokeRuntime(entry_point_offset);
1460}
1461
1462void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001463 BlockPoolsScope block_pools(GetVIXLAssembler());
1464 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1465 __ Blr(lr);
1466}
1467
Alexandre Rames67555f72014-11-18 10:55:16 +00001468void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001469 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001470 UseScratchRegisterScope temps(GetVIXLAssembler());
1471 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001472 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1473
Serban Constantinescu02164b32014-11-13 14:05:07 +00001474 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001475 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1476 __ Add(temp, class_reg, status_offset);
1477 __ Ldar(temp, HeapOperand(temp));
1478 __ Cmp(temp, mirror::Class::kStatusInitialized);
1479 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001480 __ Bind(slow_path->GetExitLabel());
1481}
Alexandre Rames5319def2014-10-23 10:03:10 +01001482
Roland Levillain44015862016-01-22 11:47:17 +00001483void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001484 BarrierType type = BarrierAll;
1485
1486 switch (kind) {
1487 case MemBarrierKind::kAnyAny:
1488 case MemBarrierKind::kAnyStore: {
1489 type = BarrierAll;
1490 break;
1491 }
1492 case MemBarrierKind::kLoadAny: {
1493 type = BarrierReads;
1494 break;
1495 }
1496 case MemBarrierKind::kStoreStore: {
1497 type = BarrierWrites;
1498 break;
1499 }
1500 default:
1501 LOG(FATAL) << "Unexpected memory barrier " << kind;
1502 }
1503 __ Dmb(InnerShareable, type);
1504}
1505
Serban Constantinescu02164b32014-11-13 14:05:07 +00001506void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1507 HBasicBlock* successor) {
1508 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001509 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1510 if (slow_path == nullptr) {
1511 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1512 instruction->SetSlowPath(slow_path);
1513 codegen_->AddSlowPath(slow_path);
1514 if (successor != nullptr) {
1515 DCHECK(successor->IsLoopHeader());
1516 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1517 }
1518 } else {
1519 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1520 }
1521
Serban Constantinescu02164b32014-11-13 14:05:07 +00001522 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1523 Register temp = temps.AcquireW();
1524
Andreas Gampe542451c2016-07-26 09:02:02 -07001525 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001526 if (successor == nullptr) {
1527 __ Cbnz(temp, slow_path->GetEntryLabel());
1528 __ Bind(slow_path->GetReturnLabel());
1529 } else {
1530 __ Cbz(temp, codegen_->GetLabelOf(successor));
1531 __ B(slow_path->GetEntryLabel());
1532 // slow_path will return to GetLabelOf(successor).
1533 }
1534}
1535
Alexandre Rames5319def2014-10-23 10:03:10 +01001536InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1537 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001538 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001539 assembler_(codegen->GetAssembler()),
1540 codegen_(codegen) {}
1541
1542#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001543 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001544
1545#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1546
1547enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001548 // Using a base helps identify when we hit such breakpoints.
1549 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001550#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1551 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1552#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1553};
1554
1555#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001556 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001557 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1558 } \
1559 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1561 locations->SetOut(Location::Any()); \
1562 }
1563 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1564#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1565
1566#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001567#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001568
Alexandre Rames67555f72014-11-18 10:55:16 +00001569void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001570 DCHECK_EQ(instr->InputCount(), 2U);
1571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1572 Primitive::Type type = instr->GetResultType();
1573 switch (type) {
1574 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001575 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001576 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001577 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001578 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001579 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001580
1581 case Primitive::kPrimFloat:
1582 case Primitive::kPrimDouble:
1583 locations->SetInAt(0, Location::RequiresFpuRegister());
1584 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001585 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001586 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001587
Alexandre Rames5319def2014-10-23 10:03:10 +01001588 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001589 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001590 }
1591}
1592
Alexandre Rames09a99962015-04-15 11:47:56 +01001593void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001594 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1595
1596 bool object_field_get_with_read_barrier =
1597 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001598 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001599 new (GetGraph()->GetArena()) LocationSummary(instruction,
1600 object_field_get_with_read_barrier ?
1601 LocationSummary::kCallOnSlowPath :
1602 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001603 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001604 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001605 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001606 locations->SetInAt(0, Location::RequiresRegister());
1607 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1608 locations->SetOut(Location::RequiresFpuRegister());
1609 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001610 // The output overlaps for an object field get when read barriers
1611 // are enabled: we do not want the load to overwrite the object's
1612 // location, as we need it to emit the read barrier.
1613 locations->SetOut(
1614 Location::RequiresRegister(),
1615 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001616 }
1617}
1618
1619void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1620 const FieldInfo& field_info) {
1621 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001622 LocationSummary* locations = instruction->GetLocations();
1623 Location base_loc = locations->InAt(0);
1624 Location out = locations->Out();
1625 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001626 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001627 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001628 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001629
Roland Levillain44015862016-01-22 11:47:17 +00001630 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1631 // Object FieldGet with Baker's read barrier case.
1632 MacroAssembler* masm = GetVIXLAssembler();
1633 UseScratchRegisterScope temps(masm);
1634 // /* HeapReference<Object> */ out = *(base + offset)
1635 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1636 Register temp = temps.AcquireW();
1637 // Note that potential implicit null checks are handled in this
1638 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1639 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1640 instruction,
1641 out,
1642 base,
1643 offset,
1644 temp,
1645 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001646 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001647 } else {
1648 // General case.
1649 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001650 // Note that a potential implicit null check is handled in this
1651 // CodeGeneratorARM64::LoadAcquire call.
1652 // NB: LoadAcquire will record the pc info if needed.
1653 codegen_->LoadAcquire(
1654 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001655 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001656 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001657 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001658 }
Roland Levillain44015862016-01-22 11:47:17 +00001659 if (field_type == Primitive::kPrimNot) {
1660 // If read barriers are enabled, emit read barriers other than
1661 // Baker's using a slow path (and also unpoison the loaded
1662 // reference, if heap poisoning is enabled).
1663 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1664 }
Roland Levillain4d027112015-07-01 15:41:14 +01001665 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001666}
1667
1668void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1669 LocationSummary* locations =
1670 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1671 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001672 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1673 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1674 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001675 locations->SetInAt(1, Location::RequiresFpuRegister());
1676 } else {
1677 locations->SetInAt(1, Location::RequiresRegister());
1678 }
1679}
1680
1681void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001682 const FieldInfo& field_info,
1683 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001684 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001685 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001686
1687 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001688 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001689 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001690 Offset offset = field_info.GetFieldOffset();
1691 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001692
Roland Levillain4d027112015-07-01 15:41:14 +01001693 {
1694 // We use a block to end the scratch scope before the write barrier, thus
1695 // freeing the temporary registers so they can be used in `MarkGCCard`.
1696 UseScratchRegisterScope temps(GetVIXLAssembler());
1697
1698 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1699 DCHECK(value.IsW());
1700 Register temp = temps.AcquireW();
1701 __ Mov(temp, value.W());
1702 GetAssembler()->PoisonHeapReference(temp.W());
1703 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001704 }
Roland Levillain4d027112015-07-01 15:41:14 +01001705
1706 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001707 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001709 } else {
1710 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1711 codegen_->MaybeRecordImplicitNullCheck(instruction);
1712 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001713 }
1714
1715 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001716 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001717 }
1718}
1719
Alexandre Rames67555f72014-11-18 10:55:16 +00001720void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001721 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001722
1723 switch (type) {
1724 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001725 case Primitive::kPrimLong: {
1726 Register dst = OutputRegister(instr);
1727 Register lhs = InputRegisterAt(instr, 0);
1728 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001729 if (instr->IsAdd()) {
1730 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001731 } else if (instr->IsAnd()) {
1732 __ And(dst, lhs, rhs);
1733 } else if (instr->IsOr()) {
1734 __ Orr(dst, lhs, rhs);
1735 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001736 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001737 } else if (instr->IsRor()) {
1738 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001739 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001740 __ Ror(dst, lhs, shift);
1741 } else {
1742 // Ensure shift distance is in the same size register as the result. If
1743 // we are rotating a long and the shift comes in a w register originally,
1744 // we don't need to sxtw for use as an x since the shift distances are
1745 // all & reg_bits - 1.
1746 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1747 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 } else {
1749 DCHECK(instr->IsXor());
1750 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001751 }
1752 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001753 }
1754 case Primitive::kPrimFloat:
1755 case Primitive::kPrimDouble: {
1756 FPRegister dst = OutputFPRegister(instr);
1757 FPRegister lhs = InputFPRegisterAt(instr, 0);
1758 FPRegister rhs = InputFPRegisterAt(instr, 1);
1759 if (instr->IsAdd()) {
1760 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001761 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001762 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001763 } else {
1764 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001765 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001766 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001767 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001768 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001769 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001770 }
1771}
1772
Serban Constantinescu02164b32014-11-13 14:05:07 +00001773void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1774 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1775
1776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1777 Primitive::Type type = instr->GetResultType();
1778 switch (type) {
1779 case Primitive::kPrimInt:
1780 case Primitive::kPrimLong: {
1781 locations->SetInAt(0, Location::RequiresRegister());
1782 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1783 locations->SetOut(Location::RequiresRegister());
1784 break;
1785 }
1786 default:
1787 LOG(FATAL) << "Unexpected shift type " << type;
1788 }
1789}
1790
1791void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1792 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1793
1794 Primitive::Type type = instr->GetType();
1795 switch (type) {
1796 case Primitive::kPrimInt:
1797 case Primitive::kPrimLong: {
1798 Register dst = OutputRegister(instr);
1799 Register lhs = InputRegisterAt(instr, 0);
1800 Operand rhs = InputOperandAt(instr, 1);
1801 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001802 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001803 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001804 if (instr->IsShl()) {
1805 __ Lsl(dst, lhs, shift_value);
1806 } else if (instr->IsShr()) {
1807 __ Asr(dst, lhs, shift_value);
1808 } else {
1809 __ Lsr(dst, lhs, shift_value);
1810 }
1811 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001812 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001813
1814 if (instr->IsShl()) {
1815 __ Lsl(dst, lhs, rhs_reg);
1816 } else if (instr->IsShr()) {
1817 __ Asr(dst, lhs, rhs_reg);
1818 } else {
1819 __ Lsr(dst, lhs, rhs_reg);
1820 }
1821 }
1822 break;
1823 }
1824 default:
1825 LOG(FATAL) << "Unexpected shift operation type " << type;
1826 }
1827}
1828
Alexandre Rames5319def2014-10-23 10:03:10 +01001829void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001830 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001831}
1832
1833void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001834 HandleBinaryOp(instruction);
1835}
1836
1837void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1838 HandleBinaryOp(instruction);
1839}
1840
1841void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1842 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001843}
1844
Artem Serov7fc63502016-02-09 17:15:29 +00001845void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001846 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1847 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1848 locations->SetInAt(0, Location::RequiresRegister());
1849 // There is no immediate variant of negated bitwise instructions in AArch64.
1850 locations->SetInAt(1, Location::RequiresRegister());
1851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1852}
1853
Artem Serov7fc63502016-02-09 17:15:29 +00001854void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001855 Register dst = OutputRegister(instr);
1856 Register lhs = InputRegisterAt(instr, 0);
1857 Register rhs = InputRegisterAt(instr, 1);
1858
1859 switch (instr->GetOpKind()) {
1860 case HInstruction::kAnd:
1861 __ Bic(dst, lhs, rhs);
1862 break;
1863 case HInstruction::kOr:
1864 __ Orn(dst, lhs, rhs);
1865 break;
1866 case HInstruction::kXor:
1867 __ Eon(dst, lhs, rhs);
1868 break;
1869 default:
1870 LOG(FATAL) << "Unreachable";
1871 }
1872}
1873
Alexandre Rames8626b742015-11-25 16:28:08 +00001874void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1875 HArm64DataProcWithShifterOp* instruction) {
1876 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1877 instruction->GetType() == Primitive::kPrimLong);
1878 LocationSummary* locations =
1879 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1880 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1881 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1882 } else {
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 }
1885 locations->SetInAt(1, Location::RequiresRegister());
1886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1887}
1888
1889void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1890 HArm64DataProcWithShifterOp* instruction) {
1891 Primitive::Type type = instruction->GetType();
1892 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1893 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1894 Register out = OutputRegister(instruction);
1895 Register left;
1896 if (kind != HInstruction::kNeg) {
1897 left = InputRegisterAt(instruction, 0);
1898 }
1899 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1900 // shifter operand operation, the IR generating `right_reg` (input to the type
1901 // conversion) can have a different type from the current instruction's type,
1902 // so we manually indicate the type.
1903 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001904 int64_t shift_amount = instruction->GetShiftAmount() &
1905 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001906
1907 Operand right_operand(0);
1908
1909 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1910 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1911 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1912 } else {
1913 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1914 }
1915
1916 // Logical binary operations do not support extension operations in the
1917 // operand. Note that VIXL would still manage if it was passed by generating
1918 // the extension as a separate instruction.
1919 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1920 DCHECK(!right_operand.IsExtendedRegister() ||
1921 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1922 kind != HInstruction::kNeg));
1923 switch (kind) {
1924 case HInstruction::kAdd:
1925 __ Add(out, left, right_operand);
1926 break;
1927 case HInstruction::kAnd:
1928 __ And(out, left, right_operand);
1929 break;
1930 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001931 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001932 __ Neg(out, right_operand);
1933 break;
1934 case HInstruction::kOr:
1935 __ Orr(out, left, right_operand);
1936 break;
1937 case HInstruction::kSub:
1938 __ Sub(out, left, right_operand);
1939 break;
1940 case HInstruction::kXor:
1941 __ Eor(out, left, right_operand);
1942 break;
1943 default:
1944 LOG(FATAL) << "Unexpected operation kind: " << kind;
1945 UNREACHABLE();
1946 }
1947}
1948
Artem Serov328429f2016-07-06 16:23:04 +01001949void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001950 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1951 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001952 LocationSummary* locations =
1953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1954 locations->SetInAt(0, Location::RequiresRegister());
1955 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1956 locations->SetOut(Location::RequiresRegister());
1957}
1958
Roland Levillain4a3aa572016-08-15 13:17:06 +00001959void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1960 HIntermediateAddress* instruction) {
1961 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1962 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001963 __ Add(OutputRegister(instruction),
1964 InputRegisterAt(instruction, 0),
1965 Operand(InputOperandAt(instruction, 1)));
1966}
1967
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001968void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001969 LocationSummary* locations =
1970 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001971 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1972 if (instr->GetOpKind() == HInstruction::kSub &&
1973 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00001974 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001975 // Don't allocate register for Mneg instruction.
1976 } else {
1977 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1978 Location::RequiresRegister());
1979 }
1980 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1981 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00001982 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1983}
1984
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001985void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001986 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001987 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
1988 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00001989
1990 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1991 // This fixup should be carried out for all multiply-accumulate instructions:
1992 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1993 if (instr->GetType() == Primitive::kPrimLong &&
1994 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1995 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01001996 vixl::aarch64::Instruction* prev =
1997 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00001998 if (prev->IsLoadOrStore()) {
1999 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002000 vixl::aarch64::CodeBufferCheckScope scope(masm,
2001 kInstructionSize,
2002 vixl::aarch64::CodeBufferCheckScope::kCheck,
2003 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002004 __ nop();
2005 }
2006 }
2007
2008 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002009 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002010 __ Madd(res, mul_left, mul_right, accumulator);
2011 } else {
2012 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002013 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002014 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002015 __ Mneg(res, mul_left, mul_right);
2016 } else {
2017 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2018 __ Msub(res, mul_left, mul_right, accumulator);
2019 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002020 }
2021}
2022
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002023void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002024 bool object_array_get_with_read_barrier =
2025 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002026 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002027 new (GetGraph()->GetArena()) LocationSummary(instruction,
2028 object_array_get_with_read_barrier ?
2029 LocationSummary::kCallOnSlowPath :
2030 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002031 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002032 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002033 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002034 locations->SetInAt(0, Location::RequiresRegister());
2035 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002036 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2037 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2038 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002039 // The output overlaps in the case of an object array get with
2040 // read barriers enabled: we do not want the move to overwrite the
2041 // array's location, as we need it to emit the read barrier.
2042 locations->SetOut(
2043 Location::RequiresRegister(),
2044 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002045 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002046}
2047
2048void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002049 Primitive::Type type = instruction->GetType();
2050 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002051 LocationSummary* locations = instruction->GetLocations();
2052 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002053 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002054 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002055 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2056 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002057 MacroAssembler* masm = GetVIXLAssembler();
2058 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002059 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002060 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061
Roland Levillain44015862016-01-22 11:47:17 +00002062 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2063 // Object ArrayGet with Baker's read barrier case.
2064 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002065 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2066 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002067 // Note that a potential implicit null check is handled in the
2068 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2069 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2070 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002071 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002072 // General case.
2073 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002074 Register length;
2075 if (maybe_compressed_char_at) {
2076 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2077 length = temps.AcquireW();
2078 __ Ldr(length, HeapOperand(obj, count_offset));
2079 codegen_->MaybeRecordImplicitNullCheck(instruction);
2080 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002081 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002082 if (maybe_compressed_char_at) {
2083 vixl::aarch64::Label uncompressed_load, done;
2084 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2085 __ Ldrb(Register(OutputCPURegister(instruction)),
2086 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2087 __ B(&done);
2088 __ Bind(&uncompressed_load);
2089 __ Ldrh(Register(OutputCPURegister(instruction)),
2090 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2091 __ Bind(&done);
2092 } else {
2093 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2094 source = HeapOperand(obj, offset);
2095 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002096 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002097 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002098 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002099 // The read barrier instrumentation does not support the
2100 // HIntermediateAddress instruction yet.
2101 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002102 // We do not need to compute the intermediate address from the array: the
2103 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002104 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002105 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002106 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002107 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2108 }
2109 temp = obj;
2110 } else {
2111 __ Add(temp, obj, offset);
2112 }
jessicahandojo05765752016-09-09 19:01:32 -07002113 if (maybe_compressed_char_at) {
2114 vixl::aarch64::Label uncompressed_load, done;
2115 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2116 __ Ldrb(Register(OutputCPURegister(instruction)),
2117 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2118 __ B(&done);
2119 __ Bind(&uncompressed_load);
2120 __ Ldrh(Register(OutputCPURegister(instruction)),
2121 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2122 __ Bind(&done);
2123 } else {
2124 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2125 }
Roland Levillain44015862016-01-22 11:47:17 +00002126 }
jessicahandojo05765752016-09-09 19:01:32 -07002127 if (!maybe_compressed_char_at) {
2128 codegen_->Load(type, OutputCPURegister(instruction), source);
2129 codegen_->MaybeRecordImplicitNullCheck(instruction);
2130 }
Roland Levillain44015862016-01-22 11:47:17 +00002131
2132 if (type == Primitive::kPrimNot) {
2133 static_assert(
2134 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2135 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2136 Location obj_loc = locations->InAt(0);
2137 if (index.IsConstant()) {
2138 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2139 } else {
2140 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2141 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002142 }
Roland Levillain4d027112015-07-01 15:41:14 +01002143 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002144}
2145
Alexandre Rames5319def2014-10-23 10:03:10 +01002146void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2148 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002150}
2151
2152void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002153 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002154 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002155 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002156 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002157 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002158 // Mask out compression flag from String's array length.
2159 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2160 __ And(out.W(), out.W(), Operand(static_cast<int32_t>(INT32_MAX)));
2161 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002162}
2163
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002164void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002165 Primitive::Type value_type = instruction->GetComponentType();
2166
2167 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2169 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002170 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002171 LocationSummary::kCallOnSlowPath :
2172 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002173 locations->SetInAt(0, Location::RequiresRegister());
2174 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002175 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2176 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2177 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002178 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002179 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002180 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002181 }
2182}
2183
2184void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2185 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002186 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002187 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002188 bool needs_write_barrier =
2189 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002190
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002191 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002192 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002193 CPURegister source = value;
2194 Location index = locations->InAt(1);
2195 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2196 MemOperand destination = HeapOperand(array);
2197 MacroAssembler* masm = GetVIXLAssembler();
2198 BlockPoolsScope block_pools(masm);
2199
2200 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002201 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002202 if (index.IsConstant()) {
2203 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2204 destination = HeapOperand(array, offset);
2205 } else {
2206 UseScratchRegisterScope temps(masm);
2207 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002208 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002209 // The read barrier instrumentation does not support the
2210 // HIntermediateAddress instruction yet.
2211 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002212 // We do not need to compute the intermediate address from the array: the
2213 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002214 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002215 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002216 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002217 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2218 }
2219 temp = array;
2220 } else {
2221 __ Add(temp, array, offset);
2222 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002223 destination = HeapOperand(temp,
2224 XRegisterFrom(index),
2225 LSL,
2226 Primitive::ComponentSizeShift(value_type));
2227 }
2228 codegen_->Store(value_type, value, destination);
2229 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002230 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002231 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002232 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002233 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002234 {
2235 // We use a block to end the scratch scope before the write barrier, thus
2236 // freeing the temporary registers so they can be used in `MarkGCCard`.
2237 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002238 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002239 if (index.IsConstant()) {
2240 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002241 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002242 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002243 destination = HeapOperand(temp,
2244 XRegisterFrom(index),
2245 LSL,
2246 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002247 }
2248
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002249 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2250 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2251 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2252
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002253 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002254 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2255 codegen_->AddSlowPath(slow_path);
2256 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002257 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002258 __ Cbnz(Register(value), &non_zero);
2259 if (!index.IsConstant()) {
2260 __ Add(temp, array, offset);
2261 }
2262 __ Str(wzr, destination);
2263 codegen_->MaybeRecordImplicitNullCheck(instruction);
2264 __ B(&done);
2265 __ Bind(&non_zero);
2266 }
2267
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002268 // Note that when Baker read barriers are enabled, the type
2269 // checks are performed without read barriers. This is fine,
2270 // even in the case where a class object is in the from-space
2271 // after the flip, as a comparison involving such a type would
2272 // not produce a false positive; it may of course produce a
2273 // false negative, in which case we would take the ArraySet
2274 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002275
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002276 Register temp2 = temps.AcquireSameSizeAs(array);
2277 // /* HeapReference<Class> */ temp = array->klass_
2278 __ Ldr(temp, HeapOperand(array, class_offset));
2279 codegen_->MaybeRecordImplicitNullCheck(instruction);
2280 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002281
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002282 // /* HeapReference<Class> */ temp = temp->component_type_
2283 __ Ldr(temp, HeapOperand(temp, component_offset));
2284 // /* HeapReference<Class> */ temp2 = value->klass_
2285 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2286 // If heap poisoning is enabled, no need to unpoison `temp`
2287 // nor `temp2`, as we are comparing two poisoned references.
2288 __ Cmp(temp, temp2);
2289 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002290
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002291 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2292 vixl::aarch64::Label do_put;
2293 __ B(eq, &do_put);
2294 // If heap poisoning is enabled, the `temp` reference has
2295 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002296 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2297
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002298 // /* HeapReference<Class> */ temp = temp->super_class_
2299 __ Ldr(temp, HeapOperand(temp, super_offset));
2300 // If heap poisoning is enabled, no need to unpoison
2301 // `temp`, as we are comparing against null below.
2302 __ Cbnz(temp, slow_path->GetEntryLabel());
2303 __ Bind(&do_put);
2304 } else {
2305 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002306 }
2307 }
2308
2309 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002310 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002311 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002312 __ Mov(temp2, value.W());
2313 GetAssembler()->PoisonHeapReference(temp2);
2314 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002315 }
2316
2317 if (!index.IsConstant()) {
2318 __ Add(temp, array, offset);
2319 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002320 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002321
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002322 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002323 codegen_->MaybeRecordImplicitNullCheck(instruction);
2324 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002325 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002326
2327 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2328
2329 if (done.IsLinked()) {
2330 __ Bind(&done);
2331 }
2332
2333 if (slow_path != nullptr) {
2334 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002335 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002336 }
2337}
2338
Alexandre Rames67555f72014-11-18 10:55:16 +00002339void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002340 RegisterSet caller_saves = RegisterSet::Empty();
2341 InvokeRuntimeCallingConvention calling_convention;
2342 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2343 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2344 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002345 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002346 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002347}
2348
2349void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002350 BoundsCheckSlowPathARM64* slow_path =
2351 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002352 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002353 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2354 __ B(slow_path->GetEntryLabel(), hs);
2355}
2356
Alexandre Rames67555f72014-11-18 10:55:16 +00002357void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2358 LocationSummary* locations =
2359 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2360 locations->SetInAt(0, Location::RequiresRegister());
2361 if (check->HasUses()) {
2362 locations->SetOut(Location::SameAsFirstInput());
2363 }
2364}
2365
2366void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2367 // We assume the class is not null.
2368 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2369 check->GetLoadClass(), check, check->GetDexPc(), true);
2370 codegen_->AddSlowPath(slow_path);
2371 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2372}
2373
Roland Levillain1a653882016-03-18 18:05:57 +00002374static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2375 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2376 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2377}
2378
2379void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2380 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2381 Location rhs_loc = instruction->GetLocations()->InAt(1);
2382 if (rhs_loc.IsConstant()) {
2383 // 0.0 is the only immediate that can be encoded directly in
2384 // an FCMP instruction.
2385 //
2386 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2387 // specify that in a floating-point comparison, positive zero
2388 // and negative zero are considered equal, so we can use the
2389 // literal 0.0 for both cases here.
2390 //
2391 // Note however that some methods (Float.equal, Float.compare,
2392 // Float.compareTo, Double.equal, Double.compare,
2393 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2394 // StrictMath.min) consider 0.0 to be (strictly) greater than
2395 // -0.0. So if we ever translate calls to these methods into a
2396 // HCompare instruction, we must handle the -0.0 case with
2397 // care here.
2398 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2399 __ Fcmp(lhs_reg, 0.0);
2400 } else {
2401 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2402 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002403}
2404
Serban Constantinescu02164b32014-11-13 14:05:07 +00002405void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002406 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002407 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2408 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002409 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002410 case Primitive::kPrimBoolean:
2411 case Primitive::kPrimByte:
2412 case Primitive::kPrimShort:
2413 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002414 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002415 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002416 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002417 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2419 break;
2420 }
2421 case Primitive::kPrimFloat:
2422 case Primitive::kPrimDouble: {
2423 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002424 locations->SetInAt(1,
2425 IsFloatingPointZeroConstant(compare->InputAt(1))
2426 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2427 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002428 locations->SetOut(Location::RequiresRegister());
2429 break;
2430 }
2431 default:
2432 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2433 }
2434}
2435
2436void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2437 Primitive::Type in_type = compare->InputAt(0)->GetType();
2438
2439 // 0 if: left == right
2440 // 1 if: left > right
2441 // -1 if: left < right
2442 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002443 case Primitive::kPrimBoolean:
2444 case Primitive::kPrimByte:
2445 case Primitive::kPrimShort:
2446 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002447 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002448 case Primitive::kPrimLong: {
2449 Register result = OutputRegister(compare);
2450 Register left = InputRegisterAt(compare, 0);
2451 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002452 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002453 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2454 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002455 break;
2456 }
2457 case Primitive::kPrimFloat:
2458 case Primitive::kPrimDouble: {
2459 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002460 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002461 __ Cset(result, ne);
2462 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002463 break;
2464 }
2465 default:
2466 LOG(FATAL) << "Unimplemented compare type " << in_type;
2467 }
2468}
2469
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002470void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002472
2473 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2474 locations->SetInAt(0, Location::RequiresFpuRegister());
2475 locations->SetInAt(1,
2476 IsFloatingPointZeroConstant(instruction->InputAt(1))
2477 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2478 : Location::RequiresFpuRegister());
2479 } else {
2480 // Integer cases.
2481 locations->SetInAt(0, Location::RequiresRegister());
2482 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2483 }
2484
David Brazdilb3e773e2016-01-26 11:28:37 +00002485 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002487 }
2488}
2489
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002490void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002491 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002492 return;
2493 }
2494
2495 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002496 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002497 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002498
Roland Levillain7f63c522015-07-13 15:54:55 +00002499 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002500 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002501 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002502 } else {
2503 // Integer cases.
2504 Register lhs = InputRegisterAt(instruction, 0);
2505 Operand rhs = InputOperandAt(instruction, 1);
2506 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002507 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002508 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002509}
2510
2511#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2512 M(Equal) \
2513 M(NotEqual) \
2514 M(LessThan) \
2515 M(LessThanOrEqual) \
2516 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002517 M(GreaterThanOrEqual) \
2518 M(Below) \
2519 M(BelowOrEqual) \
2520 M(Above) \
2521 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002522#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002523void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2524void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002525FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002526#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002527#undef FOR_EACH_CONDITION_INSTRUCTION
2528
Zheng Xuc6667102015-05-15 16:08:45 +08002529void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2530 DCHECK(instruction->IsDiv() || instruction->IsRem());
2531
2532 LocationSummary* locations = instruction->GetLocations();
2533 Location second = locations->InAt(1);
2534 DCHECK(second.IsConstant());
2535
2536 Register out = OutputRegister(instruction);
2537 Register dividend = InputRegisterAt(instruction, 0);
2538 int64_t imm = Int64FromConstant(second.GetConstant());
2539 DCHECK(imm == 1 || imm == -1);
2540
2541 if (instruction->IsRem()) {
2542 __ Mov(out, 0);
2543 } else {
2544 if (imm == 1) {
2545 __ Mov(out, dividend);
2546 } else {
2547 __ Neg(out, dividend);
2548 }
2549 }
2550}
2551
2552void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2553 DCHECK(instruction->IsDiv() || instruction->IsRem());
2554
2555 LocationSummary* locations = instruction->GetLocations();
2556 Location second = locations->InAt(1);
2557 DCHECK(second.IsConstant());
2558
2559 Register out = OutputRegister(instruction);
2560 Register dividend = InputRegisterAt(instruction, 0);
2561 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002562 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002563 int ctz_imm = CTZ(abs_imm);
2564
2565 UseScratchRegisterScope temps(GetVIXLAssembler());
2566 Register temp = temps.AcquireSameSizeAs(out);
2567
2568 if (instruction->IsDiv()) {
2569 __ Add(temp, dividend, abs_imm - 1);
2570 __ Cmp(dividend, 0);
2571 __ Csel(out, temp, dividend, lt);
2572 if (imm > 0) {
2573 __ Asr(out, out, ctz_imm);
2574 } else {
2575 __ Neg(out, Operand(out, ASR, ctz_imm));
2576 }
2577 } else {
2578 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2579 __ Asr(temp, dividend, bits - 1);
2580 __ Lsr(temp, temp, bits - ctz_imm);
2581 __ Add(out, dividend, temp);
2582 __ And(out, out, abs_imm - 1);
2583 __ Sub(out, out, temp);
2584 }
2585}
2586
2587void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2588 DCHECK(instruction->IsDiv() || instruction->IsRem());
2589
2590 LocationSummary* locations = instruction->GetLocations();
2591 Location second = locations->InAt(1);
2592 DCHECK(second.IsConstant());
2593
2594 Register out = OutputRegister(instruction);
2595 Register dividend = InputRegisterAt(instruction, 0);
2596 int64_t imm = Int64FromConstant(second.GetConstant());
2597
2598 Primitive::Type type = instruction->GetResultType();
2599 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2600
2601 int64_t magic;
2602 int shift;
2603 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2604
2605 UseScratchRegisterScope temps(GetVIXLAssembler());
2606 Register temp = temps.AcquireSameSizeAs(out);
2607
2608 // temp = get_high(dividend * magic)
2609 __ Mov(temp, magic);
2610 if (type == Primitive::kPrimLong) {
2611 __ Smulh(temp, dividend, temp);
2612 } else {
2613 __ Smull(temp.X(), dividend, temp);
2614 __ Lsr(temp.X(), temp.X(), 32);
2615 }
2616
2617 if (imm > 0 && magic < 0) {
2618 __ Add(temp, temp, dividend);
2619 } else if (imm < 0 && magic > 0) {
2620 __ Sub(temp, temp, dividend);
2621 }
2622
2623 if (shift != 0) {
2624 __ Asr(temp, temp, shift);
2625 }
2626
2627 if (instruction->IsDiv()) {
2628 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2629 } else {
2630 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2631 // TODO: Strength reduction for msub.
2632 Register temp_imm = temps.AcquireSameSizeAs(out);
2633 __ Mov(temp_imm, imm);
2634 __ Msub(out, temp, temp_imm, dividend);
2635 }
2636}
2637
2638void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2639 DCHECK(instruction->IsDiv() || instruction->IsRem());
2640 Primitive::Type type = instruction->GetResultType();
2641 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2642
2643 LocationSummary* locations = instruction->GetLocations();
2644 Register out = OutputRegister(instruction);
2645 Location second = locations->InAt(1);
2646
2647 if (second.IsConstant()) {
2648 int64_t imm = Int64FromConstant(second.GetConstant());
2649
2650 if (imm == 0) {
2651 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2652 } else if (imm == 1 || imm == -1) {
2653 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002654 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002655 DivRemByPowerOfTwo(instruction);
2656 } else {
2657 DCHECK(imm <= -2 || imm >= 2);
2658 GenerateDivRemWithAnyConstant(instruction);
2659 }
2660 } else {
2661 Register dividend = InputRegisterAt(instruction, 0);
2662 Register divisor = InputRegisterAt(instruction, 1);
2663 if (instruction->IsDiv()) {
2664 __ Sdiv(out, dividend, divisor);
2665 } else {
2666 UseScratchRegisterScope temps(GetVIXLAssembler());
2667 Register temp = temps.AcquireSameSizeAs(out);
2668 __ Sdiv(temp, dividend, divisor);
2669 __ Msub(out, temp, divisor, dividend);
2670 }
2671 }
2672}
2673
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002674void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2675 LocationSummary* locations =
2676 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2677 switch (div->GetResultType()) {
2678 case Primitive::kPrimInt:
2679 case Primitive::kPrimLong:
2680 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002681 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2683 break;
2684
2685 case Primitive::kPrimFloat:
2686 case Primitive::kPrimDouble:
2687 locations->SetInAt(0, Location::RequiresFpuRegister());
2688 locations->SetInAt(1, Location::RequiresFpuRegister());
2689 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2690 break;
2691
2692 default:
2693 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2694 }
2695}
2696
2697void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2698 Primitive::Type type = div->GetResultType();
2699 switch (type) {
2700 case Primitive::kPrimInt:
2701 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002702 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002703 break;
2704
2705 case Primitive::kPrimFloat:
2706 case Primitive::kPrimDouble:
2707 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2708 break;
2709
2710 default:
2711 LOG(FATAL) << "Unexpected div type " << type;
2712 }
2713}
2714
Alexandre Rames67555f72014-11-18 10:55:16 +00002715void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002716 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002717 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002718}
2719
2720void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2721 SlowPathCodeARM64* slow_path =
2722 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2723 codegen_->AddSlowPath(slow_path);
2724 Location value = instruction->GetLocations()->InAt(0);
2725
Alexandre Rames3e69f162014-12-10 10:36:50 +00002726 Primitive::Type type = instruction->GetType();
2727
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002728 if (!Primitive::IsIntegralType(type)) {
2729 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002730 return;
2731 }
2732
Alexandre Rames67555f72014-11-18 10:55:16 +00002733 if (value.IsConstant()) {
2734 int64_t divisor = Int64ConstantFrom(value);
2735 if (divisor == 0) {
2736 __ B(slow_path->GetEntryLabel());
2737 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002738 // A division by a non-null constant is valid. We don't need to perform
2739 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002740 }
2741 } else {
2742 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2743 }
2744}
2745
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002746void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2747 LocationSummary* locations =
2748 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2749 locations->SetOut(Location::ConstantLocation(constant));
2750}
2751
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002752void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2753 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002754 // Will be generated at use site.
2755}
2756
Alexandre Rames5319def2014-10-23 10:03:10 +01002757void LocationsBuilderARM64::VisitExit(HExit* exit) {
2758 exit->SetLocations(nullptr);
2759}
2760
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002761void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002762}
2763
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002764void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2765 LocationSummary* locations =
2766 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2767 locations->SetOut(Location::ConstantLocation(constant));
2768}
2769
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002770void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002771 // Will be generated at use site.
2772}
2773
David Brazdilfc6a86a2015-06-26 10:33:45 +00002774void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002775 DCHECK(!successor->IsExitBlock());
2776 HBasicBlock* block = got->GetBlock();
2777 HInstruction* previous = got->GetPrevious();
2778 HLoopInformation* info = block->GetLoopInformation();
2779
David Brazdil46e2a392015-03-16 17:31:52 +00002780 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002781 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2782 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2783 return;
2784 }
2785 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2786 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2787 }
2788 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002789 __ B(codegen_->GetLabelOf(successor));
2790 }
2791}
2792
David Brazdilfc6a86a2015-06-26 10:33:45 +00002793void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2794 got->SetLocations(nullptr);
2795}
2796
2797void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2798 HandleGoto(got, got->GetSuccessor());
2799}
2800
2801void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2802 try_boundary->SetLocations(nullptr);
2803}
2804
2805void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2806 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2807 if (!successor->IsExitBlock()) {
2808 HandleGoto(try_boundary, successor);
2809 }
2810}
2811
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002812void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002813 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002814 vixl::aarch64::Label* true_target,
2815 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002816 // FP branching requires both targets to be explicit. If either of the targets
2817 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002818 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002819 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002820
David Brazdil0debae72015-11-12 18:37:00 +00002821 if (true_target == nullptr && false_target == nullptr) {
2822 // Nothing to do. The code always falls through.
2823 return;
2824 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002825 // Constant condition, statically compared against "true" (integer value 1).
2826 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002827 if (true_target != nullptr) {
2828 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002829 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002830 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002831 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002832 if (false_target != nullptr) {
2833 __ B(false_target);
2834 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002835 }
David Brazdil0debae72015-11-12 18:37:00 +00002836 return;
2837 }
2838
2839 // The following code generates these patterns:
2840 // (1) true_target == nullptr && false_target != nullptr
2841 // - opposite condition true => branch to false_target
2842 // (2) true_target != nullptr && false_target == nullptr
2843 // - condition true => branch to true_target
2844 // (3) true_target != nullptr && false_target != nullptr
2845 // - condition true => branch to true_target
2846 // - branch to false_target
2847 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002848 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002849 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002850 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002851 if (true_target == nullptr) {
2852 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2853 } else {
2854 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2855 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002856 } else {
2857 // The condition instruction has not been materialized, use its inputs as
2858 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002859 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002860
David Brazdil0debae72015-11-12 18:37:00 +00002861 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002862 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002863 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002864 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002865 IfCondition opposite_condition = condition->GetOppositeCondition();
2866 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002867 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002868 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002869 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002870 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002871 // Integer cases.
2872 Register lhs = InputRegisterAt(condition, 0);
2873 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002874
2875 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002876 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002877 if (true_target == nullptr) {
2878 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2879 non_fallthrough_target = false_target;
2880 } else {
2881 arm64_cond = ARM64Condition(condition->GetCondition());
2882 non_fallthrough_target = true_target;
2883 }
2884
Aart Bik086d27e2016-01-20 17:02:00 -08002885 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002886 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002887 switch (arm64_cond) {
2888 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002889 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002890 break;
2891 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002892 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002893 break;
2894 case lt:
2895 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002896 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002897 break;
2898 case ge:
2899 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002900 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002901 break;
2902 default:
2903 // Without the `static_cast` the compiler throws an error for
2904 // `-Werror=sign-promo`.
2905 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2906 }
2907 } else {
2908 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002909 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002910 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002911 }
2912 }
David Brazdil0debae72015-11-12 18:37:00 +00002913
2914 // If neither branch falls through (case 3), the conditional branch to `true_target`
2915 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2916 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002917 __ B(false_target);
2918 }
David Brazdil0debae72015-11-12 18:37:00 +00002919
2920 if (fallthrough_target.IsLinked()) {
2921 __ Bind(&fallthrough_target);
2922 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002923}
2924
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002925void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2926 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002927 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002928 locations->SetInAt(0, Location::RequiresRegister());
2929 }
2930}
2931
2932void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002933 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2934 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002935 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2936 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2937 true_target = nullptr;
2938 }
2939 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2940 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2941 false_target = nullptr;
2942 }
David Brazdil0debae72015-11-12 18:37:00 +00002943 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002944}
2945
2946void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2947 LocationSummary* locations = new (GetGraph()->GetArena())
2948 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002949 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002950 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002951 locations->SetInAt(0, Location::RequiresRegister());
2952 }
2953}
2954
2955void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002956 SlowPathCodeARM64* slow_path =
2957 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002958 GenerateTestAndBranch(deoptimize,
2959 /* condition_input_index */ 0,
2960 slow_path->GetEntryLabel(),
2961 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002962}
2963
David Brazdilc0b601b2016-02-08 14:20:45 +00002964static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2965 return condition->IsCondition() &&
2966 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2967}
2968
Alexandre Rames880f1192016-06-13 16:04:50 +01002969static inline Condition GetConditionForSelect(HCondition* condition) {
2970 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002971 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2972 : ARM64Condition(cond);
2973}
2974
David Brazdil74eb1b22015-12-14 11:44:01 +00002975void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002977 if (Primitive::IsFloatingPointType(select->GetType())) {
2978 locations->SetInAt(0, Location::RequiresFpuRegister());
2979 locations->SetInAt(1, Location::RequiresFpuRegister());
2980 locations->SetOut(Location::RequiresFpuRegister());
2981 } else {
2982 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2983 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2984 bool is_true_value_constant = cst_true_value != nullptr;
2985 bool is_false_value_constant = cst_false_value != nullptr;
2986 // Ask VIXL whether we should synthesize constants in registers.
2987 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
2988 Operand true_op = is_true_value_constant ?
2989 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
2990 Operand false_op = is_false_value_constant ?
2991 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
2992 bool true_value_in_register = false;
2993 bool false_value_in_register = false;
2994 MacroAssembler::GetCselSynthesisInformation(
2995 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
2996 true_value_in_register |= !is_true_value_constant;
2997 false_value_in_register |= !is_false_value_constant;
2998
2999 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3000 : Location::ConstantLocation(cst_true_value));
3001 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3002 : Location::ConstantLocation(cst_false_value));
3003 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003004 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003005
David Brazdil74eb1b22015-12-14 11:44:01 +00003006 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3007 locations->SetInAt(2, Location::RequiresRegister());
3008 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003009}
3010
3011void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003012 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003013 Condition csel_cond;
3014
3015 if (IsBooleanValueOrMaterializedCondition(cond)) {
3016 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003017 // Use the condition flags set by the previous instruction.
3018 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003019 } else {
3020 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003021 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003022 }
3023 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003024 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003025 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003026 } else {
3027 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003028 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003029 }
3030
Alexandre Rames880f1192016-06-13 16:04:50 +01003031 if (Primitive::IsFloatingPointType(select->GetType())) {
3032 __ Fcsel(OutputFPRegister(select),
3033 InputFPRegisterAt(select, 1),
3034 InputFPRegisterAt(select, 0),
3035 csel_cond);
3036 } else {
3037 __ Csel(OutputRegister(select),
3038 InputOperandAt(select, 1),
3039 InputOperandAt(select, 0),
3040 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003041 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003042}
3043
David Srbecky0cf44932015-12-09 14:09:59 +00003044void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3045 new (GetGraph()->GetArena()) LocationSummary(info);
3046}
3047
David Srbeckyd28f4a02016-03-14 17:14:24 +00003048void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3049 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003050}
3051
3052void CodeGeneratorARM64::GenerateNop() {
3053 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003054}
3055
Alexandre Rames5319def2014-10-23 10:03:10 +01003056void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003057 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003058}
3059
3060void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003061 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003062}
3063
3064void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003065 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003066}
3067
3068void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003069 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003070}
3071
Roland Levillain44015862016-01-22 11:47:17 +00003072static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3073 return kEmitCompilerReadBarrier &&
3074 (kUseBakerReadBarrier ||
3075 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3076 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3077 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3078}
3079
Alexandre Rames67555f72014-11-18 10:55:16 +00003080void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003081 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003082 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003083 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003084 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003085 case TypeCheckKind::kExactCheck:
3086 case TypeCheckKind::kAbstractClassCheck:
3087 case TypeCheckKind::kClassHierarchyCheck:
3088 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003089 call_kind =
3090 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003091 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003092 break;
3093 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003094 case TypeCheckKind::kUnresolvedCheck:
3095 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003096 call_kind = LocationSummary::kCallOnSlowPath;
3097 break;
3098 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003099
Alexandre Rames67555f72014-11-18 10:55:16 +00003100 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003101 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003102 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003103 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003104 locations->SetInAt(0, Location::RequiresRegister());
3105 locations->SetInAt(1, Location::RequiresRegister());
3106 // The "out" register is used as a temporary, so it overlaps with the inputs.
3107 // Note that TypeCheckSlowPathARM64 uses this register too.
3108 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3109 // When read barriers are enabled, we need a temporary register for
3110 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003111 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003112 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003113 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003114}
3115
3116void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003117 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003118 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003119 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003120 Register obj = InputRegisterAt(instruction, 0);
3121 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003122 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003123 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003124 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3125 locations->GetTemp(0) :
3126 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003127 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3128 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3129 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3130 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003131
Scott Wakeling97c72b72016-06-24 16:19:36 +01003132 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003133 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003134
3135 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003136 // Avoid null check if we know `obj` is not null.
3137 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003138 __ Cbz(obj, &zero);
3139 }
3140
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003141 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003142 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003143
Roland Levillain44015862016-01-22 11:47:17 +00003144 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003145 case TypeCheckKind::kExactCheck: {
3146 __ Cmp(out, cls);
3147 __ Cset(out, eq);
3148 if (zero.IsLinked()) {
3149 __ B(&done);
3150 }
3151 break;
3152 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003153
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003154 case TypeCheckKind::kAbstractClassCheck: {
3155 // If the class is abstract, we eagerly fetch the super class of the
3156 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003157 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003158 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003159 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003160 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003161 // If `out` is null, we use it for the result, and jump to `done`.
3162 __ Cbz(out, &done);
3163 __ Cmp(out, cls);
3164 __ B(ne, &loop);
3165 __ Mov(out, 1);
3166 if (zero.IsLinked()) {
3167 __ B(&done);
3168 }
3169 break;
3170 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003171
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003172 case TypeCheckKind::kClassHierarchyCheck: {
3173 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003174 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003175 __ Bind(&loop);
3176 __ Cmp(out, cls);
3177 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003178 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003179 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003180 __ Cbnz(out, &loop);
3181 // If `out` is null, we use it for the result, and jump to `done`.
3182 __ B(&done);
3183 __ Bind(&success);
3184 __ Mov(out, 1);
3185 if (zero.IsLinked()) {
3186 __ B(&done);
3187 }
3188 break;
3189 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003190
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003191 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003192 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003193 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003194 __ Cmp(out, cls);
3195 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003196 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003197 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003198 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003199 // If `out` is null, we use it for the result, and jump to `done`.
3200 __ Cbz(out, &done);
3201 __ Ldrh(out, HeapOperand(out, primitive_offset));
3202 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3203 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003204 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003205 __ Mov(out, 1);
3206 __ B(&done);
3207 break;
3208 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003209
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003210 case TypeCheckKind::kArrayCheck: {
3211 __ Cmp(out, cls);
3212 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003213 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3214 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003215 codegen_->AddSlowPath(slow_path);
3216 __ B(ne, slow_path->GetEntryLabel());
3217 __ Mov(out, 1);
3218 if (zero.IsLinked()) {
3219 __ B(&done);
3220 }
3221 break;
3222 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003223
Calin Juravle98893e12015-10-02 21:05:03 +01003224 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003225 case TypeCheckKind::kInterfaceCheck: {
3226 // Note that we indeed only call on slow path, but we always go
3227 // into the slow path for the unresolved and interface check
3228 // cases.
3229 //
3230 // We cannot directly call the InstanceofNonTrivial runtime
3231 // entry point without resorting to a type checking slow path
3232 // here (i.e. by calling InvokeRuntime directly), as it would
3233 // require to assign fixed registers for the inputs of this
3234 // HInstanceOf instruction (following the runtime calling
3235 // convention), which might be cluttered by the potential first
3236 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003237 //
3238 // TODO: Introduce a new runtime entry point taking the object
3239 // to test (instead of its class) as argument, and let it deal
3240 // with the read barrier issues. This will let us refactor this
3241 // case of the `switch` code as it was previously (with a direct
3242 // call to the runtime not using a type checking slow path).
3243 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003244 DCHECK(locations->OnlyCallsOnSlowPath());
3245 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3246 /* is_fatal */ false);
3247 codegen_->AddSlowPath(slow_path);
3248 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003249 if (zero.IsLinked()) {
3250 __ B(&done);
3251 }
3252 break;
3253 }
3254 }
3255
3256 if (zero.IsLinked()) {
3257 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003258 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003259 }
3260
3261 if (done.IsLinked()) {
3262 __ Bind(&done);
3263 }
3264
3265 if (slow_path != nullptr) {
3266 __ Bind(slow_path->GetExitLabel());
3267 }
3268}
3269
3270void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3271 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3272 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3273
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003274 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3275 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003276 case TypeCheckKind::kExactCheck:
3277 case TypeCheckKind::kAbstractClassCheck:
3278 case TypeCheckKind::kClassHierarchyCheck:
3279 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003280 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3281 LocationSummary::kCallOnSlowPath :
3282 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003283 break;
3284 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003285 case TypeCheckKind::kUnresolvedCheck:
3286 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003287 call_kind = LocationSummary::kCallOnSlowPath;
3288 break;
3289 }
3290
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003291 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3292 locations->SetInAt(0, Location::RequiresRegister());
3293 locations->SetInAt(1, Location::RequiresRegister());
3294 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3295 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003296 // When read barriers are enabled, we need an additional temporary
3297 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003298 if (TypeCheckNeedsATemporary(type_check_kind)) {
3299 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003300 }
3301}
3302
3303void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003304 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003305 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003306 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003307 Register obj = InputRegisterAt(instruction, 0);
3308 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003309 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003310 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3311 locations->GetTemp(1) :
3312 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003313 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003314 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3315 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3316 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3317 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003318
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003319 bool is_type_check_slow_path_fatal =
3320 (type_check_kind == TypeCheckKind::kExactCheck ||
3321 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3322 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3323 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3324 !instruction->CanThrowIntoCatchBlock();
3325 SlowPathCodeARM64* type_check_slow_path =
3326 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3327 is_type_check_slow_path_fatal);
3328 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003329
Scott Wakeling97c72b72016-06-24 16:19:36 +01003330 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003331 // Avoid null check if we know obj is not null.
3332 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003333 __ Cbz(obj, &done);
3334 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003335
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003336 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003337 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003338
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003339 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003340 case TypeCheckKind::kExactCheck:
3341 case TypeCheckKind::kArrayCheck: {
3342 __ Cmp(temp, cls);
3343 // Jump to slow path for throwing the exception or doing a
3344 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003345 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003346 break;
3347 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003349 case TypeCheckKind::kAbstractClassCheck: {
3350 // If the class is abstract, we eagerly fetch the super class of the
3351 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003352 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003353 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003355 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003356
3357 // If the class reference currently in `temp` is not null, jump
3358 // to the `compare_classes` label to compare it with the checked
3359 // class.
3360 __ Cbnz(temp, &compare_classes);
3361 // Otherwise, jump to the slow path to throw the exception.
3362 //
3363 // But before, move back the object's class into `temp` before
3364 // going into the slow path, as it has been overwritten in the
3365 // meantime.
3366 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003367 GenerateReferenceLoadTwoRegisters(
3368 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369 __ B(type_check_slow_path->GetEntryLabel());
3370
3371 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003372 __ Cmp(temp, cls);
3373 __ B(ne, &loop);
3374 break;
3375 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003377 case TypeCheckKind::kClassHierarchyCheck: {
3378 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003379 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003380 __ Bind(&loop);
3381 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003382 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003383
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003384 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003385 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386
3387 // If the class reference currently in `temp` is not null, jump
3388 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003389 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003390 // Otherwise, jump to the slow path to throw the exception.
3391 //
3392 // But before, move back the object's class into `temp` before
3393 // going into the slow path, as it has been overwritten in the
3394 // meantime.
3395 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003396 GenerateReferenceLoadTwoRegisters(
3397 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003398 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003399 break;
3400 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003401
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003402 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003403 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003404 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003405 __ Cmp(temp, cls);
3406 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003407
3408 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003409 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003410 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003411
3412 // If the component type is not null (i.e. the object is indeed
3413 // an array), jump to label `check_non_primitive_component_type`
3414 // to further check that this component type is not a primitive
3415 // type.
3416 __ Cbnz(temp, &check_non_primitive_component_type);
3417 // Otherwise, jump to the slow path to throw the exception.
3418 //
3419 // But before, move back the object's class into `temp` before
3420 // going into the slow path, as it has been overwritten in the
3421 // meantime.
3422 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003423 GenerateReferenceLoadTwoRegisters(
3424 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003425 __ B(type_check_slow_path->GetEntryLabel());
3426
3427 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003428 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3429 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 __ Cbz(temp, &done);
3431 // Same comment as above regarding `temp` and the slow path.
3432 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003433 GenerateReferenceLoadTwoRegisters(
3434 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003435 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003436 break;
3437 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438
Calin Juravle98893e12015-10-02 21:05:03 +01003439 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003440 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441 // We always go into the type check slow path for the unresolved
3442 // and interface check cases.
3443 //
3444 // We cannot directly call the CheckCast runtime entry point
3445 // without resorting to a type checking slow path here (i.e. by
3446 // calling InvokeRuntime directly), as it would require to
3447 // assign fixed registers for the inputs of this HInstanceOf
3448 // instruction (following the runtime calling convention), which
3449 // might be cluttered by the potential first read barrier
3450 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003451 //
3452 // TODO: Introduce a new runtime entry point taking the object
3453 // to test (instead of its class) as argument, and let it deal
3454 // with the read barrier issues. This will let us refactor this
3455 // case of the `switch` code as it was previously (with a direct
3456 // call to the runtime not using a type checking slow path).
3457 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003458 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 break;
3460 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003461 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003462
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003463 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003464}
3465
Alexandre Rames5319def2014-10-23 10:03:10 +01003466void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3467 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3468 locations->SetOut(Location::ConstantLocation(constant));
3469}
3470
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003471void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003472 // Will be generated at use site.
3473}
3474
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003475void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3477 locations->SetOut(Location::ConstantLocation(constant));
3478}
3479
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003480void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003481 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003482}
3483
Calin Juravle175dc732015-08-25 15:42:32 +01003484void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3485 // The trampoline uses the same calling convention as dex calling conventions,
3486 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3487 // the method_idx.
3488 HandleInvoke(invoke);
3489}
3490
3491void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3492 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3493}
3494
Alexandre Rames5319def2014-10-23 10:03:10 +01003495void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003496 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003497 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003498}
3499
Alexandre Rames67555f72014-11-18 10:55:16 +00003500void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3501 HandleInvoke(invoke);
3502}
3503
3504void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3505 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003506 LocationSummary* locations = invoke->GetLocations();
3507 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003508 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003509 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003510 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003511
3512 // The register ip1 is required to be used for the hidden argument in
3513 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003514 MacroAssembler* masm = GetVIXLAssembler();
3515 UseScratchRegisterScope scratch_scope(masm);
3516 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003517 scratch_scope.Exclude(ip1);
3518 __ Mov(ip1, invoke->GetDexMethodIndex());
3519
Alexandre Rames67555f72014-11-18 10:55:16 +00003520 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003521 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003522 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003523 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003524 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003525 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003526 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003527 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003528 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003529 // Instead of simply (possibly) unpoisoning `temp` here, we should
3530 // emit a read barrier for the previous class reference load.
3531 // However this is not required in practice, as this is an
3532 // intermediate/temporary reference and because the current
3533 // concurrent copying collector keeps the from-space memory
3534 // intact/accessible until the end of the marking phase (the
3535 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003536 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003537 __ Ldr(temp,
3538 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3539 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003540 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003541 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003542 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003543 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003544 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003545 // lr();
3546 __ Blr(lr);
3547 DCHECK(!codegen_->IsLeafMethod());
3548 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3549}
3550
3551void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003552 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3553 if (intrinsic.TryDispatch(invoke)) {
3554 return;
3555 }
3556
Alexandre Rames67555f72014-11-18 10:55:16 +00003557 HandleInvoke(invoke);
3558}
3559
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003560void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003561 // Explicit clinit checks triggered by static invokes must have been pruned by
3562 // art::PrepareForRegisterAllocation.
3563 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003564
Andreas Gampe878d58c2015-01-15 23:24:00 -08003565 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3566 if (intrinsic.TryDispatch(invoke)) {
3567 return;
3568 }
3569
Alexandre Rames67555f72014-11-18 10:55:16 +00003570 HandleInvoke(invoke);
3571}
3572
Andreas Gampe878d58c2015-01-15 23:24:00 -08003573static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3574 if (invoke->GetLocations()->Intrinsified()) {
3575 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3576 intrinsic.Dispatch(invoke);
3577 return true;
3578 }
3579 return false;
3580}
3581
Vladimir Markodc151b22015-10-15 18:02:30 +01003582HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3583 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003584 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003585 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003586 return desired_dispatch_info;
3587}
3588
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003589void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003590 // For better instruction scheduling we load the direct code pointer before the method pointer.
3591 bool direct_code_loaded = false;
3592 switch (invoke->GetCodePtrLocation()) {
3593 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3594 // LR = code address from literal pool with link-time patch.
3595 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3596 direct_code_loaded = true;
3597 break;
3598 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3599 // LR = invoke->GetDirectCodePtr();
3600 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3601 direct_code_loaded = true;
3602 break;
3603 default:
3604 break;
3605 }
3606
Andreas Gampe878d58c2015-01-15 23:24:00 -08003607 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003608 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3609 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003610 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3611 uint32_t offset =
3612 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003613 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003614 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003615 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003616 }
Vladimir Marko58155012015-08-19 12:49:41 +00003617 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003618 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003619 break;
3620 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3621 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003622 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003623 break;
3624 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3625 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003626 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003627 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3628 break;
3629 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3630 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003631 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003632 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003633 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko5f926052016-09-30 17:04:49 +00003634 {
3635 SingleEmissionCheckScope guard(GetVIXLAssembler());
3636 __ Bind(adrp_label);
3637 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
3638 }
Vladimir Marko58155012015-08-19 12:49:41 +00003639 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003640 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003641 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Marko5f926052016-09-30 17:04:49 +00003642 {
3643 SingleEmissionCheckScope guard(GetVIXLAssembler());
3644 __ Bind(ldr_label);
3645 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
3646 }
Vladimir Marko58155012015-08-19 12:49:41 +00003647 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003648 }
Vladimir Marko58155012015-08-19 12:49:41 +00003649 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003650 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003651 Register reg = XRegisterFrom(temp);
3652 Register method_reg;
3653 if (current_method.IsRegister()) {
3654 method_reg = XRegisterFrom(current_method);
3655 } else {
3656 DCHECK(invoke->GetLocations()->Intrinsified());
3657 DCHECK(!current_method.IsValid());
3658 method_reg = reg;
3659 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3660 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003661
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003662 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003663 __ Ldr(reg.X(),
3664 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003665 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003666 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003667 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3668 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003669 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3670 break;
3671 }
3672 }
3673
3674 switch (invoke->GetCodePtrLocation()) {
3675 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3676 __ Bl(&frame_entry_label_);
3677 break;
3678 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Marko5f926052016-09-30 17:04:49 +00003679 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003680 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3681 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003682 __ Bind(label);
3683 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003684 break;
3685 }
3686 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3687 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3688 // LR prepared above for better instruction scheduling.
3689 DCHECK(direct_code_loaded);
3690 // lr()
3691 __ Blr(lr);
3692 break;
3693 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3694 // LR = callee_method->entry_point_from_quick_compiled_code_;
3695 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003696 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003697 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003698 // lr()
3699 __ Blr(lr);
3700 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003701 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003702
Andreas Gampe878d58c2015-01-15 23:24:00 -08003703 DCHECK(!IsLeafMethod());
3704}
3705
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003706void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003707 // Use the calling convention instead of the location of the receiver, as
3708 // intrinsics may have put the receiver in a different register. In the intrinsics
3709 // slow path, the arguments have been moved to the right place, so here we are
3710 // guaranteed that the receiver is the first register of the calling convention.
3711 InvokeDexCallingConvention calling_convention;
3712 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003713 Register temp = XRegisterFrom(temp_in);
3714 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3715 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3716 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003717 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003718
3719 BlockPoolsScope block_pools(GetVIXLAssembler());
3720
3721 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003722 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003723 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003724 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003725 // Instead of simply (possibly) unpoisoning `temp` here, we should
3726 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003727 // intermediate/temporary reference and because the current
3728 // concurrent copying collector keeps the from-space memory
3729 // intact/accessible until the end of the marking phase (the
3730 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003731 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3732 // temp = temp->GetMethodAt(method_offset);
3733 __ Ldr(temp, MemOperand(temp, method_offset));
3734 // lr = temp->GetEntryPoint();
3735 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3736 // lr();
3737 __ Blr(lr);
3738}
3739
Scott Wakeling97c72b72016-06-24 16:19:36 +01003740vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3741 const DexFile& dex_file,
3742 uint32_t string_index,
3743 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003744 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3745}
3746
Scott Wakeling97c72b72016-06-24 16:19:36 +01003747vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3748 const DexFile& dex_file,
3749 uint32_t type_index,
3750 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003751 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3752}
3753
Scott Wakeling97c72b72016-06-24 16:19:36 +01003754vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3755 const DexFile& dex_file,
3756 uint32_t element_offset,
3757 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003758 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3759}
3760
Scott Wakeling97c72b72016-06-24 16:19:36 +01003761vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3762 const DexFile& dex_file,
3763 uint32_t offset_or_index,
3764 vixl::aarch64::Label* adrp_label,
3765 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003766 // Add a patch entry and return the label.
3767 patches->emplace_back(dex_file, offset_or_index);
3768 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003769 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003770 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3771 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3772 return label;
3773}
3774
Scott Wakeling97c72b72016-06-24 16:19:36 +01003775vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003776 const DexFile& dex_file, uint32_t string_index) {
3777 return boot_image_string_patches_.GetOrCreate(
3778 StringReference(&dex_file, string_index),
3779 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3780}
3781
Scott Wakeling97c72b72016-06-24 16:19:36 +01003782vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003783 const DexFile& dex_file, uint32_t type_index) {
3784 return boot_image_type_patches_.GetOrCreate(
3785 TypeReference(&dex_file, type_index),
3786 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3787}
3788
Scott Wakeling97c72b72016-06-24 16:19:36 +01003789vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3790 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003791 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3792 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3793 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3794}
3795
Scott Wakeling97c72b72016-06-24 16:19:36 +01003796vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3797 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003798 return DeduplicateUint64Literal(address);
3799}
3800
Vladimir Marko58155012015-08-19 12:49:41 +00003801void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3802 DCHECK(linker_patches->empty());
3803 size_t size =
3804 method_patches_.size() +
3805 call_patches_.size() +
3806 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003807 pc_relative_dex_cache_patches_.size() +
3808 boot_image_string_patches_.size() +
3809 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003810 boot_image_type_patches_.size() +
3811 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003812 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003813 linker_patches->reserve(size);
3814 for (const auto& entry : method_patches_) {
3815 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003816 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3817 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003818 target_method.dex_file,
3819 target_method.dex_method_index));
3820 }
3821 for (const auto& entry : call_patches_) {
3822 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003823 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3824 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003825 target_method.dex_file,
3826 target_method.dex_method_index));
3827 }
Vladimir Marko5f926052016-09-30 17:04:49 +00003828 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3829 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
3830 info.target_method.dex_file,
3831 info.target_method.dex_method_index));
Vladimir Marko58155012015-08-19 12:49:41 +00003832 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003833 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003834 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003835 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003836 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003837 info.offset_or_index));
3838 }
3839 for (const auto& entry : boot_image_string_patches_) {
3840 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003841 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3842 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003843 target_string.dex_file,
3844 target_string.string_index));
3845 }
Vladimir Marko5f926052016-09-30 17:04:49 +00003846 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
3847 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
3848 &info.target_dex_file,
3849 info.pc_insn_label->GetLocation(),
3850 info.offset_or_index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003851 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003852 for (const auto& entry : boot_image_type_patches_) {
3853 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003854 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3855 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003856 target_type.dex_file,
3857 target_type.type_index));
3858 }
Vladimir Marko5f926052016-09-30 17:04:49 +00003859 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
3860 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
3861 &info.target_dex_file,
3862 info.pc_insn_label->GetLocation(),
3863 info.offset_or_index));
3864 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003865 for (const auto& entry : boot_image_address_patches_) {
3866 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003867 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3868 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003869 }
3870}
3871
Scott Wakeling97c72b72016-06-24 16:19:36 +01003872vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003873 Uint32ToLiteralMap* map) {
3874 return map->GetOrCreate(
3875 value,
3876 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3877}
3878
Scott Wakeling97c72b72016-06-24 16:19:36 +01003879vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003880 return uint64_literals_.GetOrCreate(
3881 value,
3882 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003883}
3884
Scott Wakeling97c72b72016-06-24 16:19:36 +01003885vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003886 MethodReference target_method,
3887 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003888 return map->GetOrCreate(
3889 target_method,
3890 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003891}
3892
Scott Wakeling97c72b72016-06-24 16:19:36 +01003893vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003894 MethodReference target_method) {
3895 return DeduplicateMethodLiteral(target_method, &method_patches_);
3896}
3897
Scott Wakeling97c72b72016-06-24 16:19:36 +01003898vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003899 MethodReference target_method) {
3900 return DeduplicateMethodLiteral(target_method, &call_patches_);
3901}
3902
3903
Andreas Gampe878d58c2015-01-15 23:24:00 -08003904void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003905 // Explicit clinit checks triggered by static invokes must have been pruned by
3906 // art::PrepareForRegisterAllocation.
3907 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003908
Andreas Gampe878d58c2015-01-15 23:24:00 -08003909 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3910 return;
3911 }
3912
Alexandre Ramesd921d642015-04-16 15:07:16 +01003913 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003914 LocationSummary* locations = invoke->GetLocations();
3915 codegen_->GenerateStaticOrDirectCall(
3916 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003917 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003918}
3919
3920void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003921 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3922 return;
3923 }
3924
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003925 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003926 DCHECK(!codegen_->IsLeafMethod());
3927 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3928}
3929
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003930HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3931 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003932 switch (desired_class_load_kind) {
3933 case HLoadClass::LoadKind::kReferrersClass:
3934 break;
3935 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3936 DCHECK(!GetCompilerOptions().GetCompilePic());
3937 break;
3938 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3939 DCHECK(GetCompilerOptions().GetCompilePic());
3940 break;
3941 case HLoadClass::LoadKind::kBootImageAddress:
3942 break;
3943 case HLoadClass::LoadKind::kDexCacheAddress:
3944 DCHECK(Runtime::Current()->UseJitCompilation());
3945 break;
3946 case HLoadClass::LoadKind::kDexCachePcRelative:
3947 DCHECK(!Runtime::Current()->UseJitCompilation());
3948 break;
3949 case HLoadClass::LoadKind::kDexCacheViaMethod:
3950 break;
3951 }
3952 return desired_class_load_kind;
3953}
3954
Alexandre Rames67555f72014-11-18 10:55:16 +00003955void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003956 if (cls->NeedsAccessCheck()) {
3957 InvokeRuntimeCallingConvention calling_convention;
3958 CodeGenerator::CreateLoadClassLocationSummary(
3959 cls,
3960 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003961 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003962 /* code_generator_supports_read_barrier */ true);
3963 return;
3964 }
3965
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003966 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
3967 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003968 ? LocationSummary::kCallOnSlowPath
3969 : LocationSummary::kNoCall;
3970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003971 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003972 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003973 }
3974
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003975 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3976 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3977 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3978 locations->SetInAt(0, Location::RequiresRegister());
3979 }
3980 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003981}
3982
3983void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003984 if (cls->NeedsAccessCheck()) {
3985 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00003986 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003987 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003988 return;
3989 }
3990
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003991 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003992 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00003993
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003994 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003995 bool generate_null_check = false;
3996 switch (cls->GetLoadKind()) {
3997 case HLoadClass::LoadKind::kReferrersClass: {
3998 DCHECK(!cls->CanCallRuntime());
3999 DCHECK(!cls->MustGenerateClinitCheck());
4000 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4001 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004002 GenerateGcRootFieldLoad(cls,
4003 out_loc,
4004 current_method,
4005 ArtMethod::DeclaringClassOffset().Int32Value(),
4006 /*fixup_label*/ nullptr,
4007 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004008 break;
4009 }
4010 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004011 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004012 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4013 cls->GetTypeIndex()));
4014 break;
4015 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004016 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004017 // Add ADRP with its PC-relative type patch.
4018 const DexFile& dex_file = cls->GetDexFile();
4019 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004020 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Marko5f926052016-09-30 17:04:49 +00004021 {
4022 SingleEmissionCheckScope guard(GetVIXLAssembler());
4023 __ Bind(adrp_label);
4024 __ adrp(out.X(), /* offset placeholder */ 0);
4025 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004026 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004027 vixl::aarch64::Label* add_label =
4028 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko5f926052016-09-30 17:04:49 +00004029 {
4030 SingleEmissionCheckScope guard(GetVIXLAssembler());
4031 __ Bind(add_label);
4032 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4033 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004034 break;
4035 }
4036 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004037 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004038 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4039 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4040 break;
4041 }
4042 case HLoadClass::LoadKind::kDexCacheAddress: {
4043 DCHECK_NE(cls->GetAddress(), 0u);
4044 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4045 // that gives a 16KiB range. To try and reduce the number of literals if we load
4046 // multiple types, simply split the dex cache address to a 16KiB aligned base
4047 // loaded from a literal and the remaining offset embedded in the load.
4048 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4049 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4050 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4051 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4052 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4053 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4054 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004055 GenerateGcRootFieldLoad(cls,
4056 out_loc,
4057 out.X(),
4058 offset,
4059 /*fixup_label*/ nullptr,
4060 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004061 generate_null_check = !cls->IsInDexCache();
4062 break;
4063 }
4064 case HLoadClass::LoadKind::kDexCachePcRelative: {
4065 // Add ADRP with its PC-relative DexCache access patch.
4066 const DexFile& dex_file = cls->GetDexFile();
4067 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004068 vixl::aarch64::Label* adrp_label =
4069 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko5f926052016-09-30 17:04:49 +00004070 {
4071 SingleEmissionCheckScope guard(GetVIXLAssembler());
4072 __ Bind(adrp_label);
4073 __ adrp(out.X(), /* offset placeholder */ 0);
4074 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004075 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004076 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004077 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4078 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004079 GenerateGcRootFieldLoad(cls,
4080 out_loc,
4081 out.X(),
4082 /* offset placeholder */ 0,
4083 ldr_label,
4084 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004085 generate_null_check = !cls->IsInDexCache();
4086 break;
4087 }
4088 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4089 MemberOffset resolved_types_offset =
4090 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4091 // /* GcRoot<mirror::Class>[] */ out =
4092 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4093 Register current_method = InputRegisterAt(cls, 0);
4094 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4095 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004096 GenerateGcRootFieldLoad(cls,
4097 out_loc,
4098 out.X(),
4099 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4100 /*fixup_label*/ nullptr,
4101 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004102 generate_null_check = !cls->IsInDexCache();
4103 break;
4104 }
4105 }
4106
4107 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4108 DCHECK(cls->CanCallRuntime());
4109 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4110 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4111 codegen_->AddSlowPath(slow_path);
4112 if (generate_null_check) {
4113 __ Cbz(out, slow_path->GetEntryLabel());
4114 }
4115 if (cls->MustGenerateClinitCheck()) {
4116 GenerateClassInitializationCheck(slow_path, out);
4117 } else {
4118 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004119 }
4120 }
4121}
4122
David Brazdilcb1c0552015-08-04 16:22:25 +01004123static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004124 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004125}
4126
Alexandre Rames67555f72014-11-18 10:55:16 +00004127void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4128 LocationSummary* locations =
4129 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4130 locations->SetOut(Location::RequiresRegister());
4131}
4132
4133void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004134 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4135}
4136
4137void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4138 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4139}
4140
4141void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4142 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004143}
4144
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004145HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4146 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004147 switch (desired_string_load_kind) {
4148 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4149 DCHECK(!GetCompilerOptions().GetCompilePic());
4150 break;
4151 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4152 DCHECK(GetCompilerOptions().GetCompilePic());
4153 break;
4154 case HLoadString::LoadKind::kBootImageAddress:
4155 break;
4156 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004157 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004158 break;
Vladimir Marko5f926052016-09-30 17:04:49 +00004159 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004160 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004161 break;
4162 case HLoadString::LoadKind::kDexCacheViaMethod:
4163 break;
4164 }
4165 return desired_string_load_kind;
4166}
4167
Alexandre Rames67555f72014-11-18 10:55:16 +00004168void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004169 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Marko5f926052016-09-30 17:04:49 +00004170 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004171 : LocationSummary::kNoCall;
4172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004173 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4174 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004175 InvokeRuntimeCallingConvention calling_convention;
4176 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4177 } else {
4178 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004179 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004180}
4181
4182void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004183 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004184
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004185 switch (load->GetLoadKind()) {
4186 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004187 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4188 load->GetStringIndex()));
4189 return; // No dex cache slow path.
4190 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004191 // Add ADRP with its PC-relative String patch.
4192 const DexFile& dex_file = load->GetDexFile();
4193 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004194 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko5f926052016-09-30 17:04:49 +00004195 {
4196 SingleEmissionCheckScope guard(GetVIXLAssembler());
4197 __ Bind(adrp_label);
4198 __ adrp(out.X(), /* offset placeholder */ 0);
4199 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004200 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004201 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004202 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Marko5f926052016-09-30 17:04:49 +00004203 {
4204 SingleEmissionCheckScope guard(GetVIXLAssembler());
4205 __ Bind(add_label);
4206 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4207 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004208 return; // No dex cache slow path.
4209 }
4210 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004211 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4212 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4213 return; // No dex cache slow path.
4214 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004215 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004216 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004217 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004218
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004219 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004220 InvokeRuntimeCallingConvention calling_convention;
4221 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4222 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4223 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004224}
4225
Alexandre Rames5319def2014-10-23 10:03:10 +01004226void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4228 locations->SetOut(Location::ConstantLocation(constant));
4229}
4230
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004231void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004232 // Will be generated at use site.
4233}
4234
Alexandre Rames67555f72014-11-18 10:55:16 +00004235void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4236 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004238 InvokeRuntimeCallingConvention calling_convention;
4239 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4240}
4241
4242void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004243 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4244 instruction,
4245 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004246 if (instruction->IsEnter()) {
4247 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4248 } else {
4249 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4250 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004251}
4252
Alexandre Rames42d641b2014-10-27 14:00:51 +00004253void LocationsBuilderARM64::VisitMul(HMul* mul) {
4254 LocationSummary* locations =
4255 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4256 switch (mul->GetResultType()) {
4257 case Primitive::kPrimInt:
4258 case Primitive::kPrimLong:
4259 locations->SetInAt(0, Location::RequiresRegister());
4260 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004262 break;
4263
4264 case Primitive::kPrimFloat:
4265 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004266 locations->SetInAt(0, Location::RequiresFpuRegister());
4267 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004268 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004269 break;
4270
4271 default:
4272 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4273 }
4274}
4275
4276void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4277 switch (mul->GetResultType()) {
4278 case Primitive::kPrimInt:
4279 case Primitive::kPrimLong:
4280 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4281 break;
4282
4283 case Primitive::kPrimFloat:
4284 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004285 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004286 break;
4287
4288 default:
4289 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4290 }
4291}
4292
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004293void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4294 LocationSummary* locations =
4295 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4296 switch (neg->GetResultType()) {
4297 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004298 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004299 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004300 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004301 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004302
4303 case Primitive::kPrimFloat:
4304 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004305 locations->SetInAt(0, Location::RequiresFpuRegister());
4306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004307 break;
4308
4309 default:
4310 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4311 }
4312}
4313
4314void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4315 switch (neg->GetResultType()) {
4316 case Primitive::kPrimInt:
4317 case Primitive::kPrimLong:
4318 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4319 break;
4320
4321 case Primitive::kPrimFloat:
4322 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004323 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004324 break;
4325
4326 default:
4327 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4328 }
4329}
4330
4331void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4332 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004334 InvokeRuntimeCallingConvention calling_convention;
4335 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004336 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004337 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004338 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004339}
4340
4341void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4342 LocationSummary* locations = instruction->GetLocations();
4343 InvokeRuntimeCallingConvention calling_convention;
4344 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4345 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004346 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004347 // Note: if heap poisoning is enabled, the entry point takes cares
4348 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004349 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004350 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004351}
4352
Alexandre Rames5319def2014-10-23 10:03:10 +01004353void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4354 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004355 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004356 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004357 if (instruction->IsStringAlloc()) {
4358 locations->AddTemp(LocationFrom(kArtMethodRegister));
4359 } else {
4360 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4361 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4362 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004363 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4364}
4365
4366void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004367 // Note: if heap poisoning is enabled, the entry point takes cares
4368 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004369 if (instruction->IsStringAlloc()) {
4370 // String is allocated through StringFactory. Call NewEmptyString entry point.
4371 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004372 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004373 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4374 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4375 __ Blr(lr);
4376 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4377 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004378 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004379 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4380 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004381}
4382
4383void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004385 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004387}
4388
4389void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004390 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004391 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004392 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004393 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004394 break;
4395
4396 default:
4397 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4398 }
4399}
4400
David Brazdil66d126e2015-04-03 16:02:44 +01004401void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4403 locations->SetInAt(0, Location::RequiresRegister());
4404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4405}
4406
4407void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004408 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004409}
4410
Alexandre Rames5319def2014-10-23 10:03:10 +01004411void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004412 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4413 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004414}
4415
Calin Juravle2ae48182016-03-16 14:05:09 +00004416void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4417 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004418 return;
4419 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004420
Alexandre Ramesd921d642015-04-16 15:07:16 +01004421 BlockPoolsScope block_pools(GetVIXLAssembler());
4422 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004423 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004424 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004425}
4426
Calin Juravle2ae48182016-03-16 14:05:09 +00004427void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004428 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004429 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004430
4431 LocationSummary* locations = instruction->GetLocations();
4432 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004433
4434 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004435}
4436
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004437void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004438 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004439}
4440
Alexandre Rames67555f72014-11-18 10:55:16 +00004441void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4442 HandleBinaryOp(instruction);
4443}
4444
4445void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4446 HandleBinaryOp(instruction);
4447}
4448
Alexandre Rames3e69f162014-12-10 10:36:50 +00004449void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4450 LOG(FATAL) << "Unreachable";
4451}
4452
4453void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4454 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4455}
4456
Alexandre Rames5319def2014-10-23 10:03:10 +01004457void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4459 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4460 if (location.IsStackSlot()) {
4461 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4462 } else if (location.IsDoubleStackSlot()) {
4463 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4464 }
4465 locations->SetOut(location);
4466}
4467
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004468void InstructionCodeGeneratorARM64::VisitParameterValue(
4469 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004470 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004471}
4472
4473void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4474 LocationSummary* locations =
4475 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004476 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004477}
4478
4479void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4480 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4481 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004482}
4483
4484void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4485 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004486 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004487 locations->SetInAt(i, Location::Any());
4488 }
4489 locations->SetOut(Location::Any());
4490}
4491
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004492void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004493 LOG(FATAL) << "Unreachable";
4494}
4495
Serban Constantinescu02164b32014-11-13 14:05:07 +00004496void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004497 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004498 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004499 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4500 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4502
4503 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004504 case Primitive::kPrimInt:
4505 case Primitive::kPrimLong:
4506 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004507 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4509 break;
4510
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004511 case Primitive::kPrimFloat:
4512 case Primitive::kPrimDouble: {
4513 InvokeRuntimeCallingConvention calling_convention;
4514 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4515 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4516 locations->SetOut(calling_convention.GetReturnLocation(type));
4517
4518 break;
4519 }
4520
Serban Constantinescu02164b32014-11-13 14:05:07 +00004521 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004522 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004523 }
4524}
4525
4526void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4527 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004528
Serban Constantinescu02164b32014-11-13 14:05:07 +00004529 switch (type) {
4530 case Primitive::kPrimInt:
4531 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004532 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004533 break;
4534 }
4535
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004536 case Primitive::kPrimFloat:
4537 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004538 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4539 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004540 if (type == Primitive::kPrimFloat) {
4541 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4542 } else {
4543 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4544 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004545 break;
4546 }
4547
Serban Constantinescu02164b32014-11-13 14:05:07 +00004548 default:
4549 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004550 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004551 }
4552}
4553
Calin Juravle27df7582015-04-17 19:12:31 +01004554void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4555 memory_barrier->SetLocations(nullptr);
4556}
4557
4558void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004559 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004560}
4561
Alexandre Rames5319def2014-10-23 10:03:10 +01004562void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4564 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004565 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004566}
4567
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004568void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004569 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004570}
4571
4572void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4573 instruction->SetLocations(nullptr);
4574}
4575
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004576void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004577 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004578}
4579
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004580void LocationsBuilderARM64::VisitRor(HRor* ror) {
4581 HandleBinaryOp(ror);
4582}
4583
4584void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4585 HandleBinaryOp(ror);
4586}
4587
Serban Constantinescu02164b32014-11-13 14:05:07 +00004588void LocationsBuilderARM64::VisitShl(HShl* shl) {
4589 HandleShift(shl);
4590}
4591
4592void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4593 HandleShift(shl);
4594}
4595
4596void LocationsBuilderARM64::VisitShr(HShr* shr) {
4597 HandleShift(shr);
4598}
4599
4600void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4601 HandleShift(shr);
4602}
4603
Alexandre Rames5319def2014-10-23 10:03:10 +01004604void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004605 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004606}
4607
4608void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004609 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004610}
4611
Alexandre Rames67555f72014-11-18 10:55:16 +00004612void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004613 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004614}
4615
4616void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004617 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004618}
4619
4620void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004621 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004622}
4623
Alexandre Rames67555f72014-11-18 10:55:16 +00004624void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004625 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004626}
4627
Calin Juravlee460d1d2015-09-29 04:52:17 +01004628void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4629 HUnresolvedInstanceFieldGet* instruction) {
4630 FieldAccessCallingConventionARM64 calling_convention;
4631 codegen_->CreateUnresolvedFieldLocationSummary(
4632 instruction, instruction->GetFieldType(), calling_convention);
4633}
4634
4635void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4636 HUnresolvedInstanceFieldGet* instruction) {
4637 FieldAccessCallingConventionARM64 calling_convention;
4638 codegen_->GenerateUnresolvedFieldAccess(instruction,
4639 instruction->GetFieldType(),
4640 instruction->GetFieldIndex(),
4641 instruction->GetDexPc(),
4642 calling_convention);
4643}
4644
4645void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4646 HUnresolvedInstanceFieldSet* instruction) {
4647 FieldAccessCallingConventionARM64 calling_convention;
4648 codegen_->CreateUnresolvedFieldLocationSummary(
4649 instruction, instruction->GetFieldType(), calling_convention);
4650}
4651
4652void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4653 HUnresolvedInstanceFieldSet* instruction) {
4654 FieldAccessCallingConventionARM64 calling_convention;
4655 codegen_->GenerateUnresolvedFieldAccess(instruction,
4656 instruction->GetFieldType(),
4657 instruction->GetFieldIndex(),
4658 instruction->GetDexPc(),
4659 calling_convention);
4660}
4661
4662void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4663 HUnresolvedStaticFieldGet* instruction) {
4664 FieldAccessCallingConventionARM64 calling_convention;
4665 codegen_->CreateUnresolvedFieldLocationSummary(
4666 instruction, instruction->GetFieldType(), calling_convention);
4667}
4668
4669void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4670 HUnresolvedStaticFieldGet* instruction) {
4671 FieldAccessCallingConventionARM64 calling_convention;
4672 codegen_->GenerateUnresolvedFieldAccess(instruction,
4673 instruction->GetFieldType(),
4674 instruction->GetFieldIndex(),
4675 instruction->GetDexPc(),
4676 calling_convention);
4677}
4678
4679void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4680 HUnresolvedStaticFieldSet* instruction) {
4681 FieldAccessCallingConventionARM64 calling_convention;
4682 codegen_->CreateUnresolvedFieldLocationSummary(
4683 instruction, instruction->GetFieldType(), calling_convention);
4684}
4685
4686void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4687 HUnresolvedStaticFieldSet* instruction) {
4688 FieldAccessCallingConventionARM64 calling_convention;
4689 codegen_->GenerateUnresolvedFieldAccess(instruction,
4690 instruction->GetFieldType(),
4691 instruction->GetFieldIndex(),
4692 instruction->GetDexPc(),
4693 calling_convention);
4694}
4695
Alexandre Rames5319def2014-10-23 10:03:10 +01004696void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004697 LocationSummary* locations =
4698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004699 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004700}
4701
4702void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004703 HBasicBlock* block = instruction->GetBlock();
4704 if (block->GetLoopInformation() != nullptr) {
4705 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4706 // The back edge will generate the suspend check.
4707 return;
4708 }
4709 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4710 // The goto will generate the suspend check.
4711 return;
4712 }
4713 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004714}
4715
Alexandre Rames67555f72014-11-18 10:55:16 +00004716void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4717 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004718 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004719 InvokeRuntimeCallingConvention calling_convention;
4720 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4721}
4722
4723void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004724 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004725 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004726}
4727
4728void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4729 LocationSummary* locations =
4730 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4731 Primitive::Type input_type = conversion->GetInputType();
4732 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004733 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004734 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4735 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4736 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4737 }
4738
Alexandre Rames542361f2015-01-29 16:57:31 +00004739 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004740 locations->SetInAt(0, Location::RequiresFpuRegister());
4741 } else {
4742 locations->SetInAt(0, Location::RequiresRegister());
4743 }
4744
Alexandre Rames542361f2015-01-29 16:57:31 +00004745 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004746 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4747 } else {
4748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4749 }
4750}
4751
4752void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4753 Primitive::Type result_type = conversion->GetResultType();
4754 Primitive::Type input_type = conversion->GetInputType();
4755
4756 DCHECK_NE(input_type, result_type);
4757
Alexandre Rames542361f2015-01-29 16:57:31 +00004758 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004759 int result_size = Primitive::ComponentSize(result_type);
4760 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004761 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004762 Register output = OutputRegister(conversion);
4763 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004764 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004765 // 'int' values are used directly as W registers, discarding the top
4766 // bits, so we don't need to sign-extend and can just perform a move.
4767 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4768 // top 32 bits of the target register. We theoretically could leave those
4769 // bits unchanged, but we would have to make sure that no code uses a
4770 // 32bit input value as a 64bit value assuming that the top 32 bits are
4771 // zero.
4772 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004773 } else if (result_type == Primitive::kPrimChar ||
4774 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4775 __ Ubfx(output,
4776 output.IsX() ? source.X() : source.W(),
4777 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004778 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004779 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004780 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004781 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004782 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004783 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004784 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4785 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004786 } else if (Primitive::IsFloatingPointType(result_type) &&
4787 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004788 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4789 } else {
4790 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4791 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004792 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004793}
Alexandre Rames67555f72014-11-18 10:55:16 +00004794
Serban Constantinescu02164b32014-11-13 14:05:07 +00004795void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4796 HandleShift(ushr);
4797}
4798
4799void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4800 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004801}
4802
4803void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4804 HandleBinaryOp(instruction);
4805}
4806
4807void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4808 HandleBinaryOp(instruction);
4809}
4810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004811void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004812 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004813 LOG(FATAL) << "Unreachable";
4814}
4815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004816void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004817 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004818 LOG(FATAL) << "Unreachable";
4819}
4820
Mark Mendellfe57faa2015-09-18 09:26:15 -04004821// Simple implementation of packed switch - generate cascaded compare/jumps.
4822void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4823 LocationSummary* locations =
4824 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4825 locations->SetInAt(0, Location::RequiresRegister());
4826}
4827
4828void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4829 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004830 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004831 Register value_reg = InputRegisterAt(switch_instr, 0);
4832 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4833
Zheng Xu3927c8b2015-11-18 17:46:25 +08004834 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004835 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004836 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4837 // make sure we don't emit it if the target may run out of range.
4838 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4839 // ranges and emit the tables only as required.
4840 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004841
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004842 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004843 // Current instruction id is an upper bound of the number of HIRs in the graph.
4844 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4845 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004846 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4847 Register temp = temps.AcquireW();
4848 __ Subs(temp, value_reg, Operand(lower_bound));
4849
Zheng Xu3927c8b2015-11-18 17:46:25 +08004850 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004851 // Jump to successors[0] if value == lower_bound.
4852 __ B(eq, codegen_->GetLabelOf(successors[0]));
4853 int32_t last_index = 0;
4854 for (; num_entries - last_index > 2; last_index += 2) {
4855 __ Subs(temp, temp, Operand(2));
4856 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4857 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4858 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4859 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4860 }
4861 if (num_entries - last_index == 2) {
4862 // The last missing case_value.
4863 __ Cmp(temp, Operand(1));
4864 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004865 }
4866
4867 // And the default for any other value.
4868 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4869 __ B(codegen_->GetLabelOf(default_block));
4870 }
4871 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004872 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004873
4874 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4875
4876 // Below instructions should use at most one blocked register. Since there are two blocked
4877 // registers, we are free to block one.
4878 Register temp_w = temps.AcquireW();
4879 Register index;
4880 // Remove the bias.
4881 if (lower_bound != 0) {
4882 index = temp_w;
4883 __ Sub(index, value_reg, Operand(lower_bound));
4884 } else {
4885 index = value_reg;
4886 }
4887
4888 // Jump to default block if index is out of the range.
4889 __ Cmp(index, Operand(num_entries));
4890 __ B(hs, codegen_->GetLabelOf(default_block));
4891
4892 // In current VIXL implementation, it won't require any blocked registers to encode the
4893 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4894 // register pressure.
4895 Register table_base = temps.AcquireX();
4896 // Load jump offset from the table.
4897 __ Adr(table_base, jump_table->GetTableStartLabel());
4898 Register jump_offset = temp_w;
4899 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4900
4901 // Jump to target block by branching to table_base(pc related) + offset.
4902 Register target_address = table_base;
4903 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4904 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004905 }
4906}
4907
Roland Levillain44015862016-01-22 11:47:17 +00004908void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4909 Location out,
4910 uint32_t offset,
4911 Location maybe_temp) {
4912 Primitive::Type type = Primitive::kPrimNot;
4913 Register out_reg = RegisterFrom(out, type);
4914 if (kEmitCompilerReadBarrier) {
4915 Register temp_reg = RegisterFrom(maybe_temp, type);
4916 if (kUseBakerReadBarrier) {
4917 // Load with fast path based Baker's read barrier.
4918 // /* HeapReference<Object> */ out = *(out + offset)
4919 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4920 out,
4921 out_reg,
4922 offset,
4923 temp_reg,
4924 /* needs_null_check */ false,
4925 /* use_load_acquire */ false);
4926 } else {
4927 // Load with slow path based read barrier.
4928 // Save the value of `out` into `maybe_temp` before overwriting it
4929 // in the following move operation, as we will need it for the
4930 // read barrier below.
4931 __ Mov(temp_reg, out_reg);
4932 // /* HeapReference<Object> */ out = *(out + offset)
4933 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4934 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4935 }
4936 } else {
4937 // Plain load with no read barrier.
4938 // /* HeapReference<Object> */ out = *(out + offset)
4939 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4940 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4941 }
4942}
4943
4944void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4945 Location out,
4946 Location obj,
4947 uint32_t offset,
4948 Location maybe_temp) {
4949 Primitive::Type type = Primitive::kPrimNot;
4950 Register out_reg = RegisterFrom(out, type);
4951 Register obj_reg = RegisterFrom(obj, type);
4952 if (kEmitCompilerReadBarrier) {
4953 if (kUseBakerReadBarrier) {
4954 // Load with fast path based Baker's read barrier.
4955 Register temp_reg = RegisterFrom(maybe_temp, type);
4956 // /* HeapReference<Object> */ out = *(obj + offset)
4957 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4958 out,
4959 obj_reg,
4960 offset,
4961 temp_reg,
4962 /* needs_null_check */ false,
4963 /* use_load_acquire */ false);
4964 } else {
4965 // Load with slow path based read barrier.
4966 // /* HeapReference<Object> */ out = *(obj + offset)
4967 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4968 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4969 }
4970 } else {
4971 // Plain load with no read barrier.
4972 // /* HeapReference<Object> */ out = *(obj + offset)
4973 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4974 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4975 }
4976}
4977
4978void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4979 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004980 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004981 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004982 vixl::aarch64::Label* fixup_label,
4983 bool requires_read_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004984 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004985 if (requires_read_barrier) {
4986 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00004987 if (kUseBakerReadBarrier) {
4988 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4989 // Baker's read barrier are used:
4990 //
4991 // root = obj.field;
4992 // if (Thread::Current()->GetIsGcMarking()) {
4993 // root = ReadBarrier::Mark(root)
4994 // }
4995
4996 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004997 if (fixup_label == nullptr) {
4998 __ Ldr(root_reg, MemOperand(obj, offset));
4999 } else {
Vladimir Marko5f926052016-09-30 17:04:49 +00005000 SingleEmissionCheckScope guard(GetVIXLAssembler());
5001 __ Bind(fixup_label);
5002 __ ldr(root_reg, MemOperand(obj, offset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005003 }
Roland Levillain44015862016-01-22 11:47:17 +00005004 static_assert(
5005 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5006 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5007 "have different sizes.");
5008 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5009 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5010 "have different sizes.");
5011
Vladimir Marko953437b2016-08-24 08:30:46 +00005012 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005013 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005014 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005015 codegen_->AddSlowPath(slow_path);
5016
5017 MacroAssembler* masm = GetVIXLAssembler();
5018 UseScratchRegisterScope temps(masm);
5019 Register temp = temps.AcquireW();
5020 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005021 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005022 __ Cbnz(temp, slow_path->GetEntryLabel());
5023 __ Bind(slow_path->GetExitLabel());
5024 } else {
5025 // GC root loaded through a slow path for read barriers other
5026 // than Baker's.
5027 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005028 if (fixup_label == nullptr) {
5029 __ Add(root_reg.X(), obj.X(), offset);
5030 } else {
Vladimir Marko5f926052016-09-30 17:04:49 +00005031 SingleEmissionCheckScope guard(GetVIXLAssembler());
5032 __ Bind(fixup_label);
5033 __ add(root_reg.X(), obj.X(), offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005034 }
Roland Levillain44015862016-01-22 11:47:17 +00005035 // /* mirror::Object* */ root = root->Read()
5036 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5037 }
5038 } else {
5039 // Plain GC root load with no read barrier.
5040 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005041 if (fixup_label == nullptr) {
5042 __ Ldr(root_reg, MemOperand(obj, offset));
5043 } else {
Vladimir Marko5f926052016-09-30 17:04:49 +00005044 SingleEmissionCheckScope guard(GetVIXLAssembler());
5045 __ Bind(fixup_label);
5046 __ ldr(root_reg, MemOperand(obj, offset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005047 }
Roland Levillain44015862016-01-22 11:47:17 +00005048 // Note that GC roots are not affected by heap poisoning, thus we
5049 // do not have to unpoison `root_reg` here.
5050 }
5051}
5052
5053void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5054 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005055 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005056 uint32_t offset,
5057 Register temp,
5058 bool needs_null_check,
5059 bool use_load_acquire) {
5060 DCHECK(kEmitCompilerReadBarrier);
5061 DCHECK(kUseBakerReadBarrier);
5062
5063 // /* HeapReference<Object> */ ref = *(obj + offset)
5064 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005065 size_t no_scale_factor = 0U;
5066 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5067 ref,
5068 obj,
5069 offset,
5070 no_index,
5071 no_scale_factor,
5072 temp,
5073 needs_null_check,
5074 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005075}
5076
5077void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5078 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005079 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005080 uint32_t data_offset,
5081 Location index,
5082 Register temp,
5083 bool needs_null_check) {
5084 DCHECK(kEmitCompilerReadBarrier);
5085 DCHECK(kUseBakerReadBarrier);
5086
5087 // Array cells are never volatile variables, therefore array loads
5088 // never use Load-Acquire instructions on ARM64.
5089 const bool use_load_acquire = false;
5090
Roland Levillainbfea3352016-06-23 13:48:47 +01005091 static_assert(
5092 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5093 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005094 // /* HeapReference<Object> */ ref =
5095 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005096 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5097 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5098 ref,
5099 obj,
5100 data_offset,
5101 index,
5102 scale_factor,
5103 temp,
5104 needs_null_check,
5105 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005106}
5107
5108void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5109 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005110 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005111 uint32_t offset,
5112 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005113 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005114 Register temp,
5115 bool needs_null_check,
5116 bool use_load_acquire) {
5117 DCHECK(kEmitCompilerReadBarrier);
5118 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005119 // If we are emitting an array load, we should not be using a
5120 // Load Acquire instruction. In other words:
5121 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5122 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005123
5124 MacroAssembler* masm = GetVIXLAssembler();
5125 UseScratchRegisterScope temps(masm);
5126
5127 // In slow path based read barriers, the read barrier call is
5128 // inserted after the original load. However, in fast path based
5129 // Baker's read barriers, we need to perform the load of
5130 // mirror::Object::monitor_ *before* the original reference load.
5131 // This load-load ordering is required by the read barrier.
5132 // The fast path/slow path (for Baker's algorithm) should look like:
5133 //
5134 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5135 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5136 // HeapReference<Object> ref = *src; // Original reference load.
5137 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5138 // if (is_gray) {
5139 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5140 // }
5141 //
5142 // Note: the original implementation in ReadBarrier::Barrier is
5143 // slightly more complex as it performs additional checks that we do
5144 // not do here for performance reasons.
5145
5146 Primitive::Type type = Primitive::kPrimNot;
5147 Register ref_reg = RegisterFrom(ref, type);
5148 DCHECK(obj.IsW());
5149 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5150
5151 // /* int32_t */ monitor = obj->monitor_
5152 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5153 if (needs_null_check) {
5154 MaybeRecordImplicitNullCheck(instruction);
5155 }
5156 // /* LockWord */ lock_word = LockWord(monitor)
5157 static_assert(sizeof(LockWord) == sizeof(int32_t),
5158 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005159
Vladimir Marko877a0332016-07-11 19:30:56 +01005160 // Introduce a dependency on the lock_word including rb_state,
5161 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005162 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005163 // `obj` is unchanged by this operation, but its value now depends
5164 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005165 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005166
5167 // The actual reference load.
5168 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005169 // Load types involving an "index".
5170 if (use_load_acquire) {
5171 // UnsafeGetObjectVolatile intrinsic case.
5172 // Register `index` is not an index in an object array, but an
5173 // offset to an object reference field within object `obj`.
5174 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5175 DCHECK(instruction->GetLocations()->Intrinsified());
5176 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5177 << instruction->AsInvoke()->GetIntrinsic();
5178 DCHECK_EQ(offset, 0U);
5179 DCHECK_EQ(scale_factor, 0U);
5180 DCHECK_EQ(needs_null_check, 0U);
5181 // /* HeapReference<Object> */ ref = *(obj + index)
5182 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5183 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005184 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005185 // ArrayGet and UnsafeGetObject intrinsics cases.
5186 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5187 if (index.IsConstant()) {
5188 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5189 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5190 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005191 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005192 __ Add(temp2, obj, offset);
5193 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5194 temps.Release(temp2);
5195 }
Roland Levillain44015862016-01-22 11:47:17 +00005196 }
Roland Levillain44015862016-01-22 11:47:17 +00005197 } else {
5198 // /* HeapReference<Object> */ ref = *(obj + offset)
5199 MemOperand field = HeapOperand(obj, offset);
5200 if (use_load_acquire) {
5201 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5202 } else {
5203 Load(type, ref_reg, field);
5204 }
5205 }
5206
5207 // Object* ref = ref_addr->AsMirrorPtr()
5208 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5209
Vladimir Marko953437b2016-08-24 08:30:46 +00005210 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005211 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005212 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005213 AddSlowPath(slow_path);
5214
5215 // if (rb_state == ReadBarrier::gray_ptr_)
5216 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005217 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5218 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5219 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5220 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5221 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005222 __ Bind(slow_path->GetExitLabel());
5223}
5224
5225void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5226 Location out,
5227 Location ref,
5228 Location obj,
5229 uint32_t offset,
5230 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005231 DCHECK(kEmitCompilerReadBarrier);
5232
Roland Levillain44015862016-01-22 11:47:17 +00005233 // Insert a slow path based read barrier *after* the reference load.
5234 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005235 // If heap poisoning is enabled, the unpoisoning of the loaded
5236 // reference will be carried out by the runtime within the slow
5237 // path.
5238 //
5239 // Note that `ref` currently does not get unpoisoned (when heap
5240 // poisoning is enabled), which is alright as the `ref` argument is
5241 // not used by the artReadBarrierSlow entry point.
5242 //
5243 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5244 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5245 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5246 AddSlowPath(slow_path);
5247
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005248 __ B(slow_path->GetEntryLabel());
5249 __ Bind(slow_path->GetExitLabel());
5250}
5251
Roland Levillain44015862016-01-22 11:47:17 +00005252void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5253 Location out,
5254 Location ref,
5255 Location obj,
5256 uint32_t offset,
5257 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005258 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005259 // Baker's read barriers shall be handled by the fast path
5260 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5261 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005262 // If heap poisoning is enabled, unpoisoning will be taken care of
5263 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005264 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005265 } else if (kPoisonHeapReferences) {
5266 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5267 }
5268}
5269
Roland Levillain44015862016-01-22 11:47:17 +00005270void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5271 Location out,
5272 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005273 DCHECK(kEmitCompilerReadBarrier);
5274
Roland Levillain44015862016-01-22 11:47:17 +00005275 // Insert a slow path based read barrier *after* the GC root load.
5276 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005277 // Note that GC roots are not affected by heap poisoning, so we do
5278 // not need to do anything special for this here.
5279 SlowPathCodeARM64* slow_path =
5280 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5281 AddSlowPath(slow_path);
5282
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005283 __ B(slow_path->GetEntryLabel());
5284 __ Bind(slow_path->GetExitLabel());
5285}
5286
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005287void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5288 LocationSummary* locations =
5289 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5290 locations->SetInAt(0, Location::RequiresRegister());
5291 locations->SetOut(Location::RequiresRegister());
5292}
5293
5294void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5295 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005296 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005297 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005298 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005299 __ Ldr(XRegisterFrom(locations->Out()),
5300 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005301 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005302 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005303 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005304 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5305 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005306 __ Ldr(XRegisterFrom(locations->Out()),
5307 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005308 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005309}
5310
5311
5312
Alexandre Rames67555f72014-11-18 10:55:16 +00005313#undef __
5314#undef QUICK_ENTRY_POINT
5315
Alexandre Rames5319def2014-10-23 10:03:10 +01005316} // namespace arm64
5317} // namespace art