blob: 1d2f33405e4e47908e0b463105c62533353570b3 [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,
142 RegisterSet* register_set,
143 int64_t spill_offset,
144 bool is_save) {
145 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
146 codegen->GetNumberOfCoreRegisters(),
147 register_set->GetFloatingPointRegisters(),
148 codegen->GetNumberOfFloatingPointRegisters()));
149
150 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100151 register_set->GetCoreRegisters() & (~callee_saved_core_registers.GetList()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000152 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100153 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.GetList()));
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) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
189 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
190 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
191 // If the register holds an object, update the stack mask.
192 if (locations->RegisterContainsObject(i)) {
193 locations->SetStackBit(stack_offset / kVRegSize);
194 }
195 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
196 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
197 saved_core_stack_offsets_[i] = stack_offset;
198 stack_offset += kXRegSizeInBytes;
199 }
200 }
201
202 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
203 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
204 register_set->ContainsFloatingPointRegister(i)) {
205 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
206 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
207 saved_fpu_stack_offsets_[i] = stack_offset;
208 stack_offset += kDRegSizeInBytes;
209 }
210 }
211
212 SaveRestoreLiveRegistersHelper(codegen, register_set,
213 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
214}
215
216void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
217 RegisterSet* register_set = locations->GetLiveRegisters();
218 SaveRestoreLiveRegistersHelper(codegen, register_set,
219 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
220}
221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
223 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000224 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100225
Alexandre Rames67555f72014-11-18 10:55:16 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000228 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000231 if (instruction_->CanThrowIntoCatchBlock()) {
232 // Live registers will be restored in the catch block if caught.
233 SaveLiveRegisters(codegen, instruction_->GetLocations());
234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000235 // We're moving two locations to locations that could overlap, so we need a parallel
236 // move resolver.
237 InvokeRuntimeCallingConvention calling_convention;
238 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100239 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
240 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000241 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
242 ? kQuickThrowStringBounds
243 : kQuickThrowArrayBounds;
244 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100245 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100247 }
248
Alexandre Rames8158f282015-08-07 10:26:17 +0100249 bool IsFatal() const OVERRIDE { return true; }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
255};
256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
258 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000259 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000260
261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000264 if (instruction_->CanThrowIntoCatchBlock()) {
265 // Live registers will be restored in the catch block if caught.
266 SaveLiveRegisters(codegen, instruction_->GetLocations());
267 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000268 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 }
271
Alexandre Rames8158f282015-08-07 10:26:17 +0100272 bool IsFatal() const OVERRIDE { return true; }
273
Alexandre Rames9931f312015-06-19 14:47:01 +0100274 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
278};
279
280class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
281 public:
282 LoadClassSlowPathARM64(HLoadClass* cls,
283 HInstruction* at,
284 uint32_t dex_pc,
285 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
288 }
289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 LocationSummary* locations = at_->GetLocations();
292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
293
294 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000295 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000296
297 InvokeRuntimeCallingConvention calling_convention;
298 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000299 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
300 : kQuickInitializeType;
301 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800302 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100303 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800304 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100305 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800306 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000307
308 // Move the class to the desired location.
309 Location out = locations->Out();
310 if (out.IsValid()) {
311 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
312 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000313 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 }
315
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 __ B(GetExitLabel());
318 }
319
Alexandre Rames9931f312015-06-19 14:47:01 +0100320 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 private:
323 // The class this slow path will load.
324 HLoadClass* const cls_;
325
326 // The instruction where this slow path is happening.
327 // (Might be the load class or an initialization check).
328 HInstruction* const at_;
329
330 // The dex PC of `at_`.
331 const uint32_t dex_pc_;
332
333 // Whether to initialize the class.
334 const bool do_clinit_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
337};
338
Alexandre Rames5319def2014-10-23 10:03:10 +0100339class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000341 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100342
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100345 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000346 if (instruction_->CanThrowIntoCatchBlock()) {
347 // Live registers will be restored in the catch block if caught.
348 SaveLiveRegisters(codegen, instruction_->GetLocations());
349 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000350 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
351 instruction_,
352 instruction_->GetDexPc(),
353 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800354 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 }
356
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 bool IsFatal() const OVERRIDE { return true; }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
360
Alexandre Rames5319def2014-10-23 10:03:10 +0100361 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100362 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
363};
364
365class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
366 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100367 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000368 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100369
Alexandre Rames67555f72014-11-18 10:55:16 +0000370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
371 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100372 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000373 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800374 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 if (successor_ == nullptr) {
376 __ B(GetReturnLabel());
377 } else {
378 __ B(arm64_codegen->GetLabelOf(successor_));
379 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 }
381
Scott Wakeling97c72b72016-06-24 16:19:36 +0100382 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 DCHECK(successor_ == nullptr);
384 return &return_label_;
385 }
386
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100387 HBasicBlock* GetSuccessor() const {
388 return successor_;
389 }
390
Alexandre Rames9931f312015-06-19 14:47:01 +0100391 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
392
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100394 // If not null, the block to branch to after the suspend check.
395 HBasicBlock* const successor_;
396
397 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100398 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100399
400 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
401};
402
Alexandre Rames67555f72014-11-18 10:55:16 +0000403class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
404 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000405 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000406 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000407
408 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000409 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100410 Location class_to_check = locations->InAt(1);
411 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
412 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000413 DCHECK(instruction_->IsCheckCast()
414 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
415 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100416 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000417
Alexandre Rames67555f72014-11-18 10:55:16 +0000418 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000419
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000420 if (!is_fatal_) {
421 SaveLiveRegisters(codegen, locations);
422 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
424 // We're moving two locations to locations that could overlap, so we need a parallel
425 // move resolver.
426 InvokeRuntimeCallingConvention calling_convention;
427 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100428 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
429 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430
431 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000432 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700433 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000434 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000435 Primitive::Type ret_type = instruction_->GetType();
436 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
437 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
438 } else {
439 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000440 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800441 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442 }
443
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000444 if (!is_fatal_) {
445 RestoreLiveRegisters(codegen, locations);
446 __ B(GetExitLabel());
447 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000448 }
449
Alexandre Rames9931f312015-06-19 14:47:01 +0100450 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000451 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100452
Alexandre Rames67555f72014-11-18 10:55:16 +0000453 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000454 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455
Alexandre Rames67555f72014-11-18 10:55:16 +0000456 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
457};
458
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700459class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
460 public:
Aart Bik42249c32016-01-07 15:33:50 -0800461 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000462 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700463
464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700466 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000467 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000468 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700469 }
470
Alexandre Rames9931f312015-06-19 14:47:01 +0100471 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
472
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700473 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700474 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
475};
476
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100477class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
478 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000479 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 LocationSummary* locations = instruction_->GetLocations();
483 __ Bind(GetEntryLabel());
484 SaveLiveRegisters(codegen, locations);
485
486 InvokeRuntimeCallingConvention calling_convention;
487 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
488 parallel_move.AddMove(
489 locations->InAt(0),
490 LocationFrom(calling_convention.GetRegisterAt(0)),
491 Primitive::kPrimNot,
492 nullptr);
493 parallel_move.AddMove(
494 locations->InAt(1),
495 LocationFrom(calling_convention.GetRegisterAt(1)),
496 Primitive::kPrimInt,
497 nullptr);
498 parallel_move.AddMove(
499 locations->InAt(2),
500 LocationFrom(calling_convention.GetRegisterAt(2)),
501 Primitive::kPrimNot,
502 nullptr);
503 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
504
505 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000506 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100507 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
508 RestoreLiveRegisters(codegen, locations);
509 __ B(GetExitLabel());
510 }
511
512 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
513
514 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100515 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
516};
517
Zheng Xu3927c8b2015-11-18 17:46:25 +0800518void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
519 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000520 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800521
522 // We are about to use the assembler to place literals directly. Make sure we have enough
523 // underlying code buffer and we have generated the jump table with right size.
524 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
525 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
526
527 __ Bind(&table_start_);
528 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
529 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100530 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800531 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100532 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800533 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
534 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
535 Literal<int32_t> literal(jump_offset);
536 __ place(&literal);
537 }
538}
539
Roland Levillain44015862016-01-22 11:47:17 +0000540// Slow path marking an object during a read barrier.
541class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
542 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100543 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
544 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000545 DCHECK(kEmitCompilerReadBarrier);
546 }
547
548 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
551 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000552 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100553 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000554 DCHECK(instruction_->IsInstanceFieldGet() ||
555 instruction_->IsStaticFieldGet() ||
556 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100557 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000558 instruction_->IsLoadClass() ||
559 instruction_->IsLoadString() ||
560 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100561 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100562 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
563 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000564 << "Unexpected instruction in read barrier marking slow path: "
565 << instruction_->DebugName();
566
567 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100568 // No need to save live registers; it's taken care of by the
569 // entrypoint. Also, there is no need to update the stack mask,
570 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000571 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100572 DCHECK_NE(obj_.reg(), LR);
573 DCHECK_NE(obj_.reg(), WSP);
574 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100575 // IP0 is used internally by the ReadBarrierMarkRegX entry point
576 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700577 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100578 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
579 // "Compact" slow path, saving two moves.
580 //
581 // Instead of using the standard runtime calling convention (input
582 // and output in W0):
583 //
584 // W0 <- obj
585 // W0 <- ReadBarrierMark(W0)
586 // obj <- W0
587 //
588 // we just use rX (the register holding `obj`) as input and output
589 // of a dedicated entrypoint:
590 //
591 // rX <- ReadBarrierMarkRegX(rX)
592 //
593 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700594 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100595 // This runtime call does not require a stack map.
596 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000597 __ B(GetExitLabel());
598 }
599
600 private:
Roland Levillain44015862016-01-22 11:47:17 +0000601 const Location obj_;
602
603 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
604};
605
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000606// Slow path generating a read barrier for a heap reference.
607class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
608 public:
609 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
610 Location out,
611 Location ref,
612 Location obj,
613 uint32_t offset,
614 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000615 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000616 out_(out),
617 ref_(ref),
618 obj_(obj),
619 offset_(offset),
620 index_(index) {
621 DCHECK(kEmitCompilerReadBarrier);
622 // If `obj` is equal to `out` or `ref`, it means the initial object
623 // has been overwritten by (or after) the heap object reference load
624 // to be instrumented, e.g.:
625 //
626 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000627 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000628 //
629 // In that case, we have lost the information about the original
630 // object, and the emitted read barrier cannot work properly.
631 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
632 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
633 }
634
635 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
636 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
637 LocationSummary* locations = instruction_->GetLocations();
638 Primitive::Type type = Primitive::kPrimNot;
639 DCHECK(locations->CanCall());
640 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100641 DCHECK(instruction_->IsInstanceFieldGet() ||
642 instruction_->IsStaticFieldGet() ||
643 instruction_->IsArrayGet() ||
644 instruction_->IsInstanceOf() ||
645 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100646 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000647 << "Unexpected instruction in read barrier for heap reference slow path: "
648 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000649 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000650 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100651 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000652
653 __ Bind(GetEntryLabel());
654
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000655 SaveLiveRegisters(codegen, locations);
656
657 // We may have to change the index's value, but as `index_` is a
658 // constant member (like other "inputs" of this slow path),
659 // introduce a copy of it, `index`.
660 Location index = index_;
661 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100662 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000663 if (instruction_->IsArrayGet()) {
664 // Compute the actual memory offset and store it in `index`.
665 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
666 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
667 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
668 // We are about to change the value of `index_reg` (see the
669 // calls to vixl::MacroAssembler::Lsl and
670 // vixl::MacroAssembler::Mov below), but it has
671 // not been saved by the previous call to
672 // art::SlowPathCode::SaveLiveRegisters, as it is a
673 // callee-save register --
674 // art::SlowPathCode::SaveLiveRegisters does not consider
675 // callee-save registers, as it has been designed with the
676 // assumption that callee-save registers are supposed to be
677 // handled by the called function. So, as a callee-save
678 // register, `index_reg` _would_ eventually be saved onto
679 // the stack, but it would be too late: we would have
680 // changed its value earlier. Therefore, we manually save
681 // it here into another freely available register,
682 // `free_reg`, chosen of course among the caller-save
683 // registers (as a callee-save `free_reg` register would
684 // exhibit the same problem).
685 //
686 // Note we could have requested a temporary register from
687 // the register allocator instead; but we prefer not to, as
688 // this is a slow path, and we know we can find a
689 // caller-save register that is available.
690 Register free_reg = FindAvailableCallerSaveRegister(codegen);
691 __ Mov(free_reg.W(), index_reg);
692 index_reg = free_reg;
693 index = LocationFrom(index_reg);
694 } else {
695 // The initial register stored in `index_` has already been
696 // saved in the call to art::SlowPathCode::SaveLiveRegisters
697 // (as it is not a callee-save register), so we can freely
698 // use it.
699 }
700 // Shifting the index value contained in `index_reg` by the scale
701 // factor (2) cannot overflow in practice, as the runtime is
702 // unable to allocate object arrays with a size larger than
703 // 2^26 - 1 (that is, 2^28 - 4 bytes).
704 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
705 static_assert(
706 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
707 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
708 __ Add(index_reg, index_reg, Operand(offset_));
709 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100710 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
711 // intrinsics, `index_` is not shifted by a scale factor of 2
712 // (as in the case of ArrayGet), as it is actually an offset
713 // to an object field within an object.
714 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000715 DCHECK(instruction_->GetLocations()->Intrinsified());
716 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
717 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
718 << instruction_->AsInvoke()->GetIntrinsic();
719 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100720 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000721 }
722 }
723
724 // We're moving two or three locations to locations that could
725 // overlap, so we need a parallel move resolver.
726 InvokeRuntimeCallingConvention calling_convention;
727 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
728 parallel_move.AddMove(ref_,
729 LocationFrom(calling_convention.GetRegisterAt(0)),
730 type,
731 nullptr);
732 parallel_move.AddMove(obj_,
733 LocationFrom(calling_convention.GetRegisterAt(1)),
734 type,
735 nullptr);
736 if (index.IsValid()) {
737 parallel_move.AddMove(index,
738 LocationFrom(calling_convention.GetRegisterAt(2)),
739 Primitive::kPrimInt,
740 nullptr);
741 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
742 } else {
743 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
744 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
745 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000746 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000747 instruction_,
748 instruction_->GetDexPc(),
749 this);
750 CheckEntrypointTypes<
751 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
752 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
753
754 RestoreLiveRegisters(codegen, locations);
755
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000756 __ B(GetExitLabel());
757 }
758
759 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
760
761 private:
762 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100763 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
764 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000765 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
766 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
767 return Register(VIXLRegCodeFromART(i), kXRegSize);
768 }
769 }
770 // We shall never fail to find a free caller-save register, as
771 // there are more than two core caller-save registers on ARM64
772 // (meaning it is possible to find one which is different from
773 // `ref` and `obj`).
774 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
775 LOG(FATAL) << "Could not find a free register";
776 UNREACHABLE();
777 }
778
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000779 const Location out_;
780 const Location ref_;
781 const Location obj_;
782 const uint32_t offset_;
783 // An additional location containing an index to an array.
784 // Only used for HArrayGet and the UnsafeGetObject &
785 // UnsafeGetObjectVolatile intrinsics.
786 const Location index_;
787
788 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
789};
790
791// Slow path generating a read barrier for a GC root.
792class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
793 public:
794 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000795 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000796 DCHECK(kEmitCompilerReadBarrier);
797 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000798
799 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
800 LocationSummary* locations = instruction_->GetLocations();
801 Primitive::Type type = Primitive::kPrimNot;
802 DCHECK(locations->CanCall());
803 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000804 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
805 << "Unexpected instruction in read barrier for GC root slow path: "
806 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807
808 __ Bind(GetEntryLabel());
809 SaveLiveRegisters(codegen, locations);
810
811 InvokeRuntimeCallingConvention calling_convention;
812 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
813 // The argument of the ReadBarrierForRootSlow is not a managed
814 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
815 // thus we need a 64-bit move here, and we cannot use
816 //
817 // arm64_codegen->MoveLocation(
818 // LocationFrom(calling_convention.GetRegisterAt(0)),
819 // root_,
820 // type);
821 //
822 // which would emit a 32-bit move, as `type` is a (32-bit wide)
823 // reference type (`Primitive::kPrimNot`).
824 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000825 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000826 instruction_,
827 instruction_->GetDexPc(),
828 this);
829 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
830 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
831
832 RestoreLiveRegisters(codegen, locations);
833 __ B(GetExitLabel());
834 }
835
836 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
837
838 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000839 const Location out_;
840 const Location root_;
841
842 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
843};
844
Alexandre Rames5319def2014-10-23 10:03:10 +0100845#undef __
846
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100847Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100848 Location next_location;
849 if (type == Primitive::kPrimVoid) {
850 LOG(FATAL) << "Unreachable type " << type;
851 }
852
Alexandre Rames542361f2015-01-29 16:57:31 +0000853 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100854 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
855 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000856 } else if (!Primitive::IsFloatingPointType(type) &&
857 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000858 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
859 } else {
860 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000861 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
862 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100863 }
864
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000865 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000866 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100867 return next_location;
868}
869
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100870Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100871 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100872}
873
Serban Constantinescu579885a2015-02-22 20:51:33 +0000874CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
875 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100876 const CompilerOptions& compiler_options,
877 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100878 : CodeGenerator(graph,
879 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000880 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000881 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100882 callee_saved_core_registers.GetList(),
883 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100884 compiler_options,
885 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100886 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800887 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100888 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000889 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000890 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100891 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000892 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000893 uint32_literals_(std::less<uint32_t>(),
894 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100895 uint64_literals_(std::less<uint64_t>(),
896 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
897 method_patches_(MethodReferenceComparator(),
898 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
899 call_patches_(MethodReferenceComparator(),
900 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
901 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000902 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
903 boot_image_string_patches_(StringReferenceValueComparator(),
904 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
905 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100906 boot_image_type_patches_(TypeReferenceValueComparator(),
907 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
908 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000909 boot_image_address_patches_(std::less<uint32_t>(),
910 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000911 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000912 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000913}
Alexandre Rames5319def2014-10-23 10:03:10 +0100914
Alexandre Rames67555f72014-11-18 10:55:16 +0000915#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100916
Zheng Xu3927c8b2015-11-18 17:46:25 +0800917void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100918 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800919 jump_table->EmitTable(this);
920 }
921}
922
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000923void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800924 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000925 // Ensure we emit the literal pool.
926 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000927
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000928 CodeGenerator::Finalize(allocator);
929}
930
Zheng Xuad4450e2015-04-17 18:48:56 +0800931void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
932 // Note: There are 6 kinds of moves:
933 // 1. constant -> GPR/FPR (non-cycle)
934 // 2. constant -> stack (non-cycle)
935 // 3. GPR/FPR -> GPR/FPR
936 // 4. GPR/FPR -> stack
937 // 5. stack -> GPR/FPR
938 // 6. stack -> stack (non-cycle)
939 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
940 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
941 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
942 // dependency.
943 vixl_temps_.Open(GetVIXLAssembler());
944}
945
946void ParallelMoveResolverARM64::FinishEmitNativeCode() {
947 vixl_temps_.Close();
948}
949
950Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
951 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
952 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
953 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
954 Location scratch = GetScratchLocation(kind);
955 if (!scratch.Equals(Location::NoLocation())) {
956 return scratch;
957 }
958 // Allocate from VIXL temp registers.
959 if (kind == Location::kRegister) {
960 scratch = LocationFrom(vixl_temps_.AcquireX());
961 } else {
962 DCHECK(kind == Location::kFpuRegister);
963 scratch = LocationFrom(vixl_temps_.AcquireD());
964 }
965 AddScratchLocation(scratch);
966 return scratch;
967}
968
969void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
970 if (loc.IsRegister()) {
971 vixl_temps_.Release(XRegisterFrom(loc));
972 } else {
973 DCHECK(loc.IsFpuRegister());
974 vixl_temps_.Release(DRegisterFrom(loc));
975 }
976 RemoveScratchLocation(loc);
977}
978
Alexandre Rames3e69f162014-12-10 10:36:50 +0000979void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100980 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100981 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000982}
983
Alexandre Rames5319def2014-10-23 10:03:10 +0100984void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100985 MacroAssembler* masm = GetVIXLAssembler();
986 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000987 __ Bind(&frame_entry_label_);
988
Serban Constantinescu02164b32014-11-13 14:05:07 +0000989 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
990 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100991 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000992 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000993 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000994 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000995 __ Ldr(wzr, MemOperand(temp, 0));
996 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000997 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100998
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000999 if (!HasEmptyFrame()) {
1000 int frame_size = GetFrameSize();
1001 // Stack layout:
1002 // sp[frame_size - 8] : lr.
1003 // ... : other preserved core registers.
1004 // ... : other preserved fp registers.
1005 // ... : reserved frame space.
1006 // sp[0] : current method.
1007 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001008 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001009 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1010 frame_size - GetCoreSpillSize());
1011 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1012 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001013 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001014}
1015
1016void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001017 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001018 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001019 if (!HasEmptyFrame()) {
1020 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001021 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1022 frame_size - FrameEntrySpillSize());
1023 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1024 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001025 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001026 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001027 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001028 __ Ret();
1029 GetAssembler()->cfi().RestoreState();
1030 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001031}
1032
Scott Wakeling97c72b72016-06-24 16:19:36 +01001033CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001034 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001035 return CPURegList(CPURegister::kRegister, kXRegSize,
1036 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001037}
1038
Scott Wakeling97c72b72016-06-24 16:19:36 +01001039CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001040 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1041 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001042 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1043 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001044}
1045
Alexandre Rames5319def2014-10-23 10:03:10 +01001046void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1047 __ Bind(GetLabelOf(block));
1048}
1049
Calin Juravle175dc732015-08-25 15:42:32 +01001050void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1051 DCHECK(location.IsRegister());
1052 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1053}
1054
Calin Juravlee460d1d2015-09-29 04:52:17 +01001055void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1056 if (location.IsRegister()) {
1057 locations->AddTemp(location);
1058 } else {
1059 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1060 }
1061}
1062
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001063void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001064 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001065 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001066 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001067 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001068 if (value_can_be_null) {
1069 __ Cbz(value, &done);
1070 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001071 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001072 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001073 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001074 if (value_can_be_null) {
1075 __ Bind(&done);
1076 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001077}
1078
David Brazdil58282f42016-01-14 12:45:10 +00001079void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001080 // Blocked core registers:
1081 // lr : Runtime reserved.
1082 // tr : Runtime reserved.
1083 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1084 // ip1 : VIXL core temp.
1085 // ip0 : VIXL core temp.
1086 //
1087 // Blocked fp registers:
1088 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001089 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1090 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001091 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001092 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001093 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001094
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001095 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001096 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001097 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001098 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001099
David Brazdil58282f42016-01-14 12:45:10 +00001100 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001101 // Stubs do not save callee-save floating point registers. If the graph
1102 // is debuggable, we need to deal with these registers differently. For
1103 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001104 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1105 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001106 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001107 }
1108 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001109}
1110
Alexandre Rames3e69f162014-12-10 10:36:50 +00001111size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1112 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1113 __ Str(reg, MemOperand(sp, stack_index));
1114 return kArm64WordSize;
1115}
1116
1117size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1118 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1119 __ Ldr(reg, MemOperand(sp, stack_index));
1120 return kArm64WordSize;
1121}
1122
1123size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1124 FPRegister reg = FPRegister(reg_id, kDRegSize);
1125 __ Str(reg, MemOperand(sp, stack_index));
1126 return kArm64WordSize;
1127}
1128
1129size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1130 FPRegister reg = FPRegister(reg_id, kDRegSize);
1131 __ Ldr(reg, MemOperand(sp, stack_index));
1132 return kArm64WordSize;
1133}
1134
Alexandre Rames5319def2014-10-23 10:03:10 +01001135void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001136 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001137}
1138
1139void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001140 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001141}
1142
Alexandre Rames67555f72014-11-18 10:55:16 +00001143void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001144 if (constant->IsIntConstant()) {
1145 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1146 } else if (constant->IsLongConstant()) {
1147 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1148 } else if (constant->IsNullConstant()) {
1149 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001150 } else if (constant->IsFloatConstant()) {
1151 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1152 } else {
1153 DCHECK(constant->IsDoubleConstant());
1154 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1155 }
1156}
1157
Alexandre Rames3e69f162014-12-10 10:36:50 +00001158
1159static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1160 DCHECK(constant.IsConstant());
1161 HConstant* cst = constant.GetConstant();
1162 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001163 // Null is mapped to a core W register, which we associate with kPrimInt.
1164 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001165 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1166 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1167 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1168}
1169
Calin Juravlee460d1d2015-09-29 04:52:17 +01001170void CodeGeneratorARM64::MoveLocation(Location destination,
1171 Location source,
1172 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001173 if (source.Equals(destination)) {
1174 return;
1175 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001176
1177 // A valid move can always be inferred from the destination and source
1178 // locations. When moving from and to a register, the argument type can be
1179 // used to generate 32bit instead of 64bit moves. In debug mode we also
1180 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001181 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001182
1183 if (destination.IsRegister() || destination.IsFpuRegister()) {
1184 if (unspecified_type) {
1185 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1186 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001187 (src_cst != nullptr && (src_cst->IsIntConstant()
1188 || src_cst->IsFloatConstant()
1189 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001190 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001191 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001192 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001193 // If the source is a double stack slot or a 64bit constant, a 64bit
1194 // type is appropriate. Else the source is a register, and since the
1195 // type has not been specified, we chose a 64bit type to force a 64bit
1196 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001197 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001198 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001199 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1201 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1202 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001203 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1204 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1205 __ Ldr(dst, StackOperandFrom(source));
1206 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001208 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001209 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001210 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001213 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001214 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1215 ? Primitive::kPrimLong
1216 : Primitive::kPrimInt;
1217 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1218 }
1219 } else {
1220 DCHECK(source.IsFpuRegister());
1221 if (destination.IsRegister()) {
1222 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1223 ? Primitive::kPrimDouble
1224 : Primitive::kPrimFloat;
1225 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1226 } else {
1227 DCHECK(destination.IsFpuRegister());
1228 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 }
1230 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001231 } else { // The destination is not a register. It must be a stack slot.
1232 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1233 if (source.IsRegister() || source.IsFpuRegister()) {
1234 if (unspecified_type) {
1235 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001237 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001238 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001239 }
1240 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1242 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1243 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001244 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1246 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001247 UseScratchRegisterScope temps(GetVIXLAssembler());
1248 HConstant* src_cst = source.GetConstant();
1249 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001250 if (src_cst->IsZeroBitPattern()) {
1251 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001252 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001253 if (src_cst->IsIntConstant()) {
1254 temp = temps.AcquireW();
1255 } else if (src_cst->IsLongConstant()) {
1256 temp = temps.AcquireX();
1257 } else if (src_cst->IsFloatConstant()) {
1258 temp = temps.AcquireS();
1259 } else {
1260 DCHECK(src_cst->IsDoubleConstant());
1261 temp = temps.AcquireD();
1262 }
1263 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001264 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001265 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001266 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001267 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001268 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001269 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001270 // There is generally less pressure on FP registers.
1271 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001272 __ Ldr(temp, StackOperandFrom(source));
1273 __ Str(temp, StackOperandFrom(destination));
1274 }
1275 }
1276}
1277
1278void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001279 CPURegister dst,
1280 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001281 switch (type) {
1282 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001283 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001284 break;
1285 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001286 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001287 break;
1288 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001289 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001290 break;
1291 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001292 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001293 break;
1294 case Primitive::kPrimInt:
1295 case Primitive::kPrimNot:
1296 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001297 case Primitive::kPrimFloat:
1298 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001299 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001300 __ Ldr(dst, src);
1301 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001302 case Primitive::kPrimVoid:
1303 LOG(FATAL) << "Unreachable type " << type;
1304 }
1305}
1306
Calin Juravle77520bc2015-01-12 18:45:46 +00001307void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001308 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001309 const MemOperand& src,
1310 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001311 MacroAssembler* masm = GetVIXLAssembler();
1312 BlockPoolsScope block_pools(masm);
1313 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001314 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001315 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001316
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001317 DCHECK(!src.IsPreIndex());
1318 DCHECK(!src.IsPostIndex());
1319
1320 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001321 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001322 MemOperand base = MemOperand(temp_base);
1323 switch (type) {
1324 case Primitive::kPrimBoolean:
1325 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001326 if (needs_null_check) {
1327 MaybeRecordImplicitNullCheck(instruction);
1328 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001329 break;
1330 case Primitive::kPrimByte:
1331 __ Ldarb(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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1336 break;
1337 case Primitive::kPrimChar:
1338 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001339 if (needs_null_check) {
1340 MaybeRecordImplicitNullCheck(instruction);
1341 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001342 break;
1343 case Primitive::kPrimShort:
1344 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001345 if (needs_null_check) {
1346 MaybeRecordImplicitNullCheck(instruction);
1347 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001348 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1349 break;
1350 case Primitive::kPrimInt:
1351 case Primitive::kPrimNot:
1352 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001353 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001354 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001355 if (needs_null_check) {
1356 MaybeRecordImplicitNullCheck(instruction);
1357 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 break;
1359 case Primitive::kPrimFloat:
1360 case Primitive::kPrimDouble: {
1361 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001362 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001363
1364 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1365 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001366 if (needs_null_check) {
1367 MaybeRecordImplicitNullCheck(instruction);
1368 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001369 __ Fmov(FPRegister(dst), temp);
1370 break;
1371 }
1372 case Primitive::kPrimVoid:
1373 LOG(FATAL) << "Unreachable type " << type;
1374 }
1375}
1376
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001377void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001378 CPURegister src,
1379 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001380 switch (type) {
1381 case Primitive::kPrimBoolean:
1382 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001383 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001384 break;
1385 case Primitive::kPrimChar:
1386 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001387 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001388 break;
1389 case Primitive::kPrimInt:
1390 case Primitive::kPrimNot:
1391 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001392 case Primitive::kPrimFloat:
1393 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001394 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001395 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001396 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001397 case Primitive::kPrimVoid:
1398 LOG(FATAL) << "Unreachable type " << type;
1399 }
1400}
1401
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1403 CPURegister src,
1404 const MemOperand& dst) {
1405 UseScratchRegisterScope temps(GetVIXLAssembler());
1406 Register temp_base = temps.AcquireX();
1407
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001408 DCHECK(!dst.IsPreIndex());
1409 DCHECK(!dst.IsPostIndex());
1410
1411 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001412 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001413 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001414 MemOperand base = MemOperand(temp_base);
1415 switch (type) {
1416 case Primitive::kPrimBoolean:
1417 case Primitive::kPrimByte:
1418 __ Stlrb(Register(src), base);
1419 break;
1420 case Primitive::kPrimChar:
1421 case Primitive::kPrimShort:
1422 __ Stlrh(Register(src), base);
1423 break;
1424 case Primitive::kPrimInt:
1425 case Primitive::kPrimNot:
1426 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001427 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001428 __ Stlr(Register(src), base);
1429 break;
1430 case Primitive::kPrimFloat:
1431 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001432 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001433 Register temp_src;
1434 if (src.IsZero()) {
1435 // The zero register is used to avoid synthesizing zero constants.
1436 temp_src = Register(src);
1437 } else {
1438 DCHECK(src.IsFPRegister());
1439 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1440 __ Fmov(temp_src, FPRegister(src));
1441 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001442
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001443 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444 break;
1445 }
1446 case Primitive::kPrimVoid:
1447 LOG(FATAL) << "Unreachable type " << type;
1448 }
1449}
1450
Calin Juravle175dc732015-08-25 15:42:32 +01001451void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1452 HInstruction* instruction,
1453 uint32_t dex_pc,
1454 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001455 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001456 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001457 if (EntrypointRequiresStackMap(entrypoint)) {
1458 RecordPcInfo(instruction, dex_pc, slow_path);
1459 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001460}
1461
Roland Levillaindec8f632016-07-22 17:10:06 +01001462void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1463 HInstruction* instruction,
1464 SlowPathCode* slow_path) {
1465 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001466 GenerateInvokeRuntime(entry_point_offset);
1467}
1468
1469void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001470 BlockPoolsScope block_pools(GetVIXLAssembler());
1471 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1472 __ Blr(lr);
1473}
1474
Alexandre Rames67555f72014-11-18 10:55:16 +00001475void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001476 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 UseScratchRegisterScope temps(GetVIXLAssembler());
1478 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001479 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1480
Serban Constantinescu02164b32014-11-13 14:05:07 +00001481 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001482 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1483 __ Add(temp, class_reg, status_offset);
1484 __ Ldar(temp, HeapOperand(temp));
1485 __ Cmp(temp, mirror::Class::kStatusInitialized);
1486 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001487 __ Bind(slow_path->GetExitLabel());
1488}
Alexandre Rames5319def2014-10-23 10:03:10 +01001489
Roland Levillain44015862016-01-22 11:47:17 +00001490void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001491 BarrierType type = BarrierAll;
1492
1493 switch (kind) {
1494 case MemBarrierKind::kAnyAny:
1495 case MemBarrierKind::kAnyStore: {
1496 type = BarrierAll;
1497 break;
1498 }
1499 case MemBarrierKind::kLoadAny: {
1500 type = BarrierReads;
1501 break;
1502 }
1503 case MemBarrierKind::kStoreStore: {
1504 type = BarrierWrites;
1505 break;
1506 }
1507 default:
1508 LOG(FATAL) << "Unexpected memory barrier " << kind;
1509 }
1510 __ Dmb(InnerShareable, type);
1511}
1512
Serban Constantinescu02164b32014-11-13 14:05:07 +00001513void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1514 HBasicBlock* successor) {
1515 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001516 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1517 if (slow_path == nullptr) {
1518 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1519 instruction->SetSlowPath(slow_path);
1520 codegen_->AddSlowPath(slow_path);
1521 if (successor != nullptr) {
1522 DCHECK(successor->IsLoopHeader());
1523 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1524 }
1525 } else {
1526 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1527 }
1528
Serban Constantinescu02164b32014-11-13 14:05:07 +00001529 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1530 Register temp = temps.AcquireW();
1531
Andreas Gampe542451c2016-07-26 09:02:02 -07001532 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001533 if (successor == nullptr) {
1534 __ Cbnz(temp, slow_path->GetEntryLabel());
1535 __ Bind(slow_path->GetReturnLabel());
1536 } else {
1537 __ Cbz(temp, codegen_->GetLabelOf(successor));
1538 __ B(slow_path->GetEntryLabel());
1539 // slow_path will return to GetLabelOf(successor).
1540 }
1541}
1542
Alexandre Rames5319def2014-10-23 10:03:10 +01001543InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1544 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001545 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001546 assembler_(codegen->GetAssembler()),
1547 codegen_(codegen) {}
1548
1549#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001550 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001551
1552#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1553
1554enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001555 // Using a base helps identify when we hit such breakpoints.
1556 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001557#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1558 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1559#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1560};
1561
1562#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001563 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001564 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1565 } \
1566 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1568 locations->SetOut(Location::Any()); \
1569 }
1570 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1571#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1572
1573#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001574#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001575
Alexandre Rames67555f72014-11-18 10:55:16 +00001576void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001577 DCHECK_EQ(instr->InputCount(), 2U);
1578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1579 Primitive::Type type = instr->GetResultType();
1580 switch (type) {
1581 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001582 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001583 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001584 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001586 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001587
1588 case Primitive::kPrimFloat:
1589 case Primitive::kPrimDouble:
1590 locations->SetInAt(0, Location::RequiresFpuRegister());
1591 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001593 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001594
Alexandre Rames5319def2014-10-23 10:03:10 +01001595 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001596 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001597 }
1598}
1599
Alexandre Rames09a99962015-04-15 11:47:56 +01001600void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001601 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1602
1603 bool object_field_get_with_read_barrier =
1604 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001605 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001606 new (GetGraph()->GetArena()) LocationSummary(instruction,
1607 object_field_get_with_read_barrier ?
1608 LocationSummary::kCallOnSlowPath :
1609 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001610 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
1611 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
1612 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001613 locations->SetInAt(0, Location::RequiresRegister());
1614 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1615 locations->SetOut(Location::RequiresFpuRegister());
1616 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001617 // The output overlaps for an object field get when read barriers
1618 // are enabled: we do not want the load to overwrite the object's
1619 // location, as we need it to emit the read barrier.
1620 locations->SetOut(
1621 Location::RequiresRegister(),
1622 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001623 }
1624}
1625
1626void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1627 const FieldInfo& field_info) {
1628 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001629 LocationSummary* locations = instruction->GetLocations();
1630 Location base_loc = locations->InAt(0);
1631 Location out = locations->Out();
1632 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001633 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001634 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001635 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001636
Roland Levillain44015862016-01-22 11:47:17 +00001637 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1638 // Object FieldGet with Baker's read barrier case.
1639 MacroAssembler* masm = GetVIXLAssembler();
1640 UseScratchRegisterScope temps(masm);
1641 // /* HeapReference<Object> */ out = *(base + offset)
1642 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1643 Register temp = temps.AcquireW();
1644 // Note that potential implicit null checks are handled in this
1645 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1646 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1647 instruction,
1648 out,
1649 base,
1650 offset,
1651 temp,
1652 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001653 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001654 } else {
1655 // General case.
1656 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001657 // Note that a potential implicit null check is handled in this
1658 // CodeGeneratorARM64::LoadAcquire call.
1659 // NB: LoadAcquire will record the pc info if needed.
1660 codegen_->LoadAcquire(
1661 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001662 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001663 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001664 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001665 }
Roland Levillain44015862016-01-22 11:47:17 +00001666 if (field_type == Primitive::kPrimNot) {
1667 // If read barriers are enabled, emit read barriers other than
1668 // Baker's using a slow path (and also unpoison the loaded
1669 // reference, if heap poisoning is enabled).
1670 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1671 }
Roland Levillain4d027112015-07-01 15:41:14 +01001672 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001673}
1674
1675void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1676 LocationSummary* locations =
1677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1678 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001679 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1680 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1681 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001682 locations->SetInAt(1, Location::RequiresFpuRegister());
1683 } else {
1684 locations->SetInAt(1, Location::RequiresRegister());
1685 }
1686}
1687
1688void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001689 const FieldInfo& field_info,
1690 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001691 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001692 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001693
1694 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001695 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001696 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001697 Offset offset = field_info.GetFieldOffset();
1698 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001699
Roland Levillain4d027112015-07-01 15:41:14 +01001700 {
1701 // We use a block to end the scratch scope before the write barrier, thus
1702 // freeing the temporary registers so they can be used in `MarkGCCard`.
1703 UseScratchRegisterScope temps(GetVIXLAssembler());
1704
1705 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1706 DCHECK(value.IsW());
1707 Register temp = temps.AcquireW();
1708 __ Mov(temp, value.W());
1709 GetAssembler()->PoisonHeapReference(temp.W());
1710 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001711 }
Roland Levillain4d027112015-07-01 15:41:14 +01001712
1713 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001714 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1715 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001716 } else {
1717 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1718 codegen_->MaybeRecordImplicitNullCheck(instruction);
1719 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001720 }
1721
1722 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001723 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001724 }
1725}
1726
Alexandre Rames67555f72014-11-18 10:55:16 +00001727void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001728 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001729
1730 switch (type) {
1731 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001732 case Primitive::kPrimLong: {
1733 Register dst = OutputRegister(instr);
1734 Register lhs = InputRegisterAt(instr, 0);
1735 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001736 if (instr->IsAdd()) {
1737 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001738 } else if (instr->IsAnd()) {
1739 __ And(dst, lhs, rhs);
1740 } else if (instr->IsOr()) {
1741 __ Orr(dst, lhs, rhs);
1742 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001743 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001744 } else if (instr->IsRor()) {
1745 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001746 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001747 __ Ror(dst, lhs, shift);
1748 } else {
1749 // Ensure shift distance is in the same size register as the result. If
1750 // we are rotating a long and the shift comes in a w register originally,
1751 // we don't need to sxtw for use as an x since the shift distances are
1752 // all & reg_bits - 1.
1753 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1754 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001755 } else {
1756 DCHECK(instr->IsXor());
1757 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001758 }
1759 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001760 }
1761 case Primitive::kPrimFloat:
1762 case Primitive::kPrimDouble: {
1763 FPRegister dst = OutputFPRegister(instr);
1764 FPRegister lhs = InputFPRegisterAt(instr, 0);
1765 FPRegister rhs = InputFPRegisterAt(instr, 1);
1766 if (instr->IsAdd()) {
1767 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001768 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001769 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001770 } else {
1771 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001772 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001773 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001774 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001775 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001776 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001777 }
1778}
1779
Serban Constantinescu02164b32014-11-13 14:05:07 +00001780void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1781 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1782
1783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1784 Primitive::Type type = instr->GetResultType();
1785 switch (type) {
1786 case Primitive::kPrimInt:
1787 case Primitive::kPrimLong: {
1788 locations->SetInAt(0, Location::RequiresRegister());
1789 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1790 locations->SetOut(Location::RequiresRegister());
1791 break;
1792 }
1793 default:
1794 LOG(FATAL) << "Unexpected shift type " << type;
1795 }
1796}
1797
1798void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1799 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1800
1801 Primitive::Type type = instr->GetType();
1802 switch (type) {
1803 case Primitive::kPrimInt:
1804 case Primitive::kPrimLong: {
1805 Register dst = OutputRegister(instr);
1806 Register lhs = InputRegisterAt(instr, 0);
1807 Operand rhs = InputOperandAt(instr, 1);
1808 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001809 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001810 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001811 if (instr->IsShl()) {
1812 __ Lsl(dst, lhs, shift_value);
1813 } else if (instr->IsShr()) {
1814 __ Asr(dst, lhs, shift_value);
1815 } else {
1816 __ Lsr(dst, lhs, shift_value);
1817 }
1818 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001819 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001820
1821 if (instr->IsShl()) {
1822 __ Lsl(dst, lhs, rhs_reg);
1823 } else if (instr->IsShr()) {
1824 __ Asr(dst, lhs, rhs_reg);
1825 } else {
1826 __ Lsr(dst, lhs, rhs_reg);
1827 }
1828 }
1829 break;
1830 }
1831 default:
1832 LOG(FATAL) << "Unexpected shift operation type " << type;
1833 }
1834}
1835
Alexandre Rames5319def2014-10-23 10:03:10 +01001836void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001837 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001838}
1839
1840void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001841 HandleBinaryOp(instruction);
1842}
1843
1844void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1845 HandleBinaryOp(instruction);
1846}
1847
1848void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1849 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001850}
1851
Artem Serov7fc63502016-02-09 17:15:29 +00001852void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001853 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1854 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1855 locations->SetInAt(0, Location::RequiresRegister());
1856 // There is no immediate variant of negated bitwise instructions in AArch64.
1857 locations->SetInAt(1, Location::RequiresRegister());
1858 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1859}
1860
Artem Serov7fc63502016-02-09 17:15:29 +00001861void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001862 Register dst = OutputRegister(instr);
1863 Register lhs = InputRegisterAt(instr, 0);
1864 Register rhs = InputRegisterAt(instr, 1);
1865
1866 switch (instr->GetOpKind()) {
1867 case HInstruction::kAnd:
1868 __ Bic(dst, lhs, rhs);
1869 break;
1870 case HInstruction::kOr:
1871 __ Orn(dst, lhs, rhs);
1872 break;
1873 case HInstruction::kXor:
1874 __ Eon(dst, lhs, rhs);
1875 break;
1876 default:
1877 LOG(FATAL) << "Unreachable";
1878 }
1879}
1880
Alexandre Rames8626b742015-11-25 16:28:08 +00001881void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1882 HArm64DataProcWithShifterOp* instruction) {
1883 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1884 instruction->GetType() == Primitive::kPrimLong);
1885 LocationSummary* locations =
1886 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1887 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1888 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1889 } else {
1890 locations->SetInAt(0, Location::RequiresRegister());
1891 }
1892 locations->SetInAt(1, Location::RequiresRegister());
1893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1894}
1895
1896void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1897 HArm64DataProcWithShifterOp* instruction) {
1898 Primitive::Type type = instruction->GetType();
1899 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1900 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1901 Register out = OutputRegister(instruction);
1902 Register left;
1903 if (kind != HInstruction::kNeg) {
1904 left = InputRegisterAt(instruction, 0);
1905 }
1906 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1907 // shifter operand operation, the IR generating `right_reg` (input to the type
1908 // conversion) can have a different type from the current instruction's type,
1909 // so we manually indicate the type.
1910 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001911 int64_t shift_amount = instruction->GetShiftAmount() &
1912 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001913
1914 Operand right_operand(0);
1915
1916 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1917 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1918 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1919 } else {
1920 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1921 }
1922
1923 // Logical binary operations do not support extension operations in the
1924 // operand. Note that VIXL would still manage if it was passed by generating
1925 // the extension as a separate instruction.
1926 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1927 DCHECK(!right_operand.IsExtendedRegister() ||
1928 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1929 kind != HInstruction::kNeg));
1930 switch (kind) {
1931 case HInstruction::kAdd:
1932 __ Add(out, left, right_operand);
1933 break;
1934 case HInstruction::kAnd:
1935 __ And(out, left, right_operand);
1936 break;
1937 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001938 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001939 __ Neg(out, right_operand);
1940 break;
1941 case HInstruction::kOr:
1942 __ Orr(out, left, right_operand);
1943 break;
1944 case HInstruction::kSub:
1945 __ Sub(out, left, right_operand);
1946 break;
1947 case HInstruction::kXor:
1948 __ Eor(out, left, right_operand);
1949 break;
1950 default:
1951 LOG(FATAL) << "Unexpected operation kind: " << kind;
1952 UNREACHABLE();
1953 }
1954}
1955
Artem Serov328429f2016-07-06 16:23:04 +01001956void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001957 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1958 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001959 LocationSummary* locations =
1960 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1961 locations->SetInAt(0, Location::RequiresRegister());
1962 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1963 locations->SetOut(Location::RequiresRegister());
1964}
1965
Roland Levillain4a3aa572016-08-15 13:17:06 +00001966void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1967 HIntermediateAddress* instruction) {
1968 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1969 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001970 __ Add(OutputRegister(instruction),
1971 InputRegisterAt(instruction, 0),
1972 Operand(InputOperandAt(instruction, 1)));
1973}
1974
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001975void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001976 LocationSummary* locations =
1977 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001978 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1979 if (instr->GetOpKind() == HInstruction::kSub &&
1980 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00001981 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001982 // Don't allocate register for Mneg instruction.
1983 } else {
1984 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1985 Location::RequiresRegister());
1986 }
1987 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1988 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00001989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1990}
1991
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001992void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001993 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001994 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
1995 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00001996
1997 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1998 // This fixup should be carried out for all multiply-accumulate instructions:
1999 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2000 if (instr->GetType() == Primitive::kPrimLong &&
2001 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2002 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002003 vixl::aarch64::Instruction* prev =
2004 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002005 if (prev->IsLoadOrStore()) {
2006 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002007 vixl::aarch64::CodeBufferCheckScope scope(masm,
2008 kInstructionSize,
2009 vixl::aarch64::CodeBufferCheckScope::kCheck,
2010 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002011 __ nop();
2012 }
2013 }
2014
2015 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002016 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002017 __ Madd(res, mul_left, mul_right, accumulator);
2018 } else {
2019 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002020 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002021 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002022 __ Mneg(res, mul_left, mul_right);
2023 } else {
2024 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2025 __ Msub(res, mul_left, mul_right, accumulator);
2026 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002027 }
2028}
2029
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002030void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002031 bool object_array_get_with_read_barrier =
2032 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002033 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002034 new (GetGraph()->GetArena()) LocationSummary(instruction,
2035 object_array_get_with_read_barrier ?
2036 LocationSummary::kCallOnSlowPath :
2037 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002038 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2039 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
2040 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002041 locations->SetInAt(0, Location::RequiresRegister());
2042 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002043 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2045 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002046 // The output overlaps in the case of an object array get with
2047 // read barriers enabled: we do not want the move to overwrite the
2048 // array's location, as we need it to emit the read barrier.
2049 locations->SetOut(
2050 Location::RequiresRegister(),
2051 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002052 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002053}
2054
2055void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002056 Primitive::Type type = instruction->GetType();
2057 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002058 LocationSummary* locations = instruction->GetLocations();
2059 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002060 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002061 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002062
Alexandre Ramesd921d642015-04-16 15:07:16 +01002063 MacroAssembler* masm = GetVIXLAssembler();
2064 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002065 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002066 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002067
Roland Levillain44015862016-01-22 11:47:17 +00002068 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2069 // Object ArrayGet with Baker's read barrier case.
2070 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002071 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2072 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002073 // Note that a potential implicit null check is handled in the
2074 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2075 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2076 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002077 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002078 // General case.
2079 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002080 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002081 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2082 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002083 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002084 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002085 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002086 // The read barrier instrumentation does not support the
2087 // HIntermediateAddress instruction yet.
2088 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002089 // We do not need to compute the intermediate address from the array: the
2090 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002091 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002092 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002093 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002094 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2095 }
2096 temp = obj;
2097 } else {
2098 __ Add(temp, obj, offset);
2099 }
2100 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2101 }
2102
2103 codegen_->Load(type, OutputCPURegister(instruction), source);
2104 codegen_->MaybeRecordImplicitNullCheck(instruction);
2105
2106 if (type == Primitive::kPrimNot) {
2107 static_assert(
2108 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2109 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2110 Location obj_loc = locations->InAt(0);
2111 if (index.IsConstant()) {
2112 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2113 } else {
2114 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2115 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002116 }
Roland Levillain4d027112015-07-01 15:41:14 +01002117 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002118}
2119
Alexandre Rames5319def2014-10-23 10:03:10 +01002120void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2121 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2122 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002123 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002124}
2125
2126void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002127 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002128 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002129 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002130 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002131}
2132
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002133void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002134 Primitive::Type value_type = instruction->GetComponentType();
2135
2136 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002137 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2138 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002139 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002140 LocationSummary::kCallOnSlowPath :
2141 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002142 locations->SetInAt(0, Location::RequiresRegister());
2143 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002144 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2145 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2146 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002147 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002148 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002149 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002150 }
2151}
2152
2153void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2154 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002155 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002156 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002157 bool needs_write_barrier =
2158 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002159
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002160 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002161 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002162 CPURegister source = value;
2163 Location index = locations->InAt(1);
2164 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2165 MemOperand destination = HeapOperand(array);
2166 MacroAssembler* masm = GetVIXLAssembler();
2167 BlockPoolsScope block_pools(masm);
2168
2169 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002170 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002171 if (index.IsConstant()) {
2172 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2173 destination = HeapOperand(array, offset);
2174 } else {
2175 UseScratchRegisterScope temps(masm);
2176 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002177 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002178 // The read barrier instrumentation does not support the
2179 // HIntermediateAddress instruction yet.
2180 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002181 // We do not need to compute the intermediate address from the array: the
2182 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002183 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002184 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002185 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002186 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2187 }
2188 temp = array;
2189 } else {
2190 __ Add(temp, array, offset);
2191 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002192 destination = HeapOperand(temp,
2193 XRegisterFrom(index),
2194 LSL,
2195 Primitive::ComponentSizeShift(value_type));
2196 }
2197 codegen_->Store(value_type, value, destination);
2198 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002199 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002200 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002201 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002202 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002203 {
2204 // We use a block to end the scratch scope before the write barrier, thus
2205 // freeing the temporary registers so they can be used in `MarkGCCard`.
2206 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002207 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002208 if (index.IsConstant()) {
2209 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002210 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002211 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002212 destination = HeapOperand(temp,
2213 XRegisterFrom(index),
2214 LSL,
2215 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002216 }
2217
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002218 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2219 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2220 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2221
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002222 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002223 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2224 codegen_->AddSlowPath(slow_path);
2225 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002226 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002227 __ Cbnz(Register(value), &non_zero);
2228 if (!index.IsConstant()) {
2229 __ Add(temp, array, offset);
2230 }
2231 __ Str(wzr, destination);
2232 codegen_->MaybeRecordImplicitNullCheck(instruction);
2233 __ B(&done);
2234 __ Bind(&non_zero);
2235 }
2236
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002237 // Note that when Baker read barriers are enabled, the type
2238 // checks are performed without read barriers. This is fine,
2239 // even in the case where a class object is in the from-space
2240 // after the flip, as a comparison involving such a type would
2241 // not produce a false positive; it may of course produce a
2242 // false negative, in which case we would take the ArraySet
2243 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002244
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002245 Register temp2 = temps.AcquireSameSizeAs(array);
2246 // /* HeapReference<Class> */ temp = array->klass_
2247 __ Ldr(temp, HeapOperand(array, class_offset));
2248 codegen_->MaybeRecordImplicitNullCheck(instruction);
2249 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002250
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002251 // /* HeapReference<Class> */ temp = temp->component_type_
2252 __ Ldr(temp, HeapOperand(temp, component_offset));
2253 // /* HeapReference<Class> */ temp2 = value->klass_
2254 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2255 // If heap poisoning is enabled, no need to unpoison `temp`
2256 // nor `temp2`, as we are comparing two poisoned references.
2257 __ Cmp(temp, temp2);
2258 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002259
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002260 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2261 vixl::aarch64::Label do_put;
2262 __ B(eq, &do_put);
2263 // If heap poisoning is enabled, the `temp` reference has
2264 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002265 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2266
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002267 // /* HeapReference<Class> */ temp = temp->super_class_
2268 __ Ldr(temp, HeapOperand(temp, super_offset));
2269 // If heap poisoning is enabled, no need to unpoison
2270 // `temp`, as we are comparing against null below.
2271 __ Cbnz(temp, slow_path->GetEntryLabel());
2272 __ Bind(&do_put);
2273 } else {
2274 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002275 }
2276 }
2277
2278 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002279 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002280 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002281 __ Mov(temp2, value.W());
2282 GetAssembler()->PoisonHeapReference(temp2);
2283 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002284 }
2285
2286 if (!index.IsConstant()) {
2287 __ Add(temp, array, offset);
2288 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002289 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002290
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002291 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002292 codegen_->MaybeRecordImplicitNullCheck(instruction);
2293 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002294 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002295
2296 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2297
2298 if (done.IsLinked()) {
2299 __ Bind(&done);
2300 }
2301
2302 if (slow_path != nullptr) {
2303 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002304 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002305 }
2306}
2307
Alexandre Rames67555f72014-11-18 10:55:16 +00002308void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002309 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2310 ? LocationSummary::kCallOnSlowPath
2311 : LocationSummary::kNoCall;
2312 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002313 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002314 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002315 if (instruction->HasUses()) {
2316 locations->SetOut(Location::SameAsFirstInput());
2317 }
2318}
2319
2320void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002321 BoundsCheckSlowPathARM64* slow_path =
2322 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002323 codegen_->AddSlowPath(slow_path);
2324
2325 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2326 __ B(slow_path->GetEntryLabel(), hs);
2327}
2328
Alexandre Rames67555f72014-11-18 10:55:16 +00002329void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2330 LocationSummary* locations =
2331 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2332 locations->SetInAt(0, Location::RequiresRegister());
2333 if (check->HasUses()) {
2334 locations->SetOut(Location::SameAsFirstInput());
2335 }
2336}
2337
2338void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2339 // We assume the class is not null.
2340 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2341 check->GetLoadClass(), check, check->GetDexPc(), true);
2342 codegen_->AddSlowPath(slow_path);
2343 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2344}
2345
Roland Levillain1a653882016-03-18 18:05:57 +00002346static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2347 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2348 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2349}
2350
2351void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2352 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2353 Location rhs_loc = instruction->GetLocations()->InAt(1);
2354 if (rhs_loc.IsConstant()) {
2355 // 0.0 is the only immediate that can be encoded directly in
2356 // an FCMP instruction.
2357 //
2358 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2359 // specify that in a floating-point comparison, positive zero
2360 // and negative zero are considered equal, so we can use the
2361 // literal 0.0 for both cases here.
2362 //
2363 // Note however that some methods (Float.equal, Float.compare,
2364 // Float.compareTo, Double.equal, Double.compare,
2365 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2366 // StrictMath.min) consider 0.0 to be (strictly) greater than
2367 // -0.0. So if we ever translate calls to these methods into a
2368 // HCompare instruction, we must handle the -0.0 case with
2369 // care here.
2370 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2371 __ Fcmp(lhs_reg, 0.0);
2372 } else {
2373 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2374 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002375}
2376
Serban Constantinescu02164b32014-11-13 14:05:07 +00002377void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002378 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002379 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2380 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002381 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002382 case Primitive::kPrimBoolean:
2383 case Primitive::kPrimByte:
2384 case Primitive::kPrimShort:
2385 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002386 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002387 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002388 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002389 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2391 break;
2392 }
2393 case Primitive::kPrimFloat:
2394 case Primitive::kPrimDouble: {
2395 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002396 locations->SetInAt(1,
2397 IsFloatingPointZeroConstant(compare->InputAt(1))
2398 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2399 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002400 locations->SetOut(Location::RequiresRegister());
2401 break;
2402 }
2403 default:
2404 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2405 }
2406}
2407
2408void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2409 Primitive::Type in_type = compare->InputAt(0)->GetType();
2410
2411 // 0 if: left == right
2412 // 1 if: left > right
2413 // -1 if: left < right
2414 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002415 case Primitive::kPrimBoolean:
2416 case Primitive::kPrimByte:
2417 case Primitive::kPrimShort:
2418 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002419 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002420 case Primitive::kPrimLong: {
2421 Register result = OutputRegister(compare);
2422 Register left = InputRegisterAt(compare, 0);
2423 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002424 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002425 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2426 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002427 break;
2428 }
2429 case Primitive::kPrimFloat:
2430 case Primitive::kPrimDouble: {
2431 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002432 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002433 __ Cset(result, ne);
2434 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002435 break;
2436 }
2437 default:
2438 LOG(FATAL) << "Unimplemented compare type " << in_type;
2439 }
2440}
2441
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002442void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002443 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002444
2445 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2446 locations->SetInAt(0, Location::RequiresFpuRegister());
2447 locations->SetInAt(1,
2448 IsFloatingPointZeroConstant(instruction->InputAt(1))
2449 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2450 : Location::RequiresFpuRegister());
2451 } else {
2452 // Integer cases.
2453 locations->SetInAt(0, Location::RequiresRegister());
2454 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2455 }
2456
David Brazdilb3e773e2016-01-26 11:28:37 +00002457 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002459 }
2460}
2461
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002462void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002463 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002464 return;
2465 }
2466
2467 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002469 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002470
Roland Levillain7f63c522015-07-13 15:54:55 +00002471 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002472 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002473 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002474 } else {
2475 // Integer cases.
2476 Register lhs = InputRegisterAt(instruction, 0);
2477 Operand rhs = InputOperandAt(instruction, 1);
2478 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002479 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002480 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002481}
2482
2483#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2484 M(Equal) \
2485 M(NotEqual) \
2486 M(LessThan) \
2487 M(LessThanOrEqual) \
2488 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002489 M(GreaterThanOrEqual) \
2490 M(Below) \
2491 M(BelowOrEqual) \
2492 M(Above) \
2493 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002494#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002495void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2496void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002497FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002498#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002499#undef FOR_EACH_CONDITION_INSTRUCTION
2500
Zheng Xuc6667102015-05-15 16:08:45 +08002501void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2502 DCHECK(instruction->IsDiv() || instruction->IsRem());
2503
2504 LocationSummary* locations = instruction->GetLocations();
2505 Location second = locations->InAt(1);
2506 DCHECK(second.IsConstant());
2507
2508 Register out = OutputRegister(instruction);
2509 Register dividend = InputRegisterAt(instruction, 0);
2510 int64_t imm = Int64FromConstant(second.GetConstant());
2511 DCHECK(imm == 1 || imm == -1);
2512
2513 if (instruction->IsRem()) {
2514 __ Mov(out, 0);
2515 } else {
2516 if (imm == 1) {
2517 __ Mov(out, dividend);
2518 } else {
2519 __ Neg(out, dividend);
2520 }
2521 }
2522}
2523
2524void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2525 DCHECK(instruction->IsDiv() || instruction->IsRem());
2526
2527 LocationSummary* locations = instruction->GetLocations();
2528 Location second = locations->InAt(1);
2529 DCHECK(second.IsConstant());
2530
2531 Register out = OutputRegister(instruction);
2532 Register dividend = InputRegisterAt(instruction, 0);
2533 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002534 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002535 int ctz_imm = CTZ(abs_imm);
2536
2537 UseScratchRegisterScope temps(GetVIXLAssembler());
2538 Register temp = temps.AcquireSameSizeAs(out);
2539
2540 if (instruction->IsDiv()) {
2541 __ Add(temp, dividend, abs_imm - 1);
2542 __ Cmp(dividend, 0);
2543 __ Csel(out, temp, dividend, lt);
2544 if (imm > 0) {
2545 __ Asr(out, out, ctz_imm);
2546 } else {
2547 __ Neg(out, Operand(out, ASR, ctz_imm));
2548 }
2549 } else {
2550 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2551 __ Asr(temp, dividend, bits - 1);
2552 __ Lsr(temp, temp, bits - ctz_imm);
2553 __ Add(out, dividend, temp);
2554 __ And(out, out, abs_imm - 1);
2555 __ Sub(out, out, temp);
2556 }
2557}
2558
2559void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2560 DCHECK(instruction->IsDiv() || instruction->IsRem());
2561
2562 LocationSummary* locations = instruction->GetLocations();
2563 Location second = locations->InAt(1);
2564 DCHECK(second.IsConstant());
2565
2566 Register out = OutputRegister(instruction);
2567 Register dividend = InputRegisterAt(instruction, 0);
2568 int64_t imm = Int64FromConstant(second.GetConstant());
2569
2570 Primitive::Type type = instruction->GetResultType();
2571 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2572
2573 int64_t magic;
2574 int shift;
2575 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2576
2577 UseScratchRegisterScope temps(GetVIXLAssembler());
2578 Register temp = temps.AcquireSameSizeAs(out);
2579
2580 // temp = get_high(dividend * magic)
2581 __ Mov(temp, magic);
2582 if (type == Primitive::kPrimLong) {
2583 __ Smulh(temp, dividend, temp);
2584 } else {
2585 __ Smull(temp.X(), dividend, temp);
2586 __ Lsr(temp.X(), temp.X(), 32);
2587 }
2588
2589 if (imm > 0 && magic < 0) {
2590 __ Add(temp, temp, dividend);
2591 } else if (imm < 0 && magic > 0) {
2592 __ Sub(temp, temp, dividend);
2593 }
2594
2595 if (shift != 0) {
2596 __ Asr(temp, temp, shift);
2597 }
2598
2599 if (instruction->IsDiv()) {
2600 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2601 } else {
2602 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2603 // TODO: Strength reduction for msub.
2604 Register temp_imm = temps.AcquireSameSizeAs(out);
2605 __ Mov(temp_imm, imm);
2606 __ Msub(out, temp, temp_imm, dividend);
2607 }
2608}
2609
2610void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2611 DCHECK(instruction->IsDiv() || instruction->IsRem());
2612 Primitive::Type type = instruction->GetResultType();
2613 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2614
2615 LocationSummary* locations = instruction->GetLocations();
2616 Register out = OutputRegister(instruction);
2617 Location second = locations->InAt(1);
2618
2619 if (second.IsConstant()) {
2620 int64_t imm = Int64FromConstant(second.GetConstant());
2621
2622 if (imm == 0) {
2623 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2624 } else if (imm == 1 || imm == -1) {
2625 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002626 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002627 DivRemByPowerOfTwo(instruction);
2628 } else {
2629 DCHECK(imm <= -2 || imm >= 2);
2630 GenerateDivRemWithAnyConstant(instruction);
2631 }
2632 } else {
2633 Register dividend = InputRegisterAt(instruction, 0);
2634 Register divisor = InputRegisterAt(instruction, 1);
2635 if (instruction->IsDiv()) {
2636 __ Sdiv(out, dividend, divisor);
2637 } else {
2638 UseScratchRegisterScope temps(GetVIXLAssembler());
2639 Register temp = temps.AcquireSameSizeAs(out);
2640 __ Sdiv(temp, dividend, divisor);
2641 __ Msub(out, temp, divisor, dividend);
2642 }
2643 }
2644}
2645
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002646void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2647 LocationSummary* locations =
2648 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2649 switch (div->GetResultType()) {
2650 case Primitive::kPrimInt:
2651 case Primitive::kPrimLong:
2652 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002653 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2655 break;
2656
2657 case Primitive::kPrimFloat:
2658 case Primitive::kPrimDouble:
2659 locations->SetInAt(0, Location::RequiresFpuRegister());
2660 locations->SetInAt(1, Location::RequiresFpuRegister());
2661 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2662 break;
2663
2664 default:
2665 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2666 }
2667}
2668
2669void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2670 Primitive::Type type = div->GetResultType();
2671 switch (type) {
2672 case Primitive::kPrimInt:
2673 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002674 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002675 break;
2676
2677 case Primitive::kPrimFloat:
2678 case Primitive::kPrimDouble:
2679 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2680 break;
2681
2682 default:
2683 LOG(FATAL) << "Unexpected div type " << type;
2684 }
2685}
2686
Alexandre Rames67555f72014-11-18 10:55:16 +00002687void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002688 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2689 ? LocationSummary::kCallOnSlowPath
2690 : LocationSummary::kNoCall;
2691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002692 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2693 if (instruction->HasUses()) {
2694 locations->SetOut(Location::SameAsFirstInput());
2695 }
2696}
2697
2698void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2699 SlowPathCodeARM64* slow_path =
2700 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2701 codegen_->AddSlowPath(slow_path);
2702 Location value = instruction->GetLocations()->InAt(0);
2703
Alexandre Rames3e69f162014-12-10 10:36:50 +00002704 Primitive::Type type = instruction->GetType();
2705
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002706 if (!Primitive::IsIntegralType(type)) {
2707 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002708 return;
2709 }
2710
Alexandre Rames67555f72014-11-18 10:55:16 +00002711 if (value.IsConstant()) {
2712 int64_t divisor = Int64ConstantFrom(value);
2713 if (divisor == 0) {
2714 __ B(slow_path->GetEntryLabel());
2715 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002716 // A division by a non-null constant is valid. We don't need to perform
2717 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002718 }
2719 } else {
2720 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2721 }
2722}
2723
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002724void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2725 LocationSummary* locations =
2726 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2727 locations->SetOut(Location::ConstantLocation(constant));
2728}
2729
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002730void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2731 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002732 // Will be generated at use site.
2733}
2734
Alexandre Rames5319def2014-10-23 10:03:10 +01002735void LocationsBuilderARM64::VisitExit(HExit* exit) {
2736 exit->SetLocations(nullptr);
2737}
2738
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002739void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002740}
2741
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002742void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2745 locations->SetOut(Location::ConstantLocation(constant));
2746}
2747
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002748void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002749 // Will be generated at use site.
2750}
2751
David Brazdilfc6a86a2015-06-26 10:33:45 +00002752void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002753 DCHECK(!successor->IsExitBlock());
2754 HBasicBlock* block = got->GetBlock();
2755 HInstruction* previous = got->GetPrevious();
2756 HLoopInformation* info = block->GetLoopInformation();
2757
David Brazdil46e2a392015-03-16 17:31:52 +00002758 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002759 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2760 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2761 return;
2762 }
2763 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2764 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2765 }
2766 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002767 __ B(codegen_->GetLabelOf(successor));
2768 }
2769}
2770
David Brazdilfc6a86a2015-06-26 10:33:45 +00002771void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2772 got->SetLocations(nullptr);
2773}
2774
2775void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2776 HandleGoto(got, got->GetSuccessor());
2777}
2778
2779void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2780 try_boundary->SetLocations(nullptr);
2781}
2782
2783void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2784 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2785 if (!successor->IsExitBlock()) {
2786 HandleGoto(try_boundary, successor);
2787 }
2788}
2789
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002790void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002791 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002792 vixl::aarch64::Label* true_target,
2793 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002794 // FP branching requires both targets to be explicit. If either of the targets
2795 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002796 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002797 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002798
David Brazdil0debae72015-11-12 18:37:00 +00002799 if (true_target == nullptr && false_target == nullptr) {
2800 // Nothing to do. The code always falls through.
2801 return;
2802 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002803 // Constant condition, statically compared against "true" (integer value 1).
2804 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002805 if (true_target != nullptr) {
2806 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002807 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002808 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002809 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002810 if (false_target != nullptr) {
2811 __ B(false_target);
2812 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002813 }
David Brazdil0debae72015-11-12 18:37:00 +00002814 return;
2815 }
2816
2817 // The following code generates these patterns:
2818 // (1) true_target == nullptr && false_target != nullptr
2819 // - opposite condition true => branch to false_target
2820 // (2) true_target != nullptr && false_target == nullptr
2821 // - condition true => branch to true_target
2822 // (3) true_target != nullptr && false_target != nullptr
2823 // - condition true => branch to true_target
2824 // - branch to false_target
2825 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002826 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002827 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002828 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002829 if (true_target == nullptr) {
2830 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2831 } else {
2832 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2833 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002834 } else {
2835 // The condition instruction has not been materialized, use its inputs as
2836 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002837 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002838
David Brazdil0debae72015-11-12 18:37:00 +00002839 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002840 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002841 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002842 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002843 IfCondition opposite_condition = condition->GetOppositeCondition();
2844 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002845 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002846 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002847 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002848 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002849 // Integer cases.
2850 Register lhs = InputRegisterAt(condition, 0);
2851 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002852
2853 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002854 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002855 if (true_target == nullptr) {
2856 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2857 non_fallthrough_target = false_target;
2858 } else {
2859 arm64_cond = ARM64Condition(condition->GetCondition());
2860 non_fallthrough_target = true_target;
2861 }
2862
Aart Bik086d27e2016-01-20 17:02:00 -08002863 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002864 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002865 switch (arm64_cond) {
2866 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002867 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002868 break;
2869 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002870 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002871 break;
2872 case lt:
2873 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002874 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002875 break;
2876 case ge:
2877 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002878 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002879 break;
2880 default:
2881 // Without the `static_cast` the compiler throws an error for
2882 // `-Werror=sign-promo`.
2883 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2884 }
2885 } else {
2886 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002887 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002888 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002889 }
2890 }
David Brazdil0debae72015-11-12 18:37:00 +00002891
2892 // If neither branch falls through (case 3), the conditional branch to `true_target`
2893 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2894 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002895 __ B(false_target);
2896 }
David Brazdil0debae72015-11-12 18:37:00 +00002897
2898 if (fallthrough_target.IsLinked()) {
2899 __ Bind(&fallthrough_target);
2900 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002901}
2902
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002903void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002905 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002906 locations->SetInAt(0, Location::RequiresRegister());
2907 }
2908}
2909
2910void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002911 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2912 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002913 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2914 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2915 true_target = nullptr;
2916 }
2917 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2918 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2919 false_target = nullptr;
2920 }
David Brazdil0debae72015-11-12 18:37:00 +00002921 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002922}
2923
2924void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2925 LocationSummary* locations = new (GetGraph()->GetArena())
2926 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01002927 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002928 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002929 locations->SetInAt(0, Location::RequiresRegister());
2930 }
2931}
2932
2933void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002934 SlowPathCodeARM64* slow_path =
2935 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002936 GenerateTestAndBranch(deoptimize,
2937 /* condition_input_index */ 0,
2938 slow_path->GetEntryLabel(),
2939 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002940}
2941
David Brazdilc0b601b2016-02-08 14:20:45 +00002942static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2943 return condition->IsCondition() &&
2944 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2945}
2946
Alexandre Rames880f1192016-06-13 16:04:50 +01002947static inline Condition GetConditionForSelect(HCondition* condition) {
2948 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002949 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2950 : ARM64Condition(cond);
2951}
2952
David Brazdil74eb1b22015-12-14 11:44:01 +00002953void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002955 if (Primitive::IsFloatingPointType(select->GetType())) {
2956 locations->SetInAt(0, Location::RequiresFpuRegister());
2957 locations->SetInAt(1, Location::RequiresFpuRegister());
2958 locations->SetOut(Location::RequiresFpuRegister());
2959 } else {
2960 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2961 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2962 bool is_true_value_constant = cst_true_value != nullptr;
2963 bool is_false_value_constant = cst_false_value != nullptr;
2964 // Ask VIXL whether we should synthesize constants in registers.
2965 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
2966 Operand true_op = is_true_value_constant ?
2967 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
2968 Operand false_op = is_false_value_constant ?
2969 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
2970 bool true_value_in_register = false;
2971 bool false_value_in_register = false;
2972 MacroAssembler::GetCselSynthesisInformation(
2973 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
2974 true_value_in_register |= !is_true_value_constant;
2975 false_value_in_register |= !is_false_value_constant;
2976
2977 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
2978 : Location::ConstantLocation(cst_true_value));
2979 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
2980 : Location::ConstantLocation(cst_false_value));
2981 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002982 }
Alexandre Rames880f1192016-06-13 16:04:50 +01002983
David Brazdil74eb1b22015-12-14 11:44:01 +00002984 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2985 locations->SetInAt(2, Location::RequiresRegister());
2986 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002987}
2988
2989void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00002990 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002991 Condition csel_cond;
2992
2993 if (IsBooleanValueOrMaterializedCondition(cond)) {
2994 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01002995 // Use the condition flags set by the previous instruction.
2996 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00002997 } else {
2998 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01002999 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003000 }
3001 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003002 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003003 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003004 } else {
3005 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003006 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003007 }
3008
Alexandre Rames880f1192016-06-13 16:04:50 +01003009 if (Primitive::IsFloatingPointType(select->GetType())) {
3010 __ Fcsel(OutputFPRegister(select),
3011 InputFPRegisterAt(select, 1),
3012 InputFPRegisterAt(select, 0),
3013 csel_cond);
3014 } else {
3015 __ Csel(OutputRegister(select),
3016 InputOperandAt(select, 1),
3017 InputOperandAt(select, 0),
3018 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003019 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003020}
3021
David Srbecky0cf44932015-12-09 14:09:59 +00003022void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3023 new (GetGraph()->GetArena()) LocationSummary(info);
3024}
3025
David Srbeckyd28f4a02016-03-14 17:14:24 +00003026void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3027 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003028}
3029
3030void CodeGeneratorARM64::GenerateNop() {
3031 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003032}
3033
Alexandre Rames5319def2014-10-23 10:03:10 +01003034void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003035 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003036}
3037
3038void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003039 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003040}
3041
3042void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003043 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003044}
3045
3046void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003047 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003048}
3049
Roland Levillain44015862016-01-22 11:47:17 +00003050static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3051 return kEmitCompilerReadBarrier &&
3052 (kUseBakerReadBarrier ||
3053 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3054 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3055 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3056}
3057
Alexandre Rames67555f72014-11-18 10:55:16 +00003058void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003059 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003060 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003061 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003062 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003063 case TypeCheckKind::kExactCheck:
3064 case TypeCheckKind::kAbstractClassCheck:
3065 case TypeCheckKind::kClassHierarchyCheck:
3066 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003067 call_kind =
3068 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003069 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003070 break;
3071 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003072 case TypeCheckKind::kUnresolvedCheck:
3073 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003074 call_kind = LocationSummary::kCallOnSlowPath;
3075 break;
3076 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003077
Alexandre Rames67555f72014-11-18 10:55:16 +00003078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003079 if (baker_read_barrier_slow_path) {
3080 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3081 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003082 locations->SetInAt(0, Location::RequiresRegister());
3083 locations->SetInAt(1, Location::RequiresRegister());
3084 // The "out" register is used as a temporary, so it overlaps with the inputs.
3085 // Note that TypeCheckSlowPathARM64 uses this register too.
3086 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3087 // When read barriers are enabled, we need a temporary register for
3088 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003089 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003090 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003091 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003092}
3093
3094void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003095 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003096 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003097 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003098 Register obj = InputRegisterAt(instruction, 0);
3099 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003100 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003101 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003102 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3103 locations->GetTemp(0) :
3104 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003105 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3106 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3107 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3108 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003109
Scott Wakeling97c72b72016-06-24 16:19:36 +01003110 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003111 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003112
3113 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003114 // Avoid null check if we know `obj` is not null.
3115 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003116 __ Cbz(obj, &zero);
3117 }
3118
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003119 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003120 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003121
Roland Levillain44015862016-01-22 11:47:17 +00003122 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003123 case TypeCheckKind::kExactCheck: {
3124 __ Cmp(out, cls);
3125 __ Cset(out, eq);
3126 if (zero.IsLinked()) {
3127 __ B(&done);
3128 }
3129 break;
3130 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003131
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003132 case TypeCheckKind::kAbstractClassCheck: {
3133 // If the class is abstract, we eagerly fetch the super class of the
3134 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003135 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003136 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003137 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003138 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 // If `out` is null, we use it for the result, and jump to `done`.
3140 __ Cbz(out, &done);
3141 __ Cmp(out, cls);
3142 __ B(ne, &loop);
3143 __ Mov(out, 1);
3144 if (zero.IsLinked()) {
3145 __ B(&done);
3146 }
3147 break;
3148 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003149
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003150 case TypeCheckKind::kClassHierarchyCheck: {
3151 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003152 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003153 __ Bind(&loop);
3154 __ Cmp(out, cls);
3155 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003156 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003157 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003158 __ Cbnz(out, &loop);
3159 // If `out` is null, we use it for the result, and jump to `done`.
3160 __ B(&done);
3161 __ Bind(&success);
3162 __ Mov(out, 1);
3163 if (zero.IsLinked()) {
3164 __ B(&done);
3165 }
3166 break;
3167 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003168
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003169 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003170 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003171 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003172 __ Cmp(out, cls);
3173 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003174 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003175 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003176 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003177 // If `out` is null, we use it for the result, and jump to `done`.
3178 __ Cbz(out, &done);
3179 __ Ldrh(out, HeapOperand(out, primitive_offset));
3180 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3181 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003182 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003183 __ Mov(out, 1);
3184 __ B(&done);
3185 break;
3186 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003187
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003188 case TypeCheckKind::kArrayCheck: {
3189 __ Cmp(out, cls);
3190 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003191 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3192 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003193 codegen_->AddSlowPath(slow_path);
3194 __ B(ne, slow_path->GetEntryLabel());
3195 __ Mov(out, 1);
3196 if (zero.IsLinked()) {
3197 __ B(&done);
3198 }
3199 break;
3200 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003201
Calin Juravle98893e12015-10-02 21:05:03 +01003202 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003203 case TypeCheckKind::kInterfaceCheck: {
3204 // Note that we indeed only call on slow path, but we always go
3205 // into the slow path for the unresolved and interface check
3206 // cases.
3207 //
3208 // We cannot directly call the InstanceofNonTrivial runtime
3209 // entry point without resorting to a type checking slow path
3210 // here (i.e. by calling InvokeRuntime directly), as it would
3211 // require to assign fixed registers for the inputs of this
3212 // HInstanceOf instruction (following the runtime calling
3213 // convention), which might be cluttered by the potential first
3214 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003215 //
3216 // TODO: Introduce a new runtime entry point taking the object
3217 // to test (instead of its class) as argument, and let it deal
3218 // with the read barrier issues. This will let us refactor this
3219 // case of the `switch` code as it was previously (with a direct
3220 // call to the runtime not using a type checking slow path).
3221 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003222 DCHECK(locations->OnlyCallsOnSlowPath());
3223 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3224 /* is_fatal */ false);
3225 codegen_->AddSlowPath(slow_path);
3226 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003227 if (zero.IsLinked()) {
3228 __ B(&done);
3229 }
3230 break;
3231 }
3232 }
3233
3234 if (zero.IsLinked()) {
3235 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003236 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 }
3238
3239 if (done.IsLinked()) {
3240 __ Bind(&done);
3241 }
3242
3243 if (slow_path != nullptr) {
3244 __ Bind(slow_path->GetExitLabel());
3245 }
3246}
3247
3248void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3249 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3250 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3251
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003252 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003253 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003254 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003255 case TypeCheckKind::kExactCheck:
3256 case TypeCheckKind::kAbstractClassCheck:
3257 case TypeCheckKind::kClassHierarchyCheck:
3258 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003259 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3260 LocationSummary::kCallOnSlowPath :
3261 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01003262 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003263 break;
3264 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003265 case TypeCheckKind::kUnresolvedCheck:
3266 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003267 call_kind = LocationSummary::kCallOnSlowPath;
3268 break;
3269 }
3270
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003271 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003272 if (baker_read_barrier_slow_path) {
3273 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3274 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003275 locations->SetInAt(0, Location::RequiresRegister());
3276 locations->SetInAt(1, Location::RequiresRegister());
3277 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3278 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003279 // When read barriers are enabled, we need an additional temporary
3280 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003281 if (TypeCheckNeedsATemporary(type_check_kind)) {
3282 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003283 }
3284}
3285
3286void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003287 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003288 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003289 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003290 Register obj = InputRegisterAt(instruction, 0);
3291 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003292 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003293 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3294 locations->GetTemp(1) :
3295 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003296 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003297 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3298 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3299 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3300 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003301
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003302 bool is_type_check_slow_path_fatal =
3303 (type_check_kind == TypeCheckKind::kExactCheck ||
3304 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3305 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3306 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3307 !instruction->CanThrowIntoCatchBlock();
3308 SlowPathCodeARM64* type_check_slow_path =
3309 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3310 is_type_check_slow_path_fatal);
3311 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003312
Scott Wakeling97c72b72016-06-24 16:19:36 +01003313 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003314 // Avoid null check if we know obj is not null.
3315 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003316 __ Cbz(obj, &done);
3317 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003318
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003319 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003320 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003321
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003322 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003323 case TypeCheckKind::kExactCheck:
3324 case TypeCheckKind::kArrayCheck: {
3325 __ Cmp(temp, cls);
3326 // Jump to slow path for throwing the exception or doing a
3327 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003328 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003329 break;
3330 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003332 case TypeCheckKind::kAbstractClassCheck: {
3333 // If the class is abstract, we eagerly fetch the super class of the
3334 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003335 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003336 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003338 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003339
3340 // If the class reference currently in `temp` is not null, jump
3341 // to the `compare_classes` label to compare it with the checked
3342 // class.
3343 __ Cbnz(temp, &compare_classes);
3344 // Otherwise, jump to the slow path to throw the exception.
3345 //
3346 // But before, move back the object's class into `temp` before
3347 // going into the slow path, as it has been overwritten in the
3348 // meantime.
3349 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003350 GenerateReferenceLoadTwoRegisters(
3351 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003352 __ B(type_check_slow_path->GetEntryLabel());
3353
3354 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 __ Cmp(temp, cls);
3356 __ B(ne, &loop);
3357 break;
3358 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003359
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003360 case TypeCheckKind::kClassHierarchyCheck: {
3361 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003362 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003363 __ Bind(&loop);
3364 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003365 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003366
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003367 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003368 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369
3370 // If the class reference currently in `temp` is not null, jump
3371 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003372 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003373 // Otherwise, jump to the slow path to throw the exception.
3374 //
3375 // But before, move back the object's class into `temp` before
3376 // going into the slow path, as it has been overwritten in the
3377 // meantime.
3378 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003379 GenerateReferenceLoadTwoRegisters(
3380 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003381 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003382 break;
3383 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003384
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003385 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003386 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003387 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003388 __ Cmp(temp, cls);
3389 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003390
3391 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003393 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394
3395 // If the component type is not null (i.e. the object is indeed
3396 // an array), jump to label `check_non_primitive_component_type`
3397 // to further check that this component type is not a primitive
3398 // type.
3399 __ Cbnz(temp, &check_non_primitive_component_type);
3400 // Otherwise, jump to the slow path to throw the exception.
3401 //
3402 // But before, move back the object's class into `temp` before
3403 // going into the slow path, as it has been overwritten in the
3404 // meantime.
3405 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003406 GenerateReferenceLoadTwoRegisters(
3407 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003408 __ B(type_check_slow_path->GetEntryLabel());
3409
3410 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003411 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3412 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 __ Cbz(temp, &done);
3414 // Same comment as above regarding `temp` and the slow path.
3415 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003416 GenerateReferenceLoadTwoRegisters(
3417 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003418 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003419 break;
3420 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003421
Calin Juravle98893e12015-10-02 21:05:03 +01003422 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003424 // We always go into the type check slow path for the unresolved
3425 // and interface check cases.
3426 //
3427 // We cannot directly call the CheckCast runtime entry point
3428 // without resorting to a type checking slow path here (i.e. by
3429 // calling InvokeRuntime directly), as it would require to
3430 // assign fixed registers for the inputs of this HInstanceOf
3431 // instruction (following the runtime calling convention), which
3432 // might be cluttered by the potential first read barrier
3433 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003434 //
3435 // TODO: Introduce a new runtime entry point taking the object
3436 // to test (instead of its class) as argument, and let it deal
3437 // with the read barrier issues. This will let us refactor this
3438 // case of the `switch` code as it was previously (with a direct
3439 // call to the runtime not using a type checking slow path).
3440 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003442 break;
3443 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003444 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003445
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003446 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003447}
3448
Alexandre Rames5319def2014-10-23 10:03:10 +01003449void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3451 locations->SetOut(Location::ConstantLocation(constant));
3452}
3453
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003454void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003455 // Will be generated at use site.
3456}
3457
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003458void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3460 locations->SetOut(Location::ConstantLocation(constant));
3461}
3462
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003463void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003464 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003465}
3466
Calin Juravle175dc732015-08-25 15:42:32 +01003467void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3468 // The trampoline uses the same calling convention as dex calling conventions,
3469 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3470 // the method_idx.
3471 HandleInvoke(invoke);
3472}
3473
3474void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3475 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3476}
3477
Alexandre Rames5319def2014-10-23 10:03:10 +01003478void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003479 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003480 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003481}
3482
Alexandre Rames67555f72014-11-18 10:55:16 +00003483void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3484 HandleInvoke(invoke);
3485}
3486
3487void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3488 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003489 LocationSummary* locations = invoke->GetLocations();
3490 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003491 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003492 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003493 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003494
3495 // The register ip1 is required to be used for the hidden argument in
3496 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003497 MacroAssembler* masm = GetVIXLAssembler();
3498 UseScratchRegisterScope scratch_scope(masm);
3499 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003500 scratch_scope.Exclude(ip1);
3501 __ Mov(ip1, invoke->GetDexMethodIndex());
3502
Alexandre Rames67555f72014-11-18 10:55:16 +00003503 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003504 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003505 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003506 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003507 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003508 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003509 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003510 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003511 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003512 // Instead of simply (possibly) unpoisoning `temp` here, we should
3513 // emit a read barrier for the previous class reference load.
3514 // However this is not required in practice, as this is an
3515 // intermediate/temporary reference and because the current
3516 // concurrent copying collector keeps the from-space memory
3517 // intact/accessible until the end of the marking phase (the
3518 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003519 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003520 __ Ldr(temp,
3521 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3522 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003523 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003524 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003525 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003526 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003527 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003528 // lr();
3529 __ Blr(lr);
3530 DCHECK(!codegen_->IsLeafMethod());
3531 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3532}
3533
3534void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003535 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3536 if (intrinsic.TryDispatch(invoke)) {
3537 return;
3538 }
3539
Alexandre Rames67555f72014-11-18 10:55:16 +00003540 HandleInvoke(invoke);
3541}
3542
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003543void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003544 // Explicit clinit checks triggered by static invokes must have been pruned by
3545 // art::PrepareForRegisterAllocation.
3546 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003547
Andreas Gampe878d58c2015-01-15 23:24:00 -08003548 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3549 if (intrinsic.TryDispatch(invoke)) {
3550 return;
3551 }
3552
Alexandre Rames67555f72014-11-18 10:55:16 +00003553 HandleInvoke(invoke);
3554}
3555
Andreas Gampe878d58c2015-01-15 23:24:00 -08003556static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3557 if (invoke->GetLocations()->Intrinsified()) {
3558 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3559 intrinsic.Dispatch(invoke);
3560 return true;
3561 }
3562 return false;
3563}
3564
Vladimir Markodc151b22015-10-15 18:02:30 +01003565HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3566 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3567 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003568 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003569 return desired_dispatch_info;
3570}
3571
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003572void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003573 // For better instruction scheduling we load the direct code pointer before the method pointer.
3574 bool direct_code_loaded = false;
3575 switch (invoke->GetCodePtrLocation()) {
3576 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3577 // LR = code address from literal pool with link-time patch.
3578 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3579 direct_code_loaded = true;
3580 break;
3581 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3582 // LR = invoke->GetDirectCodePtr();
3583 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3584 direct_code_loaded = true;
3585 break;
3586 default:
3587 break;
3588 }
3589
Andreas Gampe878d58c2015-01-15 23:24:00 -08003590 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003591 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3592 switch (invoke->GetMethodLoadKind()) {
3593 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3594 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003595 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003596 break;
3597 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003598 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003599 break;
3600 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3601 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003602 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003603 break;
3604 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3605 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003606 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003607 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3608 break;
3609 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3610 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003611 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3612 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003613 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003614 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003615 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003616 __ Bind(adrp_label);
3617 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003618 }
Vladimir Marko58155012015-08-19 12:49:41 +00003619 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003620 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003621 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003622 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003623 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003624 __ Bind(ldr_label);
3625 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003626 }
Vladimir Marko58155012015-08-19 12:49:41 +00003627 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003628 }
Vladimir Marko58155012015-08-19 12:49:41 +00003629 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003630 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003631 Register reg = XRegisterFrom(temp);
3632 Register method_reg;
3633 if (current_method.IsRegister()) {
3634 method_reg = XRegisterFrom(current_method);
3635 } else {
3636 DCHECK(invoke->GetLocations()->Intrinsified());
3637 DCHECK(!current_method.IsValid());
3638 method_reg = reg;
3639 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3640 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003641
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003642 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003643 __ Ldr(reg.X(),
3644 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003645 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003646 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003647 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3648 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003649 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3650 break;
3651 }
3652 }
3653
3654 switch (invoke->GetCodePtrLocation()) {
3655 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3656 __ Bl(&frame_entry_label_);
3657 break;
3658 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3659 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003660 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3661 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003662 __ Bind(label);
3663 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003664 break;
3665 }
3666 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3667 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3668 // LR prepared above for better instruction scheduling.
3669 DCHECK(direct_code_loaded);
3670 // lr()
3671 __ Blr(lr);
3672 break;
3673 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3674 // LR = callee_method->entry_point_from_quick_compiled_code_;
3675 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003676 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003677 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003678 // lr()
3679 __ Blr(lr);
3680 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003681 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003682
Andreas Gampe878d58c2015-01-15 23:24:00 -08003683 DCHECK(!IsLeafMethod());
3684}
3685
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003686void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003687 // Use the calling convention instead of the location of the receiver, as
3688 // intrinsics may have put the receiver in a different register. In the intrinsics
3689 // slow path, the arguments have been moved to the right place, so here we are
3690 // guaranteed that the receiver is the first register of the calling convention.
3691 InvokeDexCallingConvention calling_convention;
3692 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003693 Register temp = XRegisterFrom(temp_in);
3694 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3695 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3696 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003697 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003698
3699 BlockPoolsScope block_pools(GetVIXLAssembler());
3700
3701 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003702 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003703 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003704 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003705 // Instead of simply (possibly) unpoisoning `temp` here, we should
3706 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003707 // intermediate/temporary reference and because the current
3708 // concurrent copying collector keeps the from-space memory
3709 // intact/accessible until the end of the marking phase (the
3710 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003711 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3712 // temp = temp->GetMethodAt(method_offset);
3713 __ Ldr(temp, MemOperand(temp, method_offset));
3714 // lr = temp->GetEntryPoint();
3715 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3716 // lr();
3717 __ Blr(lr);
3718}
3719
Scott Wakeling97c72b72016-06-24 16:19:36 +01003720vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3721 const DexFile& dex_file,
3722 uint32_t string_index,
3723 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003724 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3725}
3726
Scott Wakeling97c72b72016-06-24 16:19:36 +01003727vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3728 const DexFile& dex_file,
3729 uint32_t type_index,
3730 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003731 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3732}
3733
Scott Wakeling97c72b72016-06-24 16:19:36 +01003734vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3735 const DexFile& dex_file,
3736 uint32_t element_offset,
3737 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003738 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3739}
3740
Scott Wakeling97c72b72016-06-24 16:19:36 +01003741vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3742 const DexFile& dex_file,
3743 uint32_t offset_or_index,
3744 vixl::aarch64::Label* adrp_label,
3745 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003746 // Add a patch entry and return the label.
3747 patches->emplace_back(dex_file, offset_or_index);
3748 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003749 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003750 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3751 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3752 return label;
3753}
3754
Scott Wakeling97c72b72016-06-24 16:19:36 +01003755vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003756 const DexFile& dex_file, uint32_t string_index) {
3757 return boot_image_string_patches_.GetOrCreate(
3758 StringReference(&dex_file, string_index),
3759 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3760}
3761
Scott Wakeling97c72b72016-06-24 16:19:36 +01003762vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003763 const DexFile& dex_file, uint32_t type_index) {
3764 return boot_image_type_patches_.GetOrCreate(
3765 TypeReference(&dex_file, type_index),
3766 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3767}
3768
Scott Wakeling97c72b72016-06-24 16:19:36 +01003769vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3770 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003771 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3772 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3773 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3774}
3775
Scott Wakeling97c72b72016-06-24 16:19:36 +01003776vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3777 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003778 return DeduplicateUint64Literal(address);
3779}
3780
Vladimir Marko58155012015-08-19 12:49:41 +00003781void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3782 DCHECK(linker_patches->empty());
3783 size_t size =
3784 method_patches_.size() +
3785 call_patches_.size() +
3786 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003787 pc_relative_dex_cache_patches_.size() +
3788 boot_image_string_patches_.size() +
3789 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003790 boot_image_type_patches_.size() +
3791 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003792 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003793 linker_patches->reserve(size);
3794 for (const auto& entry : method_patches_) {
3795 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003796 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3797 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003798 target_method.dex_file,
3799 target_method.dex_method_index));
3800 }
3801 for (const auto& entry : call_patches_) {
3802 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003803 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3804 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003805 target_method.dex_file,
3806 target_method.dex_method_index));
3807 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003808 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3809 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003810 info.target_method.dex_file,
3811 info.target_method.dex_method_index));
3812 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003813 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003814 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003815 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003816 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003817 info.offset_or_index));
3818 }
3819 for (const auto& entry : boot_image_string_patches_) {
3820 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003821 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3822 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003823 target_string.dex_file,
3824 target_string.string_index));
3825 }
3826 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003827 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003828 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003829 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003830 info.offset_or_index));
3831 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003832 for (const auto& entry : boot_image_type_patches_) {
3833 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003834 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3835 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003836 target_type.dex_file,
3837 target_type.type_index));
3838 }
3839 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003840 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003841 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003842 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003843 info.offset_or_index));
3844 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003845 for (const auto& entry : boot_image_address_patches_) {
3846 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003847 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3848 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003849 }
3850}
3851
Scott Wakeling97c72b72016-06-24 16:19:36 +01003852vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003853 Uint32ToLiteralMap* map) {
3854 return map->GetOrCreate(
3855 value,
3856 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3857}
3858
Scott Wakeling97c72b72016-06-24 16:19:36 +01003859vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003860 return uint64_literals_.GetOrCreate(
3861 value,
3862 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003863}
3864
Scott Wakeling97c72b72016-06-24 16:19:36 +01003865vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003866 MethodReference target_method,
3867 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003868 return map->GetOrCreate(
3869 target_method,
3870 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003871}
3872
Scott Wakeling97c72b72016-06-24 16:19:36 +01003873vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003874 MethodReference target_method) {
3875 return DeduplicateMethodLiteral(target_method, &method_patches_);
3876}
3877
Scott Wakeling97c72b72016-06-24 16:19:36 +01003878vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003879 MethodReference target_method) {
3880 return DeduplicateMethodLiteral(target_method, &call_patches_);
3881}
3882
3883
Andreas Gampe878d58c2015-01-15 23:24:00 -08003884void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003885 // Explicit clinit checks triggered by static invokes must have been pruned by
3886 // art::PrepareForRegisterAllocation.
3887 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003888
Andreas Gampe878d58c2015-01-15 23:24:00 -08003889 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3890 return;
3891 }
3892
Alexandre Ramesd921d642015-04-16 15:07:16 +01003893 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003894 LocationSummary* locations = invoke->GetLocations();
3895 codegen_->GenerateStaticOrDirectCall(
3896 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003897 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003898}
3899
3900void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003901 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3902 return;
3903 }
3904
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003905 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003906 DCHECK(!codegen_->IsLeafMethod());
3907 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3908}
3909
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003910HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3911 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003912 switch (desired_class_load_kind) {
3913 case HLoadClass::LoadKind::kReferrersClass:
3914 break;
3915 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3916 DCHECK(!GetCompilerOptions().GetCompilePic());
3917 break;
3918 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3919 DCHECK(GetCompilerOptions().GetCompilePic());
3920 break;
3921 case HLoadClass::LoadKind::kBootImageAddress:
3922 break;
3923 case HLoadClass::LoadKind::kDexCacheAddress:
3924 DCHECK(Runtime::Current()->UseJitCompilation());
3925 break;
3926 case HLoadClass::LoadKind::kDexCachePcRelative:
3927 DCHECK(!Runtime::Current()->UseJitCompilation());
3928 break;
3929 case HLoadClass::LoadKind::kDexCacheViaMethod:
3930 break;
3931 }
3932 return desired_class_load_kind;
3933}
3934
Alexandre Rames67555f72014-11-18 10:55:16 +00003935void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003936 if (cls->NeedsAccessCheck()) {
3937 InvokeRuntimeCallingConvention calling_convention;
3938 CodeGenerator::CreateLoadClassLocationSummary(
3939 cls,
3940 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003941 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003942 /* code_generator_supports_read_barrier */ true);
3943 return;
3944 }
3945
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003946 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
3947 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003948 ? LocationSummary::kCallOnSlowPath
3949 : LocationSummary::kNoCall;
3950 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003951 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003952 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3953 }
3954
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003955 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3956 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3957 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3958 locations->SetInAt(0, Location::RequiresRegister());
3959 }
3960 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003961}
3962
3963void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003964 if (cls->NeedsAccessCheck()) {
3965 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00003966 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003967 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003968 return;
3969 }
3970
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003971 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003972 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00003973
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003974 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003975 bool generate_null_check = false;
3976 switch (cls->GetLoadKind()) {
3977 case HLoadClass::LoadKind::kReferrersClass: {
3978 DCHECK(!cls->CanCallRuntime());
3979 DCHECK(!cls->MustGenerateClinitCheck());
3980 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3981 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003982 GenerateGcRootFieldLoad(cls,
3983 out_loc,
3984 current_method,
3985 ArtMethod::DeclaringClassOffset().Int32Value(),
3986 /*fixup_label*/ nullptr,
3987 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003988 break;
3989 }
3990 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003991 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003992 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3993 cls->GetTypeIndex()));
3994 break;
3995 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003996 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003997 // Add ADRP with its PC-relative type patch.
3998 const DexFile& dex_file = cls->GetDexFile();
3999 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004000 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004001 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004002 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004003 __ Bind(adrp_label);
4004 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004005 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004006 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004007 vixl::aarch64::Label* add_label =
4008 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004009 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004010 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004011 __ Bind(add_label);
4012 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004013 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004014 break;
4015 }
4016 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004017 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004018 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4019 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4020 break;
4021 }
4022 case HLoadClass::LoadKind::kDexCacheAddress: {
4023 DCHECK_NE(cls->GetAddress(), 0u);
4024 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4025 // that gives a 16KiB range. To try and reduce the number of literals if we load
4026 // multiple types, simply split the dex cache address to a 16KiB aligned base
4027 // loaded from a literal and the remaining offset embedded in the load.
4028 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4029 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4030 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4031 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4032 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4033 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4034 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004035 GenerateGcRootFieldLoad(cls,
4036 out_loc,
4037 out.X(),
4038 offset,
4039 /*fixup_label*/ nullptr,
4040 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004041 generate_null_check = !cls->IsInDexCache();
4042 break;
4043 }
4044 case HLoadClass::LoadKind::kDexCachePcRelative: {
4045 // Add ADRP with its PC-relative DexCache access patch.
4046 const DexFile& dex_file = cls->GetDexFile();
4047 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004048 vixl::aarch64::Label* adrp_label =
4049 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004050 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004051 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004052 __ Bind(adrp_label);
4053 __ adrp(out.X(), /* offset placeholder */ 0);
4054 }
4055 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004056 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004057 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4058 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004059 GenerateGcRootFieldLoad(cls,
4060 out_loc,
4061 out.X(),
4062 /* offset placeholder */ 0,
4063 ldr_label,
4064 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004065 generate_null_check = !cls->IsInDexCache();
4066 break;
4067 }
4068 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4069 MemberOffset resolved_types_offset =
4070 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4071 // /* GcRoot<mirror::Class>[] */ out =
4072 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4073 Register current_method = InputRegisterAt(cls, 0);
4074 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4075 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004076 GenerateGcRootFieldLoad(cls,
4077 out_loc,
4078 out.X(),
4079 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4080 /*fixup_label*/ nullptr,
4081 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004082 generate_null_check = !cls->IsInDexCache();
4083 break;
4084 }
4085 }
4086
4087 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4088 DCHECK(cls->CanCallRuntime());
4089 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4090 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4091 codegen_->AddSlowPath(slow_path);
4092 if (generate_null_check) {
4093 __ Cbz(out, slow_path->GetEntryLabel());
4094 }
4095 if (cls->MustGenerateClinitCheck()) {
4096 GenerateClassInitializationCheck(slow_path, out);
4097 } else {
4098 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004099 }
4100 }
4101}
4102
David Brazdilcb1c0552015-08-04 16:22:25 +01004103static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004104 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004105}
4106
Alexandre Rames67555f72014-11-18 10:55:16 +00004107void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4108 LocationSummary* locations =
4109 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4110 locations->SetOut(Location::RequiresRegister());
4111}
4112
4113void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004114 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4115}
4116
4117void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4118 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4119}
4120
4121void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4122 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004123}
4124
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004125HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4126 HLoadString::LoadKind desired_string_load_kind) {
4127 if (kEmitCompilerReadBarrier) {
4128 switch (desired_string_load_kind) {
4129 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4130 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4131 case HLoadString::LoadKind::kBootImageAddress:
4132 // TODO: Implement for read barrier.
4133 return HLoadString::LoadKind::kDexCacheViaMethod;
4134 default:
4135 break;
4136 }
4137 }
4138 switch (desired_string_load_kind) {
4139 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4140 DCHECK(!GetCompilerOptions().GetCompilePic());
4141 break;
4142 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4143 DCHECK(GetCompilerOptions().GetCompilePic());
4144 break;
4145 case HLoadString::LoadKind::kBootImageAddress:
4146 break;
4147 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004148 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004149 break;
4150 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004151 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004152 break;
4153 case HLoadString::LoadKind::kDexCacheViaMethod:
4154 break;
4155 }
4156 return desired_string_load_kind;
4157}
4158
Alexandre Rames67555f72014-11-18 10:55:16 +00004159void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004160 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4161 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004162 : LocationSummary::kNoCall;
4163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004164 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4165 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004166 InvokeRuntimeCallingConvention calling_convention;
4167 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4168 } else {
4169 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004170 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004171}
4172
4173void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004174 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004175
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004176 switch (load->GetLoadKind()) {
4177 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4178 DCHECK(!kEmitCompilerReadBarrier);
4179 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4180 load->GetStringIndex()));
4181 return; // No dex cache slow path.
4182 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4183 DCHECK(!kEmitCompilerReadBarrier);
4184 // Add ADRP with its PC-relative String patch.
4185 const DexFile& dex_file = load->GetDexFile();
4186 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004187 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004188 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004189 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004190 __ Bind(adrp_label);
4191 __ adrp(out.X(), /* offset placeholder */ 0);
4192 }
4193 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004194 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004195 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4196 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004197 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004198 __ Bind(add_label);
4199 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4200 }
4201 return; // No dex cache slow path.
4202 }
4203 case HLoadString::LoadKind::kBootImageAddress: {
4204 DCHECK(!kEmitCompilerReadBarrier);
4205 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4206 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4207 return; // No dex cache slow path.
4208 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004209 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004210 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004211 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004213 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004214 InvokeRuntimeCallingConvention calling_convention;
4215 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4216 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4217 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004218}
4219
Alexandre Rames5319def2014-10-23 10:03:10 +01004220void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4221 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4222 locations->SetOut(Location::ConstantLocation(constant));
4223}
4224
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004225void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004226 // Will be generated at use site.
4227}
4228
Alexandre Rames67555f72014-11-18 10:55:16 +00004229void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4230 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004232 InvokeRuntimeCallingConvention calling_convention;
4233 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4234}
4235
4236void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004237 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4238 instruction,
4239 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004240 if (instruction->IsEnter()) {
4241 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4242 } else {
4243 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4244 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004245}
4246
Alexandre Rames42d641b2014-10-27 14:00:51 +00004247void LocationsBuilderARM64::VisitMul(HMul* mul) {
4248 LocationSummary* locations =
4249 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4250 switch (mul->GetResultType()) {
4251 case Primitive::kPrimInt:
4252 case Primitive::kPrimLong:
4253 locations->SetInAt(0, Location::RequiresRegister());
4254 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004255 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004256 break;
4257
4258 case Primitive::kPrimFloat:
4259 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004260 locations->SetInAt(0, Location::RequiresFpuRegister());
4261 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004262 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004263 break;
4264
4265 default:
4266 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4267 }
4268}
4269
4270void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4271 switch (mul->GetResultType()) {
4272 case Primitive::kPrimInt:
4273 case Primitive::kPrimLong:
4274 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4275 break;
4276
4277 case Primitive::kPrimFloat:
4278 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004279 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004280 break;
4281
4282 default:
4283 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4284 }
4285}
4286
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004287void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4288 LocationSummary* locations =
4289 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4290 switch (neg->GetResultType()) {
4291 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004292 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004293 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004295 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004296
4297 case Primitive::kPrimFloat:
4298 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004299 locations->SetInAt(0, Location::RequiresFpuRegister());
4300 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004301 break;
4302
4303 default:
4304 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4305 }
4306}
4307
4308void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4309 switch (neg->GetResultType()) {
4310 case Primitive::kPrimInt:
4311 case Primitive::kPrimLong:
4312 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4313 break;
4314
4315 case Primitive::kPrimFloat:
4316 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004317 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004318 break;
4319
4320 default:
4321 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4322 }
4323}
4324
4325void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4326 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004328 InvokeRuntimeCallingConvention calling_convention;
4329 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004330 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004331 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004332 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004333}
4334
4335void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4336 LocationSummary* locations = instruction->GetLocations();
4337 InvokeRuntimeCallingConvention calling_convention;
4338 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4339 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004340 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004341 // Note: if heap poisoning is enabled, the entry point takes cares
4342 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004343 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004344 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004345}
4346
Alexandre Rames5319def2014-10-23 10:03:10 +01004347void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4348 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004349 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004350 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004351 if (instruction->IsStringAlloc()) {
4352 locations->AddTemp(LocationFrom(kArtMethodRegister));
4353 } else {
4354 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4355 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4356 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004357 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4358}
4359
4360void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004361 // Note: if heap poisoning is enabled, the entry point takes cares
4362 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004363 if (instruction->IsStringAlloc()) {
4364 // String is allocated through StringFactory. Call NewEmptyString entry point.
4365 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004366 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004367 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4368 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4369 __ Blr(lr);
4370 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4371 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004372 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004373 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4374 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004375}
4376
4377void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004379 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004381}
4382
4383void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004384 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004385 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004386 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004387 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004388 break;
4389
4390 default:
4391 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4392 }
4393}
4394
David Brazdil66d126e2015-04-03 16:02:44 +01004395void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4396 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4397 locations->SetInAt(0, Location::RequiresRegister());
4398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4399}
4400
4401void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004402 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004403}
4404
Alexandre Rames5319def2014-10-23 10:03:10 +01004405void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004406 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4407 ? LocationSummary::kCallOnSlowPath
4408 : LocationSummary::kNoCall;
4409 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004410 locations->SetInAt(0, Location::RequiresRegister());
4411 if (instruction->HasUses()) {
4412 locations->SetOut(Location::SameAsFirstInput());
4413 }
4414}
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);
4699 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // 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 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005000 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005001 __ Bind(fixup_label);
5002 __ ldr(root_reg, MemOperand(obj, offset));
5003 }
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 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005031 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005032 __ Bind(fixup_label);
5033 __ add(root_reg.X(), obj.X(), offset);
5034 }
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 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005044 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005045 __ Bind(fixup_label);
5046 __ ldr(root_reg, MemOperand(obj, offset));
5047 }
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