blob: 3d65e9c53ca6e3f0ad779427ca8817c08e690d58 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
Roland Levillain22ccc3a2015-11-24 13:10:05 +000045template<class MirrorType>
46class GcRoot;
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace arm64 {
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050using helpers::CPURegisterFrom;
51using helpers::DRegisterFrom;
52using helpers::FPRegisterFrom;
53using helpers::HeapOperand;
54using helpers::HeapOperandFrom;
55using helpers::InputCPURegisterAt;
56using helpers::InputFPRegisterAt;
57using helpers::InputRegisterAt;
58using helpers::InputOperandAt;
59using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080060using helpers::LocationFrom;
61using helpers::OperandFromMemOperand;
62using helpers::OutputCPURegister;
63using helpers::OutputFPRegister;
64using helpers::OutputRegister;
65using helpers::RegisterFrom;
66using helpers::StackOperandFrom;
67using helpers::VIXLRegCodeFromART;
68using helpers::WRegisterFrom;
69using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000070using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080071using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
Alexandre Rames5319def2014-10-23 10:03:10 +010073static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000074// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080075// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000077static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Vladimir Markod6e069b2016-01-18 11:11:01 +000096inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
97 // The ARM64 condition codes can express all the necessary branches, see the
98 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
99 // There is no dex instruction or HIR that would need the missing conditions
100 // "equal or unordered" or "not equal".
101 switch (cond) {
102 case kCondEQ: return eq;
103 case kCondNE: return ne /* unordered */;
104 case kCondLT: return gt_bias ? cc : lt /* unordered */;
105 case kCondLE: return gt_bias ? ls : le /* unordered */;
106 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
107 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
108 default:
109 LOG(FATAL) << "UNREACHABLE";
110 UNREACHABLE();
111 }
112}
113
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
116 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
117 // but we use the exact registers for clarity.
118 if (return_type == Primitive::kPrimFloat) {
119 return LocationFrom(s0);
120 } else if (return_type == Primitive::kPrimDouble) {
121 return LocationFrom(d0);
122 } else if (return_type == Primitive::kPrimLong) {
123 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100124 } else if (return_type == Primitive::kPrimVoid) {
125 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000126 } else {
127 return LocationFrom(w0);
128 }
129}
130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000132 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133}
134
Alexandre Rames67555f72014-11-18 10:55:16 +0000135#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
Zheng Xuda403092015-04-24 17:35:39 +0800138// Calculate memory accessing operand for save/restore live registers.
139static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
140 RegisterSet* register_set,
141 int64_t spill_offset,
142 bool is_save) {
143 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
144 codegen->GetNumberOfCoreRegisters(),
145 register_set->GetFloatingPointRegisters(),
146 codegen->GetNumberOfFloatingPointRegisters()));
147
148 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
149 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000150 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
151 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800152
153 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
154 UseScratchRegisterScope temps(masm);
155
156 Register base = masm->StackPointer();
157 int64_t core_spill_size = core_list.TotalSizeInBytes();
158 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
159 int64_t reg_size = kXRegSizeInBytes;
160 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
161 uint32_t ls_access_size = WhichPowerOf2(reg_size);
162 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
163 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
164 // If the offset does not fit in the instruction's immediate field, use an alternate register
165 // to compute the base address(float point registers spill base address).
166 Register new_base = temps.AcquireSameSizeAs(base);
167 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
168 base = new_base;
169 spill_offset = -core_spill_size;
170 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
171 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
172 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
173 }
174
175 if (is_save) {
176 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
177 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
178 } else {
179 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
180 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
181 }
182}
183
184void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
185 RegisterSet* register_set = locations->GetLiveRegisters();
186 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
187 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
188 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
189 // If the register holds an object, update the stack mask.
190 if (locations->RegisterContainsObject(i)) {
191 locations->SetStackBit(stack_offset / kVRegSize);
192 }
193 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
194 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
195 saved_core_stack_offsets_[i] = stack_offset;
196 stack_offset += kXRegSizeInBytes;
197 }
198 }
199
200 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
201 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
202 register_set->ContainsFloatingPointRegister(i)) {
203 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
204 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
205 saved_fpu_stack_offsets_[i] = stack_offset;
206 stack_offset += kDRegSizeInBytes;
207 }
208 }
209
210 SaveRestoreLiveRegistersHelper(codegen, register_set,
211 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
212}
213
214void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
215 RegisterSet* register_set = locations->GetLiveRegisters();
216 SaveRestoreLiveRegistersHelper(codegen, register_set,
217 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
218}
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
221 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100222 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000226 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000229 if (instruction_->CanThrowIntoCatchBlock()) {
230 // Live registers will be restored in the catch block if caught.
231 SaveLiveRegisters(codegen, instruction_->GetLocations());
232 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000233 // We're moving two locations to locations that could overlap, so we need a parallel
234 // move resolver.
235 InvokeRuntimeCallingConvention calling_convention;
236 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100237 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
238 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000239 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000240 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800241 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100242 }
243
Alexandre Rames8158f282015-08-07 10:26:17 +0100244 bool IsFatal() const OVERRIDE { return true; }
245
Alexandre Rames9931f312015-06-19 14:47:01 +0100246 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
247
Alexandre Rames5319def2014-10-23 10:03:10 +0100248 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000249 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000261 if (instruction_->CanThrowIntoCatchBlock()) {
262 // Live registers will be restored in the catch block if caught.
263 SaveLiveRegisters(codegen, instruction_->GetLocations());
264 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000265 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000266 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 }
269
Alexandre Rames8158f282015-08-07 10:26:17 +0100270 bool IsFatal() const OVERRIDE { return true; }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
273
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 private:
275 HDivZeroCheck* const instruction_;
276 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
277};
278
279class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
280 public:
281 LoadClassSlowPathARM64(HLoadClass* cls,
282 HInstruction* at,
283 uint32_t dex_pc,
284 bool do_clinit)
285 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
286 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
287 }
288
289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
290 LocationSummary* locations = at_->GetLocations();
291 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
292
293 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295
296 InvokeRuntimeCallingConvention calling_convention;
297 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
299 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000300 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800301 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800303 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100304 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800305 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000306
307 // Move the class to the desired location.
308 Location out = locations->Out();
309 if (out.IsValid()) {
310 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
311 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000312 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000315 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 __ B(GetExitLabel());
317 }
318
Alexandre Rames9931f312015-06-19 14:47:01 +0100319 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
320
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 private:
322 // The class this slow path will load.
323 HLoadClass* const cls_;
324
325 // The instruction where this slow path is happening.
326 // (Might be the load class or an initialization check).
327 HInstruction* const at_;
328
329 // The dex PC of `at_`.
330 const uint32_t dex_pc_;
331
332 // Whether to initialize the class.
333 const bool do_clinit_;
334
335 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
336};
337
338class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
339 public:
340 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 LocationSummary* locations = instruction_->GetLocations();
344 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
345 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
346
347 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000348 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349
350 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800351 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000353 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100354 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000356 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000357
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000358 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 __ B(GetExitLabel());
360 }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
363
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 private:
365 HLoadString* const instruction_;
366
367 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
368};
369
Alexandre Rames5319def2014-10-23 10:03:10 +0100370class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
372 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
373
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000377 if (instruction_->CanThrowIntoCatchBlock()) {
378 // Live registers will be restored in the catch block if caught.
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
380 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000381 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000382 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800383 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 }
385
Alexandre Rames8158f282015-08-07 10:26:17 +0100386 bool IsFatal() const OVERRIDE { return true; }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
389
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 private:
391 HNullCheck* const instruction_;
392
393 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
394};
395
396class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
397 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100398 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100399 : instruction_(instruction), successor_(successor) {}
400
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
402 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100403 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000404 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000406 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800407 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000408 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000409 if (successor_ == nullptr) {
410 __ B(GetReturnLabel());
411 } else {
412 __ B(arm64_codegen->GetLabelOf(successor_));
413 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 }
415
416 vixl::Label* GetReturnLabel() {
417 DCHECK(successor_ == nullptr);
418 return &return_label_;
419 }
420
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100421 HBasicBlock* GetSuccessor() const {
422 return successor_;
423 }
424
Alexandre Rames9931f312015-06-19 14:47:01 +0100425 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
426
Alexandre Rames5319def2014-10-23 10:03:10 +0100427 private:
428 HSuspendCheck* const instruction_;
429 // If not null, the block to branch to after the suspend check.
430 HBasicBlock* const successor_;
431
432 // If `successor_` is null, the label to branch to after the suspend check.
433 vixl::Label return_label_;
434
435 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
436};
437
Alexandre Rames67555f72014-11-18 10:55:16 +0000438class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
439 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000440 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
441 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000442
443 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000444 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 Location class_to_check = locations->InAt(1);
446 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
447 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000448 DCHECK(instruction_->IsCheckCast()
449 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
450 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100451 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452
Alexandre Rames67555f72014-11-18 10:55:16 +0000453 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000455 if (!is_fatal_) {
456 SaveLiveRegisters(codegen, locations);
457 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458
459 // We're moving two locations to locations that could overlap, so we need a parallel
460 // move resolver.
461 InvokeRuntimeCallingConvention calling_convention;
462 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100463 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
464 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465
466 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000467 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100468 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000469 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
470 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 Primitive::Type ret_type = instruction_->GetType();
472 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
473 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
474 } else {
475 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100476 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800477 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478 }
479
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000480 if (!is_fatal_) {
481 RestoreLiveRegisters(codegen, locations);
482 __ B(GetExitLabel());
483 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000484 }
485
Alexandre Rames9931f312015-06-19 14:47:01 +0100486 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100488
Alexandre Rames67555f72014-11-18 10:55:16 +0000489 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000490 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000491 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000492
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
494};
495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
497 public:
Aart Bik42249c32016-01-07 15:33:50 -0800498 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100499 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700500
501 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800502 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 __ Bind(GetEntryLabel());
504 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800505 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
506 instruction_,
507 instruction_->GetDexPc(),
508 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000509 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510 }
511
Alexandre Rames9931f312015-06-19 14:47:01 +0100512 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
513
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700514 private:
Aart Bik42249c32016-01-07 15:33:50 -0800515 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700516 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
517};
518
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100519class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
520 public:
521 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
522
523 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
524 LocationSummary* locations = instruction_->GetLocations();
525 __ Bind(GetEntryLabel());
526 SaveLiveRegisters(codegen, locations);
527
528 InvokeRuntimeCallingConvention calling_convention;
529 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
530 parallel_move.AddMove(
531 locations->InAt(0),
532 LocationFrom(calling_convention.GetRegisterAt(0)),
533 Primitive::kPrimNot,
534 nullptr);
535 parallel_move.AddMove(
536 locations->InAt(1),
537 LocationFrom(calling_convention.GetRegisterAt(1)),
538 Primitive::kPrimInt,
539 nullptr);
540 parallel_move.AddMove(
541 locations->InAt(2),
542 LocationFrom(calling_convention.GetRegisterAt(2)),
543 Primitive::kPrimNot,
544 nullptr);
545 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
546
547 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
548 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
549 instruction_,
550 instruction_->GetDexPc(),
551 this);
552 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
553 RestoreLiveRegisters(codegen, locations);
554 __ B(GetExitLabel());
555 }
556
557 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
558
559 private:
560 HInstruction* const instruction_;
561
562 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
563};
564
Zheng Xu3927c8b2015-11-18 17:46:25 +0800565void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
566 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000567 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800568
569 // We are about to use the assembler to place literals directly. Make sure we have enough
570 // underlying code buffer and we have generated the jump table with right size.
571 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
572 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
573
574 __ Bind(&table_start_);
575 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
576 for (uint32_t i = 0; i < num_entries; i++) {
577 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
578 DCHECK(target_label->IsBound());
579 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
580 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
581 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
582 Literal<int32_t> literal(jump_offset);
583 __ place(&literal);
584 }
585}
586
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000587// Slow path generating a read barrier for a heap reference.
588class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
589 public:
590 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
591 Location out,
592 Location ref,
593 Location obj,
594 uint32_t offset,
595 Location index)
596 : instruction_(instruction),
597 out_(out),
598 ref_(ref),
599 obj_(obj),
600 offset_(offset),
601 index_(index) {
602 DCHECK(kEmitCompilerReadBarrier);
603 // If `obj` is equal to `out` or `ref`, it means the initial object
604 // has been overwritten by (or after) the heap object reference load
605 // to be instrumented, e.g.:
606 //
607 // __ Ldr(out, HeapOperand(out, class_offset);
608 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
609 //
610 // In that case, we have lost the information about the original
611 // object, and the emitted read barrier cannot work properly.
612 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
613 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
614 }
615
616 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
617 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
618 LocationSummary* locations = instruction_->GetLocations();
619 Primitive::Type type = Primitive::kPrimNot;
620 DCHECK(locations->CanCall());
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
622 DCHECK(!instruction_->IsInvoke() ||
623 (instruction_->IsInvokeStaticOrDirect() &&
624 instruction_->GetLocations()->Intrinsified()));
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000625 // The read barrier instrumentation does not support the
626 // HArm64IntermediateAddress instruction yet.
627 DCHECK(!(instruction_->IsArrayGet() &&
628 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000629
630 __ Bind(GetEntryLabel());
631
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000632 SaveLiveRegisters(codegen, locations);
633
634 // We may have to change the index's value, but as `index_` is a
635 // constant member (like other "inputs" of this slow path),
636 // introduce a copy of it, `index`.
637 Location index = index_;
638 if (index_.IsValid()) {
639 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
640 if (instruction_->IsArrayGet()) {
641 // Compute the actual memory offset and store it in `index`.
642 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
643 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
644 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
645 // We are about to change the value of `index_reg` (see the
646 // calls to vixl::MacroAssembler::Lsl and
647 // vixl::MacroAssembler::Mov below), but it has
648 // not been saved by the previous call to
649 // art::SlowPathCode::SaveLiveRegisters, as it is a
650 // callee-save register --
651 // art::SlowPathCode::SaveLiveRegisters does not consider
652 // callee-save registers, as it has been designed with the
653 // assumption that callee-save registers are supposed to be
654 // handled by the called function. So, as a callee-save
655 // register, `index_reg` _would_ eventually be saved onto
656 // the stack, but it would be too late: we would have
657 // changed its value earlier. Therefore, we manually save
658 // it here into another freely available register,
659 // `free_reg`, chosen of course among the caller-save
660 // registers (as a callee-save `free_reg` register would
661 // exhibit the same problem).
662 //
663 // Note we could have requested a temporary register from
664 // the register allocator instead; but we prefer not to, as
665 // this is a slow path, and we know we can find a
666 // caller-save register that is available.
667 Register free_reg = FindAvailableCallerSaveRegister(codegen);
668 __ Mov(free_reg.W(), index_reg);
669 index_reg = free_reg;
670 index = LocationFrom(index_reg);
671 } else {
672 // The initial register stored in `index_` has already been
673 // saved in the call to art::SlowPathCode::SaveLiveRegisters
674 // (as it is not a callee-save register), so we can freely
675 // use it.
676 }
677 // Shifting the index value contained in `index_reg` by the scale
678 // factor (2) cannot overflow in practice, as the runtime is
679 // unable to allocate object arrays with a size larger than
680 // 2^26 - 1 (that is, 2^28 - 4 bytes).
681 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
682 static_assert(
683 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
684 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
685 __ Add(index_reg, index_reg, Operand(offset_));
686 } else {
687 DCHECK(instruction_->IsInvoke());
688 DCHECK(instruction_->GetLocations()->Intrinsified());
689 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
690 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
691 << instruction_->AsInvoke()->GetIntrinsic();
692 DCHECK_EQ(offset_, 0U);
693 DCHECK(index_.IsRegisterPair());
694 // UnsafeGet's offset location is a register pair, the low
695 // part contains the correct offset.
696 index = index_.ToLow();
697 }
698 }
699
700 // We're moving two or three locations to locations that could
701 // overlap, so we need a parallel move resolver.
702 InvokeRuntimeCallingConvention calling_convention;
703 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
704 parallel_move.AddMove(ref_,
705 LocationFrom(calling_convention.GetRegisterAt(0)),
706 type,
707 nullptr);
708 parallel_move.AddMove(obj_,
709 LocationFrom(calling_convention.GetRegisterAt(1)),
710 type,
711 nullptr);
712 if (index.IsValid()) {
713 parallel_move.AddMove(index,
714 LocationFrom(calling_convention.GetRegisterAt(2)),
715 Primitive::kPrimInt,
716 nullptr);
717 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
718 } else {
719 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
720 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
721 }
722 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
723 instruction_,
724 instruction_->GetDexPc(),
725 this);
726 CheckEntrypointTypes<
727 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
728 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
729
730 RestoreLiveRegisters(codegen, locations);
731
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000732 __ B(GetExitLabel());
733 }
734
735 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
736
737 private:
738 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
739 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
740 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
741 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
742 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
743 return Register(VIXLRegCodeFromART(i), kXRegSize);
744 }
745 }
746 // We shall never fail to find a free caller-save register, as
747 // there are more than two core caller-save registers on ARM64
748 // (meaning it is possible to find one which is different from
749 // `ref` and `obj`).
750 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
751 LOG(FATAL) << "Could not find a free register";
752 UNREACHABLE();
753 }
754
755 HInstruction* const instruction_;
756 const Location out_;
757 const Location ref_;
758 const Location obj_;
759 const uint32_t offset_;
760 // An additional location containing an index to an array.
761 // Only used for HArrayGet and the UnsafeGetObject &
762 // UnsafeGetObjectVolatile intrinsics.
763 const Location index_;
764
765 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
766};
767
768// Slow path generating a read barrier for a GC root.
769class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
770 public:
771 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
772 : instruction_(instruction), out_(out), root_(root) {}
773
774 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
775 LocationSummary* locations = instruction_->GetLocations();
776 Primitive::Type type = Primitive::kPrimNot;
777 DCHECK(locations->CanCall());
778 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
779 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
780
781 __ Bind(GetEntryLabel());
782 SaveLiveRegisters(codegen, locations);
783
784 InvokeRuntimeCallingConvention calling_convention;
785 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
786 // The argument of the ReadBarrierForRootSlow is not a managed
787 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
788 // thus we need a 64-bit move here, and we cannot use
789 //
790 // arm64_codegen->MoveLocation(
791 // LocationFrom(calling_convention.GetRegisterAt(0)),
792 // root_,
793 // type);
794 //
795 // which would emit a 32-bit move, as `type` is a (32-bit wide)
796 // reference type (`Primitive::kPrimNot`).
797 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
798 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
799 instruction_,
800 instruction_->GetDexPc(),
801 this);
802 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
803 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
804
805 RestoreLiveRegisters(codegen, locations);
806 __ B(GetExitLabel());
807 }
808
809 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
810
811 private:
812 HInstruction* const instruction_;
813 const Location out_;
814 const Location root_;
815
816 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
817};
818
Alexandre Rames5319def2014-10-23 10:03:10 +0100819#undef __
820
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100821Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100822 Location next_location;
823 if (type == Primitive::kPrimVoid) {
824 LOG(FATAL) << "Unreachable type " << type;
825 }
826
Alexandre Rames542361f2015-01-29 16:57:31 +0000827 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100828 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
829 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000830 } else if (!Primitive::IsFloatingPointType(type) &&
831 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000832 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
833 } else {
834 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000835 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
836 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100837 }
838
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000839 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000840 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100841 return next_location;
842}
843
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100844Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100845 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100846}
847
Serban Constantinescu579885a2015-02-22 20:51:33 +0000848CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
849 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100850 const CompilerOptions& compiler_options,
851 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 : CodeGenerator(graph,
853 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000854 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000855 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000856 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000857 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100858 compiler_options,
859 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100860 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800861 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100862 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000863 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000864 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000865 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100866 uint64_literals_(std::less<uint64_t>(),
867 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
868 method_patches_(MethodReferenceComparator(),
869 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
870 call_patches_(MethodReferenceComparator(),
871 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
872 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000873 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000874 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000875 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000876}
Alexandre Rames5319def2014-10-23 10:03:10 +0100877
Alexandre Rames67555f72014-11-18 10:55:16 +0000878#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100879
Zheng Xu3927c8b2015-11-18 17:46:25 +0800880void CodeGeneratorARM64::EmitJumpTables() {
881 for (auto jump_table : jump_tables_) {
882 jump_table->EmitTable(this);
883 }
884}
885
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000886void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800887 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000888 // Ensure we emit the literal pool.
889 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000890
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000891 CodeGenerator::Finalize(allocator);
892}
893
Zheng Xuad4450e2015-04-17 18:48:56 +0800894void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
895 // Note: There are 6 kinds of moves:
896 // 1. constant -> GPR/FPR (non-cycle)
897 // 2. constant -> stack (non-cycle)
898 // 3. GPR/FPR -> GPR/FPR
899 // 4. GPR/FPR -> stack
900 // 5. stack -> GPR/FPR
901 // 6. stack -> stack (non-cycle)
902 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
903 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
904 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
905 // dependency.
906 vixl_temps_.Open(GetVIXLAssembler());
907}
908
909void ParallelMoveResolverARM64::FinishEmitNativeCode() {
910 vixl_temps_.Close();
911}
912
913Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
914 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
915 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
916 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
917 Location scratch = GetScratchLocation(kind);
918 if (!scratch.Equals(Location::NoLocation())) {
919 return scratch;
920 }
921 // Allocate from VIXL temp registers.
922 if (kind == Location::kRegister) {
923 scratch = LocationFrom(vixl_temps_.AcquireX());
924 } else {
925 DCHECK(kind == Location::kFpuRegister);
926 scratch = LocationFrom(vixl_temps_.AcquireD());
927 }
928 AddScratchLocation(scratch);
929 return scratch;
930}
931
932void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
933 if (loc.IsRegister()) {
934 vixl_temps_.Release(XRegisterFrom(loc));
935 } else {
936 DCHECK(loc.IsFpuRegister());
937 vixl_temps_.Release(DRegisterFrom(loc));
938 }
939 RemoveScratchLocation(loc);
940}
941
Alexandre Rames3e69f162014-12-10 10:36:50 +0000942void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100943 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100944 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000945}
946
Alexandre Rames5319def2014-10-23 10:03:10 +0100947void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100948 MacroAssembler* masm = GetVIXLAssembler();
949 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000950 __ Bind(&frame_entry_label_);
951
Serban Constantinescu02164b32014-11-13 14:05:07 +0000952 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
953 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100954 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000955 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000956 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000957 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000958 __ Ldr(wzr, MemOperand(temp, 0));
959 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000960 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100961
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000962 if (!HasEmptyFrame()) {
963 int frame_size = GetFrameSize();
964 // Stack layout:
965 // sp[frame_size - 8] : lr.
966 // ... : other preserved core registers.
967 // ... : other preserved fp registers.
968 // ... : reserved frame space.
969 // sp[0] : current method.
970 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100971 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800972 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
973 frame_size - GetCoreSpillSize());
974 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
975 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000976 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100977}
978
979void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100980 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100981 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000982 if (!HasEmptyFrame()) {
983 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800984 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
985 frame_size - FrameEntrySpillSize());
986 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
987 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000988 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100989 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000990 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100991 __ Ret();
992 GetAssembler()->cfi().RestoreState();
993 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100994}
995
Zheng Xuda403092015-04-24 17:35:39 +0800996vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
997 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
998 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
999 core_spill_mask_);
1000}
1001
1002vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1003 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1004 GetNumberOfFloatingPointRegisters()));
1005 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1006 fpu_spill_mask_);
1007}
1008
Alexandre Rames5319def2014-10-23 10:03:10 +01001009void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1010 __ Bind(GetLabelOf(block));
1011}
1012
Alexandre Rames5319def2014-10-23 10:03:10 +01001013void CodeGeneratorARM64::Move(HInstruction* instruction,
1014 Location location,
1015 HInstruction* move_for) {
1016 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001017 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001018 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +01001019
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001020 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 MoveLocation(location,
1022 Location::DoubleStackSlot(kCurrentMethodStackOffset),
1023 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001024 } else if (locations != nullptr && locations->Out().Equals(location)) {
1025 return;
1026 } else if (instruction->IsIntConstant()
1027 || instruction->IsLongConstant()
1028 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001029 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +01001030 if (location.IsRegister()) {
1031 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001032 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +01001033 (instruction->IsLongConstant() && dst.Is64Bits()));
1034 __ Mov(dst, value);
1035 } else {
1036 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001037 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001038 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
1039 ? temps.AcquireW()
1040 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +01001041 __ Mov(temp, value);
1042 __ Str(temp, StackOperandFrom(location));
1043 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001044 } else if (instruction->IsTemporary()) {
1045 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001046 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001047 } else if (instruction->IsLoadLocal()) {
1048 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +00001049 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001050 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001051 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001052 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001053 }
1054
1055 } else {
1056 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001057 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001058 }
1059}
1060
Calin Juravle175dc732015-08-25 15:42:32 +01001061void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1062 DCHECK(location.IsRegister());
1063 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1064}
1065
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1067 if (location.IsRegister()) {
1068 locations->AddTemp(location);
1069 } else {
1070 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1071 }
1072}
1073
Alexandre Rames5319def2014-10-23 10:03:10 +01001074Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1075 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001076
Alexandre Rames5319def2014-10-23 10:03:10 +01001077 switch (type) {
1078 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001079 case Primitive::kPrimInt:
1080 case Primitive::kPrimFloat:
1081 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1082
1083 case Primitive::kPrimLong:
1084 case Primitive::kPrimDouble:
1085 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1086
Alexandre Rames5319def2014-10-23 10:03:10 +01001087 case Primitive::kPrimBoolean:
1088 case Primitive::kPrimByte:
1089 case Primitive::kPrimChar:
1090 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001091 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001092 LOG(FATAL) << "Unexpected type " << type;
1093 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001094
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 LOG(FATAL) << "Unreachable";
1096 return Location::NoLocation();
1097}
1098
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001099void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001100 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001102 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001103 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001104 if (value_can_be_null) {
1105 __ Cbz(value, &done);
1106 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1108 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001109 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001110 if (value_can_be_null) {
1111 __ Bind(&done);
1112 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001113}
1114
David Brazdil58282f42016-01-14 12:45:10 +00001115void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001116 // Blocked core registers:
1117 // lr : Runtime reserved.
1118 // tr : Runtime reserved.
1119 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1120 // ip1 : VIXL core temp.
1121 // ip0 : VIXL core temp.
1122 //
1123 // Blocked fp registers:
1124 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001125 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1126 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001127 while (!reserved_core_registers.IsEmpty()) {
1128 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1129 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001130
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001132 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001133 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1134 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001135
David Brazdil58282f42016-01-14 12:45:10 +00001136 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001137 // Stubs do not save callee-save floating point registers. If the graph
1138 // is debuggable, we need to deal with these registers differently. For
1139 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001140 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1141 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1142 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001143 }
1144 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001145}
1146
Alexandre Rames3e69f162014-12-10 10:36:50 +00001147size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1148 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1149 __ Str(reg, MemOperand(sp, stack_index));
1150 return kArm64WordSize;
1151}
1152
1153size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1155 __ Ldr(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 FPRegister reg = FPRegister(reg_id, kDRegSize);
1161 __ Str(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
1165size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1166 FPRegister reg = FPRegister(reg_id, kDRegSize);
1167 __ Ldr(reg, MemOperand(sp, stack_index));
1168 return kArm64WordSize;
1169}
1170
Alexandre Rames5319def2014-10-23 10:03:10 +01001171void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001172 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001173}
1174
1175void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001176 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001177}
1178
Alexandre Rames67555f72014-11-18 10:55:16 +00001179void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001180 if (constant->IsIntConstant()) {
1181 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1182 } else if (constant->IsLongConstant()) {
1183 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1184 } else if (constant->IsNullConstant()) {
1185 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001186 } else if (constant->IsFloatConstant()) {
1187 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1188 } else {
1189 DCHECK(constant->IsDoubleConstant());
1190 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1191 }
1192}
1193
Alexandre Rames3e69f162014-12-10 10:36:50 +00001194
1195static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1196 DCHECK(constant.IsConstant());
1197 HConstant* cst = constant.GetConstant();
1198 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001199 // Null is mapped to a core W register, which we associate with kPrimInt.
1200 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001201 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1202 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1203 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1204}
1205
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206void CodeGeneratorARM64::MoveLocation(Location destination,
1207 Location source,
1208 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001209 if (source.Equals(destination)) {
1210 return;
1211 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212
1213 // A valid move can always be inferred from the destination and source
1214 // locations. When moving from and to a register, the argument type can be
1215 // used to generate 32bit instead of 64bit moves. In debug mode we also
1216 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001218
1219 if (destination.IsRegister() || destination.IsFpuRegister()) {
1220 if (unspecified_type) {
1221 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1222 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001223 (src_cst != nullptr && (src_cst->IsIntConstant()
1224 || src_cst->IsFloatConstant()
1225 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001226 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 // If the source is a double stack slot or a 64bit constant, a 64bit
1230 // type is appropriate. Else the source is a register, and since the
1231 // type has not been specified, we chose a 64bit type to force a 64bit
1232 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001235 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1237 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1238 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001239 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1240 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1241 __ Ldr(dst, StackOperandFrom(source));
1242 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001243 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001244 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001246 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001248 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001249 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1251 ? Primitive::kPrimLong
1252 : Primitive::kPrimInt;
1253 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1254 }
1255 } else {
1256 DCHECK(source.IsFpuRegister());
1257 if (destination.IsRegister()) {
1258 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1259 ? Primitive::kPrimDouble
1260 : Primitive::kPrimFloat;
1261 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1262 } else {
1263 DCHECK(destination.IsFpuRegister());
1264 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001265 }
1266 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 } else { // The destination is not a register. It must be a stack slot.
1268 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1269 if (source.IsRegister() || source.IsFpuRegister()) {
1270 if (unspecified_type) {
1271 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001273 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001275 }
1276 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1278 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1279 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1282 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001283 UseScratchRegisterScope temps(GetVIXLAssembler());
1284 HConstant* src_cst = source.GetConstant();
1285 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001286 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001287 temp = temps.AcquireW();
1288 } else if (src_cst->IsLongConstant()) {
1289 temp = temps.AcquireX();
1290 } else if (src_cst->IsFloatConstant()) {
1291 temp = temps.AcquireS();
1292 } else {
1293 DCHECK(src_cst->IsDoubleConstant());
1294 temp = temps.AcquireD();
1295 }
1296 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001297 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001298 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001300 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001301 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001302 // There is generally less pressure on FP registers.
1303 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001304 __ Ldr(temp, StackOperandFrom(source));
1305 __ Str(temp, StackOperandFrom(destination));
1306 }
1307 }
1308}
1309
1310void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001311 CPURegister dst,
1312 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001313 switch (type) {
1314 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001315 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316 break;
1317 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001319 break;
1320 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001321 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001322 break;
1323 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001324 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001325 break;
1326 case Primitive::kPrimInt:
1327 case Primitive::kPrimNot:
1328 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001329 case Primitive::kPrimFloat:
1330 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001331 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001332 __ Ldr(dst, src);
1333 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001334 case Primitive::kPrimVoid:
1335 LOG(FATAL) << "Unreachable type " << type;
1336 }
1337}
1338
Calin Juravle77520bc2015-01-12 18:45:46 +00001339void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001340 CPURegister dst,
1341 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001342 MacroAssembler* masm = GetVIXLAssembler();
1343 BlockPoolsScope block_pools(masm);
1344 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001346 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001347
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001348 DCHECK(!src.IsPreIndex());
1349 DCHECK(!src.IsPostIndex());
1350
1351 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001352 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001353 MemOperand base = MemOperand(temp_base);
1354 switch (type) {
1355 case Primitive::kPrimBoolean:
1356 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001357 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 break;
1359 case Primitive::kPrimByte:
1360 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001361 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001362 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1363 break;
1364 case Primitive::kPrimChar:
1365 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001366 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001367 break;
1368 case Primitive::kPrimShort:
1369 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001370 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001371 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1372 break;
1373 case Primitive::kPrimInt:
1374 case Primitive::kPrimNot:
1375 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001376 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001377 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001378 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001379 break;
1380 case Primitive::kPrimFloat:
1381 case Primitive::kPrimDouble: {
1382 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001383 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384
1385 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1386 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001387 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001388 __ Fmov(FPRegister(dst), temp);
1389 break;
1390 }
1391 case Primitive::kPrimVoid:
1392 LOG(FATAL) << "Unreachable type " << type;
1393 }
1394}
1395
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001396void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001397 CPURegister src,
1398 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001399 switch (type) {
1400 case Primitive::kPrimBoolean:
1401 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001403 break;
1404 case Primitive::kPrimChar:
1405 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001407 break;
1408 case Primitive::kPrimInt:
1409 case Primitive::kPrimNot:
1410 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001411 case Primitive::kPrimFloat:
1412 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001413 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001414 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001415 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001416 case Primitive::kPrimVoid:
1417 LOG(FATAL) << "Unreachable type " << type;
1418 }
1419}
1420
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1422 CPURegister src,
1423 const MemOperand& dst) {
1424 UseScratchRegisterScope temps(GetVIXLAssembler());
1425 Register temp_base = temps.AcquireX();
1426
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001427 DCHECK(!dst.IsPreIndex());
1428 DCHECK(!dst.IsPostIndex());
1429
1430 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001431 Operand op = OperandFromMemOperand(dst);
1432 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001433 MemOperand base = MemOperand(temp_base);
1434 switch (type) {
1435 case Primitive::kPrimBoolean:
1436 case Primitive::kPrimByte:
1437 __ Stlrb(Register(src), base);
1438 break;
1439 case Primitive::kPrimChar:
1440 case Primitive::kPrimShort:
1441 __ Stlrh(Register(src), base);
1442 break;
1443 case Primitive::kPrimInt:
1444 case Primitive::kPrimNot:
1445 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001446 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001447 __ Stlr(Register(src), base);
1448 break;
1449 case Primitive::kPrimFloat:
1450 case Primitive::kPrimDouble: {
1451 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001452 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001453
1454 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1455 __ Fmov(temp, FPRegister(src));
1456 __ Stlr(temp, base);
1457 break;
1458 }
1459 case Primitive::kPrimVoid:
1460 LOG(FATAL) << "Unreachable type " << type;
1461 }
1462}
1463
Calin Juravle175dc732015-08-25 15:42:32 +01001464void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1465 HInstruction* instruction,
1466 uint32_t dex_pc,
1467 SlowPathCode* slow_path) {
1468 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1469 instruction,
1470 dex_pc,
1471 slow_path);
1472}
1473
Alexandre Rames67555f72014-11-18 10:55:16 +00001474void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1475 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001476 uint32_t dex_pc,
1477 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001478 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001479 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001480 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1481 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001482 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001483}
1484
1485void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1486 vixl::Register class_reg) {
1487 UseScratchRegisterScope temps(GetVIXLAssembler());
1488 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001489 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001490 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001491
Serban Constantinescu02164b32014-11-13 14:05:07 +00001492 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001493 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001494 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1495 __ Add(temp, class_reg, status_offset);
1496 __ Ldar(temp, HeapOperand(temp));
1497 __ Cmp(temp, mirror::Class::kStatusInitialized);
1498 __ B(lt, slow_path->GetEntryLabel());
1499 } else {
1500 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1501 __ Cmp(temp, mirror::Class::kStatusInitialized);
1502 __ B(lt, slow_path->GetEntryLabel());
1503 __ Dmb(InnerShareable, BarrierReads);
1504 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001505 __ Bind(slow_path->GetExitLabel());
1506}
Alexandre Rames5319def2014-10-23 10:03:10 +01001507
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001508void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1509 BarrierType type = BarrierAll;
1510
1511 switch (kind) {
1512 case MemBarrierKind::kAnyAny:
1513 case MemBarrierKind::kAnyStore: {
1514 type = BarrierAll;
1515 break;
1516 }
1517 case MemBarrierKind::kLoadAny: {
1518 type = BarrierReads;
1519 break;
1520 }
1521 case MemBarrierKind::kStoreStore: {
1522 type = BarrierWrites;
1523 break;
1524 }
1525 default:
1526 LOG(FATAL) << "Unexpected memory barrier " << kind;
1527 }
1528 __ Dmb(InnerShareable, type);
1529}
1530
Serban Constantinescu02164b32014-11-13 14:05:07 +00001531void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1532 HBasicBlock* successor) {
1533 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001534 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1535 if (slow_path == nullptr) {
1536 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1537 instruction->SetSlowPath(slow_path);
1538 codegen_->AddSlowPath(slow_path);
1539 if (successor != nullptr) {
1540 DCHECK(successor->IsLoopHeader());
1541 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1542 }
1543 } else {
1544 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1545 }
1546
Serban Constantinescu02164b32014-11-13 14:05:07 +00001547 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1548 Register temp = temps.AcquireW();
1549
1550 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1551 if (successor == nullptr) {
1552 __ Cbnz(temp, slow_path->GetEntryLabel());
1553 __ Bind(slow_path->GetReturnLabel());
1554 } else {
1555 __ Cbz(temp, codegen_->GetLabelOf(successor));
1556 __ B(slow_path->GetEntryLabel());
1557 // slow_path will return to GetLabelOf(successor).
1558 }
1559}
1560
Alexandre Rames5319def2014-10-23 10:03:10 +01001561InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1562 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001563 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001564 assembler_(codegen->GetAssembler()),
1565 codegen_(codegen) {}
1566
1567#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001568 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001569
1570#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1571
1572enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001573 // Using a base helps identify when we hit such breakpoints.
1574 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001575#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1576 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1577#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1578};
1579
1580#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001581 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001582 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1583 } \
1584 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1586 locations->SetOut(Location::Any()); \
1587 }
1588 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1589#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1590
1591#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001592#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001593
Alexandre Rames67555f72014-11-18 10:55:16 +00001594void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001595 DCHECK_EQ(instr->InputCount(), 2U);
1596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1597 Primitive::Type type = instr->GetResultType();
1598 switch (type) {
1599 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001600 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001602 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001603 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001604 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001605
1606 case Primitive::kPrimFloat:
1607 case Primitive::kPrimDouble:
1608 locations->SetInAt(0, Location::RequiresFpuRegister());
1609 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001610 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001611 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001612
Alexandre Rames5319def2014-10-23 10:03:10 +01001613 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001614 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001615 }
1616}
1617
Alexandre Rames09a99962015-04-15 11:47:56 +01001618void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001619 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1620
1621 bool object_field_get_with_read_barrier =
1622 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001623 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001624 new (GetGraph()->GetArena()) LocationSummary(instruction,
1625 object_field_get_with_read_barrier ?
1626 LocationSummary::kCallOnSlowPath :
1627 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001628 locations->SetInAt(0, Location::RequiresRegister());
1629 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1630 locations->SetOut(Location::RequiresFpuRegister());
1631 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001632 // The output overlaps for an object field get when read barriers
1633 // are enabled: we do not want the load to overwrite the object's
1634 // location, as we need it to emit the read barrier.
1635 locations->SetOut(
1636 Location::RequiresRegister(),
1637 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001638 }
1639}
1640
1641void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1642 const FieldInfo& field_info) {
1643 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001644 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001645 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001646
1647 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1648 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1649
1650 if (field_info.IsVolatile()) {
1651 if (use_acquire_release) {
1652 // NB: LoadAcquire will record the pc info if needed.
1653 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1654 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001655 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001656 codegen_->MaybeRecordImplicitNullCheck(instruction);
1657 // For IRIW sequential consistency kLoadAny is not sufficient.
1658 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1659 }
1660 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001661 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001662 codegen_->MaybeRecordImplicitNullCheck(instruction);
1663 }
Roland Levillain4d027112015-07-01 15:41:14 +01001664
1665 if (field_type == Primitive::kPrimNot) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001666 LocationSummary* locations = instruction->GetLocations();
1667 Location base = locations->InAt(0);
1668 Location out = locations->Out();
1669 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
1670 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01001671 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001672}
1673
1674void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1675 LocationSummary* locations =
1676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1677 locations->SetInAt(0, Location::RequiresRegister());
1678 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1679 locations->SetInAt(1, Location::RequiresFpuRegister());
1680 } else {
1681 locations->SetInAt(1, Location::RequiresRegister());
1682 }
1683}
1684
1685void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001686 const FieldInfo& field_info,
1687 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001688 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001689 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001690
1691 Register obj = InputRegisterAt(instruction, 0);
1692 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001693 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001694 Offset offset = field_info.GetFieldOffset();
1695 Primitive::Type field_type = field_info.GetFieldType();
1696 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1697
Roland Levillain4d027112015-07-01 15:41:14 +01001698 {
1699 // We use a block to end the scratch scope before the write barrier, thus
1700 // freeing the temporary registers so they can be used in `MarkGCCard`.
1701 UseScratchRegisterScope temps(GetVIXLAssembler());
1702
1703 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1704 DCHECK(value.IsW());
1705 Register temp = temps.AcquireW();
1706 __ Mov(temp, value.W());
1707 GetAssembler()->PoisonHeapReference(temp.W());
1708 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001709 }
Roland Levillain4d027112015-07-01 15:41:14 +01001710
1711 if (field_info.IsVolatile()) {
1712 if (use_acquire_release) {
1713 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1714 codegen_->MaybeRecordImplicitNullCheck(instruction);
1715 } else {
1716 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1717 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1718 codegen_->MaybeRecordImplicitNullCheck(instruction);
1719 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1720 }
1721 } else {
1722 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1723 codegen_->MaybeRecordImplicitNullCheck(instruction);
1724 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001725 }
1726
1727 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001728 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001729 }
1730}
1731
Alexandre Rames67555f72014-11-18 10:55:16 +00001732void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001733 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001734
1735 switch (type) {
1736 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001737 case Primitive::kPrimLong: {
1738 Register dst = OutputRegister(instr);
1739 Register lhs = InputRegisterAt(instr, 0);
1740 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001741 if (instr->IsAdd()) {
1742 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001743 } else if (instr->IsAnd()) {
1744 __ And(dst, lhs, rhs);
1745 } else if (instr->IsOr()) {
1746 __ Orr(dst, lhs, rhs);
1747 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001748 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001749 } else if (instr->IsRor()) {
1750 if (rhs.IsImmediate()) {
1751 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1752 __ Ror(dst, lhs, shift);
1753 } else {
1754 // Ensure shift distance is in the same size register as the result. If
1755 // we are rotating a long and the shift comes in a w register originally,
1756 // we don't need to sxtw for use as an x since the shift distances are
1757 // all & reg_bits - 1.
1758 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1759 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001760 } else {
1761 DCHECK(instr->IsXor());
1762 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001763 }
1764 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001765 }
1766 case Primitive::kPrimFloat:
1767 case Primitive::kPrimDouble: {
1768 FPRegister dst = OutputFPRegister(instr);
1769 FPRegister lhs = InputFPRegisterAt(instr, 0);
1770 FPRegister rhs = InputFPRegisterAt(instr, 1);
1771 if (instr->IsAdd()) {
1772 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001773 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001774 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001775 } else {
1776 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001777 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001778 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001779 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001780 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001781 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001782 }
1783}
1784
Serban Constantinescu02164b32014-11-13 14:05:07 +00001785void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1786 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1787
1788 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1789 Primitive::Type type = instr->GetResultType();
1790 switch (type) {
1791 case Primitive::kPrimInt:
1792 case Primitive::kPrimLong: {
1793 locations->SetInAt(0, Location::RequiresRegister());
1794 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1795 locations->SetOut(Location::RequiresRegister());
1796 break;
1797 }
1798 default:
1799 LOG(FATAL) << "Unexpected shift type " << type;
1800 }
1801}
1802
1803void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1804 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1805
1806 Primitive::Type type = instr->GetType();
1807 switch (type) {
1808 case Primitive::kPrimInt:
1809 case Primitive::kPrimLong: {
1810 Register dst = OutputRegister(instr);
1811 Register lhs = InputRegisterAt(instr, 0);
1812 Operand rhs = InputOperandAt(instr, 1);
1813 if (rhs.IsImmediate()) {
1814 uint32_t shift_value = (type == Primitive::kPrimInt)
1815 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1816 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1817 if (instr->IsShl()) {
1818 __ Lsl(dst, lhs, shift_value);
1819 } else if (instr->IsShr()) {
1820 __ Asr(dst, lhs, shift_value);
1821 } else {
1822 __ Lsr(dst, lhs, shift_value);
1823 }
1824 } else {
1825 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1826
1827 if (instr->IsShl()) {
1828 __ Lsl(dst, lhs, rhs_reg);
1829 } else if (instr->IsShr()) {
1830 __ Asr(dst, lhs, rhs_reg);
1831 } else {
1832 __ Lsr(dst, lhs, rhs_reg);
1833 }
1834 }
1835 break;
1836 }
1837 default:
1838 LOG(FATAL) << "Unexpected shift operation type " << type;
1839 }
1840}
1841
Alexandre Rames5319def2014-10-23 10:03:10 +01001842void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001843 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001844}
1845
1846void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001847 HandleBinaryOp(instruction);
1848}
1849
1850void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1851 HandleBinaryOp(instruction);
1852}
1853
1854void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1855 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001856}
1857
Alexandre Rames8626b742015-11-25 16:28:08 +00001858void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1859 HArm64DataProcWithShifterOp* instruction) {
1860 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1861 instruction->GetType() == Primitive::kPrimLong);
1862 LocationSummary* locations =
1863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1864 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1865 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1866 } else {
1867 locations->SetInAt(0, Location::RequiresRegister());
1868 }
1869 locations->SetInAt(1, Location::RequiresRegister());
1870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1871}
1872
1873void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1874 HArm64DataProcWithShifterOp* instruction) {
1875 Primitive::Type type = instruction->GetType();
1876 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1877 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1878 Register out = OutputRegister(instruction);
1879 Register left;
1880 if (kind != HInstruction::kNeg) {
1881 left = InputRegisterAt(instruction, 0);
1882 }
1883 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1884 // shifter operand operation, the IR generating `right_reg` (input to the type
1885 // conversion) can have a different type from the current instruction's type,
1886 // so we manually indicate the type.
1887 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
1888 int64_t shift_amount = (type == Primitive::kPrimInt)
1889 ? static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxIntShiftValue)
1890 : static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxLongShiftValue);
1891
1892 Operand right_operand(0);
1893
1894 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1895 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1896 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1897 } else {
1898 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1899 }
1900
1901 // Logical binary operations do not support extension operations in the
1902 // operand. Note that VIXL would still manage if it was passed by generating
1903 // the extension as a separate instruction.
1904 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1905 DCHECK(!right_operand.IsExtendedRegister() ||
1906 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1907 kind != HInstruction::kNeg));
1908 switch (kind) {
1909 case HInstruction::kAdd:
1910 __ Add(out, left, right_operand);
1911 break;
1912 case HInstruction::kAnd:
1913 __ And(out, left, right_operand);
1914 break;
1915 case HInstruction::kNeg:
1916 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1917 __ Neg(out, right_operand);
1918 break;
1919 case HInstruction::kOr:
1920 __ Orr(out, left, right_operand);
1921 break;
1922 case HInstruction::kSub:
1923 __ Sub(out, left, right_operand);
1924 break;
1925 case HInstruction::kXor:
1926 __ Eor(out, left, right_operand);
1927 break;
1928 default:
1929 LOG(FATAL) << "Unexpected operation kind: " << kind;
1930 UNREACHABLE();
1931 }
1932}
1933
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001934void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001935 // The read barrier instrumentation does not support the
1936 // HArm64IntermediateAddress instruction yet.
1937 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001938 LocationSummary* locations =
1939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1940 locations->SetInAt(0, Location::RequiresRegister());
1941 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1942 locations->SetOut(Location::RequiresRegister());
1943}
1944
1945void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1946 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001947 // The read barrier instrumentation does not support the
1948 // HArm64IntermediateAddress instruction yet.
1949 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001950 __ Add(OutputRegister(instruction),
1951 InputRegisterAt(instruction, 0),
1952 Operand(InputOperandAt(instruction, 1)));
1953}
1954
Alexandre Rames418318f2015-11-20 15:55:47 +00001955void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1956 LocationSummary* locations =
1957 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1958 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1959 Location::RequiresRegister());
1960 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1961 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1963}
1964
1965void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1966 Register res = OutputRegister(instr);
1967 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
1968 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
1969 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
1970
1971 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1972 // This fixup should be carried out for all multiply-accumulate instructions:
1973 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1974 if (instr->GetType() == Primitive::kPrimLong &&
1975 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1976 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
1977 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
1978 if (prev->IsLoadOrStore()) {
1979 // Make sure we emit only exactly one nop.
1980 vixl::CodeBufferCheckScope scope(masm,
1981 vixl::kInstructionSize,
1982 vixl::CodeBufferCheckScope::kCheck,
1983 vixl::CodeBufferCheckScope::kExactSize);
1984 __ nop();
1985 }
1986 }
1987
1988 if (instr->GetOpKind() == HInstruction::kAdd) {
1989 __ Madd(res, mul_left, mul_right, accumulator);
1990 } else {
1991 DCHECK(instr->GetOpKind() == HInstruction::kSub);
1992 __ Msub(res, mul_left, mul_right, accumulator);
1993 }
1994}
1995
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001996void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001997 bool object_array_get_with_read_barrier =
1998 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001999 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002000 new (GetGraph()->GetArena()) LocationSummary(instruction,
2001 object_array_get_with_read_barrier ?
2002 LocationSummary::kCallOnSlowPath :
2003 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002004 locations->SetInAt(0, Location::RequiresRegister());
2005 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002006 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2007 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2008 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002009 // The output overlaps in the case of an object array get with
2010 // read barriers enabled: we do not want the move to overwrite the
2011 // array's location, as we need it to emit the read barrier.
2012 locations->SetOut(
2013 Location::RequiresRegister(),
2014 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002015 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002016}
2017
2018void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002019 Primitive::Type type = instruction->GetType();
2020 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002021 LocationSummary* locations = instruction->GetLocations();
2022 Location index = locations->InAt(1);
2023 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002024 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002025 CPURegister dest = OutputCPURegister(instruction);
2026
Alexandre Ramesd921d642015-04-16 15:07:16 +01002027 MacroAssembler* masm = GetVIXLAssembler();
2028 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002029 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002030 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002031
2032 if (index.IsConstant()) {
2033 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002034 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002035 } else {
2036 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002037 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002038 // The read barrier instrumentation does not support the
2039 // HArm64IntermediateAddress instruction yet.
2040 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002041 // We do not need to compute the intermediate address from the array: the
2042 // input instruction has done it already. See the comment in
2043 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2044 if (kIsDebugBuild) {
2045 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2046 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2047 }
2048 temp = obj;
2049 } else {
2050 __ Add(temp, obj, offset);
2051 }
Alexandre Rames82000b02015-07-07 11:34:16 +01002052 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002053 }
2054
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002055 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00002056 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002057
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002058 if (type == Primitive::kPrimNot) {
2059 static_assert(
2060 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2061 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2062 Location obj_loc = locations->InAt(0);
2063 Location out = locations->Out();
2064 if (index.IsConstant()) {
2065 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
2066 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002067 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset, index);
2068 }
Roland Levillain4d027112015-07-01 15:41:14 +01002069 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002070}
2071
Alexandre Rames5319def2014-10-23 10:03:10 +01002072void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2073 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2074 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002076}
2077
2078void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002079 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002080 __ Ldr(OutputRegister(instruction),
2081 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002082 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002083}
2084
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002085void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002086 Primitive::Type value_type = instruction->GetComponentType();
2087
2088 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2089 bool object_array_set_with_read_barrier =
2090 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2092 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002093 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2094 LocationSummary::kCallOnSlowPath :
2095 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002096 locations->SetInAt(0, Location::RequiresRegister());
2097 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002098 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002099 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002100 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002101 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002102 }
2103}
2104
2105void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2106 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002107 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002108 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002109 bool needs_write_barrier =
2110 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002111
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002112 Register array = InputRegisterAt(instruction, 0);
2113 CPURegister value = InputCPURegisterAt(instruction, 2);
2114 CPURegister source = value;
2115 Location index = locations->InAt(1);
2116 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2117 MemOperand destination = HeapOperand(array);
2118 MacroAssembler* masm = GetVIXLAssembler();
2119 BlockPoolsScope block_pools(masm);
2120
2121 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002122 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002123 if (index.IsConstant()) {
2124 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2125 destination = HeapOperand(array, offset);
2126 } else {
2127 UseScratchRegisterScope temps(masm);
2128 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002129 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002130 // The read barrier instrumentation does not support the
2131 // HArm64IntermediateAddress instruction yet.
2132 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002133 // We do not need to compute the intermediate address from the array: the
2134 // input instruction has done it already. See the comment in
2135 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2136 if (kIsDebugBuild) {
2137 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2138 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2139 }
2140 temp = array;
2141 } else {
2142 __ Add(temp, array, offset);
2143 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002144 destination = HeapOperand(temp,
2145 XRegisterFrom(index),
2146 LSL,
2147 Primitive::ComponentSizeShift(value_type));
2148 }
2149 codegen_->Store(value_type, value, destination);
2150 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002151 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002152 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002153 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002154 vixl::Label done;
2155 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002156 {
2157 // We use a block to end the scratch scope before the write barrier, thus
2158 // freeing the temporary registers so they can be used in `MarkGCCard`.
2159 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002160 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002161 if (index.IsConstant()) {
2162 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002163 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002164 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002165 destination = HeapOperand(temp,
2166 XRegisterFrom(index),
2167 LSL,
2168 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002169 }
2170
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002171 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2172 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2173 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2174
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002175 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002176 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2177 codegen_->AddSlowPath(slow_path);
2178 if (instruction->GetValueCanBeNull()) {
2179 vixl::Label non_zero;
2180 __ Cbnz(Register(value), &non_zero);
2181 if (!index.IsConstant()) {
2182 __ Add(temp, array, offset);
2183 }
2184 __ Str(wzr, destination);
2185 codegen_->MaybeRecordImplicitNullCheck(instruction);
2186 __ B(&done);
2187 __ Bind(&non_zero);
2188 }
2189
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002190 if (kEmitCompilerReadBarrier) {
2191 // When read barriers are enabled, the type checking
2192 // instrumentation requires two read barriers:
2193 //
2194 // __ Mov(temp2, temp);
2195 // // /* HeapReference<Class> */ temp = temp->component_type_
2196 // __ Ldr(temp, HeapOperand(temp, component_offset));
2197 // codegen_->GenerateReadBarrier(
2198 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2199 //
2200 // // /* HeapReference<Class> */ temp2 = value->klass_
2201 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2202 // codegen_->GenerateReadBarrier(
2203 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2204 //
2205 // __ Cmp(temp, temp2);
2206 //
2207 // However, the second read barrier may trash `temp`, as it
2208 // is a temporary register, and as such would not be saved
2209 // along with live registers before calling the runtime (nor
2210 // restored afterwards). So in this case, we bail out and
2211 // delegate the work to the array set slow path.
2212 //
2213 // TODO: Extend the register allocator to support a new
2214 // "(locally) live temp" location so as to avoid always
2215 // going into the slow path when read barriers are enabled.
2216 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002217 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002218 Register temp2 = temps.AcquireSameSizeAs(array);
2219 // /* HeapReference<Class> */ temp = array->klass_
2220 __ Ldr(temp, HeapOperand(array, class_offset));
2221 codegen_->MaybeRecordImplicitNullCheck(instruction);
2222 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2223
2224 // /* HeapReference<Class> */ temp = temp->component_type_
2225 __ Ldr(temp, HeapOperand(temp, component_offset));
2226 // /* HeapReference<Class> */ temp2 = value->klass_
2227 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2228 // If heap poisoning is enabled, no need to unpoison `temp`
2229 // nor `temp2`, as we are comparing two poisoned references.
2230 __ Cmp(temp, temp2);
2231
2232 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2233 vixl::Label do_put;
2234 __ B(eq, &do_put);
2235 // If heap poisoning is enabled, the `temp` reference has
2236 // not been unpoisoned yet; unpoison it now.
2237 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2238
2239 // /* HeapReference<Class> */ temp = temp->super_class_
2240 __ Ldr(temp, HeapOperand(temp, super_offset));
2241 // If heap poisoning is enabled, no need to unpoison
2242 // `temp`, as we are comparing against null below.
2243 __ Cbnz(temp, slow_path->GetEntryLabel());
2244 __ Bind(&do_put);
2245 } else {
2246 __ B(ne, slow_path->GetEntryLabel());
2247 }
2248 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002249 }
2250 }
2251
2252 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002253 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002254 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002255 __ Mov(temp2, value.W());
2256 GetAssembler()->PoisonHeapReference(temp2);
2257 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002258 }
2259
2260 if (!index.IsConstant()) {
2261 __ Add(temp, array, offset);
2262 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002263 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002264
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002265 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002266 codegen_->MaybeRecordImplicitNullCheck(instruction);
2267 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002268 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002269
2270 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2271
2272 if (done.IsLinked()) {
2273 __ Bind(&done);
2274 }
2275
2276 if (slow_path != nullptr) {
2277 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002278 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002279 }
2280}
2281
Alexandre Rames67555f72014-11-18 10:55:16 +00002282void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002283 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2284 ? LocationSummary::kCallOnSlowPath
2285 : LocationSummary::kNoCall;
2286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002287 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002288 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002289 if (instruction->HasUses()) {
2290 locations->SetOut(Location::SameAsFirstInput());
2291 }
2292}
2293
2294void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002295 BoundsCheckSlowPathARM64* slow_path =
2296 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002297 codegen_->AddSlowPath(slow_path);
2298
2299 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2300 __ B(slow_path->GetEntryLabel(), hs);
2301}
2302
Alexandre Rames67555f72014-11-18 10:55:16 +00002303void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2304 LocationSummary* locations =
2305 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2306 locations->SetInAt(0, Location::RequiresRegister());
2307 if (check->HasUses()) {
2308 locations->SetOut(Location::SameAsFirstInput());
2309 }
2310}
2311
2312void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2313 // We assume the class is not null.
2314 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2315 check->GetLoadClass(), check, check->GetDexPc(), true);
2316 codegen_->AddSlowPath(slow_path);
2317 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2318}
2319
Roland Levillain7f63c522015-07-13 15:54:55 +00002320static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2321 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2322 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2323}
2324
Serban Constantinescu02164b32014-11-13 14:05:07 +00002325void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002326 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002327 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2328 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002329 switch (in_type) {
2330 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002331 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002332 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2334 break;
2335 }
2336 case Primitive::kPrimFloat:
2337 case Primitive::kPrimDouble: {
2338 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002339 locations->SetInAt(1,
2340 IsFloatingPointZeroConstant(compare->InputAt(1))
2341 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2342 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002343 locations->SetOut(Location::RequiresRegister());
2344 break;
2345 }
2346 default:
2347 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2348 }
2349}
2350
2351void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2352 Primitive::Type in_type = compare->InputAt(0)->GetType();
2353
2354 // 0 if: left == right
2355 // 1 if: left > right
2356 // -1 if: left < right
2357 switch (in_type) {
2358 case Primitive::kPrimLong: {
2359 Register result = OutputRegister(compare);
2360 Register left = InputRegisterAt(compare, 0);
2361 Operand right = InputOperandAt(compare, 1);
2362
2363 __ Cmp(left, right);
2364 __ Cset(result, ne);
2365 __ Cneg(result, result, lt);
2366 break;
2367 }
2368 case Primitive::kPrimFloat:
2369 case Primitive::kPrimDouble: {
2370 Register result = OutputRegister(compare);
2371 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002372 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002373 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2374 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002375 __ Fcmp(left, 0.0);
2376 } else {
2377 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2378 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002379 __ Cset(result, ne);
2380 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002381 break;
2382 }
2383 default:
2384 LOG(FATAL) << "Unimplemented compare type " << in_type;
2385 }
2386}
2387
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002388void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002390
2391 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2392 locations->SetInAt(0, Location::RequiresFpuRegister());
2393 locations->SetInAt(1,
2394 IsFloatingPointZeroConstant(instruction->InputAt(1))
2395 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2396 : Location::RequiresFpuRegister());
2397 } else {
2398 // Integer cases.
2399 locations->SetInAt(0, Location::RequiresRegister());
2400 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2401 }
2402
Alexandre Rames5319def2014-10-23 10:03:10 +01002403 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002405 }
2406}
2407
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002408void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002409 if (!instruction->NeedsMaterialization()) {
2410 return;
2411 }
2412
2413 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002414 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002415 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002416
Roland Levillain7f63c522015-07-13 15:54:55 +00002417 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2418 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2419 if (locations->InAt(1).IsConstant()) {
2420 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2421 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2422 __ Fcmp(lhs, 0.0);
2423 } else {
2424 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2425 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002426 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002427 } else {
2428 // Integer cases.
2429 Register lhs = InputRegisterAt(instruction, 0);
2430 Operand rhs = InputOperandAt(instruction, 1);
2431 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002432 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002433 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002434}
2435
2436#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2437 M(Equal) \
2438 M(NotEqual) \
2439 M(LessThan) \
2440 M(LessThanOrEqual) \
2441 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002442 M(GreaterThanOrEqual) \
2443 M(Below) \
2444 M(BelowOrEqual) \
2445 M(Above) \
2446 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002447#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002448void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2449void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002450FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002451#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002452#undef FOR_EACH_CONDITION_INSTRUCTION
2453
Zheng Xuc6667102015-05-15 16:08:45 +08002454void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2455 DCHECK(instruction->IsDiv() || instruction->IsRem());
2456
2457 LocationSummary* locations = instruction->GetLocations();
2458 Location second = locations->InAt(1);
2459 DCHECK(second.IsConstant());
2460
2461 Register out = OutputRegister(instruction);
2462 Register dividend = InputRegisterAt(instruction, 0);
2463 int64_t imm = Int64FromConstant(second.GetConstant());
2464 DCHECK(imm == 1 || imm == -1);
2465
2466 if (instruction->IsRem()) {
2467 __ Mov(out, 0);
2468 } else {
2469 if (imm == 1) {
2470 __ Mov(out, dividend);
2471 } else {
2472 __ Neg(out, dividend);
2473 }
2474 }
2475}
2476
2477void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(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());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002487 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002488 int ctz_imm = CTZ(abs_imm);
2489
2490 UseScratchRegisterScope temps(GetVIXLAssembler());
2491 Register temp = temps.AcquireSameSizeAs(out);
2492
2493 if (instruction->IsDiv()) {
2494 __ Add(temp, dividend, abs_imm - 1);
2495 __ Cmp(dividend, 0);
2496 __ Csel(out, temp, dividend, lt);
2497 if (imm > 0) {
2498 __ Asr(out, out, ctz_imm);
2499 } else {
2500 __ Neg(out, Operand(out, ASR, ctz_imm));
2501 }
2502 } else {
2503 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2504 __ Asr(temp, dividend, bits - 1);
2505 __ Lsr(temp, temp, bits - ctz_imm);
2506 __ Add(out, dividend, temp);
2507 __ And(out, out, abs_imm - 1);
2508 __ Sub(out, out, temp);
2509 }
2510}
2511
2512void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2513 DCHECK(instruction->IsDiv() || instruction->IsRem());
2514
2515 LocationSummary* locations = instruction->GetLocations();
2516 Location second = locations->InAt(1);
2517 DCHECK(second.IsConstant());
2518
2519 Register out = OutputRegister(instruction);
2520 Register dividend = InputRegisterAt(instruction, 0);
2521 int64_t imm = Int64FromConstant(second.GetConstant());
2522
2523 Primitive::Type type = instruction->GetResultType();
2524 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2525
2526 int64_t magic;
2527 int shift;
2528 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2529
2530 UseScratchRegisterScope temps(GetVIXLAssembler());
2531 Register temp = temps.AcquireSameSizeAs(out);
2532
2533 // temp = get_high(dividend * magic)
2534 __ Mov(temp, magic);
2535 if (type == Primitive::kPrimLong) {
2536 __ Smulh(temp, dividend, temp);
2537 } else {
2538 __ Smull(temp.X(), dividend, temp);
2539 __ Lsr(temp.X(), temp.X(), 32);
2540 }
2541
2542 if (imm > 0 && magic < 0) {
2543 __ Add(temp, temp, dividend);
2544 } else if (imm < 0 && magic > 0) {
2545 __ Sub(temp, temp, dividend);
2546 }
2547
2548 if (shift != 0) {
2549 __ Asr(temp, temp, shift);
2550 }
2551
2552 if (instruction->IsDiv()) {
2553 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2554 } else {
2555 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2556 // TODO: Strength reduction for msub.
2557 Register temp_imm = temps.AcquireSameSizeAs(out);
2558 __ Mov(temp_imm, imm);
2559 __ Msub(out, temp, temp_imm, dividend);
2560 }
2561}
2562
2563void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2564 DCHECK(instruction->IsDiv() || instruction->IsRem());
2565 Primitive::Type type = instruction->GetResultType();
2566 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2567
2568 LocationSummary* locations = instruction->GetLocations();
2569 Register out = OutputRegister(instruction);
2570 Location second = locations->InAt(1);
2571
2572 if (second.IsConstant()) {
2573 int64_t imm = Int64FromConstant(second.GetConstant());
2574
2575 if (imm == 0) {
2576 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2577 } else if (imm == 1 || imm == -1) {
2578 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002579 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002580 DivRemByPowerOfTwo(instruction);
2581 } else {
2582 DCHECK(imm <= -2 || imm >= 2);
2583 GenerateDivRemWithAnyConstant(instruction);
2584 }
2585 } else {
2586 Register dividend = InputRegisterAt(instruction, 0);
2587 Register divisor = InputRegisterAt(instruction, 1);
2588 if (instruction->IsDiv()) {
2589 __ Sdiv(out, dividend, divisor);
2590 } else {
2591 UseScratchRegisterScope temps(GetVIXLAssembler());
2592 Register temp = temps.AcquireSameSizeAs(out);
2593 __ Sdiv(temp, dividend, divisor);
2594 __ Msub(out, temp, divisor, dividend);
2595 }
2596 }
2597}
2598
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002599void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2600 LocationSummary* locations =
2601 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2602 switch (div->GetResultType()) {
2603 case Primitive::kPrimInt:
2604 case Primitive::kPrimLong:
2605 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002606 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2608 break;
2609
2610 case Primitive::kPrimFloat:
2611 case Primitive::kPrimDouble:
2612 locations->SetInAt(0, Location::RequiresFpuRegister());
2613 locations->SetInAt(1, Location::RequiresFpuRegister());
2614 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2615 break;
2616
2617 default:
2618 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2619 }
2620}
2621
2622void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2623 Primitive::Type type = div->GetResultType();
2624 switch (type) {
2625 case Primitive::kPrimInt:
2626 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002627 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002628 break;
2629
2630 case Primitive::kPrimFloat:
2631 case Primitive::kPrimDouble:
2632 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2633 break;
2634
2635 default:
2636 LOG(FATAL) << "Unexpected div type " << type;
2637 }
2638}
2639
Alexandre Rames67555f72014-11-18 10:55:16 +00002640void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002641 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2642 ? LocationSummary::kCallOnSlowPath
2643 : LocationSummary::kNoCall;
2644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002645 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2646 if (instruction->HasUses()) {
2647 locations->SetOut(Location::SameAsFirstInput());
2648 }
2649}
2650
2651void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2652 SlowPathCodeARM64* slow_path =
2653 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2654 codegen_->AddSlowPath(slow_path);
2655 Location value = instruction->GetLocations()->InAt(0);
2656
Alexandre Rames3e69f162014-12-10 10:36:50 +00002657 Primitive::Type type = instruction->GetType();
2658
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002659 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2660 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002661 return;
2662 }
2663
Alexandre Rames67555f72014-11-18 10:55:16 +00002664 if (value.IsConstant()) {
2665 int64_t divisor = Int64ConstantFrom(value);
2666 if (divisor == 0) {
2667 __ B(slow_path->GetEntryLabel());
2668 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002669 // A division by a non-null constant is valid. We don't need to perform
2670 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002671 }
2672 } else {
2673 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2674 }
2675}
2676
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002677void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2678 LocationSummary* locations =
2679 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2680 locations->SetOut(Location::ConstantLocation(constant));
2681}
2682
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002683void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2684 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002685 // Will be generated at use site.
2686}
2687
Alexandre Rames5319def2014-10-23 10:03:10 +01002688void LocationsBuilderARM64::VisitExit(HExit* exit) {
2689 exit->SetLocations(nullptr);
2690}
2691
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002692void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002693}
2694
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002695void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2696 LocationSummary* locations =
2697 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2698 locations->SetOut(Location::ConstantLocation(constant));
2699}
2700
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002701void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002702 // Will be generated at use site.
2703}
2704
David Brazdilfc6a86a2015-06-26 10:33:45 +00002705void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002706 DCHECK(!successor->IsExitBlock());
2707 HBasicBlock* block = got->GetBlock();
2708 HInstruction* previous = got->GetPrevious();
2709 HLoopInformation* info = block->GetLoopInformation();
2710
David Brazdil46e2a392015-03-16 17:31:52 +00002711 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002712 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2713 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2714 return;
2715 }
2716 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2717 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2718 }
2719 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002720 __ B(codegen_->GetLabelOf(successor));
2721 }
2722}
2723
David Brazdilfc6a86a2015-06-26 10:33:45 +00002724void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2725 got->SetLocations(nullptr);
2726}
2727
2728void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2729 HandleGoto(got, got->GetSuccessor());
2730}
2731
2732void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2733 try_boundary->SetLocations(nullptr);
2734}
2735
2736void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2737 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2738 if (!successor->IsExitBlock()) {
2739 HandleGoto(try_boundary, successor);
2740 }
2741}
2742
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002743void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002744 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002745 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002746 vixl::Label* false_target) {
2747 // FP branching requires both targets to be explicit. If either of the targets
2748 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2749 vixl::Label fallthrough_target;
2750 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002751
David Brazdil0debae72015-11-12 18:37:00 +00002752 if (true_target == nullptr && false_target == nullptr) {
2753 // Nothing to do. The code always falls through.
2754 return;
2755 } else if (cond->IsIntConstant()) {
2756 // Constant condition, statically compared against 1.
2757 if (cond->AsIntConstant()->IsOne()) {
2758 if (true_target != nullptr) {
2759 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002760 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002761 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002762 DCHECK(cond->AsIntConstant()->IsZero());
2763 if (false_target != nullptr) {
2764 __ B(false_target);
2765 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002766 }
David Brazdil0debae72015-11-12 18:37:00 +00002767 return;
2768 }
2769
2770 // The following code generates these patterns:
2771 // (1) true_target == nullptr && false_target != nullptr
2772 // - opposite condition true => branch to false_target
2773 // (2) true_target != nullptr && false_target == nullptr
2774 // - condition true => branch to true_target
2775 // (3) true_target != nullptr && false_target != nullptr
2776 // - condition true => branch to true_target
2777 // - branch to false_target
2778 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002779 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002780 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002781 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002782 if (true_target == nullptr) {
2783 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2784 } else {
2785 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2786 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002787 } else {
2788 // The condition instruction has not been materialized, use its inputs as
2789 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002790 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002791
David Brazdil0debae72015-11-12 18:37:00 +00002792 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002793 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002794 FPRegister lhs = InputFPRegisterAt(condition, 0);
2795 if (condition->GetLocations()->InAt(1).IsConstant()) {
2796 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2797 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2798 __ Fcmp(lhs, 0.0);
2799 } else {
2800 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2801 }
David Brazdil0debae72015-11-12 18:37:00 +00002802 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002803 IfCondition opposite_condition = condition->GetOppositeCondition();
2804 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002805 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002806 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002807 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002808 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002809 // Integer cases.
2810 Register lhs = InputRegisterAt(condition, 0);
2811 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002812
2813 Condition arm64_cond;
2814 vixl::Label* non_fallthrough_target;
2815 if (true_target == nullptr) {
2816 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2817 non_fallthrough_target = false_target;
2818 } else {
2819 arm64_cond = ARM64Condition(condition->GetCondition());
2820 non_fallthrough_target = true_target;
2821 }
2822
Aart Bik086d27e2016-01-20 17:02:00 -08002823 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2824 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002825 switch (arm64_cond) {
2826 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002827 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002828 break;
2829 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002830 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002831 break;
2832 case lt:
2833 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002834 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002835 break;
2836 case ge:
2837 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002838 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002839 break;
2840 default:
2841 // Without the `static_cast` the compiler throws an error for
2842 // `-Werror=sign-promo`.
2843 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2844 }
2845 } else {
2846 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002847 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002848 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002849 }
2850 }
David Brazdil0debae72015-11-12 18:37:00 +00002851
2852 // If neither branch falls through (case 3), the conditional branch to `true_target`
2853 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2854 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002855 __ B(false_target);
2856 }
David Brazdil0debae72015-11-12 18:37:00 +00002857
2858 if (fallthrough_target.IsLinked()) {
2859 __ Bind(&fallthrough_target);
2860 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002861}
2862
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002863void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002865 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002866 locations->SetInAt(0, Location::RequiresRegister());
2867 }
2868}
2869
2870void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002871 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2872 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2873 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2874 nullptr : codegen_->GetLabelOf(true_successor);
2875 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2876 nullptr : codegen_->GetLabelOf(false_successor);
2877 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002878}
2879
2880void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2881 LocationSummary* locations = new (GetGraph()->GetArena())
2882 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002883 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002884 locations->SetInAt(0, Location::RequiresRegister());
2885 }
2886}
2887
2888void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002889 SlowPathCodeARM64* slow_path =
2890 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002891 GenerateTestAndBranch(deoptimize,
2892 /* condition_input_index */ 0,
2893 slow_path->GetEntryLabel(),
2894 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002895}
2896
David Srbecky0cf44932015-12-09 14:09:59 +00002897void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2898 new (GetGraph()->GetArena()) LocationSummary(info);
2899}
2900
2901void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002902 if (codegen_->HasStackMapAtCurrentPc()) {
2903 // Ensure that we do not collide with the stack map of the previous instruction.
2904 __ Nop();
2905 }
David Srbecky0cf44932015-12-09 14:09:59 +00002906 codegen_->RecordPcInfo(info, info->GetDexPc());
2907}
2908
Alexandre Rames5319def2014-10-23 10:03:10 +01002909void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002910 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002911}
2912
2913void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002914 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002915}
2916
2917void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002918 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002919}
2920
2921void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002922 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002923}
2924
Alexandre Rames67555f72014-11-18 10:55:16 +00002925void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002926 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002927 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2928 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002929 case TypeCheckKind::kExactCheck:
2930 case TypeCheckKind::kAbstractClassCheck:
2931 case TypeCheckKind::kClassHierarchyCheck:
2932 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002933 call_kind =
2934 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002935 break;
2936 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002937 case TypeCheckKind::kUnresolvedCheck:
2938 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002939 call_kind = LocationSummary::kCallOnSlowPath;
2940 break;
2941 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002942
Alexandre Rames67555f72014-11-18 10:55:16 +00002943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002944 locations->SetInAt(0, Location::RequiresRegister());
2945 locations->SetInAt(1, Location::RequiresRegister());
2946 // The "out" register is used as a temporary, so it overlaps with the inputs.
2947 // Note that TypeCheckSlowPathARM64 uses this register too.
2948 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2949 // When read barriers are enabled, we need a temporary register for
2950 // some cases.
2951 if (kEmitCompilerReadBarrier &&
2952 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2953 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2954 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2955 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002956 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002957}
2958
2959void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2960 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002961 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002962 Register obj = InputRegisterAt(instruction, 0);
2963 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002964 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00002965 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002966 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2967 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2968 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2969 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002970
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002971 vixl::Label done, zero;
2972 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002973
2974 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002975 // Avoid null check if we know `obj` is not null.
2976 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002977 __ Cbz(obj, &zero);
2978 }
2979
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002980 // /* HeapReference<Class> */ out = obj->klass_
2981 __ Ldr(out, HeapOperand(obj.W(), class_offset));
2982 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002983
2984 switch (instruction->GetTypeCheckKind()) {
2985 case TypeCheckKind::kExactCheck: {
2986 __ Cmp(out, cls);
2987 __ Cset(out, eq);
2988 if (zero.IsLinked()) {
2989 __ B(&done);
2990 }
2991 break;
2992 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002993
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002994 case TypeCheckKind::kAbstractClassCheck: {
2995 // If the class is abstract, we eagerly fetch the super class of the
2996 // object to avoid doing a comparison we know will fail.
2997 vixl::Label loop, success;
2998 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002999 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3000 if (kEmitCompilerReadBarrier) {
3001 // Save the value of `out` into `temp` before overwriting it
3002 // in the following move operation, as we will need it for the
3003 // read barrier below.
3004 Register temp = WRegisterFrom(temp_loc);
3005 __ Mov(temp, out);
3006 }
3007 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003008 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003009 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003010 // If `out` is null, we use it for the result, and jump to `done`.
3011 __ Cbz(out, &done);
3012 __ Cmp(out, cls);
3013 __ B(ne, &loop);
3014 __ Mov(out, 1);
3015 if (zero.IsLinked()) {
3016 __ B(&done);
3017 }
3018 break;
3019 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003020
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003021 case TypeCheckKind::kClassHierarchyCheck: {
3022 // Walk over the class hierarchy to find a match.
3023 vixl::Label loop, success;
3024 __ Bind(&loop);
3025 __ Cmp(out, cls);
3026 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003027 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3028 if (kEmitCompilerReadBarrier) {
3029 // Save the value of `out` into `temp` before overwriting it
3030 // in the following move operation, as we will need it for the
3031 // read barrier below.
3032 Register temp = WRegisterFrom(temp_loc);
3033 __ Mov(temp, out);
3034 }
3035 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003036 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003037 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003038 __ Cbnz(out, &loop);
3039 // If `out` is null, we use it for the result, and jump to `done`.
3040 __ B(&done);
3041 __ Bind(&success);
3042 __ Mov(out, 1);
3043 if (zero.IsLinked()) {
3044 __ B(&done);
3045 }
3046 break;
3047 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003048
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003049 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003050 // Do an exact check.
3051 vixl::Label exact_check;
3052 __ Cmp(out, cls);
3053 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003054 // Otherwise, we need to check that the object's class is a non-primitive array.
3055 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3056 if (kEmitCompilerReadBarrier) {
3057 // Save the value of `out` into `temp` before overwriting it
3058 // in the following move operation, as we will need it for the
3059 // read barrier below.
3060 Register temp = WRegisterFrom(temp_loc);
3061 __ Mov(temp, out);
3062 }
3063 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003064 __ Ldr(out, HeapOperand(out, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003065 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003066 // If `out` is null, we use it for the result, and jump to `done`.
3067 __ Cbz(out, &done);
3068 __ Ldrh(out, HeapOperand(out, primitive_offset));
3069 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3070 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003071 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003072 __ Mov(out, 1);
3073 __ B(&done);
3074 break;
3075 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003076
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003077 case TypeCheckKind::kArrayCheck: {
3078 __ Cmp(out, cls);
3079 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003080 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3081 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003082 codegen_->AddSlowPath(slow_path);
3083 __ B(ne, slow_path->GetEntryLabel());
3084 __ Mov(out, 1);
3085 if (zero.IsLinked()) {
3086 __ B(&done);
3087 }
3088 break;
3089 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003090
Calin Juravle98893e12015-10-02 21:05:03 +01003091 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003092 case TypeCheckKind::kInterfaceCheck: {
3093 // Note that we indeed only call on slow path, but we always go
3094 // into the slow path for the unresolved and interface check
3095 // cases.
3096 //
3097 // We cannot directly call the InstanceofNonTrivial runtime
3098 // entry point without resorting to a type checking slow path
3099 // here (i.e. by calling InvokeRuntime directly), as it would
3100 // require to assign fixed registers for the inputs of this
3101 // HInstanceOf instruction (following the runtime calling
3102 // convention), which might be cluttered by the potential first
3103 // read barrier emission at the beginning of this method.
3104 DCHECK(locations->OnlyCallsOnSlowPath());
3105 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3106 /* is_fatal */ false);
3107 codegen_->AddSlowPath(slow_path);
3108 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003109 if (zero.IsLinked()) {
3110 __ B(&done);
3111 }
3112 break;
3113 }
3114 }
3115
3116 if (zero.IsLinked()) {
3117 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003118 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003119 }
3120
3121 if (done.IsLinked()) {
3122 __ Bind(&done);
3123 }
3124
3125 if (slow_path != nullptr) {
3126 __ Bind(slow_path->GetExitLabel());
3127 }
3128}
3129
3130void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3131 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3132 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3133
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003134 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3135 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003136 case TypeCheckKind::kExactCheck:
3137 case TypeCheckKind::kAbstractClassCheck:
3138 case TypeCheckKind::kClassHierarchyCheck:
3139 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003140 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3141 LocationSummary::kCallOnSlowPath :
3142 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003143 break;
3144 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003145 case TypeCheckKind::kUnresolvedCheck:
3146 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003147 call_kind = LocationSummary::kCallOnSlowPath;
3148 break;
3149 }
3150
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3152 locations->SetInAt(0, Location::RequiresRegister());
3153 locations->SetInAt(1, Location::RequiresRegister());
3154 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3155 locations->AddTemp(Location::RequiresRegister());
3156 locations->AddTemp(Location::RequiresRegister());
3157 // When read barriers are enabled, we need an additional temporary
3158 // register for some cases.
3159 if (kEmitCompilerReadBarrier &&
3160 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3161 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3162 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3163 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003164 }
3165}
3166
3167void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
3168 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003169 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003170 Register obj = InputRegisterAt(instruction, 0);
3171 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003172 Location temp_loc = locations->GetTemp(0);
3173 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003174 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3175 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3176 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3177 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003179 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3180 bool is_type_check_slow_path_fatal =
3181 (type_check_kind == TypeCheckKind::kExactCheck ||
3182 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3183 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3184 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3185 !instruction->CanThrowIntoCatchBlock();
3186 SlowPathCodeARM64* type_check_slow_path =
3187 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3188 is_type_check_slow_path_fatal);
3189 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003190
3191 vixl::Label done;
3192 // Avoid null check if we know obj is not null.
3193 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003194 __ Cbz(obj, &done);
3195 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003196
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003197 // /* HeapReference<Class> */ temp = obj->klass_
3198 __ Ldr(temp, HeapOperand(obj, class_offset));
3199 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003200
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003201 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003202 case TypeCheckKind::kExactCheck:
3203 case TypeCheckKind::kArrayCheck: {
3204 __ Cmp(temp, cls);
3205 // Jump to slow path for throwing the exception or doing a
3206 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003207 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003208 break;
3209 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003211 case TypeCheckKind::kAbstractClassCheck: {
3212 // If the class is abstract, we eagerly fetch the super class of the
3213 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003214 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003215 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003216 Location temp2_loc =
3217 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3218 if (kEmitCompilerReadBarrier) {
3219 // Save the value of `temp` into `temp2` before overwriting it
3220 // in the following move operation, as we will need it for the
3221 // read barrier below.
3222 Register temp2 = WRegisterFrom(temp2_loc);
3223 __ Mov(temp2, temp);
3224 }
3225 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003226 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003227 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3228
3229 // If the class reference currently in `temp` is not null, jump
3230 // to the `compare_classes` label to compare it with the checked
3231 // class.
3232 __ Cbnz(temp, &compare_classes);
3233 // Otherwise, jump to the slow path to throw the exception.
3234 //
3235 // But before, move back the object's class into `temp` before
3236 // going into the slow path, as it has been overwritten in the
3237 // meantime.
3238 // /* HeapReference<Class> */ temp = obj->klass_
3239 __ Ldr(temp, HeapOperand(obj, class_offset));
3240 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3241 __ B(type_check_slow_path->GetEntryLabel());
3242
3243 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003244 __ Cmp(temp, cls);
3245 __ B(ne, &loop);
3246 break;
3247 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003248
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003249 case TypeCheckKind::kClassHierarchyCheck: {
3250 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003251 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003252 __ Bind(&loop);
3253 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003254 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003255
3256 Location temp2_loc =
3257 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3258 if (kEmitCompilerReadBarrier) {
3259 // Save the value of `temp` into `temp2` before overwriting it
3260 // in the following move operation, as we will need it for the
3261 // read barrier below.
3262 Register temp2 = WRegisterFrom(temp2_loc);
3263 __ Mov(temp2, temp);
3264 }
3265 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003266 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003267 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3268
3269 // If the class reference currently in `temp` is not null, jump
3270 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003271 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003272 // Otherwise, jump to the slow path to throw the exception.
3273 //
3274 // But before, move back the object's class into `temp` before
3275 // going into the slow path, as it has been overwritten in the
3276 // meantime.
3277 // /* HeapReference<Class> */ temp = obj->klass_
3278 __ Ldr(temp, HeapOperand(obj, class_offset));
3279 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3280 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003281 break;
3282 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003283
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003284 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003285 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003286 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003287 __ Cmp(temp, cls);
3288 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003289
3290 // Otherwise, we need to check that the object's class is a non-primitive array.
3291 Location temp2_loc =
3292 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3293 if (kEmitCompilerReadBarrier) {
3294 // Save the value of `temp` into `temp2` before overwriting it
3295 // in the following move operation, as we will need it for the
3296 // read barrier below.
3297 Register temp2 = WRegisterFrom(temp2_loc);
3298 __ Mov(temp2, temp);
3299 }
3300 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003301 __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003302 codegen_->MaybeGenerateReadBarrier(
3303 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
3304
3305 // If the component type is not null (i.e. the object is indeed
3306 // an array), jump to label `check_non_primitive_component_type`
3307 // to further check that this component type is not a primitive
3308 // type.
3309 __ Cbnz(temp, &check_non_primitive_component_type);
3310 // Otherwise, jump to the slow path to throw the exception.
3311 //
3312 // But before, move back the object's class into `temp` before
3313 // going into the slow path, as it has been overwritten in the
3314 // meantime.
3315 // /* HeapReference<Class> */ temp = obj->klass_
3316 __ Ldr(temp, HeapOperand(obj, class_offset));
3317 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3318 __ B(type_check_slow_path->GetEntryLabel());
3319
3320 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003321 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3322 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003323 __ Cbz(temp, &done);
3324 // Same comment as above regarding `temp` and the slow path.
3325 // /* HeapReference<Class> */ temp = obj->klass_
3326 __ Ldr(temp, HeapOperand(obj, class_offset));
3327 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3328 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003329 break;
3330 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331
Calin Juravle98893e12015-10-02 21:05:03 +01003332 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003333 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003334 // We always go into the type check slow path for the unresolved
3335 // and interface check cases.
3336 //
3337 // We cannot directly call the CheckCast runtime entry point
3338 // without resorting to a type checking slow path here (i.e. by
3339 // calling InvokeRuntime directly), as it would require to
3340 // assign fixed registers for the inputs of this HInstanceOf
3341 // instruction (following the runtime calling convention), which
3342 // might be cluttered by the potential first read barrier
3343 // emission at the beginning of this method.
3344 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003345 break;
3346 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003347 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003348
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003349 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003350}
3351
Alexandre Rames5319def2014-10-23 10:03:10 +01003352void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3353 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3354 locations->SetOut(Location::ConstantLocation(constant));
3355}
3356
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003357void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003358 // Will be generated at use site.
3359}
3360
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003361void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3363 locations->SetOut(Location::ConstantLocation(constant));
3364}
3365
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003366void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003367 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003368}
3369
Calin Juravle175dc732015-08-25 15:42:32 +01003370void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3371 // The trampoline uses the same calling convention as dex calling conventions,
3372 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3373 // the method_idx.
3374 HandleInvoke(invoke);
3375}
3376
3377void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3378 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3379}
3380
Alexandre Rames5319def2014-10-23 10:03:10 +01003381void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003382 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003383 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003384}
3385
Alexandre Rames67555f72014-11-18 10:55:16 +00003386void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3387 HandleInvoke(invoke);
3388}
3389
3390void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3391 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392 LocationSummary* locations = invoke->GetLocations();
3393 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003394 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3395 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003397 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003398 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003399
3400 // The register ip1 is required to be used for the hidden argument in
3401 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003402 MacroAssembler* masm = GetVIXLAssembler();
3403 UseScratchRegisterScope scratch_scope(masm);
3404 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003405 scratch_scope.Exclude(ip1);
3406 __ Mov(ip1, invoke->GetDexMethodIndex());
3407
Alexandre Rames67555f72014-11-18 10:55:16 +00003408 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003409 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003411 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003412 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003414 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003415 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003416 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003417 // Instead of simply (possibly) unpoisoning `temp` here, we should
3418 // emit a read barrier for the previous class reference load.
3419 // However this is not required in practice, as this is an
3420 // intermediate/temporary reference and because the current
3421 // concurrent copying collector keeps the from-space memory
3422 // intact/accessible until the end of the marking phase (the
3423 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003424 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003425 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003426 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003427 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003428 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003429 // lr();
3430 __ Blr(lr);
3431 DCHECK(!codegen_->IsLeafMethod());
3432 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3433}
3434
3435void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003436 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3437 if (intrinsic.TryDispatch(invoke)) {
3438 return;
3439 }
3440
Alexandre Rames67555f72014-11-18 10:55:16 +00003441 HandleInvoke(invoke);
3442}
3443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003444void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003445 // Explicit clinit checks triggered by static invokes must have been pruned by
3446 // art::PrepareForRegisterAllocation.
3447 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003448
Andreas Gampe878d58c2015-01-15 23:24:00 -08003449 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3450 if (intrinsic.TryDispatch(invoke)) {
3451 return;
3452 }
3453
Alexandre Rames67555f72014-11-18 10:55:16 +00003454 HandleInvoke(invoke);
3455}
3456
Andreas Gampe878d58c2015-01-15 23:24:00 -08003457static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3458 if (invoke->GetLocations()->Intrinsified()) {
3459 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3460 intrinsic.Dispatch(invoke);
3461 return true;
3462 }
3463 return false;
3464}
3465
Vladimir Markodc151b22015-10-15 18:02:30 +01003466HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3467 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3468 MethodReference target_method ATTRIBUTE_UNUSED) {
3469 // On arm64 we support all dispatch types.
3470 return desired_dispatch_info;
3471}
3472
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003473void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003474 // For better instruction scheduling we load the direct code pointer before the method pointer.
3475 bool direct_code_loaded = false;
3476 switch (invoke->GetCodePtrLocation()) {
3477 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3478 // LR = code address from literal pool with link-time patch.
3479 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3480 direct_code_loaded = true;
3481 break;
3482 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3483 // LR = invoke->GetDirectCodePtr();
3484 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3485 direct_code_loaded = true;
3486 break;
3487 default:
3488 break;
3489 }
3490
Andreas Gampe878d58c2015-01-15 23:24:00 -08003491 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003492 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3493 switch (invoke->GetMethodLoadKind()) {
3494 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3495 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003496 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003497 break;
3498 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003499 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003500 break;
3501 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3502 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003503 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003504 break;
3505 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3506 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003507 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003508 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3509 break;
3510 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3511 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003512 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3513 invoke->GetDexCacheArrayOffset());
3514 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003515 {
3516 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003517 __ Bind(pc_insn_label);
3518 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003519 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003520 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003521 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003522 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3523 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003524 {
3525 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3526 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3527 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3528 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3529 }
Vladimir Marko58155012015-08-19 12:49:41 +00003530 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003531 }
Vladimir Marko58155012015-08-19 12:49:41 +00003532 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003533 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003534 Register reg = XRegisterFrom(temp);
3535 Register method_reg;
3536 if (current_method.IsRegister()) {
3537 method_reg = XRegisterFrom(current_method);
3538 } else {
3539 DCHECK(invoke->GetLocations()->Intrinsified());
3540 DCHECK(!current_method.IsValid());
3541 method_reg = reg;
3542 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3543 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003544
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003545 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003546 __ Ldr(reg.X(),
3547 MemOperand(method_reg.X(),
3548 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003549 // temp = temp[index_in_cache];
3550 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3551 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3552 break;
3553 }
3554 }
3555
3556 switch (invoke->GetCodePtrLocation()) {
3557 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3558 __ Bl(&frame_entry_label_);
3559 break;
3560 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3561 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3562 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003563 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3564 __ Bind(label);
3565 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003566 break;
3567 }
3568 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3569 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3570 // LR prepared above for better instruction scheduling.
3571 DCHECK(direct_code_loaded);
3572 // lr()
3573 __ Blr(lr);
3574 break;
3575 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3576 // LR = callee_method->entry_point_from_quick_compiled_code_;
3577 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003578 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003579 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3580 // lr()
3581 __ Blr(lr);
3582 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003583 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003584
Andreas Gampe878d58c2015-01-15 23:24:00 -08003585 DCHECK(!IsLeafMethod());
3586}
3587
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003588void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003589 // Use the calling convention instead of the location of the receiver, as
3590 // intrinsics may have put the receiver in a different register. In the intrinsics
3591 // slow path, the arguments have been moved to the right place, so here we are
3592 // guaranteed that the receiver is the first register of the calling convention.
3593 InvokeDexCallingConvention calling_convention;
3594 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003595 Register temp = XRegisterFrom(temp_in);
3596 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3597 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3598 Offset class_offset = mirror::Object::ClassOffset();
3599 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3600
3601 BlockPoolsScope block_pools(GetVIXLAssembler());
3602
3603 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003604 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003605 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003606 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003607 // Instead of simply (possibly) unpoisoning `temp` here, we should
3608 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003609 // intermediate/temporary reference and because the current
3610 // concurrent copying collector keeps the from-space memory
3611 // intact/accessible until the end of the marking phase (the
3612 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003613 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3614 // temp = temp->GetMethodAt(method_offset);
3615 __ Ldr(temp, MemOperand(temp, method_offset));
3616 // lr = temp->GetEntryPoint();
3617 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3618 // lr();
3619 __ Blr(lr);
3620}
3621
Vladimir Marko58155012015-08-19 12:49:41 +00003622void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3623 DCHECK(linker_patches->empty());
3624 size_t size =
3625 method_patches_.size() +
3626 call_patches_.size() +
3627 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003628 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003629 linker_patches->reserve(size);
3630 for (const auto& entry : method_patches_) {
3631 const MethodReference& target_method = entry.first;
3632 vixl::Literal<uint64_t>* literal = entry.second;
3633 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3634 target_method.dex_file,
3635 target_method.dex_method_index));
3636 }
3637 for (const auto& entry : call_patches_) {
3638 const MethodReference& target_method = entry.first;
3639 vixl::Literal<uint64_t>* literal = entry.second;
3640 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3641 target_method.dex_file,
3642 target_method.dex_method_index));
3643 }
3644 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003645 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003646 info.target_method.dex_file,
3647 info.target_method.dex_method_index));
3648 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003649 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003650 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003651 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003652 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003653 info.element_offset));
3654 }
3655}
3656
3657vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3658 // Look up the literal for value.
3659 auto lb = uint64_literals_.lower_bound(value);
3660 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3661 return lb->second;
3662 }
3663 // We don't have a literal for this value, insert a new one.
3664 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3665 uint64_literals_.PutBefore(lb, value, literal);
3666 return literal;
3667}
3668
3669vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3670 MethodReference target_method,
3671 MethodToLiteralMap* map) {
3672 // Look up the literal for target_method.
3673 auto lb = map->lower_bound(target_method);
3674 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3675 return lb->second;
3676 }
3677 // We don't have a literal for this method yet, insert a new one.
3678 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3679 map->PutBefore(lb, target_method, literal);
3680 return literal;
3681}
3682
3683vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3684 MethodReference target_method) {
3685 return DeduplicateMethodLiteral(target_method, &method_patches_);
3686}
3687
3688vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3689 MethodReference target_method) {
3690 return DeduplicateMethodLiteral(target_method, &call_patches_);
3691}
3692
3693
Andreas Gampe878d58c2015-01-15 23:24:00 -08003694void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003695 // Explicit clinit checks triggered by static invokes must have been pruned by
3696 // art::PrepareForRegisterAllocation.
3697 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003698
Andreas Gampe878d58c2015-01-15 23:24:00 -08003699 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3700 return;
3701 }
3702
Alexandre Ramesd921d642015-04-16 15:07:16 +01003703 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003704 LocationSummary* locations = invoke->GetLocations();
3705 codegen_->GenerateStaticOrDirectCall(
3706 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003707 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003708}
3709
3710void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003711 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3712 return;
3713 }
3714
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003715 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003716 DCHECK(!codegen_->IsLeafMethod());
3717 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3718}
3719
Alexandre Rames67555f72014-11-18 10:55:16 +00003720void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003721 InvokeRuntimeCallingConvention calling_convention;
3722 CodeGenerator::CreateLoadClassLocationSummary(
3723 cls,
3724 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003725 LocationFrom(vixl::x0),
3726 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003727}
3728
3729void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003730 if (cls->NeedsAccessCheck()) {
3731 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3732 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3733 cls,
3734 cls->GetDexPc(),
3735 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003736 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003737 return;
3738 }
3739
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003740 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003741 Register out = OutputRegister(cls);
3742 Register current_method = InputRegisterAt(cls, 0);
3743 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003744 DCHECK(!cls->CanCallRuntime());
3745 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003746 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3747 if (kEmitCompilerReadBarrier) {
3748 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3749 __ Add(out.X(), current_method.X(), declaring_class_offset);
3750 // /* mirror::Class* */ out = out->Read()
3751 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3752 } else {
3753 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3754 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3755 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003756 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003757 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003758 // /* GcRoot<mirror::Class>[] */ out =
3759 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003760 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003761
3762 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
3763 if (kEmitCompilerReadBarrier) {
3764 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
3765 __ Add(out.X(), out.X(), cache_offset);
3766 // /* mirror::Class* */ out = out->Read()
3767 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3768 } else {
3769 // /* GcRoot<mirror::Class> */ out = out[type_index]
3770 __ Ldr(out, MemOperand(out.X(), cache_offset));
3771 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003772
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003773 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3774 DCHECK(cls->CanCallRuntime());
3775 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3776 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3777 codegen_->AddSlowPath(slow_path);
3778 if (!cls->IsInDexCache()) {
3779 __ Cbz(out, slow_path->GetEntryLabel());
3780 }
3781 if (cls->MustGenerateClinitCheck()) {
3782 GenerateClassInitializationCheck(slow_path, out);
3783 } else {
3784 __ Bind(slow_path->GetExitLabel());
3785 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003786 }
3787 }
3788}
3789
David Brazdilcb1c0552015-08-04 16:22:25 +01003790static MemOperand GetExceptionTlsAddress() {
3791 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3792}
3793
Alexandre Rames67555f72014-11-18 10:55:16 +00003794void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3795 LocationSummary* locations =
3796 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3797 locations->SetOut(Location::RequiresRegister());
3798}
3799
3800void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003801 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3802}
3803
3804void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3805 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3806}
3807
3808void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3809 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003810}
3811
Alexandre Rames5319def2014-10-23 10:03:10 +01003812void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3813 load->SetLocations(nullptr);
3814}
3815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003816void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003817 // Nothing to do, this is driven by the code generator.
3818}
3819
Alexandre Rames67555f72014-11-18 10:55:16 +00003820void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003821 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
3822 ? LocationSummary::kCallOnSlowPath
3823 : LocationSummary::kNoCall;
3824 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003825 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003826 locations->SetOut(Location::RequiresRegister());
3827}
3828
3829void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003830 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003831 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003832 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003833
3834 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3835 if (kEmitCompilerReadBarrier) {
3836 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3837 __ Add(out.X(), current_method.X(), declaring_class_offset);
3838 // /* mirror::Class* */ out = out->Read()
3839 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3840 } else {
3841 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3842 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3843 }
3844
3845 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3846 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
3847
3848 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
3849 if (kEmitCompilerReadBarrier) {
3850 // /* GcRoot<mirror::String>* */ out = &out[string_index]
3851 __ Add(out.X(), out.X(), cache_offset);
3852 // /* mirror::String* */ out = out->Read()
3853 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3854 } else {
3855 // /* GcRoot<mirror::String> */ out = out[string_index]
3856 __ Ldr(out, MemOperand(out.X(), cache_offset));
3857 }
3858
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003859 if (!load->IsInDexCache()) {
3860 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3861 codegen_->AddSlowPath(slow_path);
3862 __ Cbz(out, slow_path->GetEntryLabel());
3863 __ Bind(slow_path->GetExitLabel());
3864 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003865}
3866
Alexandre Rames5319def2014-10-23 10:03:10 +01003867void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3868 local->SetLocations(nullptr);
3869}
3870
3871void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3872 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3873}
3874
3875void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3877 locations->SetOut(Location::ConstantLocation(constant));
3878}
3879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003880void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003881 // Will be generated at use site.
3882}
3883
Alexandre Rames67555f72014-11-18 10:55:16 +00003884void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3885 LocationSummary* locations =
3886 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3887 InvokeRuntimeCallingConvention calling_convention;
3888 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3889}
3890
3891void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3892 codegen_->InvokeRuntime(instruction->IsEnter()
3893 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3894 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003895 instruction->GetDexPc(),
3896 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003897 if (instruction->IsEnter()) {
3898 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3899 } else {
3900 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3901 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003902}
3903
Alexandre Rames42d641b2014-10-27 14:00:51 +00003904void LocationsBuilderARM64::VisitMul(HMul* mul) {
3905 LocationSummary* locations =
3906 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3907 switch (mul->GetResultType()) {
3908 case Primitive::kPrimInt:
3909 case Primitive::kPrimLong:
3910 locations->SetInAt(0, Location::RequiresRegister());
3911 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003913 break;
3914
3915 case Primitive::kPrimFloat:
3916 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003917 locations->SetInAt(0, Location::RequiresFpuRegister());
3918 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003919 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003920 break;
3921
3922 default:
3923 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3924 }
3925}
3926
3927void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3928 switch (mul->GetResultType()) {
3929 case Primitive::kPrimInt:
3930 case Primitive::kPrimLong:
3931 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3932 break;
3933
3934 case Primitive::kPrimFloat:
3935 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003936 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003937 break;
3938
3939 default:
3940 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3941 }
3942}
3943
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003944void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3945 LocationSummary* locations =
3946 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3947 switch (neg->GetResultType()) {
3948 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003949 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003950 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003952 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003953
3954 case Primitive::kPrimFloat:
3955 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003956 locations->SetInAt(0, Location::RequiresFpuRegister());
3957 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003958 break;
3959
3960 default:
3961 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3962 }
3963}
3964
3965void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3966 switch (neg->GetResultType()) {
3967 case Primitive::kPrimInt:
3968 case Primitive::kPrimLong:
3969 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3970 break;
3971
3972 case Primitive::kPrimFloat:
3973 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003974 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003975 break;
3976
3977 default:
3978 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3979 }
3980}
3981
3982void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3983 LocationSummary* locations =
3984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3985 InvokeRuntimeCallingConvention calling_convention;
3986 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003987 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003988 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003989 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003990}
3991
3992void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3993 LocationSummary* locations = instruction->GetLocations();
3994 InvokeRuntimeCallingConvention calling_convention;
3995 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3996 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003997 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003998 // Note: if heap poisoning is enabled, the entry point takes cares
3999 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004000 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4001 instruction,
4002 instruction->GetDexPc(),
4003 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004004 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004005}
4006
Alexandre Rames5319def2014-10-23 10:03:10 +01004007void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4008 LocationSummary* locations =
4009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4010 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004011 if (instruction->IsStringAlloc()) {
4012 locations->AddTemp(LocationFrom(kArtMethodRegister));
4013 } else {
4014 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4015 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4016 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004017 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4018}
4019
4020void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004021 // Note: if heap poisoning is enabled, the entry point takes cares
4022 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004023 if (instruction->IsStringAlloc()) {
4024 // String is allocated through StringFactory. Call NewEmptyString entry point.
4025 Location temp = instruction->GetLocations()->GetTemp(0);
4026 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4027 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4028 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4029 __ Blr(lr);
4030 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4031 } else {
4032 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4033 instruction,
4034 instruction->GetDexPc(),
4035 nullptr);
4036 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4037 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004038}
4039
4040void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4041 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004042 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004043 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004044}
4045
4046void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004047 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004048 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004049 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004050 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004051 break;
4052
4053 default:
4054 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4055 }
4056}
4057
David Brazdil66d126e2015-04-03 16:02:44 +01004058void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4059 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4060 locations->SetInAt(0, Location::RequiresRegister());
4061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4062}
4063
4064void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004065 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4066}
4067
Alexandre Rames5319def2014-10-23 10:03:10 +01004068void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004069 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4070 ? LocationSummary::kCallOnSlowPath
4071 : LocationSummary::kNoCall;
4072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004073 locations->SetInAt(0, Location::RequiresRegister());
4074 if (instruction->HasUses()) {
4075 locations->SetOut(Location::SameAsFirstInput());
4076 }
4077}
4078
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004079void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004080 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4081 return;
4082 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004083
Alexandre Ramesd921d642015-04-16 15:07:16 +01004084 BlockPoolsScope block_pools(GetVIXLAssembler());
4085 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004086 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4087 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4088}
4089
4090void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004091 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4092 codegen_->AddSlowPath(slow_path);
4093
4094 LocationSummary* locations = instruction->GetLocations();
4095 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004096
4097 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004098}
4099
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004100void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004101 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004102 GenerateImplicitNullCheck(instruction);
4103 } else {
4104 GenerateExplicitNullCheck(instruction);
4105 }
4106}
4107
Alexandre Rames67555f72014-11-18 10:55:16 +00004108void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4109 HandleBinaryOp(instruction);
4110}
4111
4112void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4113 HandleBinaryOp(instruction);
4114}
4115
Alexandre Rames3e69f162014-12-10 10:36:50 +00004116void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4117 LOG(FATAL) << "Unreachable";
4118}
4119
4120void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4121 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4122}
4123
Alexandre Rames5319def2014-10-23 10:03:10 +01004124void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4126 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4127 if (location.IsStackSlot()) {
4128 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4129 } else if (location.IsDoubleStackSlot()) {
4130 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4131 }
4132 locations->SetOut(location);
4133}
4134
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004135void InstructionCodeGeneratorARM64::VisitParameterValue(
4136 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004137 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004138}
4139
4140void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4141 LocationSummary* locations =
4142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004143 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004144}
4145
4146void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4147 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4148 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004149}
4150
4151void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4153 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4154 locations->SetInAt(i, Location::Any());
4155 }
4156 locations->SetOut(Location::Any());
4157}
4158
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004159void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004160 LOG(FATAL) << "Unreachable";
4161}
4162
Serban Constantinescu02164b32014-11-13 14:05:07 +00004163void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004164 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004165 LocationSummary::CallKind call_kind =
4166 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004167 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4168
4169 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004170 case Primitive::kPrimInt:
4171 case Primitive::kPrimLong:
4172 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004173 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4175 break;
4176
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004177 case Primitive::kPrimFloat:
4178 case Primitive::kPrimDouble: {
4179 InvokeRuntimeCallingConvention calling_convention;
4180 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4181 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4182 locations->SetOut(calling_convention.GetReturnLocation(type));
4183
4184 break;
4185 }
4186
Serban Constantinescu02164b32014-11-13 14:05:07 +00004187 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004188 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004189 }
4190}
4191
4192void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4193 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004194
Serban Constantinescu02164b32014-11-13 14:05:07 +00004195 switch (type) {
4196 case Primitive::kPrimInt:
4197 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004198 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004199 break;
4200 }
4201
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004202 case Primitive::kPrimFloat:
4203 case Primitive::kPrimDouble: {
4204 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4205 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004206 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004207 if (type == Primitive::kPrimFloat) {
4208 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4209 } else {
4210 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4211 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004212 break;
4213 }
4214
Serban Constantinescu02164b32014-11-13 14:05:07 +00004215 default:
4216 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004217 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004218 }
4219}
4220
Calin Juravle27df7582015-04-17 19:12:31 +01004221void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4222 memory_barrier->SetLocations(nullptr);
4223}
4224
4225void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4226 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4227}
4228
Alexandre Rames5319def2014-10-23 10:03:10 +01004229void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4231 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004232 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004233}
4234
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004235void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004236 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004237}
4238
4239void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4240 instruction->SetLocations(nullptr);
4241}
4242
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004243void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004244 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004245}
4246
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004247void LocationsBuilderARM64::VisitRor(HRor* ror) {
4248 HandleBinaryOp(ror);
4249}
4250
4251void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4252 HandleBinaryOp(ror);
4253}
4254
Serban Constantinescu02164b32014-11-13 14:05:07 +00004255void LocationsBuilderARM64::VisitShl(HShl* shl) {
4256 HandleShift(shl);
4257}
4258
4259void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4260 HandleShift(shl);
4261}
4262
4263void LocationsBuilderARM64::VisitShr(HShr* shr) {
4264 HandleShift(shr);
4265}
4266
4267void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4268 HandleShift(shr);
4269}
4270
Alexandre Rames5319def2014-10-23 10:03:10 +01004271void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4272 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4273 Primitive::Type field_type = store->InputAt(1)->GetType();
4274 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004275 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004276 case Primitive::kPrimBoolean:
4277 case Primitive::kPrimByte:
4278 case Primitive::kPrimChar:
4279 case Primitive::kPrimShort:
4280 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004281 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004282 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4283 break;
4284
4285 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004286 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004287 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4288 break;
4289
4290 default:
4291 LOG(FATAL) << "Unimplemented local type " << field_type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004292 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +01004293 }
4294}
4295
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004296void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004297}
4298
4299void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004300 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004301}
4302
4303void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004304 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004305}
4306
Alexandre Rames67555f72014-11-18 10:55:16 +00004307void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004308 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004309}
4310
4311void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004312 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004313}
4314
4315void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004316 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004317}
4318
Alexandre Rames67555f72014-11-18 10:55:16 +00004319void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004320 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004321}
4322
Calin Juravlee460d1d2015-09-29 04:52:17 +01004323void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4324 HUnresolvedInstanceFieldGet* instruction) {
4325 FieldAccessCallingConventionARM64 calling_convention;
4326 codegen_->CreateUnresolvedFieldLocationSummary(
4327 instruction, instruction->GetFieldType(), calling_convention);
4328}
4329
4330void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4331 HUnresolvedInstanceFieldGet* instruction) {
4332 FieldAccessCallingConventionARM64 calling_convention;
4333 codegen_->GenerateUnresolvedFieldAccess(instruction,
4334 instruction->GetFieldType(),
4335 instruction->GetFieldIndex(),
4336 instruction->GetDexPc(),
4337 calling_convention);
4338}
4339
4340void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4341 HUnresolvedInstanceFieldSet* instruction) {
4342 FieldAccessCallingConventionARM64 calling_convention;
4343 codegen_->CreateUnresolvedFieldLocationSummary(
4344 instruction, instruction->GetFieldType(), calling_convention);
4345}
4346
4347void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4348 HUnresolvedInstanceFieldSet* instruction) {
4349 FieldAccessCallingConventionARM64 calling_convention;
4350 codegen_->GenerateUnresolvedFieldAccess(instruction,
4351 instruction->GetFieldType(),
4352 instruction->GetFieldIndex(),
4353 instruction->GetDexPc(),
4354 calling_convention);
4355}
4356
4357void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4358 HUnresolvedStaticFieldGet* instruction) {
4359 FieldAccessCallingConventionARM64 calling_convention;
4360 codegen_->CreateUnresolvedFieldLocationSummary(
4361 instruction, instruction->GetFieldType(), calling_convention);
4362}
4363
4364void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4365 HUnresolvedStaticFieldGet* instruction) {
4366 FieldAccessCallingConventionARM64 calling_convention;
4367 codegen_->GenerateUnresolvedFieldAccess(instruction,
4368 instruction->GetFieldType(),
4369 instruction->GetFieldIndex(),
4370 instruction->GetDexPc(),
4371 calling_convention);
4372}
4373
4374void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4375 HUnresolvedStaticFieldSet* instruction) {
4376 FieldAccessCallingConventionARM64 calling_convention;
4377 codegen_->CreateUnresolvedFieldLocationSummary(
4378 instruction, instruction->GetFieldType(), calling_convention);
4379}
4380
4381void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4382 HUnresolvedStaticFieldSet* instruction) {
4383 FieldAccessCallingConventionARM64 calling_convention;
4384 codegen_->GenerateUnresolvedFieldAccess(instruction,
4385 instruction->GetFieldType(),
4386 instruction->GetFieldIndex(),
4387 instruction->GetDexPc(),
4388 calling_convention);
4389}
4390
Alexandre Rames5319def2014-10-23 10:03:10 +01004391void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4393}
4394
4395void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004396 HBasicBlock* block = instruction->GetBlock();
4397 if (block->GetLoopInformation() != nullptr) {
4398 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4399 // The back edge will generate the suspend check.
4400 return;
4401 }
4402 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4403 // The goto will generate the suspend check.
4404 return;
4405 }
4406 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004407}
4408
4409void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4410 temp->SetLocations(nullptr);
4411}
4412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004413void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004414 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004415}
4416
Alexandre Rames67555f72014-11-18 10:55:16 +00004417void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4418 LocationSummary* locations =
4419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4420 InvokeRuntimeCallingConvention calling_convention;
4421 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4422}
4423
4424void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4425 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004426 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004427 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004428}
4429
4430void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4431 LocationSummary* locations =
4432 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4433 Primitive::Type input_type = conversion->GetInputType();
4434 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004435 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004436 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4437 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4438 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4439 }
4440
Alexandre Rames542361f2015-01-29 16:57:31 +00004441 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004442 locations->SetInAt(0, Location::RequiresFpuRegister());
4443 } else {
4444 locations->SetInAt(0, Location::RequiresRegister());
4445 }
4446
Alexandre Rames542361f2015-01-29 16:57:31 +00004447 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004448 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4449 } else {
4450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4451 }
4452}
4453
4454void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4455 Primitive::Type result_type = conversion->GetResultType();
4456 Primitive::Type input_type = conversion->GetInputType();
4457
4458 DCHECK_NE(input_type, result_type);
4459
Alexandre Rames542361f2015-01-29 16:57:31 +00004460 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004461 int result_size = Primitive::ComponentSize(result_type);
4462 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004463 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004464 Register output = OutputRegister(conversion);
4465 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004466 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004467 // 'int' values are used directly as W registers, discarding the top
4468 // bits, so we don't need to sign-extend and can just perform a move.
4469 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4470 // top 32 bits of the target register. We theoretically could leave those
4471 // bits unchanged, but we would have to make sure that no code uses a
4472 // 32bit input value as a 64bit value assuming that the top 32 bits are
4473 // zero.
4474 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004475 } else if (result_type == Primitive::kPrimChar ||
4476 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4477 __ Ubfx(output,
4478 output.IsX() ? source.X() : source.W(),
4479 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004480 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004481 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004482 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004483 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004484 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004485 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004486 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4487 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004488 } else if (Primitive::IsFloatingPointType(result_type) &&
4489 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004490 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4491 } else {
4492 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4493 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004494 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004495}
Alexandre Rames67555f72014-11-18 10:55:16 +00004496
Serban Constantinescu02164b32014-11-13 14:05:07 +00004497void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4498 HandleShift(ushr);
4499}
4500
4501void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4502 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004503}
4504
4505void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4506 HandleBinaryOp(instruction);
4507}
4508
4509void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4510 HandleBinaryOp(instruction);
4511}
4512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004513void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004514 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004515 LOG(FATAL) << "Unreachable";
4516}
4517
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004518void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004519 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004520 LOG(FATAL) << "Unreachable";
4521}
4522
Mark Mendellfe57faa2015-09-18 09:26:15 -04004523// Simple implementation of packed switch - generate cascaded compare/jumps.
4524void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4525 LocationSummary* locations =
4526 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4527 locations->SetInAt(0, Location::RequiresRegister());
4528}
4529
4530void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4531 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004532 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004533 Register value_reg = InputRegisterAt(switch_instr, 0);
4534 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4535
Zheng Xu3927c8b2015-11-18 17:46:25 +08004536 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4537 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4538 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4539 // make sure we don't emit it if the target may run out of range.
4540 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4541 // ranges and emit the tables only as required.
4542 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004543
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004544 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004545 // Current instruction id is an upper bound of the number of HIRs in the graph.
4546 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4547 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004548 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4549 Register temp = temps.AcquireW();
4550 __ Subs(temp, value_reg, Operand(lower_bound));
4551
Zheng Xu3927c8b2015-11-18 17:46:25 +08004552 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004553 // Jump to successors[0] if value == lower_bound.
4554 __ B(eq, codegen_->GetLabelOf(successors[0]));
4555 int32_t last_index = 0;
4556 for (; num_entries - last_index > 2; last_index += 2) {
4557 __ Subs(temp, temp, Operand(2));
4558 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4559 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4560 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4561 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4562 }
4563 if (num_entries - last_index == 2) {
4564 // The last missing case_value.
4565 __ Cmp(temp, Operand(1));
4566 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004567 }
4568
4569 // And the default for any other value.
4570 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4571 __ B(codegen_->GetLabelOf(default_block));
4572 }
4573 } else {
4574 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4575 codegen_->AddJumpTable(jump_table);
4576
4577 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4578
4579 // Below instructions should use at most one blocked register. Since there are two blocked
4580 // registers, we are free to block one.
4581 Register temp_w = temps.AcquireW();
4582 Register index;
4583 // Remove the bias.
4584 if (lower_bound != 0) {
4585 index = temp_w;
4586 __ Sub(index, value_reg, Operand(lower_bound));
4587 } else {
4588 index = value_reg;
4589 }
4590
4591 // Jump to default block if index is out of the range.
4592 __ Cmp(index, Operand(num_entries));
4593 __ B(hs, codegen_->GetLabelOf(default_block));
4594
4595 // In current VIXL implementation, it won't require any blocked registers to encode the
4596 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4597 // register pressure.
4598 Register table_base = temps.AcquireX();
4599 // Load jump offset from the table.
4600 __ Adr(table_base, jump_table->GetTableStartLabel());
4601 Register jump_offset = temp_w;
4602 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4603
4604 // Jump to target block by branching to table_base(pc related) + offset.
4605 Register target_address = table_base;
4606 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4607 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004608 }
4609}
4610
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004611void CodeGeneratorARM64::GenerateReadBarrier(HInstruction* instruction,
4612 Location out,
4613 Location ref,
4614 Location obj,
4615 uint32_t offset,
4616 Location index) {
4617 DCHECK(kEmitCompilerReadBarrier);
4618
4619 // If heap poisoning is enabled, the unpoisoning of the loaded
4620 // reference will be carried out by the runtime within the slow
4621 // path.
4622 //
4623 // Note that `ref` currently does not get unpoisoned (when heap
4624 // poisoning is enabled), which is alright as the `ref` argument is
4625 // not used by the artReadBarrierSlow entry point.
4626 //
4627 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4628 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4629 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4630 AddSlowPath(slow_path);
4631
4632 // TODO: When read barrier has a fast path, add it here.
4633 /* Currently the read barrier call is inserted after the original load.
4634 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
4635 * original load. This load-load ordering is required by the read barrier.
4636 * The fast path/slow path (for Baker's algorithm) should look like:
4637 *
4638 * bool isGray = obj.LockWord & kReadBarrierMask;
4639 * lfence; // load fence or artificial data dependence to prevent load-load reordering
4640 * ref = obj.field; // this is the original load
4641 * if (isGray) {
4642 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
4643 * }
4644 */
4645
4646 __ B(slow_path->GetEntryLabel());
4647 __ Bind(slow_path->GetExitLabel());
4648}
4649
4650void CodeGeneratorARM64::MaybeGenerateReadBarrier(HInstruction* instruction,
4651 Location out,
4652 Location ref,
4653 Location obj,
4654 uint32_t offset,
4655 Location index) {
4656 if (kEmitCompilerReadBarrier) {
4657 // If heap poisoning is enabled, unpoisoning will be taken care of
4658 // by the runtime within the slow path.
4659 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
4660 } else if (kPoisonHeapReferences) {
4661 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4662 }
4663}
4664
4665void CodeGeneratorARM64::GenerateReadBarrierForRoot(HInstruction* instruction,
4666 Location out,
4667 Location root) {
4668 DCHECK(kEmitCompilerReadBarrier);
4669
4670 // Note that GC roots are not affected by heap poisoning, so we do
4671 // not need to do anything special for this here.
4672 SlowPathCodeARM64* slow_path =
4673 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4674 AddSlowPath(slow_path);
4675
4676 // TODO: Implement a fast path for ReadBarrierForRoot, performing
4677 // the following operation (for Baker's algorithm):
4678 //
4679 // if (thread.tls32_.is_gc_marking) {
4680 // root = Mark(root);
4681 // }
4682
4683 __ B(slow_path->GetEntryLabel());
4684 __ Bind(slow_path->GetExitLabel());
4685}
4686
Alexandre Rames67555f72014-11-18 10:55:16 +00004687#undef __
4688#undef QUICK_ENTRY_POINT
4689
Alexandre Rames5319def2014-10-23 10:03:10 +01004690} // namespace arm64
4691} // namespace art