blob: 599185acd387a15a74f5f75465a5d70545f0d691 [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"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100451 bool IsFatal() const OVERRIDE { 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();
3253 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003254 case TypeCheckKind::kExactCheck:
3255 case TypeCheckKind::kAbstractClassCheck:
3256 case TypeCheckKind::kClassHierarchyCheck:
3257 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003258 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3259 LocationSummary::kCallOnSlowPath :
3260 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003261 break;
3262 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003263 case TypeCheckKind::kUnresolvedCheck:
3264 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003265 call_kind = LocationSummary::kCallOnSlowPath;
3266 break;
3267 }
3268
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3270 locations->SetInAt(0, Location::RequiresRegister());
3271 locations->SetInAt(1, Location::RequiresRegister());
3272 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3273 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003274 // When read barriers are enabled, we need an additional temporary
3275 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003276 if (TypeCheckNeedsATemporary(type_check_kind)) {
3277 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003278 }
3279}
3280
3281void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003282 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003283 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003284 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003285 Register obj = InputRegisterAt(instruction, 0);
3286 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003287 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003288 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3289 locations->GetTemp(1) :
3290 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003291 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003292 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3293 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3294 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3295 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003296
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003297 bool is_type_check_slow_path_fatal =
3298 (type_check_kind == TypeCheckKind::kExactCheck ||
3299 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3300 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3301 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3302 !instruction->CanThrowIntoCatchBlock();
3303 SlowPathCodeARM64* type_check_slow_path =
3304 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3305 is_type_check_slow_path_fatal);
3306 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003307
Scott Wakeling97c72b72016-06-24 16:19:36 +01003308 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003309 // Avoid null check if we know obj is not null.
3310 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003311 __ Cbz(obj, &done);
3312 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003313
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003314 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003315 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003316
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003317 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003318 case TypeCheckKind::kExactCheck:
3319 case TypeCheckKind::kArrayCheck: {
3320 __ Cmp(temp, cls);
3321 // Jump to slow path for throwing the exception or doing a
3322 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003323 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003324 break;
3325 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003326
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003327 case TypeCheckKind::kAbstractClassCheck: {
3328 // If the class is abstract, we eagerly fetch the super class of the
3329 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003330 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003331 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003332 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003333 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003334
3335 // If the class reference currently in `temp` is not null, jump
3336 // to the `compare_classes` label to compare it with the checked
3337 // class.
3338 __ Cbnz(temp, &compare_classes);
3339 // Otherwise, jump to the slow path to throw the exception.
3340 //
3341 // But before, move back the object's class into `temp` before
3342 // going into the slow path, as it has been overwritten in the
3343 // meantime.
3344 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003345 GenerateReferenceLoadTwoRegisters(
3346 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003347 __ B(type_check_slow_path->GetEntryLabel());
3348
3349 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003350 __ Cmp(temp, cls);
3351 __ B(ne, &loop);
3352 break;
3353 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 case TypeCheckKind::kClassHierarchyCheck: {
3356 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003357 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003358 __ Bind(&loop);
3359 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003360 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003361
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003362 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003363 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003364
3365 // If the class reference currently in `temp` is not null, jump
3366 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003367 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 // Otherwise, jump to the slow path to throw the exception.
3369 //
3370 // But before, move back the object's class into `temp` before
3371 // going into the slow path, as it has been overwritten in the
3372 // meantime.
3373 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003374 GenerateReferenceLoadTwoRegisters(
3375 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003377 break;
3378 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003379
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003380 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003381 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003382 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003383 __ Cmp(temp, cls);
3384 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003385
3386 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003387 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003388 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389
3390 // If the component type is not null (i.e. the object is indeed
3391 // an array), jump to label `check_non_primitive_component_type`
3392 // to further check that this component type is not a primitive
3393 // type.
3394 __ Cbnz(temp, &check_non_primitive_component_type);
3395 // Otherwise, jump to the slow path to throw the exception.
3396 //
3397 // But before, move back the object's class into `temp` before
3398 // going into the slow path, as it has been overwritten in the
3399 // meantime.
3400 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003401 GenerateReferenceLoadTwoRegisters(
3402 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003403 __ B(type_check_slow_path->GetEntryLabel());
3404
3405 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003406 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3407 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003408 __ Cbz(temp, &done);
3409 // Same comment as above regarding `temp` and the slow path.
3410 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003411 GenerateReferenceLoadTwoRegisters(
3412 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 break;
3415 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003416
Calin Juravle98893e12015-10-02 21:05:03 +01003417 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003419 // We always go into the type check slow path for the unresolved
3420 // and interface check cases.
3421 //
3422 // We cannot directly call the CheckCast runtime entry point
3423 // without resorting to a type checking slow path here (i.e. by
3424 // calling InvokeRuntime directly), as it would require to
3425 // assign fixed registers for the inputs of this HInstanceOf
3426 // instruction (following the runtime calling convention), which
3427 // might be cluttered by the potential first read barrier
3428 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003429 //
3430 // TODO: Introduce a new runtime entry point taking the object
3431 // to test (instead of its class) as argument, and let it deal
3432 // with the read barrier issues. This will let us refactor this
3433 // case of the `switch` code as it was previously (with a direct
3434 // call to the runtime not using a type checking slow path).
3435 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003436 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003437 break;
3438 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003439 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003440
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003442}
3443
Alexandre Rames5319def2014-10-23 10:03:10 +01003444void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3446 locations->SetOut(Location::ConstantLocation(constant));
3447}
3448
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003449void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003450 // Will be generated at use site.
3451}
3452
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003453void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3455 locations->SetOut(Location::ConstantLocation(constant));
3456}
3457
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003458void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003459 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003460}
3461
Calin Juravle175dc732015-08-25 15:42:32 +01003462void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3463 // The trampoline uses the same calling convention as dex calling conventions,
3464 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3465 // the method_idx.
3466 HandleInvoke(invoke);
3467}
3468
3469void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3470 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3471}
3472
Alexandre Rames5319def2014-10-23 10:03:10 +01003473void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003474 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003475 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003476}
3477
Alexandre Rames67555f72014-11-18 10:55:16 +00003478void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3479 HandleInvoke(invoke);
3480}
3481
3482void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3483 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003484 LocationSummary* locations = invoke->GetLocations();
3485 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003486 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003487 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003488 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003489
3490 // The register ip1 is required to be used for the hidden argument in
3491 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003492 MacroAssembler* masm = GetVIXLAssembler();
3493 UseScratchRegisterScope scratch_scope(masm);
3494 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003495 scratch_scope.Exclude(ip1);
3496 __ Mov(ip1, invoke->GetDexMethodIndex());
3497
Alexandre Rames67555f72014-11-18 10:55:16 +00003498 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003499 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003500 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003501 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003502 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003503 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003504 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003505 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003506 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003507 // Instead of simply (possibly) unpoisoning `temp` here, we should
3508 // emit a read barrier for the previous class reference load.
3509 // However this is not required in practice, as this is an
3510 // intermediate/temporary reference and because the current
3511 // concurrent copying collector keeps the from-space memory
3512 // intact/accessible until the end of the marking phase (the
3513 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003514 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003515 __ Ldr(temp,
3516 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3517 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003518 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003519 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003520 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003521 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003522 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003523 // lr();
3524 __ Blr(lr);
3525 DCHECK(!codegen_->IsLeafMethod());
3526 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3527}
3528
3529void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003530 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3531 if (intrinsic.TryDispatch(invoke)) {
3532 return;
3533 }
3534
Alexandre Rames67555f72014-11-18 10:55:16 +00003535 HandleInvoke(invoke);
3536}
3537
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003538void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003539 // Explicit clinit checks triggered by static invokes must have been pruned by
3540 // art::PrepareForRegisterAllocation.
3541 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003542
Andreas Gampe878d58c2015-01-15 23:24:00 -08003543 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3544 if (intrinsic.TryDispatch(invoke)) {
3545 return;
3546 }
3547
Alexandre Rames67555f72014-11-18 10:55:16 +00003548 HandleInvoke(invoke);
3549}
3550
Andreas Gampe878d58c2015-01-15 23:24:00 -08003551static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3552 if (invoke->GetLocations()->Intrinsified()) {
3553 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3554 intrinsic.Dispatch(invoke);
3555 return true;
3556 }
3557 return false;
3558}
3559
Vladimir Markodc151b22015-10-15 18:02:30 +01003560HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3561 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3562 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003563 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003564 return desired_dispatch_info;
3565}
3566
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003567void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003568 // For better instruction scheduling we load the direct code pointer before the method pointer.
3569 bool direct_code_loaded = false;
3570 switch (invoke->GetCodePtrLocation()) {
3571 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3572 // LR = code address from literal pool with link-time patch.
3573 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3574 direct_code_loaded = true;
3575 break;
3576 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3577 // LR = invoke->GetDirectCodePtr();
3578 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3579 direct_code_loaded = true;
3580 break;
3581 default:
3582 break;
3583 }
3584
Andreas Gampe878d58c2015-01-15 23:24:00 -08003585 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003586 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3587 switch (invoke->GetMethodLoadKind()) {
3588 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3589 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003590 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003591 break;
3592 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003593 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003594 break;
3595 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3596 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003597 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003598 break;
3599 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3600 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003601 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003602 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3603 break;
3604 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3605 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003606 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3607 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003608 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003609 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003610 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003611 __ Bind(adrp_label);
3612 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003613 }
Vladimir Marko58155012015-08-19 12:49:41 +00003614 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003615 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003616 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003617 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003618 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003619 __ Bind(ldr_label);
3620 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003621 }
Vladimir Marko58155012015-08-19 12:49:41 +00003622 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003623 }
Vladimir Marko58155012015-08-19 12:49:41 +00003624 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003625 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003626 Register reg = XRegisterFrom(temp);
3627 Register method_reg;
3628 if (current_method.IsRegister()) {
3629 method_reg = XRegisterFrom(current_method);
3630 } else {
3631 DCHECK(invoke->GetLocations()->Intrinsified());
3632 DCHECK(!current_method.IsValid());
3633 method_reg = reg;
3634 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3635 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003636
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003637 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003638 __ Ldr(reg.X(),
3639 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003640 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003641 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003642 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3643 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003644 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3645 break;
3646 }
3647 }
3648
3649 switch (invoke->GetCodePtrLocation()) {
3650 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3651 __ Bl(&frame_entry_label_);
3652 break;
3653 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3654 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003655 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3656 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003657 __ Bind(label);
3658 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003659 break;
3660 }
3661 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3662 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3663 // LR prepared above for better instruction scheduling.
3664 DCHECK(direct_code_loaded);
3665 // lr()
3666 __ Blr(lr);
3667 break;
3668 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3669 // LR = callee_method->entry_point_from_quick_compiled_code_;
3670 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003671 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003672 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003673 // lr()
3674 __ Blr(lr);
3675 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003676 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003677
Andreas Gampe878d58c2015-01-15 23:24:00 -08003678 DCHECK(!IsLeafMethod());
3679}
3680
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003681void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003682 // Use the calling convention instead of the location of the receiver, as
3683 // intrinsics may have put the receiver in a different register. In the intrinsics
3684 // slow path, the arguments have been moved to the right place, so here we are
3685 // guaranteed that the receiver is the first register of the calling convention.
3686 InvokeDexCallingConvention calling_convention;
3687 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003688 Register temp = XRegisterFrom(temp_in);
3689 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3690 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3691 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003692 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003693
3694 BlockPoolsScope block_pools(GetVIXLAssembler());
3695
3696 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003697 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003698 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003699 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003700 // Instead of simply (possibly) unpoisoning `temp` here, we should
3701 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003702 // intermediate/temporary reference and because the current
3703 // concurrent copying collector keeps the from-space memory
3704 // intact/accessible until the end of the marking phase (the
3705 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003706 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3707 // temp = temp->GetMethodAt(method_offset);
3708 __ Ldr(temp, MemOperand(temp, method_offset));
3709 // lr = temp->GetEntryPoint();
3710 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3711 // lr();
3712 __ Blr(lr);
3713}
3714
Scott Wakeling97c72b72016-06-24 16:19:36 +01003715vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3716 const DexFile& dex_file,
3717 uint32_t string_index,
3718 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003719 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3720}
3721
Scott Wakeling97c72b72016-06-24 16:19:36 +01003722vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3723 const DexFile& dex_file,
3724 uint32_t type_index,
3725 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003726 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3727}
3728
Scott Wakeling97c72b72016-06-24 16:19:36 +01003729vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3730 const DexFile& dex_file,
3731 uint32_t element_offset,
3732 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003733 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3734}
3735
Scott Wakeling97c72b72016-06-24 16:19:36 +01003736vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3737 const DexFile& dex_file,
3738 uint32_t offset_or_index,
3739 vixl::aarch64::Label* adrp_label,
3740 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003741 // Add a patch entry and return the label.
3742 patches->emplace_back(dex_file, offset_or_index);
3743 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003744 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003745 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3746 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3747 return label;
3748}
3749
Scott Wakeling97c72b72016-06-24 16:19:36 +01003750vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003751 const DexFile& dex_file, uint32_t string_index) {
3752 return boot_image_string_patches_.GetOrCreate(
3753 StringReference(&dex_file, string_index),
3754 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3755}
3756
Scott Wakeling97c72b72016-06-24 16:19:36 +01003757vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003758 const DexFile& dex_file, uint32_t type_index) {
3759 return boot_image_type_patches_.GetOrCreate(
3760 TypeReference(&dex_file, type_index),
3761 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3762}
3763
Scott Wakeling97c72b72016-06-24 16:19:36 +01003764vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3765 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003766 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3767 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3768 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3769}
3770
Scott Wakeling97c72b72016-06-24 16:19:36 +01003771vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3772 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003773 return DeduplicateUint64Literal(address);
3774}
3775
Vladimir Marko58155012015-08-19 12:49:41 +00003776void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3777 DCHECK(linker_patches->empty());
3778 size_t size =
3779 method_patches_.size() +
3780 call_patches_.size() +
3781 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003782 pc_relative_dex_cache_patches_.size() +
3783 boot_image_string_patches_.size() +
3784 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003785 boot_image_type_patches_.size() +
3786 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003787 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003788 linker_patches->reserve(size);
3789 for (const auto& entry : method_patches_) {
3790 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003791 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3792 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003793 target_method.dex_file,
3794 target_method.dex_method_index));
3795 }
3796 for (const auto& entry : call_patches_) {
3797 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003798 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3799 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003800 target_method.dex_file,
3801 target_method.dex_method_index));
3802 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003803 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3804 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003805 info.target_method.dex_file,
3806 info.target_method.dex_method_index));
3807 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003808 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003809 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003810 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003811 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003812 info.offset_or_index));
3813 }
3814 for (const auto& entry : boot_image_string_patches_) {
3815 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003816 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3817 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003818 target_string.dex_file,
3819 target_string.string_index));
3820 }
3821 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003822 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003823 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003824 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003825 info.offset_or_index));
3826 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003827 for (const auto& entry : boot_image_type_patches_) {
3828 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003829 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3830 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003831 target_type.dex_file,
3832 target_type.type_index));
3833 }
3834 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003835 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003836 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003837 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003838 info.offset_or_index));
3839 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003840 for (const auto& entry : boot_image_address_patches_) {
3841 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003842 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3843 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003844 }
3845}
3846
Scott Wakeling97c72b72016-06-24 16:19:36 +01003847vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003848 Uint32ToLiteralMap* map) {
3849 return map->GetOrCreate(
3850 value,
3851 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3852}
3853
Scott Wakeling97c72b72016-06-24 16:19:36 +01003854vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003855 return uint64_literals_.GetOrCreate(
3856 value,
3857 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003858}
3859
Scott Wakeling97c72b72016-06-24 16:19:36 +01003860vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003861 MethodReference target_method,
3862 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003863 return map->GetOrCreate(
3864 target_method,
3865 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003866}
3867
Scott Wakeling97c72b72016-06-24 16:19:36 +01003868vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003869 MethodReference target_method) {
3870 return DeduplicateMethodLiteral(target_method, &method_patches_);
3871}
3872
Scott Wakeling97c72b72016-06-24 16:19:36 +01003873vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003874 MethodReference target_method) {
3875 return DeduplicateMethodLiteral(target_method, &call_patches_);
3876}
3877
3878
Andreas Gampe878d58c2015-01-15 23:24:00 -08003879void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003880 // Explicit clinit checks triggered by static invokes must have been pruned by
3881 // art::PrepareForRegisterAllocation.
3882 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003883
Andreas Gampe878d58c2015-01-15 23:24:00 -08003884 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3885 return;
3886 }
3887
Alexandre Ramesd921d642015-04-16 15:07:16 +01003888 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003889 LocationSummary* locations = invoke->GetLocations();
3890 codegen_->GenerateStaticOrDirectCall(
3891 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003892 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003893}
3894
3895void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003896 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3897 return;
3898 }
3899
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003900 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003901 DCHECK(!codegen_->IsLeafMethod());
3902 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3903}
3904
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003905HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3906 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003907 switch (desired_class_load_kind) {
3908 case HLoadClass::LoadKind::kReferrersClass:
3909 break;
3910 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3911 DCHECK(!GetCompilerOptions().GetCompilePic());
3912 break;
3913 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3914 DCHECK(GetCompilerOptions().GetCompilePic());
3915 break;
3916 case HLoadClass::LoadKind::kBootImageAddress:
3917 break;
3918 case HLoadClass::LoadKind::kDexCacheAddress:
3919 DCHECK(Runtime::Current()->UseJitCompilation());
3920 break;
3921 case HLoadClass::LoadKind::kDexCachePcRelative:
3922 DCHECK(!Runtime::Current()->UseJitCompilation());
3923 break;
3924 case HLoadClass::LoadKind::kDexCacheViaMethod:
3925 break;
3926 }
3927 return desired_class_load_kind;
3928}
3929
Alexandre Rames67555f72014-11-18 10:55:16 +00003930void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003931 if (cls->NeedsAccessCheck()) {
3932 InvokeRuntimeCallingConvention calling_convention;
3933 CodeGenerator::CreateLoadClassLocationSummary(
3934 cls,
3935 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003936 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003937 /* code_generator_supports_read_barrier */ true);
3938 return;
3939 }
3940
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003941 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
3942 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003943 ? LocationSummary::kCallOnSlowPath
3944 : LocationSummary::kNoCall;
3945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003946 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003947 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3948 }
3949
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003950 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3951 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3952 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3953 locations->SetInAt(0, Location::RequiresRegister());
3954 }
3955 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003956}
3957
3958void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003959 if (cls->NeedsAccessCheck()) {
3960 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00003961 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003962 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003963 return;
3964 }
3965
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003966 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003967 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00003968
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003969 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003970 bool generate_null_check = false;
3971 switch (cls->GetLoadKind()) {
3972 case HLoadClass::LoadKind::kReferrersClass: {
3973 DCHECK(!cls->CanCallRuntime());
3974 DCHECK(!cls->MustGenerateClinitCheck());
3975 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3976 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003977 GenerateGcRootFieldLoad(cls,
3978 out_loc,
3979 current_method,
3980 ArtMethod::DeclaringClassOffset().Int32Value(),
3981 /*fixup_label*/ nullptr,
3982 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003983 break;
3984 }
3985 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003986 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003987 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3988 cls->GetTypeIndex()));
3989 break;
3990 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07003991 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003992 // Add ADRP with its PC-relative type patch.
3993 const DexFile& dex_file = cls->GetDexFile();
3994 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003995 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003996 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003997 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003998 __ Bind(adrp_label);
3999 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004000 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004001 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004002 vixl::aarch64::Label* add_label =
4003 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004004 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004005 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004006 __ Bind(add_label);
4007 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004008 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004009 break;
4010 }
4011 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004012 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004013 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4014 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4015 break;
4016 }
4017 case HLoadClass::LoadKind::kDexCacheAddress: {
4018 DCHECK_NE(cls->GetAddress(), 0u);
4019 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4020 // that gives a 16KiB range. To try and reduce the number of literals if we load
4021 // multiple types, simply split the dex cache address to a 16KiB aligned base
4022 // loaded from a literal and the remaining offset embedded in the load.
4023 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4024 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4025 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4026 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4027 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4028 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4029 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004030 GenerateGcRootFieldLoad(cls,
4031 out_loc,
4032 out.X(),
4033 offset,
4034 /*fixup_label*/ nullptr,
4035 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004036 generate_null_check = !cls->IsInDexCache();
4037 break;
4038 }
4039 case HLoadClass::LoadKind::kDexCachePcRelative: {
4040 // Add ADRP with its PC-relative DexCache access patch.
4041 const DexFile& dex_file = cls->GetDexFile();
4042 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004043 vixl::aarch64::Label* adrp_label =
4044 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004045 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004046 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004047 __ Bind(adrp_label);
4048 __ adrp(out.X(), /* offset placeholder */ 0);
4049 }
4050 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004051 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004052 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4053 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004054 GenerateGcRootFieldLoad(cls,
4055 out_loc,
4056 out.X(),
4057 /* offset placeholder */ 0,
4058 ldr_label,
4059 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004060 generate_null_check = !cls->IsInDexCache();
4061 break;
4062 }
4063 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4064 MemberOffset resolved_types_offset =
4065 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4066 // /* GcRoot<mirror::Class>[] */ out =
4067 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4068 Register current_method = InputRegisterAt(cls, 0);
4069 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4070 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004071 GenerateGcRootFieldLoad(cls,
4072 out_loc,
4073 out.X(),
4074 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4075 /*fixup_label*/ nullptr,
4076 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004077 generate_null_check = !cls->IsInDexCache();
4078 break;
4079 }
4080 }
4081
4082 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4083 DCHECK(cls->CanCallRuntime());
4084 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4085 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4086 codegen_->AddSlowPath(slow_path);
4087 if (generate_null_check) {
4088 __ Cbz(out, slow_path->GetEntryLabel());
4089 }
4090 if (cls->MustGenerateClinitCheck()) {
4091 GenerateClassInitializationCheck(slow_path, out);
4092 } else {
4093 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004094 }
4095 }
4096}
4097
David Brazdilcb1c0552015-08-04 16:22:25 +01004098static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004099 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004100}
4101
Alexandre Rames67555f72014-11-18 10:55:16 +00004102void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4103 LocationSummary* locations =
4104 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4105 locations->SetOut(Location::RequiresRegister());
4106}
4107
4108void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004109 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4110}
4111
4112void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4113 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4114}
4115
4116void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4117 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004118}
4119
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004120HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4121 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004122 switch (desired_string_load_kind) {
4123 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4124 DCHECK(!GetCompilerOptions().GetCompilePic());
4125 break;
4126 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4127 DCHECK(GetCompilerOptions().GetCompilePic());
4128 break;
4129 case HLoadString::LoadKind::kBootImageAddress:
4130 break;
4131 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004132 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004133 break;
4134 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004135 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004136 break;
4137 case HLoadString::LoadKind::kDexCacheViaMethod:
4138 break;
4139 }
4140 return desired_string_load_kind;
4141}
4142
Alexandre Rames67555f72014-11-18 10:55:16 +00004143void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004144 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4145 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004146 : LocationSummary::kNoCall;
4147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004148 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4149 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004150 InvokeRuntimeCallingConvention calling_convention;
4151 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4152 } else {
4153 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004154 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004155}
4156
4157void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004158 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004159
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004160 switch (load->GetLoadKind()) {
4161 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004162 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4163 load->GetStringIndex()));
4164 return; // No dex cache slow path.
4165 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004166 // Add ADRP with its PC-relative String patch.
4167 const DexFile& dex_file = load->GetDexFile();
4168 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004169 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004170 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004171 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004172 __ Bind(adrp_label);
4173 __ adrp(out.X(), /* offset placeholder */ 0);
4174 }
4175 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004176 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004177 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4178 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004179 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004180 __ Bind(add_label);
4181 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4182 }
4183 return; // No dex cache slow path.
4184 }
4185 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004186 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4187 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4188 return; // No dex cache slow path.
4189 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004190 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004191 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004192 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004193
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004194 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004195 InvokeRuntimeCallingConvention calling_convention;
4196 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4197 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4198 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004199}
4200
Alexandre Rames5319def2014-10-23 10:03:10 +01004201void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4202 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4203 locations->SetOut(Location::ConstantLocation(constant));
4204}
4205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004206void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004207 // Will be generated at use site.
4208}
4209
Alexandre Rames67555f72014-11-18 10:55:16 +00004210void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4211 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004212 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004213 InvokeRuntimeCallingConvention calling_convention;
4214 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4215}
4216
4217void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004218 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4219 instruction,
4220 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004221 if (instruction->IsEnter()) {
4222 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4223 } else {
4224 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4225 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004226}
4227
Alexandre Rames42d641b2014-10-27 14:00:51 +00004228void LocationsBuilderARM64::VisitMul(HMul* mul) {
4229 LocationSummary* locations =
4230 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4231 switch (mul->GetResultType()) {
4232 case Primitive::kPrimInt:
4233 case Primitive::kPrimLong:
4234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004237 break;
4238
4239 case Primitive::kPrimFloat:
4240 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004241 locations->SetInAt(0, Location::RequiresFpuRegister());
4242 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004243 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004244 break;
4245
4246 default:
4247 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4248 }
4249}
4250
4251void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4252 switch (mul->GetResultType()) {
4253 case Primitive::kPrimInt:
4254 case Primitive::kPrimLong:
4255 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4256 break;
4257
4258 case Primitive::kPrimFloat:
4259 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004260 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004261 break;
4262
4263 default:
4264 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4265 }
4266}
4267
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004268void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4269 LocationSummary* locations =
4270 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4271 switch (neg->GetResultType()) {
4272 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004273 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004274 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004276 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004277
4278 case Primitive::kPrimFloat:
4279 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004280 locations->SetInAt(0, Location::RequiresFpuRegister());
4281 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004282 break;
4283
4284 default:
4285 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4286 }
4287}
4288
4289void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4290 switch (neg->GetResultType()) {
4291 case Primitive::kPrimInt:
4292 case Primitive::kPrimLong:
4293 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4294 break;
4295
4296 case Primitive::kPrimFloat:
4297 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004298 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004299 break;
4300
4301 default:
4302 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4303 }
4304}
4305
4306void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4307 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004309 InvokeRuntimeCallingConvention calling_convention;
4310 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004311 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004312 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004313 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004314}
4315
4316void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4317 LocationSummary* locations = instruction->GetLocations();
4318 InvokeRuntimeCallingConvention calling_convention;
4319 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4320 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004321 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004322 // Note: if heap poisoning is enabled, the entry point takes cares
4323 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004324 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004325 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004326}
4327
Alexandre Rames5319def2014-10-23 10:03:10 +01004328void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4329 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004331 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004332 if (instruction->IsStringAlloc()) {
4333 locations->AddTemp(LocationFrom(kArtMethodRegister));
4334 } else {
4335 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4336 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4337 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004338 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4339}
4340
4341void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004342 // Note: if heap poisoning is enabled, the entry point takes cares
4343 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004344 if (instruction->IsStringAlloc()) {
4345 // String is allocated through StringFactory. Call NewEmptyString entry point.
4346 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004347 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004348 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4349 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4350 __ Blr(lr);
4351 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4352 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004353 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004354 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4355 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004356}
4357
4358void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004360 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004362}
4363
4364void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004365 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004366 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004367 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004368 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004369 break;
4370
4371 default:
4372 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4373 }
4374}
4375
David Brazdil66d126e2015-04-03 16:02:44 +01004376void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4378 locations->SetInAt(0, Location::RequiresRegister());
4379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4380}
4381
4382void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004383 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004384}
4385
Alexandre Rames5319def2014-10-23 10:03:10 +01004386void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00004387 codegen_->CreateNullCheckLocations(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004388}
4389
Calin Juravle2ae48182016-03-16 14:05:09 +00004390void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4391 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004392 return;
4393 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004394
Alexandre Ramesd921d642015-04-16 15:07:16 +01004395 BlockPoolsScope block_pools(GetVIXLAssembler());
4396 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004397 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004398 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004399}
4400
Calin Juravle2ae48182016-03-16 14:05:09 +00004401void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004402 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004403 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004404
4405 LocationSummary* locations = instruction->GetLocations();
4406 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004407
4408 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004409}
4410
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004411void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004412 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004413}
4414
Alexandre Rames67555f72014-11-18 10:55:16 +00004415void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4416 HandleBinaryOp(instruction);
4417}
4418
4419void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4420 HandleBinaryOp(instruction);
4421}
4422
Alexandre Rames3e69f162014-12-10 10:36:50 +00004423void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4424 LOG(FATAL) << "Unreachable";
4425}
4426
4427void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4428 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4429}
4430
Alexandre Rames5319def2014-10-23 10:03:10 +01004431void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4433 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4434 if (location.IsStackSlot()) {
4435 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4436 } else if (location.IsDoubleStackSlot()) {
4437 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4438 }
4439 locations->SetOut(location);
4440}
4441
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004442void InstructionCodeGeneratorARM64::VisitParameterValue(
4443 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004444 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004445}
4446
4447void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4448 LocationSummary* locations =
4449 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004450 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004451}
4452
4453void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4454 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4455 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004456}
4457
4458void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004460 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004461 locations->SetInAt(i, Location::Any());
4462 }
4463 locations->SetOut(Location::Any());
4464}
4465
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004466void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004467 LOG(FATAL) << "Unreachable";
4468}
4469
Serban Constantinescu02164b32014-11-13 14:05:07 +00004470void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004471 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004472 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004473 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4474 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4476
4477 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004478 case Primitive::kPrimInt:
4479 case Primitive::kPrimLong:
4480 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004481 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4483 break;
4484
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004485 case Primitive::kPrimFloat:
4486 case Primitive::kPrimDouble: {
4487 InvokeRuntimeCallingConvention calling_convention;
4488 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4489 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4490 locations->SetOut(calling_convention.GetReturnLocation(type));
4491
4492 break;
4493 }
4494
Serban Constantinescu02164b32014-11-13 14:05:07 +00004495 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004496 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004497 }
4498}
4499
4500void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4501 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004502
Serban Constantinescu02164b32014-11-13 14:05:07 +00004503 switch (type) {
4504 case Primitive::kPrimInt:
4505 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004506 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004507 break;
4508 }
4509
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004510 case Primitive::kPrimFloat:
4511 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004512 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4513 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004514 if (type == Primitive::kPrimFloat) {
4515 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4516 } else {
4517 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4518 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004519 break;
4520 }
4521
Serban Constantinescu02164b32014-11-13 14:05:07 +00004522 default:
4523 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004524 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004525 }
4526}
4527
Calin Juravle27df7582015-04-17 19:12:31 +01004528void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4529 memory_barrier->SetLocations(nullptr);
4530}
4531
4532void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004533 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004534}
4535
Alexandre Rames5319def2014-10-23 10:03:10 +01004536void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4538 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004539 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004540}
4541
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004542void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004543 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004544}
4545
4546void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4547 instruction->SetLocations(nullptr);
4548}
4549
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004550void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004551 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004552}
4553
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004554void LocationsBuilderARM64::VisitRor(HRor* ror) {
4555 HandleBinaryOp(ror);
4556}
4557
4558void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4559 HandleBinaryOp(ror);
4560}
4561
Serban Constantinescu02164b32014-11-13 14:05:07 +00004562void LocationsBuilderARM64::VisitShl(HShl* shl) {
4563 HandleShift(shl);
4564}
4565
4566void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4567 HandleShift(shl);
4568}
4569
4570void LocationsBuilderARM64::VisitShr(HShr* shr) {
4571 HandleShift(shr);
4572}
4573
4574void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4575 HandleShift(shr);
4576}
4577
Alexandre Rames5319def2014-10-23 10:03:10 +01004578void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004579 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004580}
4581
4582void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004583 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004584}
4585
Alexandre Rames67555f72014-11-18 10:55:16 +00004586void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004587 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004588}
4589
4590void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004591 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004592}
4593
4594void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004595 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004596}
4597
Alexandre Rames67555f72014-11-18 10:55:16 +00004598void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004599 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004600}
4601
Calin Juravlee460d1d2015-09-29 04:52:17 +01004602void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4603 HUnresolvedInstanceFieldGet* instruction) {
4604 FieldAccessCallingConventionARM64 calling_convention;
4605 codegen_->CreateUnresolvedFieldLocationSummary(
4606 instruction, instruction->GetFieldType(), calling_convention);
4607}
4608
4609void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4610 HUnresolvedInstanceFieldGet* instruction) {
4611 FieldAccessCallingConventionARM64 calling_convention;
4612 codegen_->GenerateUnresolvedFieldAccess(instruction,
4613 instruction->GetFieldType(),
4614 instruction->GetFieldIndex(),
4615 instruction->GetDexPc(),
4616 calling_convention);
4617}
4618
4619void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4620 HUnresolvedInstanceFieldSet* instruction) {
4621 FieldAccessCallingConventionARM64 calling_convention;
4622 codegen_->CreateUnresolvedFieldLocationSummary(
4623 instruction, instruction->GetFieldType(), calling_convention);
4624}
4625
4626void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4627 HUnresolvedInstanceFieldSet* instruction) {
4628 FieldAccessCallingConventionARM64 calling_convention;
4629 codegen_->GenerateUnresolvedFieldAccess(instruction,
4630 instruction->GetFieldType(),
4631 instruction->GetFieldIndex(),
4632 instruction->GetDexPc(),
4633 calling_convention);
4634}
4635
4636void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4637 HUnresolvedStaticFieldGet* instruction) {
4638 FieldAccessCallingConventionARM64 calling_convention;
4639 codegen_->CreateUnresolvedFieldLocationSummary(
4640 instruction, instruction->GetFieldType(), calling_convention);
4641}
4642
4643void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4644 HUnresolvedStaticFieldGet* instruction) {
4645 FieldAccessCallingConventionARM64 calling_convention;
4646 codegen_->GenerateUnresolvedFieldAccess(instruction,
4647 instruction->GetFieldType(),
4648 instruction->GetFieldIndex(),
4649 instruction->GetDexPc(),
4650 calling_convention);
4651}
4652
4653void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4654 HUnresolvedStaticFieldSet* instruction) {
4655 FieldAccessCallingConventionARM64 calling_convention;
4656 codegen_->CreateUnresolvedFieldLocationSummary(
4657 instruction, instruction->GetFieldType(), calling_convention);
4658}
4659
4660void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4661 HUnresolvedStaticFieldSet* instruction) {
4662 FieldAccessCallingConventionARM64 calling_convention;
4663 codegen_->GenerateUnresolvedFieldAccess(instruction,
4664 instruction->GetFieldType(),
4665 instruction->GetFieldIndex(),
4666 instruction->GetDexPc(),
4667 calling_convention);
4668}
4669
Alexandre Rames5319def2014-10-23 10:03:10 +01004670void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004671 LocationSummary* locations =
4672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4673 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004674}
4675
4676void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004677 HBasicBlock* block = instruction->GetBlock();
4678 if (block->GetLoopInformation() != nullptr) {
4679 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4680 // The back edge will generate the suspend check.
4681 return;
4682 }
4683 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4684 // The goto will generate the suspend check.
4685 return;
4686 }
4687 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004688}
4689
Alexandre Rames67555f72014-11-18 10:55:16 +00004690void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4691 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004692 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004693 InvokeRuntimeCallingConvention calling_convention;
4694 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4695}
4696
4697void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004698 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004699 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004700}
4701
4702void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4703 LocationSummary* locations =
4704 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4705 Primitive::Type input_type = conversion->GetInputType();
4706 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004707 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004708 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4709 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4710 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4711 }
4712
Alexandre Rames542361f2015-01-29 16:57:31 +00004713 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004714 locations->SetInAt(0, Location::RequiresFpuRegister());
4715 } else {
4716 locations->SetInAt(0, Location::RequiresRegister());
4717 }
4718
Alexandre Rames542361f2015-01-29 16:57:31 +00004719 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004720 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4721 } else {
4722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4723 }
4724}
4725
4726void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4727 Primitive::Type result_type = conversion->GetResultType();
4728 Primitive::Type input_type = conversion->GetInputType();
4729
4730 DCHECK_NE(input_type, result_type);
4731
Alexandre Rames542361f2015-01-29 16:57:31 +00004732 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004733 int result_size = Primitive::ComponentSize(result_type);
4734 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004735 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004736 Register output = OutputRegister(conversion);
4737 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004738 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004739 // 'int' values are used directly as W registers, discarding the top
4740 // bits, so we don't need to sign-extend and can just perform a move.
4741 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4742 // top 32 bits of the target register. We theoretically could leave those
4743 // bits unchanged, but we would have to make sure that no code uses a
4744 // 32bit input value as a 64bit value assuming that the top 32 bits are
4745 // zero.
4746 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004747 } else if (result_type == Primitive::kPrimChar ||
4748 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4749 __ Ubfx(output,
4750 output.IsX() ? source.X() : source.W(),
4751 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004752 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004753 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004754 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004755 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004756 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004757 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004758 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4759 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004760 } else if (Primitive::IsFloatingPointType(result_type) &&
4761 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004762 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4763 } else {
4764 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4765 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004766 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004767}
Alexandre Rames67555f72014-11-18 10:55:16 +00004768
Serban Constantinescu02164b32014-11-13 14:05:07 +00004769void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4770 HandleShift(ushr);
4771}
4772
4773void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4774 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004775}
4776
4777void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4778 HandleBinaryOp(instruction);
4779}
4780
4781void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4782 HandleBinaryOp(instruction);
4783}
4784
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004785void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004786 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004787 LOG(FATAL) << "Unreachable";
4788}
4789
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004790void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004791 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004792 LOG(FATAL) << "Unreachable";
4793}
4794
Mark Mendellfe57faa2015-09-18 09:26:15 -04004795// Simple implementation of packed switch - generate cascaded compare/jumps.
4796void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4797 LocationSummary* locations =
4798 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4799 locations->SetInAt(0, Location::RequiresRegister());
4800}
4801
4802void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4803 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004804 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004805 Register value_reg = InputRegisterAt(switch_instr, 0);
4806 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4807
Zheng Xu3927c8b2015-11-18 17:46:25 +08004808 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004809 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004810 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4811 // make sure we don't emit it if the target may run out of range.
4812 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4813 // ranges and emit the tables only as required.
4814 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004815
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004816 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004817 // Current instruction id is an upper bound of the number of HIRs in the graph.
4818 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4819 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004820 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4821 Register temp = temps.AcquireW();
4822 __ Subs(temp, value_reg, Operand(lower_bound));
4823
Zheng Xu3927c8b2015-11-18 17:46:25 +08004824 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004825 // Jump to successors[0] if value == lower_bound.
4826 __ B(eq, codegen_->GetLabelOf(successors[0]));
4827 int32_t last_index = 0;
4828 for (; num_entries - last_index > 2; last_index += 2) {
4829 __ Subs(temp, temp, Operand(2));
4830 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4831 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4832 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4833 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4834 }
4835 if (num_entries - last_index == 2) {
4836 // The last missing case_value.
4837 __ Cmp(temp, Operand(1));
4838 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004839 }
4840
4841 // And the default for any other value.
4842 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4843 __ B(codegen_->GetLabelOf(default_block));
4844 }
4845 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004846 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004847
4848 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4849
4850 // Below instructions should use at most one blocked register. Since there are two blocked
4851 // registers, we are free to block one.
4852 Register temp_w = temps.AcquireW();
4853 Register index;
4854 // Remove the bias.
4855 if (lower_bound != 0) {
4856 index = temp_w;
4857 __ Sub(index, value_reg, Operand(lower_bound));
4858 } else {
4859 index = value_reg;
4860 }
4861
4862 // Jump to default block if index is out of the range.
4863 __ Cmp(index, Operand(num_entries));
4864 __ B(hs, codegen_->GetLabelOf(default_block));
4865
4866 // In current VIXL implementation, it won't require any blocked registers to encode the
4867 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4868 // register pressure.
4869 Register table_base = temps.AcquireX();
4870 // Load jump offset from the table.
4871 __ Adr(table_base, jump_table->GetTableStartLabel());
4872 Register jump_offset = temp_w;
4873 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4874
4875 // Jump to target block by branching to table_base(pc related) + offset.
4876 Register target_address = table_base;
4877 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4878 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004879 }
4880}
4881
Roland Levillain44015862016-01-22 11:47:17 +00004882void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4883 Location out,
4884 uint32_t offset,
4885 Location maybe_temp) {
4886 Primitive::Type type = Primitive::kPrimNot;
4887 Register out_reg = RegisterFrom(out, type);
4888 if (kEmitCompilerReadBarrier) {
4889 Register temp_reg = RegisterFrom(maybe_temp, type);
4890 if (kUseBakerReadBarrier) {
4891 // Load with fast path based Baker's read barrier.
4892 // /* HeapReference<Object> */ out = *(out + offset)
4893 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4894 out,
4895 out_reg,
4896 offset,
4897 temp_reg,
4898 /* needs_null_check */ false,
4899 /* use_load_acquire */ false);
4900 } else {
4901 // Load with slow path based read barrier.
4902 // Save the value of `out` into `maybe_temp` before overwriting it
4903 // in the following move operation, as we will need it for the
4904 // read barrier below.
4905 __ Mov(temp_reg, out_reg);
4906 // /* HeapReference<Object> */ out = *(out + offset)
4907 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4908 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4909 }
4910 } else {
4911 // Plain load with no read barrier.
4912 // /* HeapReference<Object> */ out = *(out + offset)
4913 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4914 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4915 }
4916}
4917
4918void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4919 Location out,
4920 Location obj,
4921 uint32_t offset,
4922 Location maybe_temp) {
4923 Primitive::Type type = Primitive::kPrimNot;
4924 Register out_reg = RegisterFrom(out, type);
4925 Register obj_reg = RegisterFrom(obj, type);
4926 if (kEmitCompilerReadBarrier) {
4927 if (kUseBakerReadBarrier) {
4928 // Load with fast path based Baker's read barrier.
4929 Register temp_reg = RegisterFrom(maybe_temp, type);
4930 // /* HeapReference<Object> */ out = *(obj + offset)
4931 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4932 out,
4933 obj_reg,
4934 offset,
4935 temp_reg,
4936 /* needs_null_check */ false,
4937 /* use_load_acquire */ false);
4938 } else {
4939 // Load with slow path based read barrier.
4940 // /* HeapReference<Object> */ out = *(obj + offset)
4941 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4942 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4943 }
4944 } else {
4945 // Plain load with no read barrier.
4946 // /* HeapReference<Object> */ out = *(obj + offset)
4947 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4948 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4949 }
4950}
4951
4952void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4953 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004954 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004955 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004956 vixl::aarch64::Label* fixup_label,
4957 bool requires_read_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004958 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004959 if (requires_read_barrier) {
4960 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00004961 if (kUseBakerReadBarrier) {
4962 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4963 // Baker's read barrier are used:
4964 //
4965 // root = obj.field;
4966 // if (Thread::Current()->GetIsGcMarking()) {
4967 // root = ReadBarrier::Mark(root)
4968 // }
4969
4970 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004971 if (fixup_label == nullptr) {
4972 __ Ldr(root_reg, MemOperand(obj, offset));
4973 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004974 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004975 __ Bind(fixup_label);
4976 __ ldr(root_reg, MemOperand(obj, offset));
4977 }
Roland Levillain44015862016-01-22 11:47:17 +00004978 static_assert(
4979 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4980 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4981 "have different sizes.");
4982 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4983 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4984 "have different sizes.");
4985
Vladimir Marko953437b2016-08-24 08:30:46 +00004986 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00004987 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01004988 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00004989 codegen_->AddSlowPath(slow_path);
4990
4991 MacroAssembler* masm = GetVIXLAssembler();
4992 UseScratchRegisterScope temps(masm);
4993 Register temp = temps.AcquireW();
4994 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07004995 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00004996 __ Cbnz(temp, slow_path->GetEntryLabel());
4997 __ Bind(slow_path->GetExitLabel());
4998 } else {
4999 // GC root loaded through a slow path for read barriers other
5000 // than Baker's.
5001 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005002 if (fixup_label == nullptr) {
5003 __ Add(root_reg.X(), obj.X(), offset);
5004 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005005 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005006 __ Bind(fixup_label);
5007 __ add(root_reg.X(), obj.X(), offset);
5008 }
Roland Levillain44015862016-01-22 11:47:17 +00005009 // /* mirror::Object* */ root = root->Read()
5010 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5011 }
5012 } else {
5013 // Plain GC root load with no read barrier.
5014 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005015 if (fixup_label == nullptr) {
5016 __ Ldr(root_reg, MemOperand(obj, offset));
5017 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005018 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005019 __ Bind(fixup_label);
5020 __ ldr(root_reg, MemOperand(obj, offset));
5021 }
Roland Levillain44015862016-01-22 11:47:17 +00005022 // Note that GC roots are not affected by heap poisoning, thus we
5023 // do not have to unpoison `root_reg` here.
5024 }
5025}
5026
5027void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5028 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005029 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005030 uint32_t offset,
5031 Register temp,
5032 bool needs_null_check,
5033 bool use_load_acquire) {
5034 DCHECK(kEmitCompilerReadBarrier);
5035 DCHECK(kUseBakerReadBarrier);
5036
5037 // /* HeapReference<Object> */ ref = *(obj + offset)
5038 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005039 size_t no_scale_factor = 0U;
5040 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5041 ref,
5042 obj,
5043 offset,
5044 no_index,
5045 no_scale_factor,
5046 temp,
5047 needs_null_check,
5048 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005049}
5050
5051void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5052 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005053 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005054 uint32_t data_offset,
5055 Location index,
5056 Register temp,
5057 bool needs_null_check) {
5058 DCHECK(kEmitCompilerReadBarrier);
5059 DCHECK(kUseBakerReadBarrier);
5060
5061 // Array cells are never volatile variables, therefore array loads
5062 // never use Load-Acquire instructions on ARM64.
5063 const bool use_load_acquire = false;
5064
Roland Levillainbfea3352016-06-23 13:48:47 +01005065 static_assert(
5066 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5067 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005068 // /* HeapReference<Object> */ ref =
5069 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005070 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5071 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5072 ref,
5073 obj,
5074 data_offset,
5075 index,
5076 scale_factor,
5077 temp,
5078 needs_null_check,
5079 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005080}
5081
5082void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5083 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005084 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005085 uint32_t offset,
5086 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005087 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005088 Register temp,
5089 bool needs_null_check,
5090 bool use_load_acquire) {
5091 DCHECK(kEmitCompilerReadBarrier);
5092 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005093 // If we are emitting an array load, we should not be using a
5094 // Load Acquire instruction. In other words:
5095 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5096 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005097
5098 MacroAssembler* masm = GetVIXLAssembler();
5099 UseScratchRegisterScope temps(masm);
5100
5101 // In slow path based read barriers, the read barrier call is
5102 // inserted after the original load. However, in fast path based
5103 // Baker's read barriers, we need to perform the load of
5104 // mirror::Object::monitor_ *before* the original reference load.
5105 // This load-load ordering is required by the read barrier.
5106 // The fast path/slow path (for Baker's algorithm) should look like:
5107 //
5108 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5109 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5110 // HeapReference<Object> ref = *src; // Original reference load.
5111 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5112 // if (is_gray) {
5113 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5114 // }
5115 //
5116 // Note: the original implementation in ReadBarrier::Barrier is
5117 // slightly more complex as it performs additional checks that we do
5118 // not do here for performance reasons.
5119
5120 Primitive::Type type = Primitive::kPrimNot;
5121 Register ref_reg = RegisterFrom(ref, type);
5122 DCHECK(obj.IsW());
5123 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5124
5125 // /* int32_t */ monitor = obj->monitor_
5126 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5127 if (needs_null_check) {
5128 MaybeRecordImplicitNullCheck(instruction);
5129 }
5130 // /* LockWord */ lock_word = LockWord(monitor)
5131 static_assert(sizeof(LockWord) == sizeof(int32_t),
5132 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005133
Vladimir Marko877a0332016-07-11 19:30:56 +01005134 // Introduce a dependency on the lock_word including rb_state,
5135 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005136 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005137 // `obj` is unchanged by this operation, but its value now depends
5138 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005139 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005140
5141 // The actual reference load.
5142 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005143 // Load types involving an "index".
5144 if (use_load_acquire) {
5145 // UnsafeGetObjectVolatile intrinsic case.
5146 // Register `index` is not an index in an object array, but an
5147 // offset to an object reference field within object `obj`.
5148 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5149 DCHECK(instruction->GetLocations()->Intrinsified());
5150 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5151 << instruction->AsInvoke()->GetIntrinsic();
5152 DCHECK_EQ(offset, 0U);
5153 DCHECK_EQ(scale_factor, 0U);
5154 DCHECK_EQ(needs_null_check, 0U);
5155 // /* HeapReference<Object> */ ref = *(obj + index)
5156 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5157 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005158 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005159 // ArrayGet and UnsafeGetObject intrinsics cases.
5160 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5161 if (index.IsConstant()) {
5162 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5163 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5164 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005165 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005166 __ Add(temp2, obj, offset);
5167 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5168 temps.Release(temp2);
5169 }
Roland Levillain44015862016-01-22 11:47:17 +00005170 }
Roland Levillain44015862016-01-22 11:47:17 +00005171 } else {
5172 // /* HeapReference<Object> */ ref = *(obj + offset)
5173 MemOperand field = HeapOperand(obj, offset);
5174 if (use_load_acquire) {
5175 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5176 } else {
5177 Load(type, ref_reg, field);
5178 }
5179 }
5180
5181 // Object* ref = ref_addr->AsMirrorPtr()
5182 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5183
Vladimir Marko953437b2016-08-24 08:30:46 +00005184 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005185 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005186 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005187 AddSlowPath(slow_path);
5188
5189 // if (rb_state == ReadBarrier::gray_ptr_)
5190 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005191 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5192 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5193 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5194 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5195 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005196 __ Bind(slow_path->GetExitLabel());
5197}
5198
5199void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5200 Location out,
5201 Location ref,
5202 Location obj,
5203 uint32_t offset,
5204 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005205 DCHECK(kEmitCompilerReadBarrier);
5206
Roland Levillain44015862016-01-22 11:47:17 +00005207 // Insert a slow path based read barrier *after* the reference load.
5208 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005209 // If heap poisoning is enabled, the unpoisoning of the loaded
5210 // reference will be carried out by the runtime within the slow
5211 // path.
5212 //
5213 // Note that `ref` currently does not get unpoisoned (when heap
5214 // poisoning is enabled), which is alright as the `ref` argument is
5215 // not used by the artReadBarrierSlow entry point.
5216 //
5217 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5218 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5219 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5220 AddSlowPath(slow_path);
5221
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005222 __ B(slow_path->GetEntryLabel());
5223 __ Bind(slow_path->GetExitLabel());
5224}
5225
Roland Levillain44015862016-01-22 11:47:17 +00005226void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5227 Location out,
5228 Location ref,
5229 Location obj,
5230 uint32_t offset,
5231 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005232 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005233 // Baker's read barriers shall be handled by the fast path
5234 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5235 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005236 // If heap poisoning is enabled, unpoisoning will be taken care of
5237 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005238 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005239 } else if (kPoisonHeapReferences) {
5240 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5241 }
5242}
5243
Roland Levillain44015862016-01-22 11:47:17 +00005244void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5245 Location out,
5246 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005247 DCHECK(kEmitCompilerReadBarrier);
5248
Roland Levillain44015862016-01-22 11:47:17 +00005249 // Insert a slow path based read barrier *after* the GC root load.
5250 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005251 // Note that GC roots are not affected by heap poisoning, so we do
5252 // not need to do anything special for this here.
5253 SlowPathCodeARM64* slow_path =
5254 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5255 AddSlowPath(slow_path);
5256
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005257 __ B(slow_path->GetEntryLabel());
5258 __ Bind(slow_path->GetExitLabel());
5259}
5260
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005261void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5262 LocationSummary* locations =
5263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5264 locations->SetInAt(0, Location::RequiresRegister());
5265 locations->SetOut(Location::RequiresRegister());
5266}
5267
5268void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5269 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005270 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005271 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005272 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005273 __ Ldr(XRegisterFrom(locations->Out()),
5274 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005275 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005276 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005277 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005278 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5279 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005280 __ Ldr(XRegisterFrom(locations->Out()),
5281 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005282 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005283}
5284
5285
5286
Alexandre Rames67555f72014-11-18 10:55:16 +00005287#undef __
5288#undef QUICK_ENTRY_POINT
5289
Alexandre Rames5319def2014-10-23 10:03:10 +01005290} // namespace arm64
5291} // namespace art