blob: 3fdd7186d1a75905b7089b82711050cf8b26dd10 [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
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
Roland Levillain22ccc3a2015-11-24 13:10:05 +000045template<class MirrorType>
46class GcRoot;
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace arm64 {
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050using helpers::CPURegisterFrom;
51using helpers::DRegisterFrom;
52using helpers::FPRegisterFrom;
53using helpers::HeapOperand;
54using helpers::HeapOperandFrom;
55using helpers::InputCPURegisterAt;
56using helpers::InputFPRegisterAt;
57using helpers::InputRegisterAt;
58using helpers::InputOperandAt;
59using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080060using helpers::LocationFrom;
61using helpers::OperandFromMemOperand;
62using helpers::OutputCPURegister;
63using helpers::OutputFPRegister;
64using helpers::OutputRegister;
65using helpers::RegisterFrom;
66using helpers::StackOperandFrom;
67using helpers::VIXLRegCodeFromART;
68using helpers::WRegisterFrom;
69using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000070using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080071using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
Alexandre Rames5319def2014-10-23 10:03:10 +010073static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000074// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080075// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000077static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Vladimir Markod6e069b2016-01-18 11:11:01 +000096inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
97 // The ARM64 condition codes can express all the necessary branches, see the
98 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
99 // There is no dex instruction or HIR that would need the missing conditions
100 // "equal or unordered" or "not equal".
101 switch (cond) {
102 case kCondEQ: return eq;
103 case kCondNE: return ne /* unordered */;
104 case kCondLT: return gt_bias ? cc : lt /* unordered */;
105 case kCondLE: return gt_bias ? ls : le /* unordered */;
106 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
107 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
108 default:
109 LOG(FATAL) << "UNREACHABLE";
110 UNREACHABLE();
111 }
112}
113
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
116 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
117 // but we use the exact registers for clarity.
118 if (return_type == Primitive::kPrimFloat) {
119 return LocationFrom(s0);
120 } else if (return_type == Primitive::kPrimDouble) {
121 return LocationFrom(d0);
122 } else if (return_type == Primitive::kPrimLong) {
123 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100124 } else if (return_type == Primitive::kPrimVoid) {
125 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000126 } else {
127 return LocationFrom(w0);
128 }
129}
130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000132 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133}
134
Alexandre Rames67555f72014-11-18 10:55:16 +0000135#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
Zheng Xuda403092015-04-24 17:35:39 +0800138// Calculate memory accessing operand for save/restore live registers.
139static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
140 RegisterSet* register_set,
141 int64_t spill_offset,
142 bool is_save) {
143 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
144 codegen->GetNumberOfCoreRegisters(),
145 register_set->GetFloatingPointRegisters(),
146 codegen->GetNumberOfFloatingPointRegisters()));
147
148 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
149 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000150 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
151 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800152
153 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
154 UseScratchRegisterScope temps(masm);
155
156 Register base = masm->StackPointer();
157 int64_t core_spill_size = core_list.TotalSizeInBytes();
158 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
159 int64_t reg_size = kXRegSizeInBytes;
160 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
161 uint32_t ls_access_size = WhichPowerOf2(reg_size);
162 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
163 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
164 // If the offset does not fit in the instruction's immediate field, use an alternate register
165 // to compute the base address(float point registers spill base address).
166 Register new_base = temps.AcquireSameSizeAs(base);
167 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
168 base = new_base;
169 spill_offset = -core_spill_size;
170 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
171 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
172 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
173 }
174
175 if (is_save) {
176 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
177 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
178 } else {
179 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
180 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
181 }
182}
183
184void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
185 RegisterSet* register_set = locations->GetLiveRegisters();
186 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
187 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
188 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
189 // If the register holds an object, update the stack mask.
190 if (locations->RegisterContainsObject(i)) {
191 locations->SetStackBit(stack_offset / kVRegSize);
192 }
193 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
194 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
195 saved_core_stack_offsets_[i] = stack_offset;
196 stack_offset += kXRegSizeInBytes;
197 }
198 }
199
200 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
201 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
202 register_set->ContainsFloatingPointRegister(i)) {
203 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
204 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
205 saved_fpu_stack_offsets_[i] = stack_offset;
206 stack_offset += kDRegSizeInBytes;
207 }
208 }
209
210 SaveRestoreLiveRegistersHelper(codegen, register_set,
211 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
212}
213
214void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
215 RegisterSet* register_set = locations->GetLiveRegisters();
216 SaveRestoreLiveRegistersHelper(codegen, register_set,
217 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
218}
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
221 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100222 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000226 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000229 if (instruction_->CanThrowIntoCatchBlock()) {
230 // Live registers will be restored in the catch block if caught.
231 SaveLiveRegisters(codegen, instruction_->GetLocations());
232 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000233 // We're moving two locations to locations that could overlap, so we need a parallel
234 // move resolver.
235 InvokeRuntimeCallingConvention calling_convention;
236 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100237 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
238 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000239 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000240 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800241 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100242 }
243
Alexandre Rames8158f282015-08-07 10:26:17 +0100244 bool IsFatal() const OVERRIDE { return true; }
245
Alexandre Rames9931f312015-06-19 14:47:01 +0100246 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
247
Alexandre Rames5319def2014-10-23 10:03:10 +0100248 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000249 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000261 if (instruction_->CanThrowIntoCatchBlock()) {
262 // Live registers will be restored in the catch block if caught.
263 SaveLiveRegisters(codegen, instruction_->GetLocations());
264 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000265 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000266 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 }
269
Alexandre Rames8158f282015-08-07 10:26:17 +0100270 bool IsFatal() const OVERRIDE { return true; }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
273
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 private:
275 HDivZeroCheck* const instruction_;
276 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
277};
278
279class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
280 public:
281 LoadClassSlowPathARM64(HLoadClass* cls,
282 HInstruction* at,
283 uint32_t dex_pc,
284 bool do_clinit)
285 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
286 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
287 }
288
289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
290 LocationSummary* locations = at_->GetLocations();
291 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
292
293 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295
296 InvokeRuntimeCallingConvention calling_convention;
297 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
299 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000300 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800301 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800303 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100304 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800305 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000306
307 // Move the class to the desired location.
308 Location out = locations->Out();
309 if (out.IsValid()) {
310 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
311 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000312 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000315 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 __ B(GetExitLabel());
317 }
318
Alexandre Rames9931f312015-06-19 14:47:01 +0100319 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
320
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 private:
322 // The class this slow path will load.
323 HLoadClass* const cls_;
324
325 // The instruction where this slow path is happening.
326 // (Might be the load class or an initialization check).
327 HInstruction* const at_;
328
329 // The dex PC of `at_`.
330 const uint32_t dex_pc_;
331
332 // Whether to initialize the class.
333 const bool do_clinit_;
334
335 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
336};
337
338class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
339 public:
340 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 LocationSummary* locations = instruction_->GetLocations();
344 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
345 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
346
347 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000348 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349
350 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800351 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000353 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100354 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000356 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000357
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000358 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 __ B(GetExitLabel());
360 }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
363
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 private:
365 HLoadString* const instruction_;
366
367 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
368};
369
Alexandre Rames5319def2014-10-23 10:03:10 +0100370class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
372 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
373
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000377 if (instruction_->CanThrowIntoCatchBlock()) {
378 // Live registers will be restored in the catch block if caught.
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
380 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000381 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000382 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800383 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 }
385
Alexandre Rames8158f282015-08-07 10:26:17 +0100386 bool IsFatal() const OVERRIDE { return true; }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
389
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 private:
391 HNullCheck* const instruction_;
392
393 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
394};
395
396class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
397 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100398 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100399 : instruction_(instruction), successor_(successor) {}
400
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
402 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100403 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000404 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000406 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800407 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000408 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000409 if (successor_ == nullptr) {
410 __ B(GetReturnLabel());
411 } else {
412 __ B(arm64_codegen->GetLabelOf(successor_));
413 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 }
415
416 vixl::Label* GetReturnLabel() {
417 DCHECK(successor_ == nullptr);
418 return &return_label_;
419 }
420
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100421 HBasicBlock* GetSuccessor() const {
422 return successor_;
423 }
424
Alexandre Rames9931f312015-06-19 14:47:01 +0100425 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
426
Alexandre Rames5319def2014-10-23 10:03:10 +0100427 private:
428 HSuspendCheck* const instruction_;
429 // If not null, the block to branch to after the suspend check.
430 HBasicBlock* const successor_;
431
432 // If `successor_` is null, the label to branch to after the suspend check.
433 vixl::Label return_label_;
434
435 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
436};
437
Alexandre Rames67555f72014-11-18 10:55:16 +0000438class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
439 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000440 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
441 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000442
443 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000444 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 Location class_to_check = locations->InAt(1);
446 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
447 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000448 DCHECK(instruction_->IsCheckCast()
449 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
450 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100451 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452
Alexandre Rames67555f72014-11-18 10:55:16 +0000453 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000455 if (!is_fatal_) {
456 SaveLiveRegisters(codegen, locations);
457 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458
459 // We're moving two locations to locations that could overlap, so we need a parallel
460 // move resolver.
461 InvokeRuntimeCallingConvention calling_convention;
462 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100463 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
464 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465
466 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000467 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100468 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000469 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
470 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 Primitive::Type ret_type = instruction_->GetType();
472 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
473 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
474 } else {
475 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100476 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800477 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478 }
479
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000480 if (!is_fatal_) {
481 RestoreLiveRegisters(codegen, locations);
482 __ B(GetExitLabel());
483 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000484 }
485
Alexandre Rames9931f312015-06-19 14:47:01 +0100486 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100488
Alexandre Rames67555f72014-11-18 10:55:16 +0000489 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000490 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000491 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000492
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
494};
495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
497 public:
Aart Bik42249c32016-01-07 15:33:50 -0800498 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100499 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700500
501 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800502 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 __ Bind(GetEntryLabel());
504 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800505 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
506 instruction_,
507 instruction_->GetDexPc(),
508 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000509 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510 }
511
Alexandre Rames9931f312015-06-19 14:47:01 +0100512 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
513
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700514 private:
Aart Bik42249c32016-01-07 15:33:50 -0800515 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700516 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
517};
518
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100519class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
520 public:
521 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
522
523 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
524 LocationSummary* locations = instruction_->GetLocations();
525 __ Bind(GetEntryLabel());
526 SaveLiveRegisters(codegen, locations);
527
528 InvokeRuntimeCallingConvention calling_convention;
529 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
530 parallel_move.AddMove(
531 locations->InAt(0),
532 LocationFrom(calling_convention.GetRegisterAt(0)),
533 Primitive::kPrimNot,
534 nullptr);
535 parallel_move.AddMove(
536 locations->InAt(1),
537 LocationFrom(calling_convention.GetRegisterAt(1)),
538 Primitive::kPrimInt,
539 nullptr);
540 parallel_move.AddMove(
541 locations->InAt(2),
542 LocationFrom(calling_convention.GetRegisterAt(2)),
543 Primitive::kPrimNot,
544 nullptr);
545 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
546
547 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
548 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
549 instruction_,
550 instruction_->GetDexPc(),
551 this);
552 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
553 RestoreLiveRegisters(codegen, locations);
554 __ B(GetExitLabel());
555 }
556
557 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
558
559 private:
560 HInstruction* const instruction_;
561
562 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
563};
564
Zheng Xu3927c8b2015-11-18 17:46:25 +0800565void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
566 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000567 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800568
569 // We are about to use the assembler to place literals directly. Make sure we have enough
570 // underlying code buffer and we have generated the jump table with right size.
571 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
572 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
573
574 __ Bind(&table_start_);
575 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
576 for (uint32_t i = 0; i < num_entries; i++) {
577 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
578 DCHECK(target_label->IsBound());
579 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
580 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
581 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
582 Literal<int32_t> literal(jump_offset);
583 __ place(&literal);
584 }
585}
586
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000587// Slow path generating a read barrier for a heap reference.
588class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
589 public:
590 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
591 Location out,
592 Location ref,
593 Location obj,
594 uint32_t offset,
595 Location index)
596 : instruction_(instruction),
597 out_(out),
598 ref_(ref),
599 obj_(obj),
600 offset_(offset),
601 index_(index) {
602 DCHECK(kEmitCompilerReadBarrier);
603 // If `obj` is equal to `out` or `ref`, it means the initial object
604 // has been overwritten by (or after) the heap object reference load
605 // to be instrumented, e.g.:
606 //
607 // __ Ldr(out, HeapOperand(out, class_offset);
608 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
609 //
610 // In that case, we have lost the information about the original
611 // object, and the emitted read barrier cannot work properly.
612 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
613 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
614 }
615
616 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
617 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
618 LocationSummary* locations = instruction_->GetLocations();
619 Primitive::Type type = Primitive::kPrimNot;
620 DCHECK(locations->CanCall());
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
622 DCHECK(!instruction_->IsInvoke() ||
623 (instruction_->IsInvokeStaticOrDirect() &&
624 instruction_->GetLocations()->Intrinsified()));
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000625 // The read barrier instrumentation does not support the
626 // HArm64IntermediateAddress instruction yet.
627 DCHECK(!(instruction_->IsArrayGet() &&
628 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000629
630 __ Bind(GetEntryLabel());
631
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000632 SaveLiveRegisters(codegen, locations);
633
634 // We may have to change the index's value, but as `index_` is a
635 // constant member (like other "inputs" of this slow path),
636 // introduce a copy of it, `index`.
637 Location index = index_;
638 if (index_.IsValid()) {
639 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
640 if (instruction_->IsArrayGet()) {
641 // Compute the actual memory offset and store it in `index`.
642 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
643 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
644 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
645 // We are about to change the value of `index_reg` (see the
646 // calls to vixl::MacroAssembler::Lsl and
647 // vixl::MacroAssembler::Mov below), but it has
648 // not been saved by the previous call to
649 // art::SlowPathCode::SaveLiveRegisters, as it is a
650 // callee-save register --
651 // art::SlowPathCode::SaveLiveRegisters does not consider
652 // callee-save registers, as it has been designed with the
653 // assumption that callee-save registers are supposed to be
654 // handled by the called function. So, as a callee-save
655 // register, `index_reg` _would_ eventually be saved onto
656 // the stack, but it would be too late: we would have
657 // changed its value earlier. Therefore, we manually save
658 // it here into another freely available register,
659 // `free_reg`, chosen of course among the caller-save
660 // registers (as a callee-save `free_reg` register would
661 // exhibit the same problem).
662 //
663 // Note we could have requested a temporary register from
664 // the register allocator instead; but we prefer not to, as
665 // this is a slow path, and we know we can find a
666 // caller-save register that is available.
667 Register free_reg = FindAvailableCallerSaveRegister(codegen);
668 __ Mov(free_reg.W(), index_reg);
669 index_reg = free_reg;
670 index = LocationFrom(index_reg);
671 } else {
672 // The initial register stored in `index_` has already been
673 // saved in the call to art::SlowPathCode::SaveLiveRegisters
674 // (as it is not a callee-save register), so we can freely
675 // use it.
676 }
677 // Shifting the index value contained in `index_reg` by the scale
678 // factor (2) cannot overflow in practice, as the runtime is
679 // unable to allocate object arrays with a size larger than
680 // 2^26 - 1 (that is, 2^28 - 4 bytes).
681 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
682 static_assert(
683 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
684 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
685 __ Add(index_reg, index_reg, Operand(offset_));
686 } else {
687 DCHECK(instruction_->IsInvoke());
688 DCHECK(instruction_->GetLocations()->Intrinsified());
689 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
690 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
691 << instruction_->AsInvoke()->GetIntrinsic();
692 DCHECK_EQ(offset_, 0U);
693 DCHECK(index_.IsRegisterPair());
694 // UnsafeGet's offset location is a register pair, the low
695 // part contains the correct offset.
696 index = index_.ToLow();
697 }
698 }
699
700 // We're moving two or three locations to locations that could
701 // overlap, so we need a parallel move resolver.
702 InvokeRuntimeCallingConvention calling_convention;
703 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
704 parallel_move.AddMove(ref_,
705 LocationFrom(calling_convention.GetRegisterAt(0)),
706 type,
707 nullptr);
708 parallel_move.AddMove(obj_,
709 LocationFrom(calling_convention.GetRegisterAt(1)),
710 type,
711 nullptr);
712 if (index.IsValid()) {
713 parallel_move.AddMove(index,
714 LocationFrom(calling_convention.GetRegisterAt(2)),
715 Primitive::kPrimInt,
716 nullptr);
717 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
718 } else {
719 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
720 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
721 }
722 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
723 instruction_,
724 instruction_->GetDexPc(),
725 this);
726 CheckEntrypointTypes<
727 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
728 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
729
730 RestoreLiveRegisters(codegen, locations);
731
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000732 __ B(GetExitLabel());
733 }
734
735 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
736
737 private:
738 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
739 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
740 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
741 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
742 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
743 return Register(VIXLRegCodeFromART(i), kXRegSize);
744 }
745 }
746 // We shall never fail to find a free caller-save register, as
747 // there are more than two core caller-save registers on ARM64
748 // (meaning it is possible to find one which is different from
749 // `ref` and `obj`).
750 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
751 LOG(FATAL) << "Could not find a free register";
752 UNREACHABLE();
753 }
754
755 HInstruction* const instruction_;
756 const Location out_;
757 const Location ref_;
758 const Location obj_;
759 const uint32_t offset_;
760 // An additional location containing an index to an array.
761 // Only used for HArrayGet and the UnsafeGetObject &
762 // UnsafeGetObjectVolatile intrinsics.
763 const Location index_;
764
765 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
766};
767
768// Slow path generating a read barrier for a GC root.
769class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
770 public:
771 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
772 : instruction_(instruction), out_(out), root_(root) {}
773
774 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
775 LocationSummary* locations = instruction_->GetLocations();
776 Primitive::Type type = Primitive::kPrimNot;
777 DCHECK(locations->CanCall());
778 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
779 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
780
781 __ Bind(GetEntryLabel());
782 SaveLiveRegisters(codegen, locations);
783
784 InvokeRuntimeCallingConvention calling_convention;
785 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
786 // The argument of the ReadBarrierForRootSlow is not a managed
787 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
788 // thus we need a 64-bit move here, and we cannot use
789 //
790 // arm64_codegen->MoveLocation(
791 // LocationFrom(calling_convention.GetRegisterAt(0)),
792 // root_,
793 // type);
794 //
795 // which would emit a 32-bit move, as `type` is a (32-bit wide)
796 // reference type (`Primitive::kPrimNot`).
797 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
798 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
799 instruction_,
800 instruction_->GetDexPc(),
801 this);
802 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
803 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
804
805 RestoreLiveRegisters(codegen, locations);
806 __ B(GetExitLabel());
807 }
808
809 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
810
811 private:
812 HInstruction* const instruction_;
813 const Location out_;
814 const Location root_;
815
816 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
817};
818
Alexandre Rames5319def2014-10-23 10:03:10 +0100819#undef __
820
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100821Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100822 Location next_location;
823 if (type == Primitive::kPrimVoid) {
824 LOG(FATAL) << "Unreachable type " << type;
825 }
826
Alexandre Rames542361f2015-01-29 16:57:31 +0000827 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100828 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
829 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000830 } else if (!Primitive::IsFloatingPointType(type) &&
831 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000832 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
833 } else {
834 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000835 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
836 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100837 }
838
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000839 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000840 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100841 return next_location;
842}
843
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100844Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100845 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100846}
847
Serban Constantinescu579885a2015-02-22 20:51:33 +0000848CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
849 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100850 const CompilerOptions& compiler_options,
851 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 : CodeGenerator(graph,
853 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000854 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000855 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000856 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000857 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100858 compiler_options,
859 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100860 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800861 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100862 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000863 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000864 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000865 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100866 uint64_literals_(std::less<uint64_t>(),
867 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
868 method_patches_(MethodReferenceComparator(),
869 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
870 call_patches_(MethodReferenceComparator(),
871 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
872 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000873 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000874 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000875 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000876}
Alexandre Rames5319def2014-10-23 10:03:10 +0100877
Alexandre Rames67555f72014-11-18 10:55:16 +0000878#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100879
Zheng Xu3927c8b2015-11-18 17:46:25 +0800880void CodeGeneratorARM64::EmitJumpTables() {
881 for (auto jump_table : jump_tables_) {
882 jump_table->EmitTable(this);
883 }
884}
885
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000886void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800887 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000888 // Ensure we emit the literal pool.
889 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000890
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000891 CodeGenerator::Finalize(allocator);
892}
893
Zheng Xuad4450e2015-04-17 18:48:56 +0800894void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
895 // Note: There are 6 kinds of moves:
896 // 1. constant -> GPR/FPR (non-cycle)
897 // 2. constant -> stack (non-cycle)
898 // 3. GPR/FPR -> GPR/FPR
899 // 4. GPR/FPR -> stack
900 // 5. stack -> GPR/FPR
901 // 6. stack -> stack (non-cycle)
902 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
903 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
904 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
905 // dependency.
906 vixl_temps_.Open(GetVIXLAssembler());
907}
908
909void ParallelMoveResolverARM64::FinishEmitNativeCode() {
910 vixl_temps_.Close();
911}
912
913Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
914 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
915 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
916 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
917 Location scratch = GetScratchLocation(kind);
918 if (!scratch.Equals(Location::NoLocation())) {
919 return scratch;
920 }
921 // Allocate from VIXL temp registers.
922 if (kind == Location::kRegister) {
923 scratch = LocationFrom(vixl_temps_.AcquireX());
924 } else {
925 DCHECK(kind == Location::kFpuRegister);
926 scratch = LocationFrom(vixl_temps_.AcquireD());
927 }
928 AddScratchLocation(scratch);
929 return scratch;
930}
931
932void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
933 if (loc.IsRegister()) {
934 vixl_temps_.Release(XRegisterFrom(loc));
935 } else {
936 DCHECK(loc.IsFpuRegister());
937 vixl_temps_.Release(DRegisterFrom(loc));
938 }
939 RemoveScratchLocation(loc);
940}
941
Alexandre Rames3e69f162014-12-10 10:36:50 +0000942void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100943 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100944 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000945}
946
Alexandre Rames5319def2014-10-23 10:03:10 +0100947void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100948 MacroAssembler* masm = GetVIXLAssembler();
949 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000950 __ Bind(&frame_entry_label_);
951
Serban Constantinescu02164b32014-11-13 14:05:07 +0000952 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
953 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100954 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000955 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000956 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000957 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000958 __ Ldr(wzr, MemOperand(temp, 0));
959 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000960 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100961
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000962 if (!HasEmptyFrame()) {
963 int frame_size = GetFrameSize();
964 // Stack layout:
965 // sp[frame_size - 8] : lr.
966 // ... : other preserved core registers.
967 // ... : other preserved fp registers.
968 // ... : reserved frame space.
969 // sp[0] : current method.
970 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100971 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800972 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
973 frame_size - GetCoreSpillSize());
974 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
975 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000976 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100977}
978
979void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100980 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100981 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000982 if (!HasEmptyFrame()) {
983 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800984 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
985 frame_size - FrameEntrySpillSize());
986 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
987 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000988 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100989 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000990 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100991 __ Ret();
992 GetAssembler()->cfi().RestoreState();
993 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100994}
995
Zheng Xuda403092015-04-24 17:35:39 +0800996vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
997 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
998 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
999 core_spill_mask_);
1000}
1001
1002vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1003 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1004 GetNumberOfFloatingPointRegisters()));
1005 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1006 fpu_spill_mask_);
1007}
1008
Alexandre Rames5319def2014-10-23 10:03:10 +01001009void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1010 __ Bind(GetLabelOf(block));
1011}
1012
Alexandre Rames5319def2014-10-23 10:03:10 +01001013void CodeGeneratorARM64::Move(HInstruction* instruction,
1014 Location location,
1015 HInstruction* move_for) {
1016 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001017 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001018 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +01001019
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001020 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 MoveLocation(location,
1022 Location::DoubleStackSlot(kCurrentMethodStackOffset),
1023 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001024 } else if (locations != nullptr && locations->Out().Equals(location)) {
1025 return;
1026 } else if (instruction->IsIntConstant()
1027 || instruction->IsLongConstant()
1028 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001029 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +01001030 if (location.IsRegister()) {
1031 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001032 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +01001033 (instruction->IsLongConstant() && dst.Is64Bits()));
1034 __ Mov(dst, value);
1035 } else {
1036 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001037 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001038 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
1039 ? temps.AcquireW()
1040 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +01001041 __ Mov(temp, value);
1042 __ Str(temp, StackOperandFrom(location));
1043 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001044 } else if (instruction->IsTemporary()) {
1045 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001046 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001047 } else if (instruction->IsLoadLocal()) {
1048 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +00001049 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001050 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001051 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001052 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001053 }
1054
1055 } else {
1056 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001057 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001058 }
1059}
1060
Calin Juravle175dc732015-08-25 15:42:32 +01001061void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1062 DCHECK(location.IsRegister());
1063 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1064}
1065
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1067 if (location.IsRegister()) {
1068 locations->AddTemp(location);
1069 } else {
1070 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1071 }
1072}
1073
Alexandre Rames5319def2014-10-23 10:03:10 +01001074Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1075 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001076
Alexandre Rames5319def2014-10-23 10:03:10 +01001077 switch (type) {
1078 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001079 case Primitive::kPrimInt:
1080 case Primitive::kPrimFloat:
1081 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1082
1083 case Primitive::kPrimLong:
1084 case Primitive::kPrimDouble:
1085 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1086
Alexandre Rames5319def2014-10-23 10:03:10 +01001087 case Primitive::kPrimBoolean:
1088 case Primitive::kPrimByte:
1089 case Primitive::kPrimChar:
1090 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001091 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001092 LOG(FATAL) << "Unexpected type " << type;
1093 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001094
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 LOG(FATAL) << "Unreachable";
1096 return Location::NoLocation();
1097}
1098
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001099void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001100 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001102 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001103 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001104 if (value_can_be_null) {
1105 __ Cbz(value, &done);
1106 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1108 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001109 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001110 if (value_can_be_null) {
1111 __ Bind(&done);
1112 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001113}
1114
David Brazdil58282f42016-01-14 12:45:10 +00001115void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001116 // Blocked core registers:
1117 // lr : Runtime reserved.
1118 // tr : Runtime reserved.
1119 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1120 // ip1 : VIXL core temp.
1121 // ip0 : VIXL core temp.
1122 //
1123 // Blocked fp registers:
1124 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001125 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1126 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001127 while (!reserved_core_registers.IsEmpty()) {
1128 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1129 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001130
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001132 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001133 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1134 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001135
David Brazdil58282f42016-01-14 12:45:10 +00001136 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001137 // Stubs do not save callee-save floating point registers. If the graph
1138 // is debuggable, we need to deal with these registers differently. For
1139 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001140 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1141 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1142 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001143 }
1144 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001145}
1146
Alexandre Rames3e69f162014-12-10 10:36:50 +00001147size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1148 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1149 __ Str(reg, MemOperand(sp, stack_index));
1150 return kArm64WordSize;
1151}
1152
1153size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1155 __ Ldr(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 FPRegister reg = FPRegister(reg_id, kDRegSize);
1161 __ Str(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
1165size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1166 FPRegister reg = FPRegister(reg_id, kDRegSize);
1167 __ Ldr(reg, MemOperand(sp, stack_index));
1168 return kArm64WordSize;
1169}
1170
Alexandre Rames5319def2014-10-23 10:03:10 +01001171void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001172 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001173}
1174
1175void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001176 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001177}
1178
Alexandre Rames67555f72014-11-18 10:55:16 +00001179void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001180 if (constant->IsIntConstant()) {
1181 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1182 } else if (constant->IsLongConstant()) {
1183 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1184 } else if (constant->IsNullConstant()) {
1185 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001186 } else if (constant->IsFloatConstant()) {
1187 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1188 } else {
1189 DCHECK(constant->IsDoubleConstant());
1190 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1191 }
1192}
1193
Alexandre Rames3e69f162014-12-10 10:36:50 +00001194
1195static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1196 DCHECK(constant.IsConstant());
1197 HConstant* cst = constant.GetConstant();
1198 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001199 // Null is mapped to a core W register, which we associate with kPrimInt.
1200 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001201 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1202 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1203 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1204}
1205
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206void CodeGeneratorARM64::MoveLocation(Location destination,
1207 Location source,
1208 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001209 if (source.Equals(destination)) {
1210 return;
1211 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212
1213 // A valid move can always be inferred from the destination and source
1214 // locations. When moving from and to a register, the argument type can be
1215 // used to generate 32bit instead of 64bit moves. In debug mode we also
1216 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001218
1219 if (destination.IsRegister() || destination.IsFpuRegister()) {
1220 if (unspecified_type) {
1221 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1222 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001223 (src_cst != nullptr && (src_cst->IsIntConstant()
1224 || src_cst->IsFloatConstant()
1225 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001226 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 // If the source is a double stack slot or a 64bit constant, a 64bit
1230 // type is appropriate. Else the source is a register, and since the
1231 // type has not been specified, we chose a 64bit type to force a 64bit
1232 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001235 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1237 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1238 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001239 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1240 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1241 __ Ldr(dst, StackOperandFrom(source));
1242 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001243 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001244 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001246 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001248 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001249 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1251 ? Primitive::kPrimLong
1252 : Primitive::kPrimInt;
1253 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1254 }
1255 } else {
1256 DCHECK(source.IsFpuRegister());
1257 if (destination.IsRegister()) {
1258 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1259 ? Primitive::kPrimDouble
1260 : Primitive::kPrimFloat;
1261 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1262 } else {
1263 DCHECK(destination.IsFpuRegister());
1264 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001265 }
1266 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 } else { // The destination is not a register. It must be a stack slot.
1268 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1269 if (source.IsRegister() || source.IsFpuRegister()) {
1270 if (unspecified_type) {
1271 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001273 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001275 }
1276 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1278 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1279 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1282 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001283 UseScratchRegisterScope temps(GetVIXLAssembler());
1284 HConstant* src_cst = source.GetConstant();
1285 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001286 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001287 temp = temps.AcquireW();
1288 } else if (src_cst->IsLongConstant()) {
1289 temp = temps.AcquireX();
1290 } else if (src_cst->IsFloatConstant()) {
1291 temp = temps.AcquireS();
1292 } else {
1293 DCHECK(src_cst->IsDoubleConstant());
1294 temp = temps.AcquireD();
1295 }
1296 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001297 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001298 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001300 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001301 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001302 // There is generally less pressure on FP registers.
1303 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001304 __ Ldr(temp, StackOperandFrom(source));
1305 __ Str(temp, StackOperandFrom(destination));
1306 }
1307 }
1308}
1309
1310void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001311 CPURegister dst,
1312 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001313 switch (type) {
1314 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001315 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316 break;
1317 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001319 break;
1320 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001321 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001322 break;
1323 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001324 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001325 break;
1326 case Primitive::kPrimInt:
1327 case Primitive::kPrimNot:
1328 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001329 case Primitive::kPrimFloat:
1330 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001331 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001332 __ Ldr(dst, src);
1333 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001334 case Primitive::kPrimVoid:
1335 LOG(FATAL) << "Unreachable type " << type;
1336 }
1337}
1338
Calin Juravle77520bc2015-01-12 18:45:46 +00001339void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001340 CPURegister dst,
1341 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001342 MacroAssembler* masm = GetVIXLAssembler();
1343 BlockPoolsScope block_pools(masm);
1344 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001346 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001347
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001348 DCHECK(!src.IsPreIndex());
1349 DCHECK(!src.IsPostIndex());
1350
1351 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001352 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001353 MemOperand base = MemOperand(temp_base);
1354 switch (type) {
1355 case Primitive::kPrimBoolean:
1356 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001357 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 break;
1359 case Primitive::kPrimByte:
1360 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001361 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001362 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1363 break;
1364 case Primitive::kPrimChar:
1365 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001366 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001367 break;
1368 case Primitive::kPrimShort:
1369 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001370 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001371 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1372 break;
1373 case Primitive::kPrimInt:
1374 case Primitive::kPrimNot:
1375 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001376 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001377 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001378 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001379 break;
1380 case Primitive::kPrimFloat:
1381 case Primitive::kPrimDouble: {
1382 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001383 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384
1385 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1386 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001387 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001388 __ Fmov(FPRegister(dst), temp);
1389 break;
1390 }
1391 case Primitive::kPrimVoid:
1392 LOG(FATAL) << "Unreachable type " << type;
1393 }
1394}
1395
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001396void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001397 CPURegister src,
1398 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001399 switch (type) {
1400 case Primitive::kPrimBoolean:
1401 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001403 break;
1404 case Primitive::kPrimChar:
1405 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001407 break;
1408 case Primitive::kPrimInt:
1409 case Primitive::kPrimNot:
1410 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001411 case Primitive::kPrimFloat:
1412 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001413 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001414 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001415 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001416 case Primitive::kPrimVoid:
1417 LOG(FATAL) << "Unreachable type " << type;
1418 }
1419}
1420
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1422 CPURegister src,
1423 const MemOperand& dst) {
1424 UseScratchRegisterScope temps(GetVIXLAssembler());
1425 Register temp_base = temps.AcquireX();
1426
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001427 DCHECK(!dst.IsPreIndex());
1428 DCHECK(!dst.IsPostIndex());
1429
1430 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001431 Operand op = OperandFromMemOperand(dst);
1432 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001433 MemOperand base = MemOperand(temp_base);
1434 switch (type) {
1435 case Primitive::kPrimBoolean:
1436 case Primitive::kPrimByte:
1437 __ Stlrb(Register(src), base);
1438 break;
1439 case Primitive::kPrimChar:
1440 case Primitive::kPrimShort:
1441 __ Stlrh(Register(src), base);
1442 break;
1443 case Primitive::kPrimInt:
1444 case Primitive::kPrimNot:
1445 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001446 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001447 __ Stlr(Register(src), base);
1448 break;
1449 case Primitive::kPrimFloat:
1450 case Primitive::kPrimDouble: {
1451 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001452 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001453
1454 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1455 __ Fmov(temp, FPRegister(src));
1456 __ Stlr(temp, base);
1457 break;
1458 }
1459 case Primitive::kPrimVoid:
1460 LOG(FATAL) << "Unreachable type " << type;
1461 }
1462}
1463
Calin Juravle175dc732015-08-25 15:42:32 +01001464void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1465 HInstruction* instruction,
1466 uint32_t dex_pc,
1467 SlowPathCode* slow_path) {
1468 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1469 instruction,
1470 dex_pc,
1471 slow_path);
1472}
1473
Alexandre Rames67555f72014-11-18 10:55:16 +00001474void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1475 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001476 uint32_t dex_pc,
1477 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001478 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001479 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001480 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1481 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001482 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001483}
1484
1485void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1486 vixl::Register class_reg) {
1487 UseScratchRegisterScope temps(GetVIXLAssembler());
1488 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001489 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001490 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001491
Serban Constantinescu02164b32014-11-13 14:05:07 +00001492 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001493 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001494 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1495 __ Add(temp, class_reg, status_offset);
1496 __ Ldar(temp, HeapOperand(temp));
1497 __ Cmp(temp, mirror::Class::kStatusInitialized);
1498 __ B(lt, slow_path->GetEntryLabel());
1499 } else {
1500 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1501 __ Cmp(temp, mirror::Class::kStatusInitialized);
1502 __ B(lt, slow_path->GetEntryLabel());
1503 __ Dmb(InnerShareable, BarrierReads);
1504 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001505 __ Bind(slow_path->GetExitLabel());
1506}
Alexandre Rames5319def2014-10-23 10:03:10 +01001507
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001508void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1509 BarrierType type = BarrierAll;
1510
1511 switch (kind) {
1512 case MemBarrierKind::kAnyAny:
1513 case MemBarrierKind::kAnyStore: {
1514 type = BarrierAll;
1515 break;
1516 }
1517 case MemBarrierKind::kLoadAny: {
1518 type = BarrierReads;
1519 break;
1520 }
1521 case MemBarrierKind::kStoreStore: {
1522 type = BarrierWrites;
1523 break;
1524 }
1525 default:
1526 LOG(FATAL) << "Unexpected memory barrier " << kind;
1527 }
1528 __ Dmb(InnerShareable, type);
1529}
1530
Serban Constantinescu02164b32014-11-13 14:05:07 +00001531void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1532 HBasicBlock* successor) {
1533 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001534 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1535 if (slow_path == nullptr) {
1536 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1537 instruction->SetSlowPath(slow_path);
1538 codegen_->AddSlowPath(slow_path);
1539 if (successor != nullptr) {
1540 DCHECK(successor->IsLoopHeader());
1541 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1542 }
1543 } else {
1544 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1545 }
1546
Serban Constantinescu02164b32014-11-13 14:05:07 +00001547 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1548 Register temp = temps.AcquireW();
1549
1550 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1551 if (successor == nullptr) {
1552 __ Cbnz(temp, slow_path->GetEntryLabel());
1553 __ Bind(slow_path->GetReturnLabel());
1554 } else {
1555 __ Cbz(temp, codegen_->GetLabelOf(successor));
1556 __ B(slow_path->GetEntryLabel());
1557 // slow_path will return to GetLabelOf(successor).
1558 }
1559}
1560
Alexandre Rames5319def2014-10-23 10:03:10 +01001561InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1562 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001563 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001564 assembler_(codegen->GetAssembler()),
1565 codegen_(codegen) {}
1566
1567#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001568 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001569
1570#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1571
1572enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001573 // Using a base helps identify when we hit such breakpoints.
1574 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001575#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1576 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1577#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1578};
1579
1580#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001581 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001582 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1583 } \
1584 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1586 locations->SetOut(Location::Any()); \
1587 }
1588 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1589#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1590
1591#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001592#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001593
Alexandre Rames67555f72014-11-18 10:55:16 +00001594void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001595 DCHECK_EQ(instr->InputCount(), 2U);
1596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1597 Primitive::Type type = instr->GetResultType();
1598 switch (type) {
1599 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001600 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001602 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001603 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001604 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001605
1606 case Primitive::kPrimFloat:
1607 case Primitive::kPrimDouble:
1608 locations->SetInAt(0, Location::RequiresFpuRegister());
1609 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001610 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001611 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001612
Alexandre Rames5319def2014-10-23 10:03:10 +01001613 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001614 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001615 }
1616}
1617
Alexandre Rames09a99962015-04-15 11:47:56 +01001618void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001619 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1620
1621 bool object_field_get_with_read_barrier =
1622 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001623 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001624 new (GetGraph()->GetArena()) LocationSummary(instruction,
1625 object_field_get_with_read_barrier ?
1626 LocationSummary::kCallOnSlowPath :
1627 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001628 locations->SetInAt(0, Location::RequiresRegister());
1629 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1630 locations->SetOut(Location::RequiresFpuRegister());
1631 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001632 // The output overlaps for an object field get when read barriers
1633 // are enabled: we do not want the load to overwrite the object's
1634 // location, as we need it to emit the read barrier.
1635 locations->SetOut(
1636 Location::RequiresRegister(),
1637 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001638 }
1639}
1640
1641void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1642 const FieldInfo& field_info) {
1643 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001644 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001645 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001646
1647 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1648 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1649
1650 if (field_info.IsVolatile()) {
1651 if (use_acquire_release) {
1652 // NB: LoadAcquire will record the pc info if needed.
1653 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1654 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001655 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001656 codegen_->MaybeRecordImplicitNullCheck(instruction);
1657 // For IRIW sequential consistency kLoadAny is not sufficient.
1658 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1659 }
1660 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001661 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001662 codegen_->MaybeRecordImplicitNullCheck(instruction);
1663 }
Roland Levillain4d027112015-07-01 15:41:14 +01001664
1665 if (field_type == Primitive::kPrimNot) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001666 LocationSummary* locations = instruction->GetLocations();
1667 Location base = locations->InAt(0);
1668 Location out = locations->Out();
1669 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
1670 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01001671 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001672}
1673
1674void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1675 LocationSummary* locations =
1676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1677 locations->SetInAt(0, Location::RequiresRegister());
1678 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1679 locations->SetInAt(1, Location::RequiresFpuRegister());
1680 } else {
1681 locations->SetInAt(1, Location::RequiresRegister());
1682 }
1683}
1684
1685void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001686 const FieldInfo& field_info,
1687 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001688 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001689 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001690
1691 Register obj = InputRegisterAt(instruction, 0);
1692 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001693 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001694 Offset offset = field_info.GetFieldOffset();
1695 Primitive::Type field_type = field_info.GetFieldType();
1696 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1697
Roland Levillain4d027112015-07-01 15:41:14 +01001698 {
1699 // We use a block to end the scratch scope before the write barrier, thus
1700 // freeing the temporary registers so they can be used in `MarkGCCard`.
1701 UseScratchRegisterScope temps(GetVIXLAssembler());
1702
1703 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1704 DCHECK(value.IsW());
1705 Register temp = temps.AcquireW();
1706 __ Mov(temp, value.W());
1707 GetAssembler()->PoisonHeapReference(temp.W());
1708 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001709 }
Roland Levillain4d027112015-07-01 15:41:14 +01001710
1711 if (field_info.IsVolatile()) {
1712 if (use_acquire_release) {
1713 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1714 codegen_->MaybeRecordImplicitNullCheck(instruction);
1715 } else {
1716 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1717 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1718 codegen_->MaybeRecordImplicitNullCheck(instruction);
1719 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1720 }
1721 } else {
1722 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1723 codegen_->MaybeRecordImplicitNullCheck(instruction);
1724 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001725 }
1726
1727 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001728 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001729 }
1730}
1731
Alexandre Rames67555f72014-11-18 10:55:16 +00001732void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001733 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001734
1735 switch (type) {
1736 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001737 case Primitive::kPrimLong: {
1738 Register dst = OutputRegister(instr);
1739 Register lhs = InputRegisterAt(instr, 0);
1740 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001741 if (instr->IsAdd()) {
1742 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001743 } else if (instr->IsAnd()) {
1744 __ And(dst, lhs, rhs);
1745 } else if (instr->IsOr()) {
1746 __ Orr(dst, lhs, rhs);
1747 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001748 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001749 } else if (instr->IsRor()) {
1750 if (rhs.IsImmediate()) {
1751 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1752 __ Ror(dst, lhs, shift);
1753 } else {
1754 // Ensure shift distance is in the same size register as the result. If
1755 // we are rotating a long and the shift comes in a w register originally,
1756 // we don't need to sxtw for use as an x since the shift distances are
1757 // all & reg_bits - 1.
1758 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1759 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001760 } else {
1761 DCHECK(instr->IsXor());
1762 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001763 }
1764 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001765 }
1766 case Primitive::kPrimFloat:
1767 case Primitive::kPrimDouble: {
1768 FPRegister dst = OutputFPRegister(instr);
1769 FPRegister lhs = InputFPRegisterAt(instr, 0);
1770 FPRegister rhs = InputFPRegisterAt(instr, 1);
1771 if (instr->IsAdd()) {
1772 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001773 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001774 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001775 } else {
1776 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001777 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001778 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001779 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001780 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001781 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001782 }
1783}
1784
Serban Constantinescu02164b32014-11-13 14:05:07 +00001785void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1786 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1787
1788 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1789 Primitive::Type type = instr->GetResultType();
1790 switch (type) {
1791 case Primitive::kPrimInt:
1792 case Primitive::kPrimLong: {
1793 locations->SetInAt(0, Location::RequiresRegister());
1794 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1795 locations->SetOut(Location::RequiresRegister());
1796 break;
1797 }
1798 default:
1799 LOG(FATAL) << "Unexpected shift type " << type;
1800 }
1801}
1802
1803void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1804 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1805
1806 Primitive::Type type = instr->GetType();
1807 switch (type) {
1808 case Primitive::kPrimInt:
1809 case Primitive::kPrimLong: {
1810 Register dst = OutputRegister(instr);
1811 Register lhs = InputRegisterAt(instr, 0);
1812 Operand rhs = InputOperandAt(instr, 1);
1813 if (rhs.IsImmediate()) {
1814 uint32_t shift_value = (type == Primitive::kPrimInt)
1815 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1816 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1817 if (instr->IsShl()) {
1818 __ Lsl(dst, lhs, shift_value);
1819 } else if (instr->IsShr()) {
1820 __ Asr(dst, lhs, shift_value);
1821 } else {
1822 __ Lsr(dst, lhs, shift_value);
1823 }
1824 } else {
1825 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1826
1827 if (instr->IsShl()) {
1828 __ Lsl(dst, lhs, rhs_reg);
1829 } else if (instr->IsShr()) {
1830 __ Asr(dst, lhs, rhs_reg);
1831 } else {
1832 __ Lsr(dst, lhs, rhs_reg);
1833 }
1834 }
1835 break;
1836 }
1837 default:
1838 LOG(FATAL) << "Unexpected shift operation type " << type;
1839 }
1840}
1841
Alexandre Rames5319def2014-10-23 10:03:10 +01001842void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001843 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001844}
1845
1846void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001847 HandleBinaryOp(instruction);
1848}
1849
1850void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1851 HandleBinaryOp(instruction);
1852}
1853
1854void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1855 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001856}
1857
Alexandre Rames8626b742015-11-25 16:28:08 +00001858void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1859 HArm64DataProcWithShifterOp* instruction) {
1860 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1861 instruction->GetType() == Primitive::kPrimLong);
1862 LocationSummary* locations =
1863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1864 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1865 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1866 } else {
1867 locations->SetInAt(0, Location::RequiresRegister());
1868 }
1869 locations->SetInAt(1, Location::RequiresRegister());
1870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1871}
1872
1873void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1874 HArm64DataProcWithShifterOp* instruction) {
1875 Primitive::Type type = instruction->GetType();
1876 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1877 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1878 Register out = OutputRegister(instruction);
1879 Register left;
1880 if (kind != HInstruction::kNeg) {
1881 left = InputRegisterAt(instruction, 0);
1882 }
1883 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1884 // shifter operand operation, the IR generating `right_reg` (input to the type
1885 // conversion) can have a different type from the current instruction's type,
1886 // so we manually indicate the type.
1887 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
1888 int64_t shift_amount = (type == Primitive::kPrimInt)
1889 ? static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxIntShiftValue)
1890 : static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxLongShiftValue);
1891
1892 Operand right_operand(0);
1893
1894 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1895 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1896 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1897 } else {
1898 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1899 }
1900
1901 // Logical binary operations do not support extension operations in the
1902 // operand. Note that VIXL would still manage if it was passed by generating
1903 // the extension as a separate instruction.
1904 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1905 DCHECK(!right_operand.IsExtendedRegister() ||
1906 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1907 kind != HInstruction::kNeg));
1908 switch (kind) {
1909 case HInstruction::kAdd:
1910 __ Add(out, left, right_operand);
1911 break;
1912 case HInstruction::kAnd:
1913 __ And(out, left, right_operand);
1914 break;
1915 case HInstruction::kNeg:
1916 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1917 __ Neg(out, right_operand);
1918 break;
1919 case HInstruction::kOr:
1920 __ Orr(out, left, right_operand);
1921 break;
1922 case HInstruction::kSub:
1923 __ Sub(out, left, right_operand);
1924 break;
1925 case HInstruction::kXor:
1926 __ Eor(out, left, right_operand);
1927 break;
1928 default:
1929 LOG(FATAL) << "Unexpected operation kind: " << kind;
1930 UNREACHABLE();
1931 }
1932}
1933
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001934void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001935 // The read barrier instrumentation does not support the
1936 // HArm64IntermediateAddress instruction yet.
1937 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001938 LocationSummary* locations =
1939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1940 locations->SetInAt(0, Location::RequiresRegister());
1941 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1942 locations->SetOut(Location::RequiresRegister());
1943}
1944
1945void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1946 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001947 // The read barrier instrumentation does not support the
1948 // HArm64IntermediateAddress instruction yet.
1949 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001950 __ Add(OutputRegister(instruction),
1951 InputRegisterAt(instruction, 0),
1952 Operand(InputOperandAt(instruction, 1)));
1953}
1954
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001955void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001956 LocationSummary* locations =
1957 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001958 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1959 if (instr->GetOpKind() == HInstruction::kSub &&
1960 accumulator->IsConstant() &&
1961 accumulator->AsConstant()->IsZero()) {
1962 // Don't allocate register for Mneg instruction.
1963 } else {
1964 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1965 Location::RequiresRegister());
1966 }
1967 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1968 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00001969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1970}
1971
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001972void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001973 Register res = OutputRegister(instr);
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001974 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
1975 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00001976
1977 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1978 // This fixup should be carried out for all multiply-accumulate instructions:
1979 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1980 if (instr->GetType() == Primitive::kPrimLong &&
1981 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1982 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
1983 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
1984 if (prev->IsLoadOrStore()) {
1985 // Make sure we emit only exactly one nop.
1986 vixl::CodeBufferCheckScope scope(masm,
1987 vixl::kInstructionSize,
1988 vixl::CodeBufferCheckScope::kCheck,
1989 vixl::CodeBufferCheckScope::kExactSize);
1990 __ nop();
1991 }
1992 }
1993
1994 if (instr->GetOpKind() == HInstruction::kAdd) {
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001995 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00001996 __ Madd(res, mul_left, mul_right, accumulator);
1997 } else {
1998 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001999 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2000 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsZero()) {
2001 __ Mneg(res, mul_left, mul_right);
2002 } else {
2003 Register accumulator = InputRegisterAt(instr,
2004 HMultiplyAccumulate::kInputAccumulatorIndex);
2005 __ Msub(res, mul_left, mul_right, accumulator);
2006 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002007 }
2008}
2009
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002010void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002011 bool object_array_get_with_read_barrier =
2012 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002013 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002014 new (GetGraph()->GetArena()) LocationSummary(instruction,
2015 object_array_get_with_read_barrier ?
2016 LocationSummary::kCallOnSlowPath :
2017 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002018 locations->SetInAt(0, Location::RequiresRegister());
2019 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002020 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2021 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2022 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002023 // The output overlaps in the case of an object array get with
2024 // read barriers enabled: we do not want the move to overwrite the
2025 // array's location, as we need it to emit the read barrier.
2026 locations->SetOut(
2027 Location::RequiresRegister(),
2028 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002029 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002030}
2031
2032void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002033 Primitive::Type type = instruction->GetType();
2034 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002035 LocationSummary* locations = instruction->GetLocations();
2036 Location index = locations->InAt(1);
2037 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002038 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002039 CPURegister dest = OutputCPURegister(instruction);
2040
Alexandre Ramesd921d642015-04-16 15:07:16 +01002041 MacroAssembler* masm = GetVIXLAssembler();
2042 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002043 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002044 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002045
2046 if (index.IsConstant()) {
2047 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002048 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002049 } else {
2050 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002051 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002052 // The read barrier instrumentation does not support the
2053 // HArm64IntermediateAddress instruction yet.
2054 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002055 // We do not need to compute the intermediate address from the array: the
2056 // input instruction has done it already. See the comment in
2057 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2058 if (kIsDebugBuild) {
2059 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2060 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2061 }
2062 temp = obj;
2063 } else {
2064 __ Add(temp, obj, offset);
2065 }
Alexandre Rames82000b02015-07-07 11:34:16 +01002066 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002067 }
2068
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002069 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00002070 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002071
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002072 if (type == Primitive::kPrimNot) {
2073 static_assert(
2074 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2075 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2076 Location obj_loc = locations->InAt(0);
2077 Location out = locations->Out();
2078 if (index.IsConstant()) {
2079 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
2080 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002081 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset, index);
2082 }
Roland Levillain4d027112015-07-01 15:41:14 +01002083 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002084}
2085
Alexandre Rames5319def2014-10-23 10:03:10 +01002086void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2087 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2088 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002090}
2091
2092void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002093 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002094 __ Ldr(OutputRegister(instruction),
2095 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002096 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002097}
2098
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002099void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002100 Primitive::Type value_type = instruction->GetComponentType();
2101
2102 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2103 bool object_array_set_with_read_barrier =
2104 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2106 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002107 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2108 LocationSummary::kCallOnSlowPath :
2109 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002110 locations->SetInAt(0, Location::RequiresRegister());
2111 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002112 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002113 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002114 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002115 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002116 }
2117}
2118
2119void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2120 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002121 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002122 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002123 bool needs_write_barrier =
2124 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002125
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002126 Register array = InputRegisterAt(instruction, 0);
2127 CPURegister value = InputCPURegisterAt(instruction, 2);
2128 CPURegister source = value;
2129 Location index = locations->InAt(1);
2130 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2131 MemOperand destination = HeapOperand(array);
2132 MacroAssembler* masm = GetVIXLAssembler();
2133 BlockPoolsScope block_pools(masm);
2134
2135 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002136 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002137 if (index.IsConstant()) {
2138 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2139 destination = HeapOperand(array, offset);
2140 } else {
2141 UseScratchRegisterScope temps(masm);
2142 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002143 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002144 // The read barrier instrumentation does not support the
2145 // HArm64IntermediateAddress instruction yet.
2146 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002147 // We do not need to compute the intermediate address from the array: the
2148 // input instruction has done it already. See the comment in
2149 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2150 if (kIsDebugBuild) {
2151 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2152 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2153 }
2154 temp = array;
2155 } else {
2156 __ Add(temp, array, offset);
2157 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002158 destination = HeapOperand(temp,
2159 XRegisterFrom(index),
2160 LSL,
2161 Primitive::ComponentSizeShift(value_type));
2162 }
2163 codegen_->Store(value_type, value, destination);
2164 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002165 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002166 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002167 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 vixl::Label done;
2169 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002170 {
2171 // We use a block to end the scratch scope before the write barrier, thus
2172 // freeing the temporary registers so they can be used in `MarkGCCard`.
2173 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002174 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002175 if (index.IsConstant()) {
2176 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002177 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002178 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002179 destination = HeapOperand(temp,
2180 XRegisterFrom(index),
2181 LSL,
2182 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002183 }
2184
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002185 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2186 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2187 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2188
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002189 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002190 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2191 codegen_->AddSlowPath(slow_path);
2192 if (instruction->GetValueCanBeNull()) {
2193 vixl::Label non_zero;
2194 __ Cbnz(Register(value), &non_zero);
2195 if (!index.IsConstant()) {
2196 __ Add(temp, array, offset);
2197 }
2198 __ Str(wzr, destination);
2199 codegen_->MaybeRecordImplicitNullCheck(instruction);
2200 __ B(&done);
2201 __ Bind(&non_zero);
2202 }
2203
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002204 if (kEmitCompilerReadBarrier) {
2205 // When read barriers are enabled, the type checking
2206 // instrumentation requires two read barriers:
2207 //
2208 // __ Mov(temp2, temp);
2209 // // /* HeapReference<Class> */ temp = temp->component_type_
2210 // __ Ldr(temp, HeapOperand(temp, component_offset));
2211 // codegen_->GenerateReadBarrier(
2212 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2213 //
2214 // // /* HeapReference<Class> */ temp2 = value->klass_
2215 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2216 // codegen_->GenerateReadBarrier(
2217 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2218 //
2219 // __ Cmp(temp, temp2);
2220 //
2221 // However, the second read barrier may trash `temp`, as it
2222 // is a temporary register, and as such would not be saved
2223 // along with live registers before calling the runtime (nor
2224 // restored afterwards). So in this case, we bail out and
2225 // delegate the work to the array set slow path.
2226 //
2227 // TODO: Extend the register allocator to support a new
2228 // "(locally) live temp" location so as to avoid always
2229 // going into the slow path when read barriers are enabled.
2230 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002231 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002232 Register temp2 = temps.AcquireSameSizeAs(array);
2233 // /* HeapReference<Class> */ temp = array->klass_
2234 __ Ldr(temp, HeapOperand(array, class_offset));
2235 codegen_->MaybeRecordImplicitNullCheck(instruction);
2236 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2237
2238 // /* HeapReference<Class> */ temp = temp->component_type_
2239 __ Ldr(temp, HeapOperand(temp, component_offset));
2240 // /* HeapReference<Class> */ temp2 = value->klass_
2241 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2242 // If heap poisoning is enabled, no need to unpoison `temp`
2243 // nor `temp2`, as we are comparing two poisoned references.
2244 __ Cmp(temp, temp2);
2245
2246 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2247 vixl::Label do_put;
2248 __ B(eq, &do_put);
2249 // If heap poisoning is enabled, the `temp` reference has
2250 // not been unpoisoned yet; unpoison it now.
2251 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2252
2253 // /* HeapReference<Class> */ temp = temp->super_class_
2254 __ Ldr(temp, HeapOperand(temp, super_offset));
2255 // If heap poisoning is enabled, no need to unpoison
2256 // `temp`, as we are comparing against null below.
2257 __ Cbnz(temp, slow_path->GetEntryLabel());
2258 __ Bind(&do_put);
2259 } else {
2260 __ B(ne, slow_path->GetEntryLabel());
2261 }
2262 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002263 }
2264 }
2265
2266 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002267 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002268 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002269 __ Mov(temp2, value.W());
2270 GetAssembler()->PoisonHeapReference(temp2);
2271 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002272 }
2273
2274 if (!index.IsConstant()) {
2275 __ Add(temp, array, offset);
2276 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002277 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002278
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002279 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002280 codegen_->MaybeRecordImplicitNullCheck(instruction);
2281 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002282 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002283
2284 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2285
2286 if (done.IsLinked()) {
2287 __ Bind(&done);
2288 }
2289
2290 if (slow_path != nullptr) {
2291 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002292 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002293 }
2294}
2295
Alexandre Rames67555f72014-11-18 10:55:16 +00002296void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002297 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2298 ? LocationSummary::kCallOnSlowPath
2299 : LocationSummary::kNoCall;
2300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002301 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002302 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002303 if (instruction->HasUses()) {
2304 locations->SetOut(Location::SameAsFirstInput());
2305 }
2306}
2307
2308void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002309 BoundsCheckSlowPathARM64* slow_path =
2310 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002311 codegen_->AddSlowPath(slow_path);
2312
2313 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2314 __ B(slow_path->GetEntryLabel(), hs);
2315}
2316
Alexandre Rames67555f72014-11-18 10:55:16 +00002317void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2318 LocationSummary* locations =
2319 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2320 locations->SetInAt(0, Location::RequiresRegister());
2321 if (check->HasUses()) {
2322 locations->SetOut(Location::SameAsFirstInput());
2323 }
2324}
2325
2326void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2327 // We assume the class is not null.
2328 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2329 check->GetLoadClass(), check, check->GetDexPc(), true);
2330 codegen_->AddSlowPath(slow_path);
2331 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2332}
2333
Roland Levillain7f63c522015-07-13 15:54:55 +00002334static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2335 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2336 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2337}
2338
Serban Constantinescu02164b32014-11-13 14:05:07 +00002339void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002340 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002341 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2342 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002343 switch (in_type) {
2344 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002345 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002346 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2348 break;
2349 }
2350 case Primitive::kPrimFloat:
2351 case Primitive::kPrimDouble: {
2352 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002353 locations->SetInAt(1,
2354 IsFloatingPointZeroConstant(compare->InputAt(1))
2355 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2356 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002357 locations->SetOut(Location::RequiresRegister());
2358 break;
2359 }
2360 default:
2361 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2362 }
2363}
2364
2365void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2366 Primitive::Type in_type = compare->InputAt(0)->GetType();
2367
2368 // 0 if: left == right
2369 // 1 if: left > right
2370 // -1 if: left < right
2371 switch (in_type) {
2372 case Primitive::kPrimLong: {
2373 Register result = OutputRegister(compare);
2374 Register left = InputRegisterAt(compare, 0);
2375 Operand right = InputOperandAt(compare, 1);
2376
2377 __ Cmp(left, right);
2378 __ Cset(result, ne);
2379 __ Cneg(result, result, lt);
2380 break;
2381 }
2382 case Primitive::kPrimFloat:
2383 case Primitive::kPrimDouble: {
2384 Register result = OutputRegister(compare);
2385 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002386 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002387 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2388 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002389 __ Fcmp(left, 0.0);
2390 } else {
2391 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2392 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002393 __ Cset(result, ne);
2394 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002395 break;
2396 }
2397 default:
2398 LOG(FATAL) << "Unimplemented compare type " << in_type;
2399 }
2400}
2401
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002402void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002404
2405 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2406 locations->SetInAt(0, Location::RequiresFpuRegister());
2407 locations->SetInAt(1,
2408 IsFloatingPointZeroConstant(instruction->InputAt(1))
2409 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2410 : Location::RequiresFpuRegister());
2411 } else {
2412 // Integer cases.
2413 locations->SetInAt(0, Location::RequiresRegister());
2414 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2415 }
2416
Alexandre Rames5319def2014-10-23 10:03:10 +01002417 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002419 }
2420}
2421
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002422void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002423 if (!instruction->NeedsMaterialization()) {
2424 return;
2425 }
2426
2427 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002428 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002429 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002430
Roland Levillain7f63c522015-07-13 15:54:55 +00002431 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2432 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2433 if (locations->InAt(1).IsConstant()) {
2434 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2435 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2436 __ Fcmp(lhs, 0.0);
2437 } else {
2438 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2439 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002440 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002441 } else {
2442 // Integer cases.
2443 Register lhs = InputRegisterAt(instruction, 0);
2444 Operand rhs = InputOperandAt(instruction, 1);
2445 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002446 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002447 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002448}
2449
2450#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2451 M(Equal) \
2452 M(NotEqual) \
2453 M(LessThan) \
2454 M(LessThanOrEqual) \
2455 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002456 M(GreaterThanOrEqual) \
2457 M(Below) \
2458 M(BelowOrEqual) \
2459 M(Above) \
2460 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002461#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002462void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2463void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002464FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002465#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002466#undef FOR_EACH_CONDITION_INSTRUCTION
2467
Zheng Xuc6667102015-05-15 16:08:45 +08002468void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2469 DCHECK(instruction->IsDiv() || instruction->IsRem());
2470
2471 LocationSummary* locations = instruction->GetLocations();
2472 Location second = locations->InAt(1);
2473 DCHECK(second.IsConstant());
2474
2475 Register out = OutputRegister(instruction);
2476 Register dividend = InputRegisterAt(instruction, 0);
2477 int64_t imm = Int64FromConstant(second.GetConstant());
2478 DCHECK(imm == 1 || imm == -1);
2479
2480 if (instruction->IsRem()) {
2481 __ Mov(out, 0);
2482 } else {
2483 if (imm == 1) {
2484 __ Mov(out, dividend);
2485 } else {
2486 __ Neg(out, dividend);
2487 }
2488 }
2489}
2490
2491void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2492 DCHECK(instruction->IsDiv() || instruction->IsRem());
2493
2494 LocationSummary* locations = instruction->GetLocations();
2495 Location second = locations->InAt(1);
2496 DCHECK(second.IsConstant());
2497
2498 Register out = OutputRegister(instruction);
2499 Register dividend = InputRegisterAt(instruction, 0);
2500 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002501 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002502 int ctz_imm = CTZ(abs_imm);
2503
2504 UseScratchRegisterScope temps(GetVIXLAssembler());
2505 Register temp = temps.AcquireSameSizeAs(out);
2506
2507 if (instruction->IsDiv()) {
2508 __ Add(temp, dividend, abs_imm - 1);
2509 __ Cmp(dividend, 0);
2510 __ Csel(out, temp, dividend, lt);
2511 if (imm > 0) {
2512 __ Asr(out, out, ctz_imm);
2513 } else {
2514 __ Neg(out, Operand(out, ASR, ctz_imm));
2515 }
2516 } else {
2517 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2518 __ Asr(temp, dividend, bits - 1);
2519 __ Lsr(temp, temp, bits - ctz_imm);
2520 __ Add(out, dividend, temp);
2521 __ And(out, out, abs_imm - 1);
2522 __ Sub(out, out, temp);
2523 }
2524}
2525
2526void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2527 DCHECK(instruction->IsDiv() || instruction->IsRem());
2528
2529 LocationSummary* locations = instruction->GetLocations();
2530 Location second = locations->InAt(1);
2531 DCHECK(second.IsConstant());
2532
2533 Register out = OutputRegister(instruction);
2534 Register dividend = InputRegisterAt(instruction, 0);
2535 int64_t imm = Int64FromConstant(second.GetConstant());
2536
2537 Primitive::Type type = instruction->GetResultType();
2538 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2539
2540 int64_t magic;
2541 int shift;
2542 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2543
2544 UseScratchRegisterScope temps(GetVIXLAssembler());
2545 Register temp = temps.AcquireSameSizeAs(out);
2546
2547 // temp = get_high(dividend * magic)
2548 __ Mov(temp, magic);
2549 if (type == Primitive::kPrimLong) {
2550 __ Smulh(temp, dividend, temp);
2551 } else {
2552 __ Smull(temp.X(), dividend, temp);
2553 __ Lsr(temp.X(), temp.X(), 32);
2554 }
2555
2556 if (imm > 0 && magic < 0) {
2557 __ Add(temp, temp, dividend);
2558 } else if (imm < 0 && magic > 0) {
2559 __ Sub(temp, temp, dividend);
2560 }
2561
2562 if (shift != 0) {
2563 __ Asr(temp, temp, shift);
2564 }
2565
2566 if (instruction->IsDiv()) {
2567 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2568 } else {
2569 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2570 // TODO: Strength reduction for msub.
2571 Register temp_imm = temps.AcquireSameSizeAs(out);
2572 __ Mov(temp_imm, imm);
2573 __ Msub(out, temp, temp_imm, dividend);
2574 }
2575}
2576
2577void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2578 DCHECK(instruction->IsDiv() || instruction->IsRem());
2579 Primitive::Type type = instruction->GetResultType();
2580 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2581
2582 LocationSummary* locations = instruction->GetLocations();
2583 Register out = OutputRegister(instruction);
2584 Location second = locations->InAt(1);
2585
2586 if (second.IsConstant()) {
2587 int64_t imm = Int64FromConstant(second.GetConstant());
2588
2589 if (imm == 0) {
2590 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2591 } else if (imm == 1 || imm == -1) {
2592 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002593 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002594 DivRemByPowerOfTwo(instruction);
2595 } else {
2596 DCHECK(imm <= -2 || imm >= 2);
2597 GenerateDivRemWithAnyConstant(instruction);
2598 }
2599 } else {
2600 Register dividend = InputRegisterAt(instruction, 0);
2601 Register divisor = InputRegisterAt(instruction, 1);
2602 if (instruction->IsDiv()) {
2603 __ Sdiv(out, dividend, divisor);
2604 } else {
2605 UseScratchRegisterScope temps(GetVIXLAssembler());
2606 Register temp = temps.AcquireSameSizeAs(out);
2607 __ Sdiv(temp, dividend, divisor);
2608 __ Msub(out, temp, divisor, dividend);
2609 }
2610 }
2611}
2612
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002613void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2614 LocationSummary* locations =
2615 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2616 switch (div->GetResultType()) {
2617 case Primitive::kPrimInt:
2618 case Primitive::kPrimLong:
2619 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002620 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2622 break;
2623
2624 case Primitive::kPrimFloat:
2625 case Primitive::kPrimDouble:
2626 locations->SetInAt(0, Location::RequiresFpuRegister());
2627 locations->SetInAt(1, Location::RequiresFpuRegister());
2628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2629 break;
2630
2631 default:
2632 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2633 }
2634}
2635
2636void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2637 Primitive::Type type = div->GetResultType();
2638 switch (type) {
2639 case Primitive::kPrimInt:
2640 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002641 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002642 break;
2643
2644 case Primitive::kPrimFloat:
2645 case Primitive::kPrimDouble:
2646 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2647 break;
2648
2649 default:
2650 LOG(FATAL) << "Unexpected div type " << type;
2651 }
2652}
2653
Alexandre Rames67555f72014-11-18 10:55:16 +00002654void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002655 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2656 ? LocationSummary::kCallOnSlowPath
2657 : LocationSummary::kNoCall;
2658 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002659 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2660 if (instruction->HasUses()) {
2661 locations->SetOut(Location::SameAsFirstInput());
2662 }
2663}
2664
2665void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2666 SlowPathCodeARM64* slow_path =
2667 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2668 codegen_->AddSlowPath(slow_path);
2669 Location value = instruction->GetLocations()->InAt(0);
2670
Alexandre Rames3e69f162014-12-10 10:36:50 +00002671 Primitive::Type type = instruction->GetType();
2672
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002673 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2674 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002675 return;
2676 }
2677
Alexandre Rames67555f72014-11-18 10:55:16 +00002678 if (value.IsConstant()) {
2679 int64_t divisor = Int64ConstantFrom(value);
2680 if (divisor == 0) {
2681 __ B(slow_path->GetEntryLabel());
2682 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002683 // A division by a non-null constant is valid. We don't need to perform
2684 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002685 }
2686 } else {
2687 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2688 }
2689}
2690
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002691void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2692 LocationSummary* locations =
2693 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2694 locations->SetOut(Location::ConstantLocation(constant));
2695}
2696
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002697void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2698 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002699 // Will be generated at use site.
2700}
2701
Alexandre Rames5319def2014-10-23 10:03:10 +01002702void LocationsBuilderARM64::VisitExit(HExit* exit) {
2703 exit->SetLocations(nullptr);
2704}
2705
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002706void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002707}
2708
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002709void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2710 LocationSummary* locations =
2711 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2712 locations->SetOut(Location::ConstantLocation(constant));
2713}
2714
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002715void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002716 // Will be generated at use site.
2717}
2718
David Brazdilfc6a86a2015-06-26 10:33:45 +00002719void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002720 DCHECK(!successor->IsExitBlock());
2721 HBasicBlock* block = got->GetBlock();
2722 HInstruction* previous = got->GetPrevious();
2723 HLoopInformation* info = block->GetLoopInformation();
2724
David Brazdil46e2a392015-03-16 17:31:52 +00002725 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002726 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2727 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2728 return;
2729 }
2730 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2731 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2732 }
2733 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002734 __ B(codegen_->GetLabelOf(successor));
2735 }
2736}
2737
David Brazdilfc6a86a2015-06-26 10:33:45 +00002738void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2739 got->SetLocations(nullptr);
2740}
2741
2742void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2743 HandleGoto(got, got->GetSuccessor());
2744}
2745
2746void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2747 try_boundary->SetLocations(nullptr);
2748}
2749
2750void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2751 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2752 if (!successor->IsExitBlock()) {
2753 HandleGoto(try_boundary, successor);
2754 }
2755}
2756
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002757void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002758 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002759 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002760 vixl::Label* false_target) {
2761 // FP branching requires both targets to be explicit. If either of the targets
2762 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2763 vixl::Label fallthrough_target;
2764 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002765
David Brazdil0debae72015-11-12 18:37:00 +00002766 if (true_target == nullptr && false_target == nullptr) {
2767 // Nothing to do. The code always falls through.
2768 return;
2769 } else if (cond->IsIntConstant()) {
2770 // Constant condition, statically compared against 1.
2771 if (cond->AsIntConstant()->IsOne()) {
2772 if (true_target != nullptr) {
2773 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002774 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002775 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002776 DCHECK(cond->AsIntConstant()->IsZero());
2777 if (false_target != nullptr) {
2778 __ B(false_target);
2779 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002780 }
David Brazdil0debae72015-11-12 18:37:00 +00002781 return;
2782 }
2783
2784 // The following code generates these patterns:
2785 // (1) true_target == nullptr && false_target != nullptr
2786 // - opposite condition true => branch to false_target
2787 // (2) true_target != nullptr && false_target == nullptr
2788 // - condition true => branch to true_target
2789 // (3) true_target != nullptr && false_target != nullptr
2790 // - condition true => branch to true_target
2791 // - branch to false_target
2792 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002793 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002794 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002795 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002796 if (true_target == nullptr) {
2797 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2798 } else {
2799 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2800 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002801 } else {
2802 // The condition instruction has not been materialized, use its inputs as
2803 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002804 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002805
David Brazdil0debae72015-11-12 18:37:00 +00002806 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002807 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002808 FPRegister lhs = InputFPRegisterAt(condition, 0);
2809 if (condition->GetLocations()->InAt(1).IsConstant()) {
2810 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2811 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2812 __ Fcmp(lhs, 0.0);
2813 } else {
2814 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2815 }
David Brazdil0debae72015-11-12 18:37:00 +00002816 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002817 IfCondition opposite_condition = condition->GetOppositeCondition();
2818 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002819 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002820 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002821 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002822 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002823 // Integer cases.
2824 Register lhs = InputRegisterAt(condition, 0);
2825 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002826
2827 Condition arm64_cond;
2828 vixl::Label* non_fallthrough_target;
2829 if (true_target == nullptr) {
2830 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2831 non_fallthrough_target = false_target;
2832 } else {
2833 arm64_cond = ARM64Condition(condition->GetCondition());
2834 non_fallthrough_target = true_target;
2835 }
2836
Aart Bik086d27e2016-01-20 17:02:00 -08002837 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2838 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002839 switch (arm64_cond) {
2840 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002841 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002842 break;
2843 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002844 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002845 break;
2846 case lt:
2847 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002848 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002849 break;
2850 case ge:
2851 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002852 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002853 break;
2854 default:
2855 // Without the `static_cast` the compiler throws an error for
2856 // `-Werror=sign-promo`.
2857 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2858 }
2859 } else {
2860 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002861 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002862 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002863 }
2864 }
David Brazdil0debae72015-11-12 18:37:00 +00002865
2866 // If neither branch falls through (case 3), the conditional branch to `true_target`
2867 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2868 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002869 __ B(false_target);
2870 }
David Brazdil0debae72015-11-12 18:37:00 +00002871
2872 if (fallthrough_target.IsLinked()) {
2873 __ Bind(&fallthrough_target);
2874 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002875}
2876
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002877void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002879 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002880 locations->SetInAt(0, Location::RequiresRegister());
2881 }
2882}
2883
2884void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002885 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2886 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2887 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2888 nullptr : codegen_->GetLabelOf(true_successor);
2889 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2890 nullptr : codegen_->GetLabelOf(false_successor);
2891 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002892}
2893
2894void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2895 LocationSummary* locations = new (GetGraph()->GetArena())
2896 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002897 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002898 locations->SetInAt(0, Location::RequiresRegister());
2899 }
2900}
2901
2902void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002903 SlowPathCodeARM64* slow_path =
2904 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002905 GenerateTestAndBranch(deoptimize,
2906 /* condition_input_index */ 0,
2907 slow_path->GetEntryLabel(),
2908 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002909}
2910
David Srbecky0cf44932015-12-09 14:09:59 +00002911void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2912 new (GetGraph()->GetArena()) LocationSummary(info);
2913}
2914
2915void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002916 if (codegen_->HasStackMapAtCurrentPc()) {
2917 // Ensure that we do not collide with the stack map of the previous instruction.
2918 __ Nop();
2919 }
David Srbecky0cf44932015-12-09 14:09:59 +00002920 codegen_->RecordPcInfo(info, info->GetDexPc());
2921}
2922
Alexandre Rames5319def2014-10-23 10:03:10 +01002923void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002924 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002925}
2926
2927void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002928 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002929}
2930
2931void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002932 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002933}
2934
2935void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002936 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002937}
2938
Alexandre Rames67555f72014-11-18 10:55:16 +00002939void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002940 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002941 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2942 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002943 case TypeCheckKind::kExactCheck:
2944 case TypeCheckKind::kAbstractClassCheck:
2945 case TypeCheckKind::kClassHierarchyCheck:
2946 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002947 call_kind =
2948 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002949 break;
2950 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002951 case TypeCheckKind::kUnresolvedCheck:
2952 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002953 call_kind = LocationSummary::kCallOnSlowPath;
2954 break;
2955 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002956
Alexandre Rames67555f72014-11-18 10:55:16 +00002957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002958 locations->SetInAt(0, Location::RequiresRegister());
2959 locations->SetInAt(1, Location::RequiresRegister());
2960 // The "out" register is used as a temporary, so it overlaps with the inputs.
2961 // Note that TypeCheckSlowPathARM64 uses this register too.
2962 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2963 // When read barriers are enabled, we need a temporary register for
2964 // some cases.
2965 if (kEmitCompilerReadBarrier &&
2966 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2967 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2968 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2969 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002970 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002971}
2972
2973void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2974 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002975 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002976 Register obj = InputRegisterAt(instruction, 0);
2977 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002978 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00002979 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002980 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2981 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2982 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2983 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002985 vixl::Label done, zero;
2986 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002987
2988 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002989 // Avoid null check if we know `obj` is not null.
2990 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002991 __ Cbz(obj, &zero);
2992 }
2993
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002994 // /* HeapReference<Class> */ out = obj->klass_
2995 __ Ldr(out, HeapOperand(obj.W(), class_offset));
2996 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002997
2998 switch (instruction->GetTypeCheckKind()) {
2999 case TypeCheckKind::kExactCheck: {
3000 __ Cmp(out, cls);
3001 __ Cset(out, eq);
3002 if (zero.IsLinked()) {
3003 __ B(&done);
3004 }
3005 break;
3006 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003007
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003008 case TypeCheckKind::kAbstractClassCheck: {
3009 // If the class is abstract, we eagerly fetch the super class of the
3010 // object to avoid doing a comparison we know will fail.
3011 vixl::Label loop, success;
3012 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003013 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3014 if (kEmitCompilerReadBarrier) {
3015 // Save the value of `out` into `temp` before overwriting it
3016 // in the following move operation, as we will need it for the
3017 // read barrier below.
3018 Register temp = WRegisterFrom(temp_loc);
3019 __ Mov(temp, out);
3020 }
3021 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003022 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003023 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003024 // If `out` is null, we use it for the result, and jump to `done`.
3025 __ Cbz(out, &done);
3026 __ Cmp(out, cls);
3027 __ B(ne, &loop);
3028 __ Mov(out, 1);
3029 if (zero.IsLinked()) {
3030 __ B(&done);
3031 }
3032 break;
3033 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003034
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003035 case TypeCheckKind::kClassHierarchyCheck: {
3036 // Walk over the class hierarchy to find a match.
3037 vixl::Label loop, success;
3038 __ Bind(&loop);
3039 __ Cmp(out, cls);
3040 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003041 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3042 if (kEmitCompilerReadBarrier) {
3043 // Save the value of `out` into `temp` before overwriting it
3044 // in the following move operation, as we will need it for the
3045 // read barrier below.
3046 Register temp = WRegisterFrom(temp_loc);
3047 __ Mov(temp, out);
3048 }
3049 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003050 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003051 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003052 __ Cbnz(out, &loop);
3053 // If `out` is null, we use it for the result, and jump to `done`.
3054 __ B(&done);
3055 __ Bind(&success);
3056 __ Mov(out, 1);
3057 if (zero.IsLinked()) {
3058 __ B(&done);
3059 }
3060 break;
3061 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003062
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003063 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003064 // Do an exact check.
3065 vixl::Label exact_check;
3066 __ Cmp(out, cls);
3067 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003068 // Otherwise, we need to check that the object's class is a non-primitive array.
3069 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3070 if (kEmitCompilerReadBarrier) {
3071 // Save the value of `out` into `temp` before overwriting it
3072 // in the following move operation, as we will need it for the
3073 // read barrier below.
3074 Register temp = WRegisterFrom(temp_loc);
3075 __ Mov(temp, out);
3076 }
3077 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003078 __ Ldr(out, HeapOperand(out, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003079 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003080 // If `out` is null, we use it for the result, and jump to `done`.
3081 __ Cbz(out, &done);
3082 __ Ldrh(out, HeapOperand(out, primitive_offset));
3083 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3084 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003085 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003086 __ Mov(out, 1);
3087 __ B(&done);
3088 break;
3089 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003090
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003091 case TypeCheckKind::kArrayCheck: {
3092 __ Cmp(out, cls);
3093 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003094 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3095 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003096 codegen_->AddSlowPath(slow_path);
3097 __ B(ne, slow_path->GetEntryLabel());
3098 __ Mov(out, 1);
3099 if (zero.IsLinked()) {
3100 __ B(&done);
3101 }
3102 break;
3103 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003104
Calin Juravle98893e12015-10-02 21:05:03 +01003105 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003106 case TypeCheckKind::kInterfaceCheck: {
3107 // Note that we indeed only call on slow path, but we always go
3108 // into the slow path for the unresolved and interface check
3109 // cases.
3110 //
3111 // We cannot directly call the InstanceofNonTrivial runtime
3112 // entry point without resorting to a type checking slow path
3113 // here (i.e. by calling InvokeRuntime directly), as it would
3114 // require to assign fixed registers for the inputs of this
3115 // HInstanceOf instruction (following the runtime calling
3116 // convention), which might be cluttered by the potential first
3117 // read barrier emission at the beginning of this method.
3118 DCHECK(locations->OnlyCallsOnSlowPath());
3119 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3120 /* is_fatal */ false);
3121 codegen_->AddSlowPath(slow_path);
3122 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003123 if (zero.IsLinked()) {
3124 __ B(&done);
3125 }
3126 break;
3127 }
3128 }
3129
3130 if (zero.IsLinked()) {
3131 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003132 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003133 }
3134
3135 if (done.IsLinked()) {
3136 __ Bind(&done);
3137 }
3138
3139 if (slow_path != nullptr) {
3140 __ Bind(slow_path->GetExitLabel());
3141 }
3142}
3143
3144void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3145 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3146 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3147
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003148 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3149 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003150 case TypeCheckKind::kExactCheck:
3151 case TypeCheckKind::kAbstractClassCheck:
3152 case TypeCheckKind::kClassHierarchyCheck:
3153 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003154 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3155 LocationSummary::kCallOnSlowPath :
3156 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003157 break;
3158 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003159 case TypeCheckKind::kUnresolvedCheck:
3160 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003161 call_kind = LocationSummary::kCallOnSlowPath;
3162 break;
3163 }
3164
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3166 locations->SetInAt(0, Location::RequiresRegister());
3167 locations->SetInAt(1, Location::RequiresRegister());
3168 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3169 locations->AddTemp(Location::RequiresRegister());
3170 locations->AddTemp(Location::RequiresRegister());
3171 // When read barriers are enabled, we need an additional temporary
3172 // register for some cases.
3173 if (kEmitCompilerReadBarrier &&
3174 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3175 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3176 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3177 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178 }
3179}
3180
3181void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
3182 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003183 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 Register obj = InputRegisterAt(instruction, 0);
3185 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003186 Location temp_loc = locations->GetTemp(0);
3187 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003188 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3189 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3190 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3191 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003192
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003193 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3194 bool is_type_check_slow_path_fatal =
3195 (type_check_kind == TypeCheckKind::kExactCheck ||
3196 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3197 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3198 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3199 !instruction->CanThrowIntoCatchBlock();
3200 SlowPathCodeARM64* type_check_slow_path =
3201 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3202 is_type_check_slow_path_fatal);
3203 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003204
3205 vixl::Label done;
3206 // Avoid null check if we know obj is not null.
3207 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003208 __ Cbz(obj, &done);
3209 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003210
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003211 // /* HeapReference<Class> */ temp = obj->klass_
3212 __ Ldr(temp, HeapOperand(obj, class_offset));
3213 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003214
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003215 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003216 case TypeCheckKind::kExactCheck:
3217 case TypeCheckKind::kArrayCheck: {
3218 __ Cmp(temp, cls);
3219 // Jump to slow path for throwing the exception or doing a
3220 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003221 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003222 break;
3223 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003225 case TypeCheckKind::kAbstractClassCheck: {
3226 // If the class is abstract, we eagerly fetch the super class of the
3227 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003228 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003229 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003230 Location temp2_loc =
3231 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3232 if (kEmitCompilerReadBarrier) {
3233 // Save the value of `temp` into `temp2` before overwriting it
3234 // in the following move operation, as we will need it for the
3235 // read barrier below.
3236 Register temp2 = WRegisterFrom(temp2_loc);
3237 __ Mov(temp2, temp);
3238 }
3239 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003240 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003241 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3242
3243 // If the class reference currently in `temp` is not null, jump
3244 // to the `compare_classes` label to compare it with the checked
3245 // class.
3246 __ Cbnz(temp, &compare_classes);
3247 // Otherwise, jump to the slow path to throw the exception.
3248 //
3249 // But before, move back the object's class into `temp` before
3250 // going into the slow path, as it has been overwritten in the
3251 // meantime.
3252 // /* HeapReference<Class> */ temp = obj->klass_
3253 __ Ldr(temp, HeapOperand(obj, class_offset));
3254 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3255 __ B(type_check_slow_path->GetEntryLabel());
3256
3257 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003258 __ Cmp(temp, cls);
3259 __ B(ne, &loop);
3260 break;
3261 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003262
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003263 case TypeCheckKind::kClassHierarchyCheck: {
3264 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003265 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003266 __ Bind(&loop);
3267 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003268 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003269
3270 Location temp2_loc =
3271 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3272 if (kEmitCompilerReadBarrier) {
3273 // Save the value of `temp` into `temp2` before overwriting it
3274 // in the following move operation, as we will need it for the
3275 // read barrier below.
3276 Register temp2 = WRegisterFrom(temp2_loc);
3277 __ Mov(temp2, temp);
3278 }
3279 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003280 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003281 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3282
3283 // If the class reference currently in `temp` is not null, jump
3284 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003285 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003286 // Otherwise, jump to the slow path to throw the exception.
3287 //
3288 // But before, move back the object's class into `temp` before
3289 // going into the slow path, as it has been overwritten in the
3290 // meantime.
3291 // /* HeapReference<Class> */ temp = obj->klass_
3292 __ Ldr(temp, HeapOperand(obj, class_offset));
3293 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3294 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003295 break;
3296 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003297
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003298 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003299 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003300 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003301 __ Cmp(temp, cls);
3302 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303
3304 // Otherwise, we need to check that the object's class is a non-primitive array.
3305 Location temp2_loc =
3306 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3307 if (kEmitCompilerReadBarrier) {
3308 // Save the value of `temp` into `temp2` before overwriting it
3309 // in the following move operation, as we will need it for the
3310 // read barrier below.
3311 Register temp2 = WRegisterFrom(temp2_loc);
3312 __ Mov(temp2, temp);
3313 }
3314 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003315 __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003316 codegen_->MaybeGenerateReadBarrier(
3317 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
3318
3319 // If the component type is not null (i.e. the object is indeed
3320 // an array), jump to label `check_non_primitive_component_type`
3321 // to further check that this component type is not a primitive
3322 // type.
3323 __ Cbnz(temp, &check_non_primitive_component_type);
3324 // Otherwise, jump to the slow path to throw the exception.
3325 //
3326 // But before, move back the object's class into `temp` before
3327 // going into the slow path, as it has been overwritten in the
3328 // meantime.
3329 // /* HeapReference<Class> */ temp = obj->klass_
3330 __ Ldr(temp, HeapOperand(obj, class_offset));
3331 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3332 __ B(type_check_slow_path->GetEntryLabel());
3333
3334 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003335 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3336 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 __ Cbz(temp, &done);
3338 // Same comment as above regarding `temp` and the slow path.
3339 // /* HeapReference<Class> */ temp = obj->klass_
3340 __ Ldr(temp, HeapOperand(obj, class_offset));
3341 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3342 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003343 break;
3344 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003345
Calin Juravle98893e12015-10-02 21:05:03 +01003346 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003347 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003348 // We always go into the type check slow path for the unresolved
3349 // and interface check cases.
3350 //
3351 // We cannot directly call the CheckCast runtime entry point
3352 // without resorting to a type checking slow path here (i.e. by
3353 // calling InvokeRuntime directly), as it would require to
3354 // assign fixed registers for the inputs of this HInstanceOf
3355 // instruction (following the runtime calling convention), which
3356 // might be cluttered by the potential first read barrier
3357 // emission at the beginning of this method.
3358 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003359 break;
3360 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003361 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003363 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003364}
3365
Alexandre Rames5319def2014-10-23 10:03:10 +01003366void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3367 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3368 locations->SetOut(Location::ConstantLocation(constant));
3369}
3370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003371void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003372 // Will be generated at use site.
3373}
3374
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003375void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3377 locations->SetOut(Location::ConstantLocation(constant));
3378}
3379
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003380void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003381 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003382}
3383
Calin Juravle175dc732015-08-25 15:42:32 +01003384void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3385 // The trampoline uses the same calling convention as dex calling conventions,
3386 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3387 // the method_idx.
3388 HandleInvoke(invoke);
3389}
3390
3391void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3392 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3393}
3394
Alexandre Rames5319def2014-10-23 10:03:10 +01003395void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003396 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003397 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003398}
3399
Alexandre Rames67555f72014-11-18 10:55:16 +00003400void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3401 HandleInvoke(invoke);
3402}
3403
3404void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3405 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003406 LocationSummary* locations = invoke->GetLocations();
3407 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003408 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3409 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003411 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003412 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003413
3414 // The register ip1 is required to be used for the hidden argument in
3415 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003416 MacroAssembler* masm = GetVIXLAssembler();
3417 UseScratchRegisterScope scratch_scope(masm);
3418 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003419 scratch_scope.Exclude(ip1);
3420 __ Mov(ip1, invoke->GetDexMethodIndex());
3421
Alexandre Rames67555f72014-11-18 10:55:16 +00003422 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003423 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003424 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003425 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003426 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003427 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003428 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003429 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003430 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431 // Instead of simply (possibly) unpoisoning `temp` here, we should
3432 // emit a read barrier for the previous class reference load.
3433 // However this is not required in practice, as this is an
3434 // intermediate/temporary reference and because the current
3435 // concurrent copying collector keeps the from-space memory
3436 // intact/accessible until the end of the marking phase (the
3437 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003438 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003439 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003440 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003441 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003442 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003443 // lr();
3444 __ Blr(lr);
3445 DCHECK(!codegen_->IsLeafMethod());
3446 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3447}
3448
3449void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003450 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3451 if (intrinsic.TryDispatch(invoke)) {
3452 return;
3453 }
3454
Alexandre Rames67555f72014-11-18 10:55:16 +00003455 HandleInvoke(invoke);
3456}
3457
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003458void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003459 // Explicit clinit checks triggered by static invokes must have been pruned by
3460 // art::PrepareForRegisterAllocation.
3461 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003462
Andreas Gampe878d58c2015-01-15 23:24:00 -08003463 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3464 if (intrinsic.TryDispatch(invoke)) {
3465 return;
3466 }
3467
Alexandre Rames67555f72014-11-18 10:55:16 +00003468 HandleInvoke(invoke);
3469}
3470
Andreas Gampe878d58c2015-01-15 23:24:00 -08003471static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3472 if (invoke->GetLocations()->Intrinsified()) {
3473 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3474 intrinsic.Dispatch(invoke);
3475 return true;
3476 }
3477 return false;
3478}
3479
Vladimir Markodc151b22015-10-15 18:02:30 +01003480HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3481 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3482 MethodReference target_method ATTRIBUTE_UNUSED) {
3483 // On arm64 we support all dispatch types.
3484 return desired_dispatch_info;
3485}
3486
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003487void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003488 // For better instruction scheduling we load the direct code pointer before the method pointer.
3489 bool direct_code_loaded = false;
3490 switch (invoke->GetCodePtrLocation()) {
3491 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3492 // LR = code address from literal pool with link-time patch.
3493 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3494 direct_code_loaded = true;
3495 break;
3496 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3497 // LR = invoke->GetDirectCodePtr();
3498 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3499 direct_code_loaded = true;
3500 break;
3501 default:
3502 break;
3503 }
3504
Andreas Gampe878d58c2015-01-15 23:24:00 -08003505 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003506 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3507 switch (invoke->GetMethodLoadKind()) {
3508 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3509 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003510 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003511 break;
3512 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003513 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003514 break;
3515 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3516 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003517 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003518 break;
3519 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3520 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003521 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003522 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3523 break;
3524 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3525 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003526 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3527 invoke->GetDexCacheArrayOffset());
3528 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003529 {
3530 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003531 __ Bind(pc_insn_label);
3532 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003533 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003534 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003535 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003536 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3537 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003538 {
3539 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3540 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3541 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3542 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3543 }
Vladimir Marko58155012015-08-19 12:49:41 +00003544 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003545 }
Vladimir Marko58155012015-08-19 12:49:41 +00003546 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003547 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003548 Register reg = XRegisterFrom(temp);
3549 Register method_reg;
3550 if (current_method.IsRegister()) {
3551 method_reg = XRegisterFrom(current_method);
3552 } else {
3553 DCHECK(invoke->GetLocations()->Intrinsified());
3554 DCHECK(!current_method.IsValid());
3555 method_reg = reg;
3556 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3557 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003558
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003559 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003560 __ Ldr(reg.X(),
3561 MemOperand(method_reg.X(),
3562 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003563 // temp = temp[index_in_cache];
3564 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3565 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3566 break;
3567 }
3568 }
3569
3570 switch (invoke->GetCodePtrLocation()) {
3571 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3572 __ Bl(&frame_entry_label_);
3573 break;
3574 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3575 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3576 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003577 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3578 __ Bind(label);
3579 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003580 break;
3581 }
3582 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3583 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3584 // LR prepared above for better instruction scheduling.
3585 DCHECK(direct_code_loaded);
3586 // lr()
3587 __ Blr(lr);
3588 break;
3589 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3590 // LR = callee_method->entry_point_from_quick_compiled_code_;
3591 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003592 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003593 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3594 // lr()
3595 __ Blr(lr);
3596 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003597 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003598
Andreas Gampe878d58c2015-01-15 23:24:00 -08003599 DCHECK(!IsLeafMethod());
3600}
3601
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003602void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003603 // Use the calling convention instead of the location of the receiver, as
3604 // intrinsics may have put the receiver in a different register. In the intrinsics
3605 // slow path, the arguments have been moved to the right place, so here we are
3606 // guaranteed that the receiver is the first register of the calling convention.
3607 InvokeDexCallingConvention calling_convention;
3608 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003609 Register temp = XRegisterFrom(temp_in);
3610 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3611 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3612 Offset class_offset = mirror::Object::ClassOffset();
3613 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3614
3615 BlockPoolsScope block_pools(GetVIXLAssembler());
3616
3617 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003618 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003619 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003620 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003621 // Instead of simply (possibly) unpoisoning `temp` here, we should
3622 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003623 // intermediate/temporary reference and because the current
3624 // concurrent copying collector keeps the from-space memory
3625 // intact/accessible until the end of the marking phase (the
3626 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003627 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3628 // temp = temp->GetMethodAt(method_offset);
3629 __ Ldr(temp, MemOperand(temp, method_offset));
3630 // lr = temp->GetEntryPoint();
3631 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3632 // lr();
3633 __ Blr(lr);
3634}
3635
Vladimir Marko58155012015-08-19 12:49:41 +00003636void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3637 DCHECK(linker_patches->empty());
3638 size_t size =
3639 method_patches_.size() +
3640 call_patches_.size() +
3641 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003642 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003643 linker_patches->reserve(size);
3644 for (const auto& entry : method_patches_) {
3645 const MethodReference& target_method = entry.first;
3646 vixl::Literal<uint64_t>* literal = entry.second;
3647 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3648 target_method.dex_file,
3649 target_method.dex_method_index));
3650 }
3651 for (const auto& entry : call_patches_) {
3652 const MethodReference& target_method = entry.first;
3653 vixl::Literal<uint64_t>* literal = entry.second;
3654 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3655 target_method.dex_file,
3656 target_method.dex_method_index));
3657 }
3658 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003659 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003660 info.target_method.dex_file,
3661 info.target_method.dex_method_index));
3662 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003663 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003664 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003665 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003666 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003667 info.element_offset));
3668 }
3669}
3670
3671vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3672 // Look up the literal for value.
3673 auto lb = uint64_literals_.lower_bound(value);
3674 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3675 return lb->second;
3676 }
3677 // We don't have a literal for this value, insert a new one.
3678 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3679 uint64_literals_.PutBefore(lb, value, literal);
3680 return literal;
3681}
3682
3683vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3684 MethodReference target_method,
3685 MethodToLiteralMap* map) {
3686 // Look up the literal for target_method.
3687 auto lb = map->lower_bound(target_method);
3688 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3689 return lb->second;
3690 }
3691 // We don't have a literal for this method yet, insert a new one.
3692 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3693 map->PutBefore(lb, target_method, literal);
3694 return literal;
3695}
3696
3697vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3698 MethodReference target_method) {
3699 return DeduplicateMethodLiteral(target_method, &method_patches_);
3700}
3701
3702vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3703 MethodReference target_method) {
3704 return DeduplicateMethodLiteral(target_method, &call_patches_);
3705}
3706
3707
Andreas Gampe878d58c2015-01-15 23:24:00 -08003708void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003709 // Explicit clinit checks triggered by static invokes must have been pruned by
3710 // art::PrepareForRegisterAllocation.
3711 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003712
Andreas Gampe878d58c2015-01-15 23:24:00 -08003713 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3714 return;
3715 }
3716
Alexandre Ramesd921d642015-04-16 15:07:16 +01003717 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003718 LocationSummary* locations = invoke->GetLocations();
3719 codegen_->GenerateStaticOrDirectCall(
3720 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003721 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003722}
3723
3724void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003725 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3726 return;
3727 }
3728
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003729 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003730 DCHECK(!codegen_->IsLeafMethod());
3731 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3732}
3733
Alexandre Rames67555f72014-11-18 10:55:16 +00003734void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003735 InvokeRuntimeCallingConvention calling_convention;
3736 CodeGenerator::CreateLoadClassLocationSummary(
3737 cls,
3738 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003739 LocationFrom(vixl::x0),
3740 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003741}
3742
3743void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003744 if (cls->NeedsAccessCheck()) {
3745 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3746 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3747 cls,
3748 cls->GetDexPc(),
3749 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003750 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003751 return;
3752 }
3753
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003754 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003755 Register out = OutputRegister(cls);
3756 Register current_method = InputRegisterAt(cls, 0);
3757 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003758 DCHECK(!cls->CanCallRuntime());
3759 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003760 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3761 if (kEmitCompilerReadBarrier) {
3762 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3763 __ Add(out.X(), current_method.X(), declaring_class_offset);
3764 // /* mirror::Class* */ out = out->Read()
3765 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3766 } else {
3767 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3768 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3769 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003770 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003771 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003772 // /* GcRoot<mirror::Class>[] */ out =
3773 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003774 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003775
3776 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
3777 if (kEmitCompilerReadBarrier) {
3778 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
3779 __ Add(out.X(), out.X(), cache_offset);
3780 // /* mirror::Class* */ out = out->Read()
3781 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3782 } else {
3783 // /* GcRoot<mirror::Class> */ out = out[type_index]
3784 __ Ldr(out, MemOperand(out.X(), cache_offset));
3785 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003786
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003787 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3788 DCHECK(cls->CanCallRuntime());
3789 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3790 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3791 codegen_->AddSlowPath(slow_path);
3792 if (!cls->IsInDexCache()) {
3793 __ Cbz(out, slow_path->GetEntryLabel());
3794 }
3795 if (cls->MustGenerateClinitCheck()) {
3796 GenerateClassInitializationCheck(slow_path, out);
3797 } else {
3798 __ Bind(slow_path->GetExitLabel());
3799 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003800 }
3801 }
3802}
3803
David Brazdilcb1c0552015-08-04 16:22:25 +01003804static MemOperand GetExceptionTlsAddress() {
3805 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3806}
3807
Alexandre Rames67555f72014-11-18 10:55:16 +00003808void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3809 LocationSummary* locations =
3810 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3811 locations->SetOut(Location::RequiresRegister());
3812}
3813
3814void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003815 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3816}
3817
3818void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3819 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3820}
3821
3822void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3823 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003824}
3825
Alexandre Rames5319def2014-10-23 10:03:10 +01003826void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3827 load->SetLocations(nullptr);
3828}
3829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003830void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003831 // Nothing to do, this is driven by the code generator.
3832}
3833
Alexandre Rames67555f72014-11-18 10:55:16 +00003834void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003835 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
3836 ? LocationSummary::kCallOnSlowPath
3837 : LocationSummary::kNoCall;
3838 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003839 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003840 locations->SetOut(Location::RequiresRegister());
3841}
3842
3843void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003844 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003845 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003846 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003847
3848 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3849 if (kEmitCompilerReadBarrier) {
3850 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3851 __ Add(out.X(), current_method.X(), declaring_class_offset);
3852 // /* mirror::Class* */ out = out->Read()
3853 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3854 } else {
3855 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3856 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3857 }
3858
3859 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3860 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
3861
3862 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
3863 if (kEmitCompilerReadBarrier) {
3864 // /* GcRoot<mirror::String>* */ out = &out[string_index]
3865 __ Add(out.X(), out.X(), cache_offset);
3866 // /* mirror::String* */ out = out->Read()
3867 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3868 } else {
3869 // /* GcRoot<mirror::String> */ out = out[string_index]
3870 __ Ldr(out, MemOperand(out.X(), cache_offset));
3871 }
3872
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003873 if (!load->IsInDexCache()) {
3874 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3875 codegen_->AddSlowPath(slow_path);
3876 __ Cbz(out, slow_path->GetEntryLabel());
3877 __ Bind(slow_path->GetExitLabel());
3878 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003879}
3880
Alexandre Rames5319def2014-10-23 10:03:10 +01003881void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3882 local->SetLocations(nullptr);
3883}
3884
3885void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3886 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3887}
3888
3889void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3891 locations->SetOut(Location::ConstantLocation(constant));
3892}
3893
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003894void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003895 // Will be generated at use site.
3896}
3897
Alexandre Rames67555f72014-11-18 10:55:16 +00003898void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3899 LocationSummary* locations =
3900 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3901 InvokeRuntimeCallingConvention calling_convention;
3902 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3903}
3904
3905void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3906 codegen_->InvokeRuntime(instruction->IsEnter()
3907 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3908 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003909 instruction->GetDexPc(),
3910 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003911 if (instruction->IsEnter()) {
3912 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3913 } else {
3914 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3915 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003916}
3917
Alexandre Rames42d641b2014-10-27 14:00:51 +00003918void LocationsBuilderARM64::VisitMul(HMul* mul) {
3919 LocationSummary* locations =
3920 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3921 switch (mul->GetResultType()) {
3922 case Primitive::kPrimInt:
3923 case Primitive::kPrimLong:
3924 locations->SetInAt(0, Location::RequiresRegister());
3925 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003927 break;
3928
3929 case Primitive::kPrimFloat:
3930 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003931 locations->SetInAt(0, Location::RequiresFpuRegister());
3932 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003933 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003934 break;
3935
3936 default:
3937 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3938 }
3939}
3940
3941void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3942 switch (mul->GetResultType()) {
3943 case Primitive::kPrimInt:
3944 case Primitive::kPrimLong:
3945 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3946 break;
3947
3948 case Primitive::kPrimFloat:
3949 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003950 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003951 break;
3952
3953 default:
3954 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3955 }
3956}
3957
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003958void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3959 LocationSummary* locations =
3960 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3961 switch (neg->GetResultType()) {
3962 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003963 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003964 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003966 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003967
3968 case Primitive::kPrimFloat:
3969 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003970 locations->SetInAt(0, Location::RequiresFpuRegister());
3971 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003972 break;
3973
3974 default:
3975 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3976 }
3977}
3978
3979void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3980 switch (neg->GetResultType()) {
3981 case Primitive::kPrimInt:
3982 case Primitive::kPrimLong:
3983 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3984 break;
3985
3986 case Primitive::kPrimFloat:
3987 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003988 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003989 break;
3990
3991 default:
3992 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3993 }
3994}
3995
3996void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3997 LocationSummary* locations =
3998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3999 InvokeRuntimeCallingConvention calling_convention;
4000 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004001 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004002 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004003 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004004}
4005
4006void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4007 LocationSummary* locations = instruction->GetLocations();
4008 InvokeRuntimeCallingConvention calling_convention;
4009 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4010 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004011 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004012 // Note: if heap poisoning is enabled, the entry point takes cares
4013 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004014 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4015 instruction,
4016 instruction->GetDexPc(),
4017 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004018 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004019}
4020
Alexandre Rames5319def2014-10-23 10:03:10 +01004021void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4022 LocationSummary* locations =
4023 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4024 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004025 if (instruction->IsStringAlloc()) {
4026 locations->AddTemp(LocationFrom(kArtMethodRegister));
4027 } else {
4028 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4029 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4030 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004031 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4032}
4033
4034void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004035 // Note: if heap poisoning is enabled, the entry point takes cares
4036 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004037 if (instruction->IsStringAlloc()) {
4038 // String is allocated through StringFactory. Call NewEmptyString entry point.
4039 Location temp = instruction->GetLocations()->GetTemp(0);
4040 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4041 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4042 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4043 __ Blr(lr);
4044 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4045 } else {
4046 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4047 instruction,
4048 instruction->GetDexPc(),
4049 nullptr);
4050 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4051 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004052}
4053
4054void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4055 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004056 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004058}
4059
4060void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004061 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004062 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004063 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004064 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004065 break;
4066
4067 default:
4068 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4069 }
4070}
4071
David Brazdil66d126e2015-04-03 16:02:44 +01004072void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4073 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4074 locations->SetInAt(0, Location::RequiresRegister());
4075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4076}
4077
4078void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004079 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4080}
4081
Alexandre Rames5319def2014-10-23 10:03:10 +01004082void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004083 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4084 ? LocationSummary::kCallOnSlowPath
4085 : LocationSummary::kNoCall;
4086 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004087 locations->SetInAt(0, Location::RequiresRegister());
4088 if (instruction->HasUses()) {
4089 locations->SetOut(Location::SameAsFirstInput());
4090 }
4091}
4092
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004093void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004094 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4095 return;
4096 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004097
Alexandre Ramesd921d642015-04-16 15:07:16 +01004098 BlockPoolsScope block_pools(GetVIXLAssembler());
4099 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004100 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4101 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4102}
4103
4104void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004105 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4106 codegen_->AddSlowPath(slow_path);
4107
4108 LocationSummary* locations = instruction->GetLocations();
4109 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004110
4111 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004112}
4113
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004114void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004115 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004116 GenerateImplicitNullCheck(instruction);
4117 } else {
4118 GenerateExplicitNullCheck(instruction);
4119 }
4120}
4121
Alexandre Rames67555f72014-11-18 10:55:16 +00004122void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4123 HandleBinaryOp(instruction);
4124}
4125
4126void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4127 HandleBinaryOp(instruction);
4128}
4129
Alexandre Rames3e69f162014-12-10 10:36:50 +00004130void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4131 LOG(FATAL) << "Unreachable";
4132}
4133
4134void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4135 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4136}
4137
Alexandre Rames5319def2014-10-23 10:03:10 +01004138void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4139 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4140 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4141 if (location.IsStackSlot()) {
4142 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4143 } else if (location.IsDoubleStackSlot()) {
4144 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4145 }
4146 locations->SetOut(location);
4147}
4148
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004149void InstructionCodeGeneratorARM64::VisitParameterValue(
4150 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004151 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004152}
4153
4154void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4155 LocationSummary* locations =
4156 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004157 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004158}
4159
4160void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4161 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4162 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004163}
4164
4165void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4166 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4167 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4168 locations->SetInAt(i, Location::Any());
4169 }
4170 locations->SetOut(Location::Any());
4171}
4172
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004173void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004174 LOG(FATAL) << "Unreachable";
4175}
4176
Serban Constantinescu02164b32014-11-13 14:05:07 +00004177void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004178 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004179 LocationSummary::CallKind call_kind =
4180 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004181 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4182
4183 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004184 case Primitive::kPrimInt:
4185 case Primitive::kPrimLong:
4186 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004187 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004188 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4189 break;
4190
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004191 case Primitive::kPrimFloat:
4192 case Primitive::kPrimDouble: {
4193 InvokeRuntimeCallingConvention calling_convention;
4194 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4195 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4196 locations->SetOut(calling_convention.GetReturnLocation(type));
4197
4198 break;
4199 }
4200
Serban Constantinescu02164b32014-11-13 14:05:07 +00004201 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004202 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004203 }
4204}
4205
4206void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4207 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004208
Serban Constantinescu02164b32014-11-13 14:05:07 +00004209 switch (type) {
4210 case Primitive::kPrimInt:
4211 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004212 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004213 break;
4214 }
4215
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004216 case Primitive::kPrimFloat:
4217 case Primitive::kPrimDouble: {
4218 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4219 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004220 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004221 if (type == Primitive::kPrimFloat) {
4222 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4223 } else {
4224 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4225 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004226 break;
4227 }
4228
Serban Constantinescu02164b32014-11-13 14:05:07 +00004229 default:
4230 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004231 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004232 }
4233}
4234
Calin Juravle27df7582015-04-17 19:12:31 +01004235void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4236 memory_barrier->SetLocations(nullptr);
4237}
4238
4239void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4240 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4241}
4242
Alexandre Rames5319def2014-10-23 10:03:10 +01004243void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4245 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004246 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004247}
4248
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004249void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004250 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004251}
4252
4253void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4254 instruction->SetLocations(nullptr);
4255}
4256
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004257void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004258 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004259}
4260
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004261void LocationsBuilderARM64::VisitRor(HRor* ror) {
4262 HandleBinaryOp(ror);
4263}
4264
4265void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4266 HandleBinaryOp(ror);
4267}
4268
Serban Constantinescu02164b32014-11-13 14:05:07 +00004269void LocationsBuilderARM64::VisitShl(HShl* shl) {
4270 HandleShift(shl);
4271}
4272
4273void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4274 HandleShift(shl);
4275}
4276
4277void LocationsBuilderARM64::VisitShr(HShr* shr) {
4278 HandleShift(shr);
4279}
4280
4281void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4282 HandleShift(shr);
4283}
4284
Alexandre Rames5319def2014-10-23 10:03:10 +01004285void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4287 Primitive::Type field_type = store->InputAt(1)->GetType();
4288 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004289 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004290 case Primitive::kPrimBoolean:
4291 case Primitive::kPrimByte:
4292 case Primitive::kPrimChar:
4293 case Primitive::kPrimShort:
4294 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004295 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004296 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4297 break;
4298
4299 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004300 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004301 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4302 break;
4303
4304 default:
4305 LOG(FATAL) << "Unimplemented local type " << field_type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004306 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +01004307 }
4308}
4309
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004310void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004311}
4312
4313void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004314 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004315}
4316
4317void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004318 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004319}
4320
Alexandre Rames67555f72014-11-18 10:55:16 +00004321void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004322 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004323}
4324
4325void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004326 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004327}
4328
4329void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004330 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004331}
4332
Alexandre Rames67555f72014-11-18 10:55:16 +00004333void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004334 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004335}
4336
Calin Juravlee460d1d2015-09-29 04:52:17 +01004337void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4338 HUnresolvedInstanceFieldGet* instruction) {
4339 FieldAccessCallingConventionARM64 calling_convention;
4340 codegen_->CreateUnresolvedFieldLocationSummary(
4341 instruction, instruction->GetFieldType(), calling_convention);
4342}
4343
4344void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4345 HUnresolvedInstanceFieldGet* instruction) {
4346 FieldAccessCallingConventionARM64 calling_convention;
4347 codegen_->GenerateUnresolvedFieldAccess(instruction,
4348 instruction->GetFieldType(),
4349 instruction->GetFieldIndex(),
4350 instruction->GetDexPc(),
4351 calling_convention);
4352}
4353
4354void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4355 HUnresolvedInstanceFieldSet* instruction) {
4356 FieldAccessCallingConventionARM64 calling_convention;
4357 codegen_->CreateUnresolvedFieldLocationSummary(
4358 instruction, instruction->GetFieldType(), calling_convention);
4359}
4360
4361void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4362 HUnresolvedInstanceFieldSet* instruction) {
4363 FieldAccessCallingConventionARM64 calling_convention;
4364 codegen_->GenerateUnresolvedFieldAccess(instruction,
4365 instruction->GetFieldType(),
4366 instruction->GetFieldIndex(),
4367 instruction->GetDexPc(),
4368 calling_convention);
4369}
4370
4371void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4372 HUnresolvedStaticFieldGet* instruction) {
4373 FieldAccessCallingConventionARM64 calling_convention;
4374 codegen_->CreateUnresolvedFieldLocationSummary(
4375 instruction, instruction->GetFieldType(), calling_convention);
4376}
4377
4378void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4379 HUnresolvedStaticFieldGet* instruction) {
4380 FieldAccessCallingConventionARM64 calling_convention;
4381 codegen_->GenerateUnresolvedFieldAccess(instruction,
4382 instruction->GetFieldType(),
4383 instruction->GetFieldIndex(),
4384 instruction->GetDexPc(),
4385 calling_convention);
4386}
4387
4388void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4389 HUnresolvedStaticFieldSet* instruction) {
4390 FieldAccessCallingConventionARM64 calling_convention;
4391 codegen_->CreateUnresolvedFieldLocationSummary(
4392 instruction, instruction->GetFieldType(), calling_convention);
4393}
4394
4395void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4396 HUnresolvedStaticFieldSet* instruction) {
4397 FieldAccessCallingConventionARM64 calling_convention;
4398 codegen_->GenerateUnresolvedFieldAccess(instruction,
4399 instruction->GetFieldType(),
4400 instruction->GetFieldIndex(),
4401 instruction->GetDexPc(),
4402 calling_convention);
4403}
4404
Alexandre Rames5319def2014-10-23 10:03:10 +01004405void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4407}
4408
4409void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004410 HBasicBlock* block = instruction->GetBlock();
4411 if (block->GetLoopInformation() != nullptr) {
4412 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4413 // The back edge will generate the suspend check.
4414 return;
4415 }
4416 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4417 // The goto will generate the suspend check.
4418 return;
4419 }
4420 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004421}
4422
4423void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4424 temp->SetLocations(nullptr);
4425}
4426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004427void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004428 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004429}
4430
Alexandre Rames67555f72014-11-18 10:55:16 +00004431void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4432 LocationSummary* locations =
4433 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4434 InvokeRuntimeCallingConvention calling_convention;
4435 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4436}
4437
4438void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4439 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004440 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004441 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004442}
4443
4444void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4445 LocationSummary* locations =
4446 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4447 Primitive::Type input_type = conversion->GetInputType();
4448 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004449 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004450 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4451 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4452 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4453 }
4454
Alexandre Rames542361f2015-01-29 16:57:31 +00004455 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004456 locations->SetInAt(0, Location::RequiresFpuRegister());
4457 } else {
4458 locations->SetInAt(0, Location::RequiresRegister());
4459 }
4460
Alexandre Rames542361f2015-01-29 16:57:31 +00004461 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004462 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4463 } else {
4464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4465 }
4466}
4467
4468void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4469 Primitive::Type result_type = conversion->GetResultType();
4470 Primitive::Type input_type = conversion->GetInputType();
4471
4472 DCHECK_NE(input_type, result_type);
4473
Alexandre Rames542361f2015-01-29 16:57:31 +00004474 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004475 int result_size = Primitive::ComponentSize(result_type);
4476 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004477 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004478 Register output = OutputRegister(conversion);
4479 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004480 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004481 // 'int' values are used directly as W registers, discarding the top
4482 // bits, so we don't need to sign-extend and can just perform a move.
4483 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4484 // top 32 bits of the target register. We theoretically could leave those
4485 // bits unchanged, but we would have to make sure that no code uses a
4486 // 32bit input value as a 64bit value assuming that the top 32 bits are
4487 // zero.
4488 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004489 } else if (result_type == Primitive::kPrimChar ||
4490 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4491 __ Ubfx(output,
4492 output.IsX() ? source.X() : source.W(),
4493 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004494 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004495 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004496 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004497 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004498 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004499 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004500 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4501 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004502 } else if (Primitive::IsFloatingPointType(result_type) &&
4503 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004504 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4505 } else {
4506 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4507 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004508 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004509}
Alexandre Rames67555f72014-11-18 10:55:16 +00004510
Serban Constantinescu02164b32014-11-13 14:05:07 +00004511void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4512 HandleShift(ushr);
4513}
4514
4515void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4516 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004517}
4518
4519void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4520 HandleBinaryOp(instruction);
4521}
4522
4523void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4524 HandleBinaryOp(instruction);
4525}
4526
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004527void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004528 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004529 LOG(FATAL) << "Unreachable";
4530}
4531
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004532void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004533 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004534 LOG(FATAL) << "Unreachable";
4535}
4536
Mark Mendellfe57faa2015-09-18 09:26:15 -04004537// Simple implementation of packed switch - generate cascaded compare/jumps.
4538void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4539 LocationSummary* locations =
4540 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4541 locations->SetInAt(0, Location::RequiresRegister());
4542}
4543
4544void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4545 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004546 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004547 Register value_reg = InputRegisterAt(switch_instr, 0);
4548 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4549
Zheng Xu3927c8b2015-11-18 17:46:25 +08004550 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4551 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4552 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4553 // make sure we don't emit it if the target may run out of range.
4554 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4555 // ranges and emit the tables only as required.
4556 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004557
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004558 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004559 // Current instruction id is an upper bound of the number of HIRs in the graph.
4560 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4561 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004562 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4563 Register temp = temps.AcquireW();
4564 __ Subs(temp, value_reg, Operand(lower_bound));
4565
Zheng Xu3927c8b2015-11-18 17:46:25 +08004566 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004567 // Jump to successors[0] if value == lower_bound.
4568 __ B(eq, codegen_->GetLabelOf(successors[0]));
4569 int32_t last_index = 0;
4570 for (; num_entries - last_index > 2; last_index += 2) {
4571 __ Subs(temp, temp, Operand(2));
4572 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4573 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4574 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4575 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4576 }
4577 if (num_entries - last_index == 2) {
4578 // The last missing case_value.
4579 __ Cmp(temp, Operand(1));
4580 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004581 }
4582
4583 // And the default for any other value.
4584 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4585 __ B(codegen_->GetLabelOf(default_block));
4586 }
4587 } else {
4588 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4589 codegen_->AddJumpTable(jump_table);
4590
4591 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4592
4593 // Below instructions should use at most one blocked register. Since there are two blocked
4594 // registers, we are free to block one.
4595 Register temp_w = temps.AcquireW();
4596 Register index;
4597 // Remove the bias.
4598 if (lower_bound != 0) {
4599 index = temp_w;
4600 __ Sub(index, value_reg, Operand(lower_bound));
4601 } else {
4602 index = value_reg;
4603 }
4604
4605 // Jump to default block if index is out of the range.
4606 __ Cmp(index, Operand(num_entries));
4607 __ B(hs, codegen_->GetLabelOf(default_block));
4608
4609 // In current VIXL implementation, it won't require any blocked registers to encode the
4610 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4611 // register pressure.
4612 Register table_base = temps.AcquireX();
4613 // Load jump offset from the table.
4614 __ Adr(table_base, jump_table->GetTableStartLabel());
4615 Register jump_offset = temp_w;
4616 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4617
4618 // Jump to target block by branching to table_base(pc related) + offset.
4619 Register target_address = table_base;
4620 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4621 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004622 }
4623}
4624
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004625void CodeGeneratorARM64::GenerateReadBarrier(HInstruction* instruction,
4626 Location out,
4627 Location ref,
4628 Location obj,
4629 uint32_t offset,
4630 Location index) {
4631 DCHECK(kEmitCompilerReadBarrier);
4632
4633 // If heap poisoning is enabled, the unpoisoning of the loaded
4634 // reference will be carried out by the runtime within the slow
4635 // path.
4636 //
4637 // Note that `ref` currently does not get unpoisoned (when heap
4638 // poisoning is enabled), which is alright as the `ref` argument is
4639 // not used by the artReadBarrierSlow entry point.
4640 //
4641 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4642 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4643 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4644 AddSlowPath(slow_path);
4645
4646 // TODO: When read barrier has a fast path, add it here.
4647 /* Currently the read barrier call is inserted after the original load.
4648 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
4649 * original load. This load-load ordering is required by the read barrier.
4650 * The fast path/slow path (for Baker's algorithm) should look like:
4651 *
4652 * bool isGray = obj.LockWord & kReadBarrierMask;
4653 * lfence; // load fence or artificial data dependence to prevent load-load reordering
4654 * ref = obj.field; // this is the original load
4655 * if (isGray) {
4656 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
4657 * }
4658 */
4659
4660 __ B(slow_path->GetEntryLabel());
4661 __ Bind(slow_path->GetExitLabel());
4662}
4663
4664void CodeGeneratorARM64::MaybeGenerateReadBarrier(HInstruction* instruction,
4665 Location out,
4666 Location ref,
4667 Location obj,
4668 uint32_t offset,
4669 Location index) {
4670 if (kEmitCompilerReadBarrier) {
4671 // If heap poisoning is enabled, unpoisoning will be taken care of
4672 // by the runtime within the slow path.
4673 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
4674 } else if (kPoisonHeapReferences) {
4675 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4676 }
4677}
4678
4679void CodeGeneratorARM64::GenerateReadBarrierForRoot(HInstruction* instruction,
4680 Location out,
4681 Location root) {
4682 DCHECK(kEmitCompilerReadBarrier);
4683
4684 // Note that GC roots are not affected by heap poisoning, so we do
4685 // not need to do anything special for this here.
4686 SlowPathCodeARM64* slow_path =
4687 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4688 AddSlowPath(slow_path);
4689
4690 // TODO: Implement a fast path for ReadBarrierForRoot, performing
4691 // the following operation (for Baker's algorithm):
4692 //
4693 // if (thread.tls32_.is_gc_marking) {
4694 // root = Mark(root);
4695 // }
4696
4697 __ B(slow_path->GetEntryLabel());
4698 __ Bind(slow_path->GetExitLabel());
4699}
4700
Alexandre Rames67555f72014-11-18 10:55:16 +00004701#undef __
4702#undef QUICK_ENTRY_POINT
4703
Alexandre Rames5319def2014-10-23 10:03:10 +01004704} // namespace arm64
4705} // namespace art