blob: 55fa1faa4f2da0432ccac109d43a235d454cc594 [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
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001115void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
1116 // 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
1136 if (is_baseline) {
1137 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
1138 while (!reserved_core_baseline_registers.IsEmpty()) {
1139 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
1140 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001141 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001142
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001143 if (is_baseline || GetGraph()->IsDebuggable()) {
1144 // Stubs do not save callee-save floating point registers. If the graph
1145 // is debuggable, we need to deal with these registers differently. For
1146 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001147 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
1148 while (!reserved_fp_baseline_registers.IsEmpty()) {
1149 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
1150 }
1151 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001152}
1153
1154Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
1155 if (type == Primitive::kPrimVoid) {
1156 LOG(FATAL) << "Unreachable type " << type;
1157 }
1158
Alexandre Rames542361f2015-01-29 16:57:31 +00001159 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001160 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
1161 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001162 return Location::FpuRegisterLocation(reg);
1163 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001164 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
1165 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001166 return Location::RegisterLocation(reg);
1167 }
1168}
1169
Alexandre Rames3e69f162014-12-10 10:36:50 +00001170size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1171 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1172 __ Str(reg, MemOperand(sp, stack_index));
1173 return kArm64WordSize;
1174}
1175
1176size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1177 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1178 __ Ldr(reg, MemOperand(sp, stack_index));
1179 return kArm64WordSize;
1180}
1181
1182size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1183 FPRegister reg = FPRegister(reg_id, kDRegSize);
1184 __ Str(reg, MemOperand(sp, stack_index));
1185 return kArm64WordSize;
1186}
1187
1188size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1189 FPRegister reg = FPRegister(reg_id, kDRegSize);
1190 __ Ldr(reg, MemOperand(sp, stack_index));
1191 return kArm64WordSize;
1192}
1193
Alexandre Rames5319def2014-10-23 10:03:10 +01001194void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001195 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001196}
1197
1198void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001199 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001200}
1201
Alexandre Rames67555f72014-11-18 10:55:16 +00001202void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001203 if (constant->IsIntConstant()) {
1204 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1205 } else if (constant->IsLongConstant()) {
1206 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1207 } else if (constant->IsNullConstant()) {
1208 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001209 } else if (constant->IsFloatConstant()) {
1210 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1211 } else {
1212 DCHECK(constant->IsDoubleConstant());
1213 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1214 }
1215}
1216
Alexandre Rames3e69f162014-12-10 10:36:50 +00001217
1218static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1219 DCHECK(constant.IsConstant());
1220 HConstant* cst = constant.GetConstant();
1221 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001222 // Null is mapped to a core W register, which we associate with kPrimInt.
1223 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001224 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1225 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1226 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1227}
1228
Calin Juravlee460d1d2015-09-29 04:52:17 +01001229void CodeGeneratorARM64::MoveLocation(Location destination,
1230 Location source,
1231 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001232 if (source.Equals(destination)) {
1233 return;
1234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001235
1236 // A valid move can always be inferred from the destination and source
1237 // locations. When moving from and to a register, the argument type can be
1238 // used to generate 32bit instead of 64bit moves. In debug mode we also
1239 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001240 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001241
1242 if (destination.IsRegister() || destination.IsFpuRegister()) {
1243 if (unspecified_type) {
1244 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1245 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001246 (src_cst != nullptr && (src_cst->IsIntConstant()
1247 || src_cst->IsFloatConstant()
1248 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001251 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001252 // If the source is a double stack slot or a 64bit constant, a 64bit
1253 // type is appropriate. Else the source is a register, and since the
1254 // type has not been specified, we chose a 64bit type to force a 64bit
1255 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001256 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001257 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001258 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1260 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1261 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001262 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1263 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1264 __ Ldr(dst, StackOperandFrom(source));
1265 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001269 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001270 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001271 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001272 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1274 ? Primitive::kPrimLong
1275 : Primitive::kPrimInt;
1276 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1277 }
1278 } else {
1279 DCHECK(source.IsFpuRegister());
1280 if (destination.IsRegister()) {
1281 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1282 ? Primitive::kPrimDouble
1283 : Primitive::kPrimFloat;
1284 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1285 } else {
1286 DCHECK(destination.IsFpuRegister());
1287 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001288 }
1289 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001290 } else { // The destination is not a register. It must be a stack slot.
1291 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1292 if (source.IsRegister() || source.IsFpuRegister()) {
1293 if (unspecified_type) {
1294 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001295 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001296 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001297 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001298 }
1299 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001300 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1301 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1302 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001303 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001304 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1305 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001306 UseScratchRegisterScope temps(GetVIXLAssembler());
1307 HConstant* src_cst = source.GetConstant();
1308 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001309 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001310 temp = temps.AcquireW();
1311 } else if (src_cst->IsLongConstant()) {
1312 temp = temps.AcquireX();
1313 } else if (src_cst->IsFloatConstant()) {
1314 temp = temps.AcquireS();
1315 } else {
1316 DCHECK(src_cst->IsDoubleConstant());
1317 temp = temps.AcquireD();
1318 }
1319 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001320 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001321 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001323 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001324 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001325 // There is generally less pressure on FP registers.
1326 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 __ Ldr(temp, StackOperandFrom(source));
1328 __ Str(temp, StackOperandFrom(destination));
1329 }
1330 }
1331}
1332
1333void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001334 CPURegister dst,
1335 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001336 switch (type) {
1337 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001338 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 break;
1340 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001341 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001342 break;
1343 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001344 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001345 break;
1346 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001347 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001348 break;
1349 case Primitive::kPrimInt:
1350 case Primitive::kPrimNot:
1351 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001352 case Primitive::kPrimFloat:
1353 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001354 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001355 __ Ldr(dst, src);
1356 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001357 case Primitive::kPrimVoid:
1358 LOG(FATAL) << "Unreachable type " << type;
1359 }
1360}
1361
Calin Juravle77520bc2015-01-12 18:45:46 +00001362void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001363 CPURegister dst,
1364 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001365 MacroAssembler* masm = GetVIXLAssembler();
1366 BlockPoolsScope block_pools(masm);
1367 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001368 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001369 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001370
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001371 DCHECK(!src.IsPreIndex());
1372 DCHECK(!src.IsPostIndex());
1373
1374 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001375 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001376 MemOperand base = MemOperand(temp_base);
1377 switch (type) {
1378 case Primitive::kPrimBoolean:
1379 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001380 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001381 break;
1382 case Primitive::kPrimByte:
1383 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001384 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001385 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1386 break;
1387 case Primitive::kPrimChar:
1388 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001389 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001390 break;
1391 case Primitive::kPrimShort:
1392 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001393 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001394 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1395 break;
1396 case Primitive::kPrimInt:
1397 case Primitive::kPrimNot:
1398 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001399 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001400 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001401 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 break;
1403 case Primitive::kPrimFloat:
1404 case Primitive::kPrimDouble: {
1405 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001406 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001407
1408 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1409 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001410 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411 __ Fmov(FPRegister(dst), temp);
1412 break;
1413 }
1414 case Primitive::kPrimVoid:
1415 LOG(FATAL) << "Unreachable type " << type;
1416 }
1417}
1418
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001419void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001420 CPURegister src,
1421 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001422 switch (type) {
1423 case Primitive::kPrimBoolean:
1424 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001425 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001426 break;
1427 case Primitive::kPrimChar:
1428 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001429 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001430 break;
1431 case Primitive::kPrimInt:
1432 case Primitive::kPrimNot:
1433 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001434 case Primitive::kPrimFloat:
1435 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001436 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001437 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001438 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001439 case Primitive::kPrimVoid:
1440 LOG(FATAL) << "Unreachable type " << type;
1441 }
1442}
1443
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1445 CPURegister src,
1446 const MemOperand& dst) {
1447 UseScratchRegisterScope temps(GetVIXLAssembler());
1448 Register temp_base = temps.AcquireX();
1449
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001450 DCHECK(!dst.IsPreIndex());
1451 DCHECK(!dst.IsPostIndex());
1452
1453 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001454 Operand op = OperandFromMemOperand(dst);
1455 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001456 MemOperand base = MemOperand(temp_base);
1457 switch (type) {
1458 case Primitive::kPrimBoolean:
1459 case Primitive::kPrimByte:
1460 __ Stlrb(Register(src), base);
1461 break;
1462 case Primitive::kPrimChar:
1463 case Primitive::kPrimShort:
1464 __ Stlrh(Register(src), base);
1465 break;
1466 case Primitive::kPrimInt:
1467 case Primitive::kPrimNot:
1468 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001469 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001470 __ Stlr(Register(src), base);
1471 break;
1472 case Primitive::kPrimFloat:
1473 case Primitive::kPrimDouble: {
1474 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001475 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001476
1477 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1478 __ Fmov(temp, FPRegister(src));
1479 __ Stlr(temp, base);
1480 break;
1481 }
1482 case Primitive::kPrimVoid:
1483 LOG(FATAL) << "Unreachable type " << type;
1484 }
1485}
1486
Calin Juravle175dc732015-08-25 15:42:32 +01001487void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1488 HInstruction* instruction,
1489 uint32_t dex_pc,
1490 SlowPathCode* slow_path) {
1491 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1492 instruction,
1493 dex_pc,
1494 slow_path);
1495}
1496
Alexandre Rames67555f72014-11-18 10:55:16 +00001497void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1498 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001499 uint32_t dex_pc,
1500 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001502 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001503 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1504 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001505 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001506}
1507
1508void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1509 vixl::Register class_reg) {
1510 UseScratchRegisterScope temps(GetVIXLAssembler());
1511 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001512 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001513 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001514
Serban Constantinescu02164b32014-11-13 14:05:07 +00001515 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001516 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001517 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1518 __ Add(temp, class_reg, status_offset);
1519 __ Ldar(temp, HeapOperand(temp));
1520 __ Cmp(temp, mirror::Class::kStatusInitialized);
1521 __ B(lt, slow_path->GetEntryLabel());
1522 } else {
1523 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1524 __ Cmp(temp, mirror::Class::kStatusInitialized);
1525 __ B(lt, slow_path->GetEntryLabel());
1526 __ Dmb(InnerShareable, BarrierReads);
1527 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001528 __ Bind(slow_path->GetExitLabel());
1529}
Alexandre Rames5319def2014-10-23 10:03:10 +01001530
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001531void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1532 BarrierType type = BarrierAll;
1533
1534 switch (kind) {
1535 case MemBarrierKind::kAnyAny:
1536 case MemBarrierKind::kAnyStore: {
1537 type = BarrierAll;
1538 break;
1539 }
1540 case MemBarrierKind::kLoadAny: {
1541 type = BarrierReads;
1542 break;
1543 }
1544 case MemBarrierKind::kStoreStore: {
1545 type = BarrierWrites;
1546 break;
1547 }
1548 default:
1549 LOG(FATAL) << "Unexpected memory barrier " << kind;
1550 }
1551 __ Dmb(InnerShareable, type);
1552}
1553
Serban Constantinescu02164b32014-11-13 14:05:07 +00001554void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1555 HBasicBlock* successor) {
1556 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001557 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1558 if (slow_path == nullptr) {
1559 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1560 instruction->SetSlowPath(slow_path);
1561 codegen_->AddSlowPath(slow_path);
1562 if (successor != nullptr) {
1563 DCHECK(successor->IsLoopHeader());
1564 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1565 }
1566 } else {
1567 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1568 }
1569
Serban Constantinescu02164b32014-11-13 14:05:07 +00001570 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1571 Register temp = temps.AcquireW();
1572
1573 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1574 if (successor == nullptr) {
1575 __ Cbnz(temp, slow_path->GetEntryLabel());
1576 __ Bind(slow_path->GetReturnLabel());
1577 } else {
1578 __ Cbz(temp, codegen_->GetLabelOf(successor));
1579 __ B(slow_path->GetEntryLabel());
1580 // slow_path will return to GetLabelOf(successor).
1581 }
1582}
1583
Alexandre Rames5319def2014-10-23 10:03:10 +01001584InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1585 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001586 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001587 assembler_(codegen->GetAssembler()),
1588 codegen_(codegen) {}
1589
1590#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001591 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001592
1593#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1594
1595enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 // Using a base helps identify when we hit such breakpoints.
1597 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001598#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1599 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1600#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1601};
1602
1603#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001604 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001605 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1606 } \
1607 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1609 locations->SetOut(Location::Any()); \
1610 }
1611 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1612#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1613
1614#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001615#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001616
Alexandre Rames67555f72014-11-18 10:55:16 +00001617void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001618 DCHECK_EQ(instr->InputCount(), 2U);
1619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1620 Primitive::Type type = instr->GetResultType();
1621 switch (type) {
1622 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001623 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001624 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001625 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001627 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001628
1629 case Primitive::kPrimFloat:
1630 case Primitive::kPrimDouble:
1631 locations->SetInAt(0, Location::RequiresFpuRegister());
1632 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001633 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001634 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001635
Alexandre Rames5319def2014-10-23 10:03:10 +01001636 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001637 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001638 }
1639}
1640
Alexandre Rames09a99962015-04-15 11:47:56 +01001641void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001642 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1643
1644 bool object_field_get_with_read_barrier =
1645 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001646 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001647 new (GetGraph()->GetArena()) LocationSummary(instruction,
1648 object_field_get_with_read_barrier ?
1649 LocationSummary::kCallOnSlowPath :
1650 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001651 locations->SetInAt(0, Location::RequiresRegister());
1652 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1653 locations->SetOut(Location::RequiresFpuRegister());
1654 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001655 // The output overlaps for an object field get when read barriers
1656 // are enabled: we do not want the load to overwrite the object's
1657 // location, as we need it to emit the read barrier.
1658 locations->SetOut(
1659 Location::RequiresRegister(),
1660 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001661 }
1662}
1663
1664void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1665 const FieldInfo& field_info) {
1666 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001667 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001668 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001669
1670 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1671 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1672
1673 if (field_info.IsVolatile()) {
1674 if (use_acquire_release) {
1675 // NB: LoadAcquire will record the pc info if needed.
1676 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1677 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001678 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001679 codegen_->MaybeRecordImplicitNullCheck(instruction);
1680 // For IRIW sequential consistency kLoadAny is not sufficient.
1681 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1682 }
1683 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001684 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001685 codegen_->MaybeRecordImplicitNullCheck(instruction);
1686 }
Roland Levillain4d027112015-07-01 15:41:14 +01001687
1688 if (field_type == Primitive::kPrimNot) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001689 LocationSummary* locations = instruction->GetLocations();
1690 Location base = locations->InAt(0);
1691 Location out = locations->Out();
1692 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
1693 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01001694 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001695}
1696
1697void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1698 LocationSummary* locations =
1699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1700 locations->SetInAt(0, Location::RequiresRegister());
1701 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1702 locations->SetInAt(1, Location::RequiresFpuRegister());
1703 } else {
1704 locations->SetInAt(1, Location::RequiresRegister());
1705 }
1706}
1707
1708void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001709 const FieldInfo& field_info,
1710 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001711 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001712 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001713
1714 Register obj = InputRegisterAt(instruction, 0);
1715 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001716 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001717 Offset offset = field_info.GetFieldOffset();
1718 Primitive::Type field_type = field_info.GetFieldType();
1719 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1720
Roland Levillain4d027112015-07-01 15:41:14 +01001721 {
1722 // We use a block to end the scratch scope before the write barrier, thus
1723 // freeing the temporary registers so they can be used in `MarkGCCard`.
1724 UseScratchRegisterScope temps(GetVIXLAssembler());
1725
1726 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1727 DCHECK(value.IsW());
1728 Register temp = temps.AcquireW();
1729 __ Mov(temp, value.W());
1730 GetAssembler()->PoisonHeapReference(temp.W());
1731 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001732 }
Roland Levillain4d027112015-07-01 15:41:14 +01001733
1734 if (field_info.IsVolatile()) {
1735 if (use_acquire_release) {
1736 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1737 codegen_->MaybeRecordImplicitNullCheck(instruction);
1738 } else {
1739 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1740 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1741 codegen_->MaybeRecordImplicitNullCheck(instruction);
1742 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1743 }
1744 } else {
1745 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1746 codegen_->MaybeRecordImplicitNullCheck(instruction);
1747 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001748 }
1749
1750 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001751 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001752 }
1753}
1754
Alexandre Rames67555f72014-11-18 10:55:16 +00001755void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001756 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001757
1758 switch (type) {
1759 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001760 case Primitive::kPrimLong: {
1761 Register dst = OutputRegister(instr);
1762 Register lhs = InputRegisterAt(instr, 0);
1763 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001764 if (instr->IsAdd()) {
1765 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001766 } else if (instr->IsAnd()) {
1767 __ And(dst, lhs, rhs);
1768 } else if (instr->IsOr()) {
1769 __ Orr(dst, lhs, rhs);
1770 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001771 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001772 } else if (instr->IsRor()) {
1773 if (rhs.IsImmediate()) {
1774 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1775 __ Ror(dst, lhs, shift);
1776 } else {
1777 // Ensure shift distance is in the same size register as the result. If
1778 // we are rotating a long and the shift comes in a w register originally,
1779 // we don't need to sxtw for use as an x since the shift distances are
1780 // all & reg_bits - 1.
1781 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1782 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 } else {
1784 DCHECK(instr->IsXor());
1785 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001786 }
1787 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001788 }
1789 case Primitive::kPrimFloat:
1790 case Primitive::kPrimDouble: {
1791 FPRegister dst = OutputFPRegister(instr);
1792 FPRegister lhs = InputFPRegisterAt(instr, 0);
1793 FPRegister rhs = InputFPRegisterAt(instr, 1);
1794 if (instr->IsAdd()) {
1795 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001796 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001797 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001798 } else {
1799 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001800 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001801 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001802 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001803 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001804 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 }
1806}
1807
Serban Constantinescu02164b32014-11-13 14:05:07 +00001808void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1809 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1810
1811 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1812 Primitive::Type type = instr->GetResultType();
1813 switch (type) {
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimLong: {
1816 locations->SetInAt(0, Location::RequiresRegister());
1817 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1818 locations->SetOut(Location::RequiresRegister());
1819 break;
1820 }
1821 default:
1822 LOG(FATAL) << "Unexpected shift type " << type;
1823 }
1824}
1825
1826void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1827 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1828
1829 Primitive::Type type = instr->GetType();
1830 switch (type) {
1831 case Primitive::kPrimInt:
1832 case Primitive::kPrimLong: {
1833 Register dst = OutputRegister(instr);
1834 Register lhs = InputRegisterAt(instr, 0);
1835 Operand rhs = InputOperandAt(instr, 1);
1836 if (rhs.IsImmediate()) {
1837 uint32_t shift_value = (type == Primitive::kPrimInt)
1838 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1839 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1840 if (instr->IsShl()) {
1841 __ Lsl(dst, lhs, shift_value);
1842 } else if (instr->IsShr()) {
1843 __ Asr(dst, lhs, shift_value);
1844 } else {
1845 __ Lsr(dst, lhs, shift_value);
1846 }
1847 } else {
1848 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1849
1850 if (instr->IsShl()) {
1851 __ Lsl(dst, lhs, rhs_reg);
1852 } else if (instr->IsShr()) {
1853 __ Asr(dst, lhs, rhs_reg);
1854 } else {
1855 __ Lsr(dst, lhs, rhs_reg);
1856 }
1857 }
1858 break;
1859 }
1860 default:
1861 LOG(FATAL) << "Unexpected shift operation type " << type;
1862 }
1863}
1864
Alexandre Rames5319def2014-10-23 10:03:10 +01001865void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001866 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001867}
1868
1869void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001870 HandleBinaryOp(instruction);
1871}
1872
1873void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1874 HandleBinaryOp(instruction);
1875}
1876
1877void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1878 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001879}
1880
Alexandre Rames8626b742015-11-25 16:28:08 +00001881void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1882 HArm64DataProcWithShifterOp* instruction) {
1883 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1884 instruction->GetType() == Primitive::kPrimLong);
1885 LocationSummary* locations =
1886 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1887 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1888 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1889 } else {
1890 locations->SetInAt(0, Location::RequiresRegister());
1891 }
1892 locations->SetInAt(1, Location::RequiresRegister());
1893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1894}
1895
1896void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1897 HArm64DataProcWithShifterOp* instruction) {
1898 Primitive::Type type = instruction->GetType();
1899 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1900 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1901 Register out = OutputRegister(instruction);
1902 Register left;
1903 if (kind != HInstruction::kNeg) {
1904 left = InputRegisterAt(instruction, 0);
1905 }
1906 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1907 // shifter operand operation, the IR generating `right_reg` (input to the type
1908 // conversion) can have a different type from the current instruction's type,
1909 // so we manually indicate the type.
1910 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
1911 int64_t shift_amount = (type == Primitive::kPrimInt)
1912 ? static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxIntShiftValue)
1913 : static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxLongShiftValue);
1914
1915 Operand right_operand(0);
1916
1917 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1918 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1919 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1920 } else {
1921 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1922 }
1923
1924 // Logical binary operations do not support extension operations in the
1925 // operand. Note that VIXL would still manage if it was passed by generating
1926 // the extension as a separate instruction.
1927 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1928 DCHECK(!right_operand.IsExtendedRegister() ||
1929 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1930 kind != HInstruction::kNeg));
1931 switch (kind) {
1932 case HInstruction::kAdd:
1933 __ Add(out, left, right_operand);
1934 break;
1935 case HInstruction::kAnd:
1936 __ And(out, left, right_operand);
1937 break;
1938 case HInstruction::kNeg:
1939 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1940 __ Neg(out, right_operand);
1941 break;
1942 case HInstruction::kOr:
1943 __ Orr(out, left, right_operand);
1944 break;
1945 case HInstruction::kSub:
1946 __ Sub(out, left, right_operand);
1947 break;
1948 case HInstruction::kXor:
1949 __ Eor(out, left, right_operand);
1950 break;
1951 default:
1952 LOG(FATAL) << "Unexpected operation kind: " << kind;
1953 UNREACHABLE();
1954 }
1955}
1956
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001957void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001958 // The read barrier instrumentation does not support the
1959 // HArm64IntermediateAddress instruction yet.
1960 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001961 LocationSummary* locations =
1962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1965 locations->SetOut(Location::RequiresRegister());
1966}
1967
1968void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1969 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001970 // The read barrier instrumentation does not support the
1971 // HArm64IntermediateAddress instruction yet.
1972 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001973 __ Add(OutputRegister(instruction),
1974 InputRegisterAt(instruction, 0),
1975 Operand(InputOperandAt(instruction, 1)));
1976}
1977
Alexandre Rames418318f2015-11-20 15:55:47 +00001978void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1979 LocationSummary* locations =
1980 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1981 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1982 Location::RequiresRegister());
1983 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1984 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1986}
1987
1988void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1989 Register res = OutputRegister(instr);
1990 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
1991 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
1992 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
1993
1994 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1995 // This fixup should be carried out for all multiply-accumulate instructions:
1996 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1997 if (instr->GetType() == Primitive::kPrimLong &&
1998 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1999 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2000 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2001 if (prev->IsLoadOrStore()) {
2002 // Make sure we emit only exactly one nop.
2003 vixl::CodeBufferCheckScope scope(masm,
2004 vixl::kInstructionSize,
2005 vixl::CodeBufferCheckScope::kCheck,
2006 vixl::CodeBufferCheckScope::kExactSize);
2007 __ nop();
2008 }
2009 }
2010
2011 if (instr->GetOpKind() == HInstruction::kAdd) {
2012 __ Madd(res, mul_left, mul_right, accumulator);
2013 } else {
2014 DCHECK(instr->GetOpKind() == HInstruction::kSub);
2015 __ Msub(res, mul_left, mul_right, accumulator);
2016 }
2017}
2018
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002019void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002020 bool object_array_get_with_read_barrier =
2021 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002022 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002023 new (GetGraph()->GetArena()) LocationSummary(instruction,
2024 object_array_get_with_read_barrier ?
2025 LocationSummary::kCallOnSlowPath :
2026 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002027 locations->SetInAt(0, Location::RequiresRegister());
2028 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002029 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2030 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2031 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002032 // The output overlaps in the case of an object array get with
2033 // read barriers enabled: we do not want the move to overwrite the
2034 // array's location, as we need it to emit the read barrier.
2035 locations->SetOut(
2036 Location::RequiresRegister(),
2037 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002038 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002039}
2040
2041void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002042 Primitive::Type type = instruction->GetType();
2043 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002044 LocationSummary* locations = instruction->GetLocations();
2045 Location index = locations->InAt(1);
2046 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002047 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002048 CPURegister dest = OutputCPURegister(instruction);
2049
Alexandre Ramesd921d642015-04-16 15:07:16 +01002050 MacroAssembler* masm = GetVIXLAssembler();
2051 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002052 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002053 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002054
2055 if (index.IsConstant()) {
2056 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002057 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002058 } else {
2059 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002060 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002061 // The read barrier instrumentation does not support the
2062 // HArm64IntermediateAddress instruction yet.
2063 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002064 // We do not need to compute the intermediate address from the array: the
2065 // input instruction has done it already. See the comment in
2066 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2067 if (kIsDebugBuild) {
2068 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2069 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2070 }
2071 temp = obj;
2072 } else {
2073 __ Add(temp, obj, offset);
2074 }
Alexandre Rames82000b02015-07-07 11:34:16 +01002075 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002076 }
2077
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002078 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00002079 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002080
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002081 if (type == Primitive::kPrimNot) {
2082 static_assert(
2083 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2084 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2085 Location obj_loc = locations->InAt(0);
2086 Location out = locations->Out();
2087 if (index.IsConstant()) {
2088 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
2089 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002090 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset, index);
2091 }
Roland Levillain4d027112015-07-01 15:41:14 +01002092 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002093}
2094
Alexandre Rames5319def2014-10-23 10:03:10 +01002095void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2097 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002099}
2100
2101void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002102 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002103 __ Ldr(OutputRegister(instruction),
2104 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002105 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002106}
2107
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002108void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002109 Primitive::Type value_type = instruction->GetComponentType();
2110
2111 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2112 bool object_array_set_with_read_barrier =
2113 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2115 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002116 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2117 LocationSummary::kCallOnSlowPath :
2118 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002119 locations->SetInAt(0, Location::RequiresRegister());
2120 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002121 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002122 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002123 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002124 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002125 }
2126}
2127
2128void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2129 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002130 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002131 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002132 bool needs_write_barrier =
2133 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002134
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002135 Register array = InputRegisterAt(instruction, 0);
2136 CPURegister value = InputCPURegisterAt(instruction, 2);
2137 CPURegister source = value;
2138 Location index = locations->InAt(1);
2139 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2140 MemOperand destination = HeapOperand(array);
2141 MacroAssembler* masm = GetVIXLAssembler();
2142 BlockPoolsScope block_pools(masm);
2143
2144 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002145 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002146 if (index.IsConstant()) {
2147 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2148 destination = HeapOperand(array, offset);
2149 } else {
2150 UseScratchRegisterScope temps(masm);
2151 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002152 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002153 // The read barrier instrumentation does not support the
2154 // HArm64IntermediateAddress instruction yet.
2155 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002156 // We do not need to compute the intermediate address from the array: the
2157 // input instruction has done it already. See the comment in
2158 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2159 if (kIsDebugBuild) {
2160 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2161 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2162 }
2163 temp = array;
2164 } else {
2165 __ Add(temp, array, offset);
2166 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002167 destination = HeapOperand(temp,
2168 XRegisterFrom(index),
2169 LSL,
2170 Primitive::ComponentSizeShift(value_type));
2171 }
2172 codegen_->Store(value_type, value, destination);
2173 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002174 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002175 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002176 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002177 vixl::Label done;
2178 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002179 {
2180 // We use a block to end the scratch scope before the write barrier, thus
2181 // freeing the temporary registers so they can be used in `MarkGCCard`.
2182 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002183 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002184 if (index.IsConstant()) {
2185 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002186 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002187 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002188 destination = HeapOperand(temp,
2189 XRegisterFrom(index),
2190 LSL,
2191 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002192 }
2193
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002194 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2195 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2196 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2197
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002198 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002199 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2200 codegen_->AddSlowPath(slow_path);
2201 if (instruction->GetValueCanBeNull()) {
2202 vixl::Label non_zero;
2203 __ Cbnz(Register(value), &non_zero);
2204 if (!index.IsConstant()) {
2205 __ Add(temp, array, offset);
2206 }
2207 __ Str(wzr, destination);
2208 codegen_->MaybeRecordImplicitNullCheck(instruction);
2209 __ B(&done);
2210 __ Bind(&non_zero);
2211 }
2212
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002213 if (kEmitCompilerReadBarrier) {
2214 // When read barriers are enabled, the type checking
2215 // instrumentation requires two read barriers:
2216 //
2217 // __ Mov(temp2, temp);
2218 // // /* HeapReference<Class> */ temp = temp->component_type_
2219 // __ Ldr(temp, HeapOperand(temp, component_offset));
2220 // codegen_->GenerateReadBarrier(
2221 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2222 //
2223 // // /* HeapReference<Class> */ temp2 = value->klass_
2224 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2225 // codegen_->GenerateReadBarrier(
2226 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2227 //
2228 // __ Cmp(temp, temp2);
2229 //
2230 // However, the second read barrier may trash `temp`, as it
2231 // is a temporary register, and as such would not be saved
2232 // along with live registers before calling the runtime (nor
2233 // restored afterwards). So in this case, we bail out and
2234 // delegate the work to the array set slow path.
2235 //
2236 // TODO: Extend the register allocator to support a new
2237 // "(locally) live temp" location so as to avoid always
2238 // going into the slow path when read barriers are enabled.
2239 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002240 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002241 Register temp2 = temps.AcquireSameSizeAs(array);
2242 // /* HeapReference<Class> */ temp = array->klass_
2243 __ Ldr(temp, HeapOperand(array, class_offset));
2244 codegen_->MaybeRecordImplicitNullCheck(instruction);
2245 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2246
2247 // /* HeapReference<Class> */ temp = temp->component_type_
2248 __ Ldr(temp, HeapOperand(temp, component_offset));
2249 // /* HeapReference<Class> */ temp2 = value->klass_
2250 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2251 // If heap poisoning is enabled, no need to unpoison `temp`
2252 // nor `temp2`, as we are comparing two poisoned references.
2253 __ Cmp(temp, temp2);
2254
2255 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2256 vixl::Label do_put;
2257 __ B(eq, &do_put);
2258 // If heap poisoning is enabled, the `temp` reference has
2259 // not been unpoisoned yet; unpoison it now.
2260 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2261
2262 // /* HeapReference<Class> */ temp = temp->super_class_
2263 __ Ldr(temp, HeapOperand(temp, super_offset));
2264 // If heap poisoning is enabled, no need to unpoison
2265 // `temp`, as we are comparing against null below.
2266 __ Cbnz(temp, slow_path->GetEntryLabel());
2267 __ Bind(&do_put);
2268 } else {
2269 __ B(ne, slow_path->GetEntryLabel());
2270 }
2271 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002272 }
2273 }
2274
2275 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002276 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002277 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002278 __ Mov(temp2, value.W());
2279 GetAssembler()->PoisonHeapReference(temp2);
2280 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002281 }
2282
2283 if (!index.IsConstant()) {
2284 __ Add(temp, array, offset);
2285 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002286 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002287
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002288 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002289 codegen_->MaybeRecordImplicitNullCheck(instruction);
2290 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002291 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002292
2293 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2294
2295 if (done.IsLinked()) {
2296 __ Bind(&done);
2297 }
2298
2299 if (slow_path != nullptr) {
2300 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002301 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002302 }
2303}
2304
Alexandre Rames67555f72014-11-18 10:55:16 +00002305void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002306 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2307 ? LocationSummary::kCallOnSlowPath
2308 : LocationSummary::kNoCall;
2309 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002310 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002311 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002312 if (instruction->HasUses()) {
2313 locations->SetOut(Location::SameAsFirstInput());
2314 }
2315}
2316
2317void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002318 BoundsCheckSlowPathARM64* slow_path =
2319 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002320 codegen_->AddSlowPath(slow_path);
2321
2322 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2323 __ B(slow_path->GetEntryLabel(), hs);
2324}
2325
Alexandre Rames67555f72014-11-18 10:55:16 +00002326void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2327 LocationSummary* locations =
2328 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2329 locations->SetInAt(0, Location::RequiresRegister());
2330 if (check->HasUses()) {
2331 locations->SetOut(Location::SameAsFirstInput());
2332 }
2333}
2334
2335void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2336 // We assume the class is not null.
2337 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2338 check->GetLoadClass(), check, check->GetDexPc(), true);
2339 codegen_->AddSlowPath(slow_path);
2340 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2341}
2342
Roland Levillain7f63c522015-07-13 15:54:55 +00002343static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2344 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2345 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2346}
2347
Serban Constantinescu02164b32014-11-13 14:05:07 +00002348void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002349 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002350 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2351 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002352 switch (in_type) {
2353 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002354 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002355 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2357 break;
2358 }
2359 case Primitive::kPrimFloat:
2360 case Primitive::kPrimDouble: {
2361 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002362 locations->SetInAt(1,
2363 IsFloatingPointZeroConstant(compare->InputAt(1))
2364 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2365 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002366 locations->SetOut(Location::RequiresRegister());
2367 break;
2368 }
2369 default:
2370 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2371 }
2372}
2373
2374void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2375 Primitive::Type in_type = compare->InputAt(0)->GetType();
2376
2377 // 0 if: left == right
2378 // 1 if: left > right
2379 // -1 if: left < right
2380 switch (in_type) {
2381 case Primitive::kPrimLong: {
2382 Register result = OutputRegister(compare);
2383 Register left = InputRegisterAt(compare, 0);
2384 Operand right = InputOperandAt(compare, 1);
2385
2386 __ Cmp(left, right);
2387 __ Cset(result, ne);
2388 __ Cneg(result, result, lt);
2389 break;
2390 }
2391 case Primitive::kPrimFloat:
2392 case Primitive::kPrimDouble: {
2393 Register result = OutputRegister(compare);
2394 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002395 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002396 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2397 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002398 __ Fcmp(left, 0.0);
2399 } else {
2400 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2401 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002402 __ Cset(result, ne);
2403 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002404 break;
2405 }
2406 default:
2407 LOG(FATAL) << "Unimplemented compare type " << in_type;
2408 }
2409}
2410
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002411void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002413
2414 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2415 locations->SetInAt(0, Location::RequiresFpuRegister());
2416 locations->SetInAt(1,
2417 IsFloatingPointZeroConstant(instruction->InputAt(1))
2418 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2419 : Location::RequiresFpuRegister());
2420 } else {
2421 // Integer cases.
2422 locations->SetInAt(0, Location::RequiresRegister());
2423 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2424 }
2425
Alexandre Rames5319def2014-10-23 10:03:10 +01002426 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002428 }
2429}
2430
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002431void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002432 if (!instruction->NeedsMaterialization()) {
2433 return;
2434 }
2435
2436 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002437 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002438 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002439
Roland Levillain7f63c522015-07-13 15:54:55 +00002440 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2441 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2442 if (locations->InAt(1).IsConstant()) {
2443 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2444 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2445 __ Fcmp(lhs, 0.0);
2446 } else {
2447 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2448 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002449 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002450 } else {
2451 // Integer cases.
2452 Register lhs = InputRegisterAt(instruction, 0);
2453 Operand rhs = InputOperandAt(instruction, 1);
2454 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002455 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002456 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002457}
2458
2459#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2460 M(Equal) \
2461 M(NotEqual) \
2462 M(LessThan) \
2463 M(LessThanOrEqual) \
2464 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002465 M(GreaterThanOrEqual) \
2466 M(Below) \
2467 M(BelowOrEqual) \
2468 M(Above) \
2469 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002470#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002471void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2472void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002473FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002474#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002475#undef FOR_EACH_CONDITION_INSTRUCTION
2476
Zheng Xuc6667102015-05-15 16:08:45 +08002477void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2478 DCHECK(instruction->IsDiv() || instruction->IsRem());
2479
2480 LocationSummary* locations = instruction->GetLocations();
2481 Location second = locations->InAt(1);
2482 DCHECK(second.IsConstant());
2483
2484 Register out = OutputRegister(instruction);
2485 Register dividend = InputRegisterAt(instruction, 0);
2486 int64_t imm = Int64FromConstant(second.GetConstant());
2487 DCHECK(imm == 1 || imm == -1);
2488
2489 if (instruction->IsRem()) {
2490 __ Mov(out, 0);
2491 } else {
2492 if (imm == 1) {
2493 __ Mov(out, dividend);
2494 } else {
2495 __ Neg(out, dividend);
2496 }
2497 }
2498}
2499
2500void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2501 DCHECK(instruction->IsDiv() || instruction->IsRem());
2502
2503 LocationSummary* locations = instruction->GetLocations();
2504 Location second = locations->InAt(1);
2505 DCHECK(second.IsConstant());
2506
2507 Register out = OutputRegister(instruction);
2508 Register dividend = InputRegisterAt(instruction, 0);
2509 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002510 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002511 int ctz_imm = CTZ(abs_imm);
2512
2513 UseScratchRegisterScope temps(GetVIXLAssembler());
2514 Register temp = temps.AcquireSameSizeAs(out);
2515
2516 if (instruction->IsDiv()) {
2517 __ Add(temp, dividend, abs_imm - 1);
2518 __ Cmp(dividend, 0);
2519 __ Csel(out, temp, dividend, lt);
2520 if (imm > 0) {
2521 __ Asr(out, out, ctz_imm);
2522 } else {
2523 __ Neg(out, Operand(out, ASR, ctz_imm));
2524 }
2525 } else {
2526 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2527 __ Asr(temp, dividend, bits - 1);
2528 __ Lsr(temp, temp, bits - ctz_imm);
2529 __ Add(out, dividend, temp);
2530 __ And(out, out, abs_imm - 1);
2531 __ Sub(out, out, temp);
2532 }
2533}
2534
2535void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2536 DCHECK(instruction->IsDiv() || instruction->IsRem());
2537
2538 LocationSummary* locations = instruction->GetLocations();
2539 Location second = locations->InAt(1);
2540 DCHECK(second.IsConstant());
2541
2542 Register out = OutputRegister(instruction);
2543 Register dividend = InputRegisterAt(instruction, 0);
2544 int64_t imm = Int64FromConstant(second.GetConstant());
2545
2546 Primitive::Type type = instruction->GetResultType();
2547 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2548
2549 int64_t magic;
2550 int shift;
2551 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2552
2553 UseScratchRegisterScope temps(GetVIXLAssembler());
2554 Register temp = temps.AcquireSameSizeAs(out);
2555
2556 // temp = get_high(dividend * magic)
2557 __ Mov(temp, magic);
2558 if (type == Primitive::kPrimLong) {
2559 __ Smulh(temp, dividend, temp);
2560 } else {
2561 __ Smull(temp.X(), dividend, temp);
2562 __ Lsr(temp.X(), temp.X(), 32);
2563 }
2564
2565 if (imm > 0 && magic < 0) {
2566 __ Add(temp, temp, dividend);
2567 } else if (imm < 0 && magic > 0) {
2568 __ Sub(temp, temp, dividend);
2569 }
2570
2571 if (shift != 0) {
2572 __ Asr(temp, temp, shift);
2573 }
2574
2575 if (instruction->IsDiv()) {
2576 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2577 } else {
2578 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2579 // TODO: Strength reduction for msub.
2580 Register temp_imm = temps.AcquireSameSizeAs(out);
2581 __ Mov(temp_imm, imm);
2582 __ Msub(out, temp, temp_imm, dividend);
2583 }
2584}
2585
2586void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2587 DCHECK(instruction->IsDiv() || instruction->IsRem());
2588 Primitive::Type type = instruction->GetResultType();
2589 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2590
2591 LocationSummary* locations = instruction->GetLocations();
2592 Register out = OutputRegister(instruction);
2593 Location second = locations->InAt(1);
2594
2595 if (second.IsConstant()) {
2596 int64_t imm = Int64FromConstant(second.GetConstant());
2597
2598 if (imm == 0) {
2599 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2600 } else if (imm == 1 || imm == -1) {
2601 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002602 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002603 DivRemByPowerOfTwo(instruction);
2604 } else {
2605 DCHECK(imm <= -2 || imm >= 2);
2606 GenerateDivRemWithAnyConstant(instruction);
2607 }
2608 } else {
2609 Register dividend = InputRegisterAt(instruction, 0);
2610 Register divisor = InputRegisterAt(instruction, 1);
2611 if (instruction->IsDiv()) {
2612 __ Sdiv(out, dividend, divisor);
2613 } else {
2614 UseScratchRegisterScope temps(GetVIXLAssembler());
2615 Register temp = temps.AcquireSameSizeAs(out);
2616 __ Sdiv(temp, dividend, divisor);
2617 __ Msub(out, temp, divisor, dividend);
2618 }
2619 }
2620}
2621
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002622void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2623 LocationSummary* locations =
2624 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2625 switch (div->GetResultType()) {
2626 case Primitive::kPrimInt:
2627 case Primitive::kPrimLong:
2628 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002629 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2631 break;
2632
2633 case Primitive::kPrimFloat:
2634 case Primitive::kPrimDouble:
2635 locations->SetInAt(0, Location::RequiresFpuRegister());
2636 locations->SetInAt(1, Location::RequiresFpuRegister());
2637 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2638 break;
2639
2640 default:
2641 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2642 }
2643}
2644
2645void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2646 Primitive::Type type = div->GetResultType();
2647 switch (type) {
2648 case Primitive::kPrimInt:
2649 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002650 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002651 break;
2652
2653 case Primitive::kPrimFloat:
2654 case Primitive::kPrimDouble:
2655 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2656 break;
2657
2658 default:
2659 LOG(FATAL) << "Unexpected div type " << type;
2660 }
2661}
2662
Alexandre Rames67555f72014-11-18 10:55:16 +00002663void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002664 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2665 ? LocationSummary::kCallOnSlowPath
2666 : LocationSummary::kNoCall;
2667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002668 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2669 if (instruction->HasUses()) {
2670 locations->SetOut(Location::SameAsFirstInput());
2671 }
2672}
2673
2674void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2675 SlowPathCodeARM64* slow_path =
2676 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2677 codegen_->AddSlowPath(slow_path);
2678 Location value = instruction->GetLocations()->InAt(0);
2679
Alexandre Rames3e69f162014-12-10 10:36:50 +00002680 Primitive::Type type = instruction->GetType();
2681
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002682 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2683 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002684 return;
2685 }
2686
Alexandre Rames67555f72014-11-18 10:55:16 +00002687 if (value.IsConstant()) {
2688 int64_t divisor = Int64ConstantFrom(value);
2689 if (divisor == 0) {
2690 __ B(slow_path->GetEntryLabel());
2691 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002692 // A division by a non-null constant is valid. We don't need to perform
2693 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002694 }
2695 } else {
2696 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2697 }
2698}
2699
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002700void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2701 LocationSummary* locations =
2702 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2703 locations->SetOut(Location::ConstantLocation(constant));
2704}
2705
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002706void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2707 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002708 // Will be generated at use site.
2709}
2710
Alexandre Rames5319def2014-10-23 10:03:10 +01002711void LocationsBuilderARM64::VisitExit(HExit* exit) {
2712 exit->SetLocations(nullptr);
2713}
2714
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002715void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002716}
2717
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002718void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2719 LocationSummary* locations =
2720 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2721 locations->SetOut(Location::ConstantLocation(constant));
2722}
2723
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002724void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002725 // Will be generated at use site.
2726}
2727
David Brazdilfc6a86a2015-06-26 10:33:45 +00002728void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002729 DCHECK(!successor->IsExitBlock());
2730 HBasicBlock* block = got->GetBlock();
2731 HInstruction* previous = got->GetPrevious();
2732 HLoopInformation* info = block->GetLoopInformation();
2733
David Brazdil46e2a392015-03-16 17:31:52 +00002734 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002735 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2736 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2737 return;
2738 }
2739 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2740 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2741 }
2742 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002743 __ B(codegen_->GetLabelOf(successor));
2744 }
2745}
2746
David Brazdilfc6a86a2015-06-26 10:33:45 +00002747void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2748 got->SetLocations(nullptr);
2749}
2750
2751void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2752 HandleGoto(got, got->GetSuccessor());
2753}
2754
2755void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2756 try_boundary->SetLocations(nullptr);
2757}
2758
2759void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2760 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2761 if (!successor->IsExitBlock()) {
2762 HandleGoto(try_boundary, successor);
2763 }
2764}
2765
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002766void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002767 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002768 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002769 vixl::Label* false_target) {
2770 // FP branching requires both targets to be explicit. If either of the targets
2771 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2772 vixl::Label fallthrough_target;
2773 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002774
David Brazdil0debae72015-11-12 18:37:00 +00002775 if (true_target == nullptr && false_target == nullptr) {
2776 // Nothing to do. The code always falls through.
2777 return;
2778 } else if (cond->IsIntConstant()) {
2779 // Constant condition, statically compared against 1.
2780 if (cond->AsIntConstant()->IsOne()) {
2781 if (true_target != nullptr) {
2782 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002783 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002784 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002785 DCHECK(cond->AsIntConstant()->IsZero());
2786 if (false_target != nullptr) {
2787 __ B(false_target);
2788 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002789 }
David Brazdil0debae72015-11-12 18:37:00 +00002790 return;
2791 }
2792
2793 // The following code generates these patterns:
2794 // (1) true_target == nullptr && false_target != nullptr
2795 // - opposite condition true => branch to false_target
2796 // (2) true_target != nullptr && false_target == nullptr
2797 // - condition true => branch to true_target
2798 // (3) true_target != nullptr && false_target != nullptr
2799 // - condition true => branch to true_target
2800 // - branch to false_target
2801 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002802 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002803 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002804 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002805 if (true_target == nullptr) {
2806 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2807 } else {
2808 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2809 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002810 } else {
2811 // The condition instruction has not been materialized, use its inputs as
2812 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002813 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002814
David Brazdil0debae72015-11-12 18:37:00 +00002815 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002816 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002817 FPRegister lhs = InputFPRegisterAt(condition, 0);
2818 if (condition->GetLocations()->InAt(1).IsConstant()) {
2819 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2820 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2821 __ Fcmp(lhs, 0.0);
2822 } else {
2823 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2824 }
David Brazdil0debae72015-11-12 18:37:00 +00002825 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002826 IfCondition opposite_condition = condition->GetOppositeCondition();
2827 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002828 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002829 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002830 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002831 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002832 // Integer cases.
2833 Register lhs = InputRegisterAt(condition, 0);
2834 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002835
2836 Condition arm64_cond;
2837 vixl::Label* non_fallthrough_target;
2838 if (true_target == nullptr) {
2839 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2840 non_fallthrough_target = false_target;
2841 } else {
2842 arm64_cond = ARM64Condition(condition->GetCondition());
2843 non_fallthrough_target = true_target;
2844 }
2845
Roland Levillain7f63c522015-07-13 15:54:55 +00002846 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2847 switch (arm64_cond) {
2848 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002849 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002850 break;
2851 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002852 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002853 break;
2854 case lt:
2855 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002856 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002857 break;
2858 case ge:
2859 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002860 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002861 break;
2862 default:
2863 // Without the `static_cast` the compiler throws an error for
2864 // `-Werror=sign-promo`.
2865 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2866 }
2867 } else {
2868 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002869 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002870 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002871 }
2872 }
David Brazdil0debae72015-11-12 18:37:00 +00002873
2874 // If neither branch falls through (case 3), the conditional branch to `true_target`
2875 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2876 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002877 __ B(false_target);
2878 }
David Brazdil0debae72015-11-12 18:37:00 +00002879
2880 if (fallthrough_target.IsLinked()) {
2881 __ Bind(&fallthrough_target);
2882 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002883}
2884
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002885void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002887 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002888 locations->SetInAt(0, Location::RequiresRegister());
2889 }
2890}
2891
2892void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002893 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2894 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2895 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2896 nullptr : codegen_->GetLabelOf(true_successor);
2897 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2898 nullptr : codegen_->GetLabelOf(false_successor);
2899 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002900}
2901
2902void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2903 LocationSummary* locations = new (GetGraph()->GetArena())
2904 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002905 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002906 locations->SetInAt(0, Location::RequiresRegister());
2907 }
2908}
2909
2910void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002911 SlowPathCodeARM64* slow_path =
2912 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002913 GenerateTestAndBranch(deoptimize,
2914 /* condition_input_index */ 0,
2915 slow_path->GetEntryLabel(),
2916 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002917}
2918
David Srbecky0cf44932015-12-09 14:09:59 +00002919void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2920 new (GetGraph()->GetArena()) LocationSummary(info);
2921}
2922
2923void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002924 if (codegen_->HasStackMapAtCurrentPc()) {
2925 // Ensure that we do not collide with the stack map of the previous instruction.
2926 __ Nop();
2927 }
David Srbecky0cf44932015-12-09 14:09:59 +00002928 codegen_->RecordPcInfo(info, info->GetDexPc());
2929}
2930
Alexandre Rames5319def2014-10-23 10:03:10 +01002931void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002932 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002933}
2934
2935void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002936 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002937}
2938
2939void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002940 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002941}
2942
2943void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002944 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002945}
2946
Alexandre Rames67555f72014-11-18 10:55:16 +00002947void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002948 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002949 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2950 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002951 case TypeCheckKind::kExactCheck:
2952 case TypeCheckKind::kAbstractClassCheck:
2953 case TypeCheckKind::kClassHierarchyCheck:
2954 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002955 call_kind =
2956 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002957 break;
2958 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002959 case TypeCheckKind::kUnresolvedCheck:
2960 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002961 call_kind = LocationSummary::kCallOnSlowPath;
2962 break;
2963 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002964
Alexandre Rames67555f72014-11-18 10:55:16 +00002965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002966 locations->SetInAt(0, Location::RequiresRegister());
2967 locations->SetInAt(1, Location::RequiresRegister());
2968 // The "out" register is used as a temporary, so it overlaps with the inputs.
2969 // Note that TypeCheckSlowPathARM64 uses this register too.
2970 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2971 // When read barriers are enabled, we need a temporary register for
2972 // some cases.
2973 if (kEmitCompilerReadBarrier &&
2974 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2975 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2976 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2977 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002978 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002979}
2980
2981void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2982 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002983 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002984 Register obj = InputRegisterAt(instruction, 0);
2985 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002986 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00002987 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002988 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2989 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2990 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2991 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002992
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002993 vixl::Label done, zero;
2994 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002995
2996 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002997 // Avoid null check if we know `obj` is not null.
2998 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002999 __ Cbz(obj, &zero);
3000 }
3001
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003002 // /* HeapReference<Class> */ out = obj->klass_
3003 __ Ldr(out, HeapOperand(obj.W(), class_offset));
3004 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003005
3006 switch (instruction->GetTypeCheckKind()) {
3007 case TypeCheckKind::kExactCheck: {
3008 __ Cmp(out, cls);
3009 __ Cset(out, eq);
3010 if (zero.IsLinked()) {
3011 __ B(&done);
3012 }
3013 break;
3014 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003015
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003016 case TypeCheckKind::kAbstractClassCheck: {
3017 // If the class is abstract, we eagerly fetch the super class of the
3018 // object to avoid doing a comparison we know will fail.
3019 vixl::Label loop, success;
3020 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003021 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3022 if (kEmitCompilerReadBarrier) {
3023 // Save the value of `out` into `temp` before overwriting it
3024 // in the following move operation, as we will need it for the
3025 // read barrier below.
3026 Register temp = WRegisterFrom(temp_loc);
3027 __ Mov(temp, out);
3028 }
3029 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003030 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003031 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003032 // If `out` is null, we use it for the result, and jump to `done`.
3033 __ Cbz(out, &done);
3034 __ Cmp(out, cls);
3035 __ B(ne, &loop);
3036 __ Mov(out, 1);
3037 if (zero.IsLinked()) {
3038 __ B(&done);
3039 }
3040 break;
3041 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003042
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003043 case TypeCheckKind::kClassHierarchyCheck: {
3044 // Walk over the class hierarchy to find a match.
3045 vixl::Label loop, success;
3046 __ Bind(&loop);
3047 __ Cmp(out, cls);
3048 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003049 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3050 if (kEmitCompilerReadBarrier) {
3051 // Save the value of `out` into `temp` before overwriting it
3052 // in the following move operation, as we will need it for the
3053 // read barrier below.
3054 Register temp = WRegisterFrom(temp_loc);
3055 __ Mov(temp, out);
3056 }
3057 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003058 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003059 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003060 __ Cbnz(out, &loop);
3061 // If `out` is null, we use it for the result, and jump to `done`.
3062 __ B(&done);
3063 __ Bind(&success);
3064 __ Mov(out, 1);
3065 if (zero.IsLinked()) {
3066 __ B(&done);
3067 }
3068 break;
3069 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003070
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003071 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003072 // Do an exact check.
3073 vixl::Label exact_check;
3074 __ Cmp(out, cls);
3075 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003076 // Otherwise, we need to check that the object's class is a non-primitive array.
3077 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3078 if (kEmitCompilerReadBarrier) {
3079 // Save the value of `out` into `temp` before overwriting it
3080 // in the following move operation, as we will need it for the
3081 // read barrier below.
3082 Register temp = WRegisterFrom(temp_loc);
3083 __ Mov(temp, out);
3084 }
3085 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003086 __ Ldr(out, HeapOperand(out, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003087 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003088 // If `out` is null, we use it for the result, and jump to `done`.
3089 __ Cbz(out, &done);
3090 __ Ldrh(out, HeapOperand(out, primitive_offset));
3091 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3092 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003093 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003094 __ Mov(out, 1);
3095 __ B(&done);
3096 break;
3097 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003098
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003099 case TypeCheckKind::kArrayCheck: {
3100 __ Cmp(out, cls);
3101 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003102 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3103 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003104 codegen_->AddSlowPath(slow_path);
3105 __ B(ne, slow_path->GetEntryLabel());
3106 __ Mov(out, 1);
3107 if (zero.IsLinked()) {
3108 __ B(&done);
3109 }
3110 break;
3111 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003112
Calin Juravle98893e12015-10-02 21:05:03 +01003113 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003114 case TypeCheckKind::kInterfaceCheck: {
3115 // Note that we indeed only call on slow path, but we always go
3116 // into the slow path for the unresolved and interface check
3117 // cases.
3118 //
3119 // We cannot directly call the InstanceofNonTrivial runtime
3120 // entry point without resorting to a type checking slow path
3121 // here (i.e. by calling InvokeRuntime directly), as it would
3122 // require to assign fixed registers for the inputs of this
3123 // HInstanceOf instruction (following the runtime calling
3124 // convention), which might be cluttered by the potential first
3125 // read barrier emission at the beginning of this method.
3126 DCHECK(locations->OnlyCallsOnSlowPath());
3127 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3128 /* is_fatal */ false);
3129 codegen_->AddSlowPath(slow_path);
3130 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003131 if (zero.IsLinked()) {
3132 __ B(&done);
3133 }
3134 break;
3135 }
3136 }
3137
3138 if (zero.IsLinked()) {
3139 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003140 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003141 }
3142
3143 if (done.IsLinked()) {
3144 __ Bind(&done);
3145 }
3146
3147 if (slow_path != nullptr) {
3148 __ Bind(slow_path->GetExitLabel());
3149 }
3150}
3151
3152void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3153 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3154 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3155
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003156 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3157 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003158 case TypeCheckKind::kExactCheck:
3159 case TypeCheckKind::kAbstractClassCheck:
3160 case TypeCheckKind::kClassHierarchyCheck:
3161 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003162 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3163 LocationSummary::kCallOnSlowPath :
3164 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003165 break;
3166 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003167 case TypeCheckKind::kUnresolvedCheck:
3168 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003169 call_kind = LocationSummary::kCallOnSlowPath;
3170 break;
3171 }
3172
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003173 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3174 locations->SetInAt(0, Location::RequiresRegister());
3175 locations->SetInAt(1, Location::RequiresRegister());
3176 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3177 locations->AddTemp(Location::RequiresRegister());
3178 locations->AddTemp(Location::RequiresRegister());
3179 // When read barriers are enabled, we need an additional temporary
3180 // register for some cases.
3181 if (kEmitCompilerReadBarrier &&
3182 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3183 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3184 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3185 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003186 }
3187}
3188
3189void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
3190 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003191 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003192 Register obj = InputRegisterAt(instruction, 0);
3193 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003194 Location temp_loc = locations->GetTemp(0);
3195 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003196 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3197 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3198 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3199 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003201 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3202 bool is_type_check_slow_path_fatal =
3203 (type_check_kind == TypeCheckKind::kExactCheck ||
3204 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3205 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3206 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3207 !instruction->CanThrowIntoCatchBlock();
3208 SlowPathCodeARM64* type_check_slow_path =
3209 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3210 is_type_check_slow_path_fatal);
3211 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003212
3213 vixl::Label done;
3214 // Avoid null check if we know obj is not null.
3215 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003216 __ Cbz(obj, &done);
3217 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003218
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003219 // /* HeapReference<Class> */ temp = obj->klass_
3220 __ Ldr(temp, HeapOperand(obj, class_offset));
3221 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003222
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003223 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003224 case TypeCheckKind::kExactCheck:
3225 case TypeCheckKind::kArrayCheck: {
3226 __ Cmp(temp, cls);
3227 // Jump to slow path for throwing the exception or doing a
3228 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003229 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003230 break;
3231 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003233 case TypeCheckKind::kAbstractClassCheck: {
3234 // If the class is abstract, we eagerly fetch the super class of the
3235 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003236 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003238 Location temp2_loc =
3239 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3240 if (kEmitCompilerReadBarrier) {
3241 // Save the value of `temp` into `temp2` before overwriting it
3242 // in the following move operation, as we will need it for the
3243 // read barrier below.
3244 Register temp2 = WRegisterFrom(temp2_loc);
3245 __ Mov(temp2, temp);
3246 }
3247 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003248 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003249 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3250
3251 // If the class reference currently in `temp` is not null, jump
3252 // to the `compare_classes` label to compare it with the checked
3253 // class.
3254 __ Cbnz(temp, &compare_classes);
3255 // Otherwise, jump to the slow path to throw the exception.
3256 //
3257 // But before, move back the object's class into `temp` before
3258 // going into the slow path, as it has been overwritten in the
3259 // meantime.
3260 // /* HeapReference<Class> */ temp = obj->klass_
3261 __ Ldr(temp, HeapOperand(obj, class_offset));
3262 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3263 __ B(type_check_slow_path->GetEntryLabel());
3264
3265 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003266 __ Cmp(temp, cls);
3267 __ B(ne, &loop);
3268 break;
3269 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003270
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003271 case TypeCheckKind::kClassHierarchyCheck: {
3272 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003273 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003274 __ Bind(&loop);
3275 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003276 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003277
3278 Location temp2_loc =
3279 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3280 if (kEmitCompilerReadBarrier) {
3281 // Save the value of `temp` into `temp2` before overwriting it
3282 // in the following move operation, as we will need it for the
3283 // read barrier below.
3284 Register temp2 = WRegisterFrom(temp2_loc);
3285 __ Mov(temp2, temp);
3286 }
3287 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003288 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003289 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3290
3291 // If the class reference currently in `temp` is not null, jump
3292 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003293 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003294 // Otherwise, jump to the slow path to throw the exception.
3295 //
3296 // But before, move back the object's class into `temp` before
3297 // going into the slow path, as it has been overwritten in the
3298 // meantime.
3299 // /* HeapReference<Class> */ temp = obj->klass_
3300 __ Ldr(temp, HeapOperand(obj, class_offset));
3301 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3302 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003303 break;
3304 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003305
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003306 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003307 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003308 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003309 __ Cmp(temp, cls);
3310 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003311
3312 // Otherwise, we need to check that the object's class is a non-primitive array.
3313 Location temp2_loc =
3314 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3315 if (kEmitCompilerReadBarrier) {
3316 // Save the value of `temp` into `temp2` before overwriting it
3317 // in the following move operation, as we will need it for the
3318 // read barrier below.
3319 Register temp2 = WRegisterFrom(temp2_loc);
3320 __ Mov(temp2, temp);
3321 }
3322 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003323 __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003324 codegen_->MaybeGenerateReadBarrier(
3325 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
3326
3327 // If the component type is not null (i.e. the object is indeed
3328 // an array), jump to label `check_non_primitive_component_type`
3329 // to further check that this component type is not a primitive
3330 // type.
3331 __ Cbnz(temp, &check_non_primitive_component_type);
3332 // Otherwise, jump to the slow path to throw the exception.
3333 //
3334 // But before, move back the object's class into `temp` before
3335 // going into the slow path, as it has been overwritten in the
3336 // meantime.
3337 // /* HeapReference<Class> */ temp = obj->klass_
3338 __ Ldr(temp, HeapOperand(obj, class_offset));
3339 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3340 __ B(type_check_slow_path->GetEntryLabel());
3341
3342 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003343 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3344 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003345 __ Cbz(temp, &done);
3346 // Same comment as above regarding `temp` and the slow path.
3347 // /* HeapReference<Class> */ temp = obj->klass_
3348 __ Ldr(temp, HeapOperand(obj, class_offset));
3349 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3350 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003351 break;
3352 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003353
Calin Juravle98893e12015-10-02 21:05:03 +01003354 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003356 // We always go into the type check slow path for the unresolved
3357 // and interface check cases.
3358 //
3359 // We cannot directly call the CheckCast runtime entry point
3360 // without resorting to a type checking slow path here (i.e. by
3361 // calling InvokeRuntime directly), as it would require to
3362 // assign fixed registers for the inputs of this HInstanceOf
3363 // instruction (following the runtime calling convention), which
3364 // might be cluttered by the potential first read barrier
3365 // emission at the beginning of this method.
3366 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003367 break;
3368 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003369 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003370
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003371 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003372}
3373
Alexandre Rames5319def2014-10-23 10:03:10 +01003374void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3376 locations->SetOut(Location::ConstantLocation(constant));
3377}
3378
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003379void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003380 // Will be generated at use site.
3381}
3382
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003383void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3385 locations->SetOut(Location::ConstantLocation(constant));
3386}
3387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003388void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003389 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003390}
3391
Calin Juravle175dc732015-08-25 15:42:32 +01003392void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3393 // The trampoline uses the same calling convention as dex calling conventions,
3394 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3395 // the method_idx.
3396 HandleInvoke(invoke);
3397}
3398
3399void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3400 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3401}
3402
Alexandre Rames5319def2014-10-23 10:03:10 +01003403void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003404 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003405 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003406}
3407
Alexandre Rames67555f72014-11-18 10:55:16 +00003408void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3409 HandleInvoke(invoke);
3410}
3411
3412void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3413 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003414 LocationSummary* locations = invoke->GetLocations();
3415 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003416 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3417 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003418 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003419 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003420 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003421
3422 // The register ip1 is required to be used for the hidden argument in
3423 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003424 MacroAssembler* masm = GetVIXLAssembler();
3425 UseScratchRegisterScope scratch_scope(masm);
3426 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003427 scratch_scope.Exclude(ip1);
3428 __ Mov(ip1, invoke->GetDexMethodIndex());
3429
Alexandre Rames67555f72014-11-18 10:55:16 +00003430 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003431 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003432 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003433 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003434 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003435 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003436 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003437 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003438 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003439 // Instead of simply (possibly) unpoisoning `temp` here, we should
3440 // emit a read barrier for the previous class reference load.
3441 // However this is not required in practice, as this is an
3442 // intermediate/temporary reference and because the current
3443 // concurrent copying collector keeps the from-space memory
3444 // intact/accessible until the end of the marking phase (the
3445 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003446 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003447 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003448 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003449 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003450 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003451 // lr();
3452 __ Blr(lr);
3453 DCHECK(!codegen_->IsLeafMethod());
3454 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3455}
3456
3457void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003458 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3459 if (intrinsic.TryDispatch(invoke)) {
3460 return;
3461 }
3462
Alexandre Rames67555f72014-11-18 10:55:16 +00003463 HandleInvoke(invoke);
3464}
3465
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003466void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003467 // When we do not run baseline, explicit clinit checks triggered by static
3468 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3469 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003470
Andreas Gampe878d58c2015-01-15 23:24:00 -08003471 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3472 if (intrinsic.TryDispatch(invoke)) {
3473 return;
3474 }
3475
Alexandre Rames67555f72014-11-18 10:55:16 +00003476 HandleInvoke(invoke);
3477}
3478
Andreas Gampe878d58c2015-01-15 23:24:00 -08003479static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3480 if (invoke->GetLocations()->Intrinsified()) {
3481 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3482 intrinsic.Dispatch(invoke);
3483 return true;
3484 }
3485 return false;
3486}
3487
Vladimir Markodc151b22015-10-15 18:02:30 +01003488HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3489 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3490 MethodReference target_method ATTRIBUTE_UNUSED) {
3491 // On arm64 we support all dispatch types.
3492 return desired_dispatch_info;
3493}
3494
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003495void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003496 // For better instruction scheduling we load the direct code pointer before the method pointer.
3497 bool direct_code_loaded = false;
3498 switch (invoke->GetCodePtrLocation()) {
3499 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3500 // LR = code address from literal pool with link-time patch.
3501 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3502 direct_code_loaded = true;
3503 break;
3504 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3505 // LR = invoke->GetDirectCodePtr();
3506 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3507 direct_code_loaded = true;
3508 break;
3509 default:
3510 break;
3511 }
3512
Andreas Gampe878d58c2015-01-15 23:24:00 -08003513 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003514 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3515 switch (invoke->GetMethodLoadKind()) {
3516 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3517 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003518 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003519 break;
3520 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003521 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003522 break;
3523 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3524 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003525 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003526 break;
3527 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3528 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003529 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003530 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3531 break;
3532 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3533 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003534 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3535 invoke->GetDexCacheArrayOffset());
3536 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003537 {
3538 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003539 __ Bind(pc_insn_label);
3540 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003541 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003542 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003543 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003544 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3545 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003546 {
3547 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3548 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3549 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3550 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3551 }
Vladimir Marko58155012015-08-19 12:49:41 +00003552 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003553 }
Vladimir Marko58155012015-08-19 12:49:41 +00003554 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003555 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003556 Register reg = XRegisterFrom(temp);
3557 Register method_reg;
3558 if (current_method.IsRegister()) {
3559 method_reg = XRegisterFrom(current_method);
3560 } else {
3561 DCHECK(invoke->GetLocations()->Intrinsified());
3562 DCHECK(!current_method.IsValid());
3563 method_reg = reg;
3564 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3565 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003566
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003567 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003568 __ Ldr(reg.X(),
3569 MemOperand(method_reg.X(),
3570 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003571 // temp = temp[index_in_cache];
3572 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3573 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3574 break;
3575 }
3576 }
3577
3578 switch (invoke->GetCodePtrLocation()) {
3579 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3580 __ Bl(&frame_entry_label_);
3581 break;
3582 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3583 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3584 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003585 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3586 __ Bind(label);
3587 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003588 break;
3589 }
3590 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3591 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3592 // LR prepared above for better instruction scheduling.
3593 DCHECK(direct_code_loaded);
3594 // lr()
3595 __ Blr(lr);
3596 break;
3597 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3598 // LR = callee_method->entry_point_from_quick_compiled_code_;
3599 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003600 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003601 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3602 // lr()
3603 __ Blr(lr);
3604 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003605 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003606
Andreas Gampe878d58c2015-01-15 23:24:00 -08003607 DCHECK(!IsLeafMethod());
3608}
3609
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003610void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003611 // Use the calling convention instead of the location of the receiver, as
3612 // intrinsics may have put the receiver in a different register. In the intrinsics
3613 // slow path, the arguments have been moved to the right place, so here we are
3614 // guaranteed that the receiver is the first register of the calling convention.
3615 InvokeDexCallingConvention calling_convention;
3616 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003617 Register temp = XRegisterFrom(temp_in);
3618 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3619 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3620 Offset class_offset = mirror::Object::ClassOffset();
3621 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3622
3623 BlockPoolsScope block_pools(GetVIXLAssembler());
3624
3625 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003626 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003627 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003628 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003629 // Instead of simply (possibly) unpoisoning `temp` here, we should
3630 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003631 // intermediate/temporary reference and because the current
3632 // concurrent copying collector keeps the from-space memory
3633 // intact/accessible until the end of the marking phase (the
3634 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003635 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3636 // temp = temp->GetMethodAt(method_offset);
3637 __ Ldr(temp, MemOperand(temp, method_offset));
3638 // lr = temp->GetEntryPoint();
3639 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3640 // lr();
3641 __ Blr(lr);
3642}
3643
Vladimir Marko58155012015-08-19 12:49:41 +00003644void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3645 DCHECK(linker_patches->empty());
3646 size_t size =
3647 method_patches_.size() +
3648 call_patches_.size() +
3649 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003650 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003651 linker_patches->reserve(size);
3652 for (const auto& entry : method_patches_) {
3653 const MethodReference& target_method = entry.first;
3654 vixl::Literal<uint64_t>* literal = entry.second;
3655 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3656 target_method.dex_file,
3657 target_method.dex_method_index));
3658 }
3659 for (const auto& entry : call_patches_) {
3660 const MethodReference& target_method = entry.first;
3661 vixl::Literal<uint64_t>* literal = entry.second;
3662 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3663 target_method.dex_file,
3664 target_method.dex_method_index));
3665 }
3666 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003667 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003668 info.target_method.dex_file,
3669 info.target_method.dex_method_index));
3670 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003671 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003672 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003673 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003674 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003675 info.element_offset));
3676 }
3677}
3678
3679vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3680 // Look up the literal for value.
3681 auto lb = uint64_literals_.lower_bound(value);
3682 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3683 return lb->second;
3684 }
3685 // We don't have a literal for this value, insert a new one.
3686 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3687 uint64_literals_.PutBefore(lb, value, literal);
3688 return literal;
3689}
3690
3691vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3692 MethodReference target_method,
3693 MethodToLiteralMap* map) {
3694 // Look up the literal for target_method.
3695 auto lb = map->lower_bound(target_method);
3696 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3697 return lb->second;
3698 }
3699 // We don't have a literal for this method yet, insert a new one.
3700 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3701 map->PutBefore(lb, target_method, literal);
3702 return literal;
3703}
3704
3705vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3706 MethodReference target_method) {
3707 return DeduplicateMethodLiteral(target_method, &method_patches_);
3708}
3709
3710vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3711 MethodReference target_method) {
3712 return DeduplicateMethodLiteral(target_method, &call_patches_);
3713}
3714
3715
Andreas Gampe878d58c2015-01-15 23:24:00 -08003716void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003717 // When we do not run baseline, explicit clinit checks triggered by static
3718 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3719 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003720
Andreas Gampe878d58c2015-01-15 23:24:00 -08003721 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3722 return;
3723 }
3724
Alexandre Ramesd921d642015-04-16 15:07:16 +01003725 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003726 LocationSummary* locations = invoke->GetLocations();
3727 codegen_->GenerateStaticOrDirectCall(
3728 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003729 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003730}
3731
3732void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003733 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3734 return;
3735 }
3736
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003737 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003738 DCHECK(!codegen_->IsLeafMethod());
3739 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3740}
3741
Alexandre Rames67555f72014-11-18 10:55:16 +00003742void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003743 InvokeRuntimeCallingConvention calling_convention;
3744 CodeGenerator::CreateLoadClassLocationSummary(
3745 cls,
3746 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003747 LocationFrom(vixl::x0),
3748 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003749}
3750
3751void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003752 if (cls->NeedsAccessCheck()) {
3753 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3754 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3755 cls,
3756 cls->GetDexPc(),
3757 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003758 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003759 return;
3760 }
3761
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003762 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003763 Register out = OutputRegister(cls);
3764 Register current_method = InputRegisterAt(cls, 0);
3765 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003766 DCHECK(!cls->CanCallRuntime());
3767 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003768 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3769 if (kEmitCompilerReadBarrier) {
3770 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3771 __ Add(out.X(), current_method.X(), declaring_class_offset);
3772 // /* mirror::Class* */ out = out->Read()
3773 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3774 } else {
3775 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3776 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3777 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003778 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003779 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003780 // /* GcRoot<mirror::Class>[] */ out =
3781 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003782 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003783
3784 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
3785 if (kEmitCompilerReadBarrier) {
3786 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
3787 __ Add(out.X(), out.X(), cache_offset);
3788 // /* mirror::Class* */ out = out->Read()
3789 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3790 } else {
3791 // /* GcRoot<mirror::Class> */ out = out[type_index]
3792 __ Ldr(out, MemOperand(out.X(), cache_offset));
3793 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003794
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003795 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3796 DCHECK(cls->CanCallRuntime());
3797 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3798 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3799 codegen_->AddSlowPath(slow_path);
3800 if (!cls->IsInDexCache()) {
3801 __ Cbz(out, slow_path->GetEntryLabel());
3802 }
3803 if (cls->MustGenerateClinitCheck()) {
3804 GenerateClassInitializationCheck(slow_path, out);
3805 } else {
3806 __ Bind(slow_path->GetExitLabel());
3807 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003808 }
3809 }
3810}
3811
David Brazdilcb1c0552015-08-04 16:22:25 +01003812static MemOperand GetExceptionTlsAddress() {
3813 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3814}
3815
Alexandre Rames67555f72014-11-18 10:55:16 +00003816void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3817 LocationSummary* locations =
3818 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3819 locations->SetOut(Location::RequiresRegister());
3820}
3821
3822void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003823 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3824}
3825
3826void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3827 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3828}
3829
3830void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3831 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003832}
3833
Alexandre Rames5319def2014-10-23 10:03:10 +01003834void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3835 load->SetLocations(nullptr);
3836}
3837
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003838void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003839 // Nothing to do, this is driven by the code generator.
3840}
3841
Alexandre Rames67555f72014-11-18 10:55:16 +00003842void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003843 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
3844 ? LocationSummary::kCallOnSlowPath
3845 : LocationSummary::kNoCall;
3846 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003847 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003848 locations->SetOut(Location::RequiresRegister());
3849}
3850
3851void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003852 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003853 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003854 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003855
3856 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3857 if (kEmitCompilerReadBarrier) {
3858 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3859 __ Add(out.X(), current_method.X(), declaring_class_offset);
3860 // /* mirror::Class* */ out = out->Read()
3861 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3862 } else {
3863 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3864 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3865 }
3866
3867 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3868 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
3869
3870 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
3871 if (kEmitCompilerReadBarrier) {
3872 // /* GcRoot<mirror::String>* */ out = &out[string_index]
3873 __ Add(out.X(), out.X(), cache_offset);
3874 // /* mirror::String* */ out = out->Read()
3875 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3876 } else {
3877 // /* GcRoot<mirror::String> */ out = out[string_index]
3878 __ Ldr(out, MemOperand(out.X(), cache_offset));
3879 }
3880
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003881 if (!load->IsInDexCache()) {
3882 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3883 codegen_->AddSlowPath(slow_path);
3884 __ Cbz(out, slow_path->GetEntryLabel());
3885 __ Bind(slow_path->GetExitLabel());
3886 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003887}
3888
Alexandre Rames5319def2014-10-23 10:03:10 +01003889void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3890 local->SetLocations(nullptr);
3891}
3892
3893void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3894 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3895}
3896
3897void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3899 locations->SetOut(Location::ConstantLocation(constant));
3900}
3901
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003902void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003903 // Will be generated at use site.
3904}
3905
Alexandre Rames67555f72014-11-18 10:55:16 +00003906void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3907 LocationSummary* locations =
3908 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3909 InvokeRuntimeCallingConvention calling_convention;
3910 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3911}
3912
3913void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3914 codegen_->InvokeRuntime(instruction->IsEnter()
3915 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3916 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003917 instruction->GetDexPc(),
3918 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003919 if (instruction->IsEnter()) {
3920 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3921 } else {
3922 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3923 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003924}
3925
Alexandre Rames42d641b2014-10-27 14:00:51 +00003926void LocationsBuilderARM64::VisitMul(HMul* mul) {
3927 LocationSummary* locations =
3928 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3929 switch (mul->GetResultType()) {
3930 case Primitive::kPrimInt:
3931 case Primitive::kPrimLong:
3932 locations->SetInAt(0, Location::RequiresRegister());
3933 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003934 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003935 break;
3936
3937 case Primitive::kPrimFloat:
3938 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003939 locations->SetInAt(0, Location::RequiresFpuRegister());
3940 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003941 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003942 break;
3943
3944 default:
3945 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3946 }
3947}
3948
3949void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3950 switch (mul->GetResultType()) {
3951 case Primitive::kPrimInt:
3952 case Primitive::kPrimLong:
3953 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3954 break;
3955
3956 case Primitive::kPrimFloat:
3957 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003958 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003959 break;
3960
3961 default:
3962 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3963 }
3964}
3965
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003966void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3967 LocationSummary* locations =
3968 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3969 switch (neg->GetResultType()) {
3970 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003971 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003972 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003974 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003975
3976 case Primitive::kPrimFloat:
3977 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003978 locations->SetInAt(0, Location::RequiresFpuRegister());
3979 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003980 break;
3981
3982 default:
3983 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3984 }
3985}
3986
3987void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3988 switch (neg->GetResultType()) {
3989 case Primitive::kPrimInt:
3990 case Primitive::kPrimLong:
3991 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3992 break;
3993
3994 case Primitive::kPrimFloat:
3995 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003996 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003997 break;
3998
3999 default:
4000 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4001 }
4002}
4003
4004void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4005 LocationSummary* locations =
4006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4007 InvokeRuntimeCallingConvention calling_convention;
4008 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004009 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004010 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004011 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004012}
4013
4014void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4015 LocationSummary* locations = instruction->GetLocations();
4016 InvokeRuntimeCallingConvention calling_convention;
4017 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4018 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004019 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004020 // Note: if heap poisoning is enabled, the entry point takes cares
4021 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004022 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4023 instruction,
4024 instruction->GetDexPc(),
4025 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004026 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004027}
4028
Alexandre Rames5319def2014-10-23 10:03:10 +01004029void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4030 LocationSummary* locations =
4031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4032 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004033 if (instruction->IsStringAlloc()) {
4034 locations->AddTemp(LocationFrom(kArtMethodRegister));
4035 } else {
4036 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4037 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4038 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004039 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4040}
4041
4042void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004043 // Note: if heap poisoning is enabled, the entry point takes cares
4044 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004045 if (instruction->IsStringAlloc()) {
4046 // String is allocated through StringFactory. Call NewEmptyString entry point.
4047 Location temp = instruction->GetLocations()->GetTemp(0);
4048 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4049 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4050 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4051 __ Blr(lr);
4052 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4053 } else {
4054 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4055 instruction,
4056 instruction->GetDexPc(),
4057 nullptr);
4058 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4059 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004060}
4061
4062void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004064 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004066}
4067
4068void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004069 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004070 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004071 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004072 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004073 break;
4074
4075 default:
4076 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4077 }
4078}
4079
David Brazdil66d126e2015-04-03 16:02:44 +01004080void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4081 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4082 locations->SetInAt(0, Location::RequiresRegister());
4083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4084}
4085
4086void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004087 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4088}
4089
Alexandre Rames5319def2014-10-23 10:03:10 +01004090void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004091 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4092 ? LocationSummary::kCallOnSlowPath
4093 : LocationSummary::kNoCall;
4094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004095 locations->SetInAt(0, Location::RequiresRegister());
4096 if (instruction->HasUses()) {
4097 locations->SetOut(Location::SameAsFirstInput());
4098 }
4099}
4100
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004101void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004102 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4103 return;
4104 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004105
Alexandre Ramesd921d642015-04-16 15:07:16 +01004106 BlockPoolsScope block_pools(GetVIXLAssembler());
4107 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004108 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4109 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4110}
4111
4112void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004113 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4114 codegen_->AddSlowPath(slow_path);
4115
4116 LocationSummary* locations = instruction->GetLocations();
4117 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004118
4119 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004120}
4121
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004122void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004123 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004124 GenerateImplicitNullCheck(instruction);
4125 } else {
4126 GenerateExplicitNullCheck(instruction);
4127 }
4128}
4129
Alexandre Rames67555f72014-11-18 10:55:16 +00004130void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4131 HandleBinaryOp(instruction);
4132}
4133
4134void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4135 HandleBinaryOp(instruction);
4136}
4137
Alexandre Rames3e69f162014-12-10 10:36:50 +00004138void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4139 LOG(FATAL) << "Unreachable";
4140}
4141
4142void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4143 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4144}
4145
Alexandre Rames5319def2014-10-23 10:03:10 +01004146void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4148 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4149 if (location.IsStackSlot()) {
4150 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4151 } else if (location.IsDoubleStackSlot()) {
4152 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4153 }
4154 locations->SetOut(location);
4155}
4156
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004157void InstructionCodeGeneratorARM64::VisitParameterValue(
4158 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004159 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004160}
4161
4162void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4163 LocationSummary* locations =
4164 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004165 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004166}
4167
4168void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4169 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4170 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004171}
4172
4173void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4175 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4176 locations->SetInAt(i, Location::Any());
4177 }
4178 locations->SetOut(Location::Any());
4179}
4180
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004181void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004182 LOG(FATAL) << "Unreachable";
4183}
4184
Serban Constantinescu02164b32014-11-13 14:05:07 +00004185void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004186 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004187 LocationSummary::CallKind call_kind =
4188 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4190
4191 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004192 case Primitive::kPrimInt:
4193 case Primitive::kPrimLong:
4194 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004195 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4197 break;
4198
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004199 case Primitive::kPrimFloat:
4200 case Primitive::kPrimDouble: {
4201 InvokeRuntimeCallingConvention calling_convention;
4202 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4203 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4204 locations->SetOut(calling_convention.GetReturnLocation(type));
4205
4206 break;
4207 }
4208
Serban Constantinescu02164b32014-11-13 14:05:07 +00004209 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004210 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004211 }
4212}
4213
4214void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4215 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004216
Serban Constantinescu02164b32014-11-13 14:05:07 +00004217 switch (type) {
4218 case Primitive::kPrimInt:
4219 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004220 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004221 break;
4222 }
4223
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004224 case Primitive::kPrimFloat:
4225 case Primitive::kPrimDouble: {
4226 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4227 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004228 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004229 if (type == Primitive::kPrimFloat) {
4230 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4231 } else {
4232 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4233 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004234 break;
4235 }
4236
Serban Constantinescu02164b32014-11-13 14:05:07 +00004237 default:
4238 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004239 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004240 }
4241}
4242
Calin Juravle27df7582015-04-17 19:12:31 +01004243void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4244 memory_barrier->SetLocations(nullptr);
4245}
4246
4247void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4248 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4249}
4250
Alexandre Rames5319def2014-10-23 10:03:10 +01004251void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4252 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4253 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004254 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004255}
4256
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004257void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004258 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004259}
4260
4261void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4262 instruction->SetLocations(nullptr);
4263}
4264
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004265void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004266 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004267}
4268
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004269void LocationsBuilderARM64::VisitRor(HRor* ror) {
4270 HandleBinaryOp(ror);
4271}
4272
4273void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4274 HandleBinaryOp(ror);
4275}
4276
Serban Constantinescu02164b32014-11-13 14:05:07 +00004277void LocationsBuilderARM64::VisitShl(HShl* shl) {
4278 HandleShift(shl);
4279}
4280
4281void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4282 HandleShift(shl);
4283}
4284
4285void LocationsBuilderARM64::VisitShr(HShr* shr) {
4286 HandleShift(shr);
4287}
4288
4289void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4290 HandleShift(shr);
4291}
4292
Alexandre Rames5319def2014-10-23 10:03:10 +01004293void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4294 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4295 Primitive::Type field_type = store->InputAt(1)->GetType();
4296 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004297 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004298 case Primitive::kPrimBoolean:
4299 case Primitive::kPrimByte:
4300 case Primitive::kPrimChar:
4301 case Primitive::kPrimShort:
4302 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004303 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004304 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4305 break;
4306
4307 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004308 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004309 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4310 break;
4311
4312 default:
4313 LOG(FATAL) << "Unimplemented local type " << field_type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004314 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +01004315 }
4316}
4317
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004318void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004319}
4320
4321void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004322 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004323}
4324
4325void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004326 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004327}
4328
Alexandre Rames67555f72014-11-18 10:55:16 +00004329void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004330 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004331}
4332
4333void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004334 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004335}
4336
4337void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004338 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004339}
4340
Alexandre Rames67555f72014-11-18 10:55:16 +00004341void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004342 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004343}
4344
Calin Juravlee460d1d2015-09-29 04:52:17 +01004345void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4346 HUnresolvedInstanceFieldGet* instruction) {
4347 FieldAccessCallingConventionARM64 calling_convention;
4348 codegen_->CreateUnresolvedFieldLocationSummary(
4349 instruction, instruction->GetFieldType(), calling_convention);
4350}
4351
4352void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4353 HUnresolvedInstanceFieldGet* instruction) {
4354 FieldAccessCallingConventionARM64 calling_convention;
4355 codegen_->GenerateUnresolvedFieldAccess(instruction,
4356 instruction->GetFieldType(),
4357 instruction->GetFieldIndex(),
4358 instruction->GetDexPc(),
4359 calling_convention);
4360}
4361
4362void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4363 HUnresolvedInstanceFieldSet* instruction) {
4364 FieldAccessCallingConventionARM64 calling_convention;
4365 codegen_->CreateUnresolvedFieldLocationSummary(
4366 instruction, instruction->GetFieldType(), calling_convention);
4367}
4368
4369void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4370 HUnresolvedInstanceFieldSet* instruction) {
4371 FieldAccessCallingConventionARM64 calling_convention;
4372 codegen_->GenerateUnresolvedFieldAccess(instruction,
4373 instruction->GetFieldType(),
4374 instruction->GetFieldIndex(),
4375 instruction->GetDexPc(),
4376 calling_convention);
4377}
4378
4379void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4380 HUnresolvedStaticFieldGet* instruction) {
4381 FieldAccessCallingConventionARM64 calling_convention;
4382 codegen_->CreateUnresolvedFieldLocationSummary(
4383 instruction, instruction->GetFieldType(), calling_convention);
4384}
4385
4386void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4387 HUnresolvedStaticFieldGet* instruction) {
4388 FieldAccessCallingConventionARM64 calling_convention;
4389 codegen_->GenerateUnresolvedFieldAccess(instruction,
4390 instruction->GetFieldType(),
4391 instruction->GetFieldIndex(),
4392 instruction->GetDexPc(),
4393 calling_convention);
4394}
4395
4396void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4397 HUnresolvedStaticFieldSet* instruction) {
4398 FieldAccessCallingConventionARM64 calling_convention;
4399 codegen_->CreateUnresolvedFieldLocationSummary(
4400 instruction, instruction->GetFieldType(), calling_convention);
4401}
4402
4403void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4404 HUnresolvedStaticFieldSet* instruction) {
4405 FieldAccessCallingConventionARM64 calling_convention;
4406 codegen_->GenerateUnresolvedFieldAccess(instruction,
4407 instruction->GetFieldType(),
4408 instruction->GetFieldIndex(),
4409 instruction->GetDexPc(),
4410 calling_convention);
4411}
4412
Alexandre Rames5319def2014-10-23 10:03:10 +01004413void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4415}
4416
4417void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004418 HBasicBlock* block = instruction->GetBlock();
4419 if (block->GetLoopInformation() != nullptr) {
4420 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4421 // The back edge will generate the suspend check.
4422 return;
4423 }
4424 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4425 // The goto will generate the suspend check.
4426 return;
4427 }
4428 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004429}
4430
4431void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4432 temp->SetLocations(nullptr);
4433}
4434
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004435void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004436 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004437}
4438
Alexandre Rames67555f72014-11-18 10:55:16 +00004439void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4440 LocationSummary* locations =
4441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4442 InvokeRuntimeCallingConvention calling_convention;
4443 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4444}
4445
4446void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4447 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004448 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004449 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004450}
4451
4452void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4453 LocationSummary* locations =
4454 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4455 Primitive::Type input_type = conversion->GetInputType();
4456 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004457 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004458 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4459 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4460 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4461 }
4462
Alexandre Rames542361f2015-01-29 16:57:31 +00004463 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004464 locations->SetInAt(0, Location::RequiresFpuRegister());
4465 } else {
4466 locations->SetInAt(0, Location::RequiresRegister());
4467 }
4468
Alexandre Rames542361f2015-01-29 16:57:31 +00004469 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004470 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4471 } else {
4472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4473 }
4474}
4475
4476void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4477 Primitive::Type result_type = conversion->GetResultType();
4478 Primitive::Type input_type = conversion->GetInputType();
4479
4480 DCHECK_NE(input_type, result_type);
4481
Alexandre Rames542361f2015-01-29 16:57:31 +00004482 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004483 int result_size = Primitive::ComponentSize(result_type);
4484 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004485 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004486 Register output = OutputRegister(conversion);
4487 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004488 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004489 // 'int' values are used directly as W registers, discarding the top
4490 // bits, so we don't need to sign-extend and can just perform a move.
4491 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4492 // top 32 bits of the target register. We theoretically could leave those
4493 // bits unchanged, but we would have to make sure that no code uses a
4494 // 32bit input value as a 64bit value assuming that the top 32 bits are
4495 // zero.
4496 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004497 } else if (result_type == Primitive::kPrimChar ||
4498 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4499 __ Ubfx(output,
4500 output.IsX() ? source.X() : source.W(),
4501 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004502 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004503 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004504 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004505 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004506 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004507 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004508 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4509 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004510 } else if (Primitive::IsFloatingPointType(result_type) &&
4511 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004512 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4513 } else {
4514 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4515 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004516 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004517}
Alexandre Rames67555f72014-11-18 10:55:16 +00004518
Serban Constantinescu02164b32014-11-13 14:05:07 +00004519void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4520 HandleShift(ushr);
4521}
4522
4523void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4524 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004525}
4526
4527void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4528 HandleBinaryOp(instruction);
4529}
4530
4531void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4532 HandleBinaryOp(instruction);
4533}
4534
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004535void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004536 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004537 LOG(FATAL) << "Unreachable";
4538}
4539
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004540void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004541 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004542 LOG(FATAL) << "Unreachable";
4543}
4544
Mark Mendellfe57faa2015-09-18 09:26:15 -04004545// Simple implementation of packed switch - generate cascaded compare/jumps.
4546void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4547 LocationSummary* locations =
4548 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4549 locations->SetInAt(0, Location::RequiresRegister());
4550}
4551
4552void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4553 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004554 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004555 Register value_reg = InputRegisterAt(switch_instr, 0);
4556 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4557
Zheng Xu3927c8b2015-11-18 17:46:25 +08004558 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4559 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4560 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4561 // make sure we don't emit it if the target may run out of range.
4562 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4563 // ranges and emit the tables only as required.
4564 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004565
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004566 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004567 // Current instruction id is an upper bound of the number of HIRs in the graph.
4568 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4569 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004570 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4571 Register temp = temps.AcquireW();
4572 __ Subs(temp, value_reg, Operand(lower_bound));
4573
Zheng Xu3927c8b2015-11-18 17:46:25 +08004574 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004575 // Jump to successors[0] if value == lower_bound.
4576 __ B(eq, codegen_->GetLabelOf(successors[0]));
4577 int32_t last_index = 0;
4578 for (; num_entries - last_index > 2; last_index += 2) {
4579 __ Subs(temp, temp, Operand(2));
4580 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4581 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4582 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4583 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4584 }
4585 if (num_entries - last_index == 2) {
4586 // The last missing case_value.
4587 __ Cmp(temp, Operand(1));
4588 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004589 }
4590
4591 // And the default for any other value.
4592 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4593 __ B(codegen_->GetLabelOf(default_block));
4594 }
4595 } else {
4596 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4597 codegen_->AddJumpTable(jump_table);
4598
4599 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4600
4601 // Below instructions should use at most one blocked register. Since there are two blocked
4602 // registers, we are free to block one.
4603 Register temp_w = temps.AcquireW();
4604 Register index;
4605 // Remove the bias.
4606 if (lower_bound != 0) {
4607 index = temp_w;
4608 __ Sub(index, value_reg, Operand(lower_bound));
4609 } else {
4610 index = value_reg;
4611 }
4612
4613 // Jump to default block if index is out of the range.
4614 __ Cmp(index, Operand(num_entries));
4615 __ B(hs, codegen_->GetLabelOf(default_block));
4616
4617 // In current VIXL implementation, it won't require any blocked registers to encode the
4618 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4619 // register pressure.
4620 Register table_base = temps.AcquireX();
4621 // Load jump offset from the table.
4622 __ Adr(table_base, jump_table->GetTableStartLabel());
4623 Register jump_offset = temp_w;
4624 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4625
4626 // Jump to target block by branching to table_base(pc related) + offset.
4627 Register target_address = table_base;
4628 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4629 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004630 }
4631}
4632
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004633void CodeGeneratorARM64::GenerateReadBarrier(HInstruction* instruction,
4634 Location out,
4635 Location ref,
4636 Location obj,
4637 uint32_t offset,
4638 Location index) {
4639 DCHECK(kEmitCompilerReadBarrier);
4640
4641 // If heap poisoning is enabled, the unpoisoning of the loaded
4642 // reference will be carried out by the runtime within the slow
4643 // path.
4644 //
4645 // Note that `ref` currently does not get unpoisoned (when heap
4646 // poisoning is enabled), which is alright as the `ref` argument is
4647 // not used by the artReadBarrierSlow entry point.
4648 //
4649 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4650 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4651 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4652 AddSlowPath(slow_path);
4653
4654 // TODO: When read barrier has a fast path, add it here.
4655 /* Currently the read barrier call is inserted after the original load.
4656 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
4657 * original load. This load-load ordering is required by the read barrier.
4658 * The fast path/slow path (for Baker's algorithm) should look like:
4659 *
4660 * bool isGray = obj.LockWord & kReadBarrierMask;
4661 * lfence; // load fence or artificial data dependence to prevent load-load reordering
4662 * ref = obj.field; // this is the original load
4663 * if (isGray) {
4664 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
4665 * }
4666 */
4667
4668 __ B(slow_path->GetEntryLabel());
4669 __ Bind(slow_path->GetExitLabel());
4670}
4671
4672void CodeGeneratorARM64::MaybeGenerateReadBarrier(HInstruction* instruction,
4673 Location out,
4674 Location ref,
4675 Location obj,
4676 uint32_t offset,
4677 Location index) {
4678 if (kEmitCompilerReadBarrier) {
4679 // If heap poisoning is enabled, unpoisoning will be taken care of
4680 // by the runtime within the slow path.
4681 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
4682 } else if (kPoisonHeapReferences) {
4683 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4684 }
4685}
4686
4687void CodeGeneratorARM64::GenerateReadBarrierForRoot(HInstruction* instruction,
4688 Location out,
4689 Location root) {
4690 DCHECK(kEmitCompilerReadBarrier);
4691
4692 // Note that GC roots are not affected by heap poisoning, so we do
4693 // not need to do anything special for this here.
4694 SlowPathCodeARM64* slow_path =
4695 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4696 AddSlowPath(slow_path);
4697
4698 // TODO: Implement a fast path for ReadBarrierForRoot, performing
4699 // the following operation (for Baker's algorithm):
4700 //
4701 // if (thread.tls32_.is_gc_marking) {
4702 // root = Mark(root);
4703 // }
4704
4705 __ B(slow_path->GetEntryLabel());
4706 __ Bind(slow_path->GetExitLabel());
4707}
4708
Alexandre Rames67555f72014-11-18 10:55:16 +00004709#undef __
4710#undef QUICK_ENTRY_POINT
4711
Alexandre Rames5319def2014-10-23 10:03:10 +01004712} // namespace arm64
4713} // namespace art