blob: c7b24ba1a3a3d29566f2582dd3aca978326820f1 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Andreas Gampe878d58c2015-01-15 23:24:00 -080049using helpers::CPURegisterFrom;
50using helpers::DRegisterFrom;
51using helpers::FPRegisterFrom;
52using helpers::HeapOperand;
53using helpers::HeapOperandFrom;
54using helpers::InputCPURegisterAt;
55using helpers::InputFPRegisterAt;
56using helpers::InputRegisterAt;
57using helpers::InputOperandAt;
58using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::LocationFrom;
60using helpers::OperandFromMemOperand;
61using helpers::OutputCPURegister;
62using helpers::OutputFPRegister;
63using helpers::OutputRegister;
64using helpers::RegisterFrom;
65using helpers::StackOperandFrom;
66using helpers::VIXLRegCodeFromART;
67using helpers::WRegisterFrom;
68using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000069using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080070using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071
Alexandre Rames5319def2014-10-23 10:03:10 +010072static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000073// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080074// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
75// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000076static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010077
Alexandre Rames5319def2014-10-23 10:03:10 +010078inline Condition ARM64Condition(IfCondition cond) {
79 switch (cond) {
80 case kCondEQ: return eq;
81 case kCondNE: return ne;
82 case kCondLT: return lt;
83 case kCondLE: return le;
84 case kCondGT: return gt;
85 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070086 case kCondB: return lo;
87 case kCondBE: return ls;
88 case kCondA: return hi;
89 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010090 }
Roland Levillain7f63c522015-07-13 15:54:55 +000091 LOG(FATAL) << "Unreachable";
92 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010093}
94
Vladimir Markod6e069b2016-01-18 11:11:01 +000095inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
96 // The ARM64 condition codes can express all the necessary branches, see the
97 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
98 // There is no dex instruction or HIR that would need the missing conditions
99 // "equal or unordered" or "not equal".
100 switch (cond) {
101 case kCondEQ: return eq;
102 case kCondNE: return ne /* unordered */;
103 case kCondLT: return gt_bias ? cc : lt /* unordered */;
104 case kCondLE: return gt_bias ? ls : le /* unordered */;
105 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
106 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
107 default:
108 LOG(FATAL) << "UNREACHABLE";
109 UNREACHABLE();
110 }
111}
112
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000113Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
115 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
116 // but we use the exact registers for clarity.
117 if (return_type == Primitive::kPrimFloat) {
118 return LocationFrom(s0);
119 } else if (return_type == Primitive::kPrimDouble) {
120 return LocationFrom(d0);
121 } else if (return_type == Primitive::kPrimLong) {
122 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100123 } else if (return_type == Primitive::kPrimVoid) {
124 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000125 } else {
126 return LocationFrom(w0);
127 }
128}
129
Alexandre Rames5319def2014-10-23 10:03:10 +0100130Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000131 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100132}
133
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700134// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
135#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Alexandre Rames67555f72014-11-18 10:55:16 +0000136#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,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100149 register_set->GetCoreRegisters() & (~callee_saved_core_registers.GetList()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000150 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100151 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.GetList()));
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();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100157 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
158 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800159 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);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100162 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800163 !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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000222 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(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);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100239 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
240 ? QUICK_ENTRY_POINT(pThrowStringBounds)
241 : QUICK_ENTRY_POINT(pThrowArrayBounds);
242 arm64_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
243 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800244 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100245 }
246
Alexandre Rames8158f282015-08-07 10:26:17 +0100247 bool IsFatal() const OVERRIDE { return true; }
248
Alexandre Rames9931f312015-06-19 14:47:01 +0100249 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100252 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
253};
254
Alexandre Rames67555f72014-11-18 10:55:16 +0000255class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
256 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000257 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000258
259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
260 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
261 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000262 if (instruction_->CanThrowIntoCatchBlock()) {
263 // Live registers will be restored in the catch block if caught.
264 SaveLiveRegisters(codegen, instruction_->GetLocations());
265 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000266 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000267 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800268 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 }
270
Alexandre Rames8158f282015-08-07 10:26:17 +0100271 bool IsFatal() const OVERRIDE { return true; }
272
Alexandre Rames9931f312015-06-19 14:47:01 +0100273 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 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)
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000340 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
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;
David Srbecky9cd6d372016-02-09 15:24:47 +0000351 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
352 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000354 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 __ B(GetExitLabel());
361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
364
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
367};
368
Alexandre Rames5319def2014-10-23 10:03:10 +0100369class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
370 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000376 if (instruction_->CanThrowIntoCatchBlock()) {
377 // Live registers will be restored in the catch block if caught.
378 SaveLiveRegisters(codegen, instruction_->GetLocations());
379 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000381 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800382 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 }
384
Alexandre Rames8158f282015-08-07 10:26:17 +0100385 bool IsFatal() const OVERRIDE { return true; }
386
Alexandre Rames9931f312015-06-19 14:47:01 +0100387 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
388
Alexandre Rames5319def2014-10-23 10:03:10 +0100389 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
391};
392
393class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
394 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100395 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100397
Alexandre Rames67555f72014-11-18 10:55:16 +0000398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100400 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000401 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000402 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000403 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800404 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000405 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000406 if (successor_ == nullptr) {
407 __ B(GetReturnLabel());
408 } else {
409 __ B(arm64_codegen->GetLabelOf(successor_));
410 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100411 }
412
Scott Wakeling97c72b72016-06-24 16:19:36 +0100413 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DCHECK(successor_ == nullptr);
415 return &return_label_;
416 }
417
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100418 HBasicBlock* GetSuccessor() const {
419 return successor_;
420 }
421
Alexandre Rames9931f312015-06-19 14:47:01 +0100422 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
423
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 // If not null, the block to branch to after the suspend check.
426 HBasicBlock* const successor_;
427
428 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100429 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100430
431 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
432};
433
Alexandre Rames67555f72014-11-18 10:55:16 +0000434class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
435 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000436 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000437 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000438
439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000440 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100441 Location class_to_check = locations->InAt(1);
442 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
443 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000444 DCHECK(instruction_->IsCheckCast()
445 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
446 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100447 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000448
Alexandre Rames67555f72014-11-18 10:55:16 +0000449 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000450
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000451 if (!is_fatal_) {
452 SaveLiveRegisters(codegen, locations);
453 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000454
455 // We're moving two locations to locations that could overlap, so we need a parallel
456 // move resolver.
457 InvokeRuntimeCallingConvention calling_convention;
458 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100459 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
460 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461
462 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000463 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100464 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000465 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
466 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467 Primitive::Type ret_type = instruction_->GetType();
468 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
469 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
470 } else {
471 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100472 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800473 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474 }
475
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000476 if (!is_fatal_) {
477 RestoreLiveRegisters(codegen, locations);
478 __ B(GetExitLabel());
479 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000480 }
481
Alexandre Rames9931f312015-06-19 14:47:01 +0100482 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100484
Alexandre Rames67555f72014-11-18 10:55:16 +0000485 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000486 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487
Alexandre Rames67555f72014-11-18 10:55:16 +0000488 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
489};
490
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700491class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
492 public:
Aart Bik42249c32016-01-07 15:33:50 -0800493 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000494 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700495
496 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800497 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 __ Bind(GetEntryLabel());
499 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800500 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
501 instruction_,
502 instruction_->GetDexPc(),
503 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000504 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700505 }
506
Alexandre Rames9931f312015-06-19 14:47:01 +0100507 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
508
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700509 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
511};
512
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100513class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
514 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000515 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100516
517 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
518 LocationSummary* locations = instruction_->GetLocations();
519 __ Bind(GetEntryLabel());
520 SaveLiveRegisters(codegen, locations);
521
522 InvokeRuntimeCallingConvention calling_convention;
523 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
524 parallel_move.AddMove(
525 locations->InAt(0),
526 LocationFrom(calling_convention.GetRegisterAt(0)),
527 Primitive::kPrimNot,
528 nullptr);
529 parallel_move.AddMove(
530 locations->InAt(1),
531 LocationFrom(calling_convention.GetRegisterAt(1)),
532 Primitive::kPrimInt,
533 nullptr);
534 parallel_move.AddMove(
535 locations->InAt(2),
536 LocationFrom(calling_convention.GetRegisterAt(2)),
537 Primitive::kPrimNot,
538 nullptr);
539 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
540
541 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
542 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
543 instruction_,
544 instruction_->GetDexPc(),
545 this);
546 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
547 RestoreLiveRegisters(codegen, locations);
548 __ B(GetExitLabel());
549 }
550
551 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
552
553 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100554 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
555};
556
Zheng Xu3927c8b2015-11-18 17:46:25 +0800557void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
558 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000559 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800560
561 // We are about to use the assembler to place literals directly. Make sure we have enough
562 // underlying code buffer and we have generated the jump table with right size.
563 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
564 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
565
566 __ Bind(&table_start_);
567 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
568 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100569 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800570 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100571 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800572 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
573 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
574 Literal<int32_t> literal(jump_offset);
575 __ place(&literal);
576 }
577}
578
Roland Levillain44015862016-01-22 11:47:17 +0000579// Slow path marking an object during a read barrier.
580class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
581 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100582 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
583 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000584 DCHECK(kEmitCompilerReadBarrier);
585 }
586
587 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
588
589 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
590 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000591 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100592 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000593 DCHECK(instruction_->IsInstanceFieldGet() ||
594 instruction_->IsStaticFieldGet() ||
595 instruction_->IsArrayGet() ||
596 instruction_->IsLoadClass() ||
597 instruction_->IsLoadString() ||
598 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100599 instruction_->IsCheckCast() ||
600 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
601 instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000602 << "Unexpected instruction in read barrier marking slow path: "
603 << instruction_->DebugName();
604
605 __ Bind(GetEntryLabel());
Roland Levillain02b75802016-07-13 11:54:35 +0100606 // Save live registers before the runtime call, and in particular
607 // W0 (if it is live), as it is clobbered by functions
608 // art_quick_read_barrier_mark_regX.
Roland Levillain44015862016-01-22 11:47:17 +0000609 SaveLiveRegisters(codegen, locations);
610
611 InvokeRuntimeCallingConvention calling_convention;
612 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100613 DCHECK_NE(obj_.reg(), LR);
614 DCHECK_NE(obj_.reg(), WSP);
615 DCHECK_NE(obj_.reg(), WZR);
616 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
617 // "Compact" slow path, saving two moves.
618 //
619 // Instead of using the standard runtime calling convention (input
620 // and output in W0):
621 //
622 // W0 <- obj
623 // W0 <- ReadBarrierMark(W0)
624 // obj <- W0
625 //
626 // we just use rX (the register holding `obj`) as input and output
627 // of a dedicated entrypoint:
628 //
629 // rX <- ReadBarrierMarkRegX(rX)
630 //
631 int32_t entry_point_offset =
632 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64WordSize>(obj_.reg());
633 // TODO: Do not emit a stack map for this runtime call.
634 arm64_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain44015862016-01-22 11:47:17 +0000635 instruction_,
636 instruction_->GetDexPc(),
637 this);
Roland Levillain44015862016-01-22 11:47:17 +0000638
639 RestoreLiveRegisters(codegen, locations);
640 __ B(GetExitLabel());
641 }
642
643 private:
Roland Levillain44015862016-01-22 11:47:17 +0000644 const Location obj_;
645
646 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
647};
648
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000649// Slow path generating a read barrier for a heap reference.
650class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
651 public:
652 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
653 Location out,
654 Location ref,
655 Location obj,
656 uint32_t offset,
657 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000658 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000659 out_(out),
660 ref_(ref),
661 obj_(obj),
662 offset_(offset),
663 index_(index) {
664 DCHECK(kEmitCompilerReadBarrier);
665 // If `obj` is equal to `out` or `ref`, it means the initial object
666 // has been overwritten by (or after) the heap object reference load
667 // to be instrumented, e.g.:
668 //
669 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000670 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000671 //
672 // In that case, we have lost the information about the original
673 // object, and the emitted read barrier cannot work properly.
674 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
675 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
676 }
677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
680 LocationSummary* locations = instruction_->GetLocations();
681 Primitive::Type type = Primitive::kPrimNot;
682 DCHECK(locations->CanCall());
683 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100684 DCHECK(instruction_->IsInstanceFieldGet() ||
685 instruction_->IsStaticFieldGet() ||
686 instruction_->IsArrayGet() ||
687 instruction_->IsInstanceOf() ||
688 instruction_->IsCheckCast() ||
689 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain44015862016-01-22 11:47:17 +0000690 instruction_->GetLocations()->Intrinsified()))
691 << "Unexpected instruction in read barrier for heap reference slow path: "
692 << instruction_->DebugName();
Artem Serov328429f2016-07-06 16:23:04 +0100693 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000694 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100695 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000696
697 __ Bind(GetEntryLabel());
698
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000699 SaveLiveRegisters(codegen, locations);
700
701 // We may have to change the index's value, but as `index_` is a
702 // constant member (like other "inputs" of this slow path),
703 // introduce a copy of it, `index`.
704 Location index = index_;
705 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100706 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000707 if (instruction_->IsArrayGet()) {
708 // Compute the actual memory offset and store it in `index`.
709 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
710 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
711 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
712 // We are about to change the value of `index_reg` (see the
713 // calls to vixl::MacroAssembler::Lsl and
714 // vixl::MacroAssembler::Mov below), but it has
715 // not been saved by the previous call to
716 // art::SlowPathCode::SaveLiveRegisters, as it is a
717 // callee-save register --
718 // art::SlowPathCode::SaveLiveRegisters does not consider
719 // callee-save registers, as it has been designed with the
720 // assumption that callee-save registers are supposed to be
721 // handled by the called function. So, as a callee-save
722 // register, `index_reg` _would_ eventually be saved onto
723 // the stack, but it would be too late: we would have
724 // changed its value earlier. Therefore, we manually save
725 // it here into another freely available register,
726 // `free_reg`, chosen of course among the caller-save
727 // registers (as a callee-save `free_reg` register would
728 // exhibit the same problem).
729 //
730 // Note we could have requested a temporary register from
731 // the register allocator instead; but we prefer not to, as
732 // this is a slow path, and we know we can find a
733 // caller-save register that is available.
734 Register free_reg = FindAvailableCallerSaveRegister(codegen);
735 __ Mov(free_reg.W(), index_reg);
736 index_reg = free_reg;
737 index = LocationFrom(index_reg);
738 } else {
739 // The initial register stored in `index_` has already been
740 // saved in the call to art::SlowPathCode::SaveLiveRegisters
741 // (as it is not a callee-save register), so we can freely
742 // use it.
743 }
744 // Shifting the index value contained in `index_reg` by the scale
745 // factor (2) cannot overflow in practice, as the runtime is
746 // unable to allocate object arrays with a size larger than
747 // 2^26 - 1 (that is, 2^28 - 4 bytes).
748 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
749 static_assert(
750 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
751 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
752 __ Add(index_reg, index_reg, Operand(offset_));
753 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100754 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
755 // intrinsics, `index_` is not shifted by a scale factor of 2
756 // (as in the case of ArrayGet), as it is actually an offset
757 // to an object field within an object.
758 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000759 DCHECK(instruction_->GetLocations()->Intrinsified());
760 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
761 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
762 << instruction_->AsInvoke()->GetIntrinsic();
763 DCHECK_EQ(offset_, 0U);
764 DCHECK(index_.IsRegisterPair());
765 // UnsafeGet's offset location is a register pair, the low
766 // part contains the correct offset.
767 index = index_.ToLow();
768 }
769 }
770
771 // We're moving two or three locations to locations that could
772 // overlap, so we need a parallel move resolver.
773 InvokeRuntimeCallingConvention calling_convention;
774 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
775 parallel_move.AddMove(ref_,
776 LocationFrom(calling_convention.GetRegisterAt(0)),
777 type,
778 nullptr);
779 parallel_move.AddMove(obj_,
780 LocationFrom(calling_convention.GetRegisterAt(1)),
781 type,
782 nullptr);
783 if (index.IsValid()) {
784 parallel_move.AddMove(index,
785 LocationFrom(calling_convention.GetRegisterAt(2)),
786 Primitive::kPrimInt,
787 nullptr);
788 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
789 } else {
790 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
791 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
792 }
793 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
794 instruction_,
795 instruction_->GetDexPc(),
796 this);
797 CheckEntrypointTypes<
798 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
799 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
800
801 RestoreLiveRegisters(codegen, locations);
802
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000803 __ B(GetExitLabel());
804 }
805
806 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
807
808 private:
809 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100810 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
811 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000812 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
813 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
814 return Register(VIXLRegCodeFromART(i), kXRegSize);
815 }
816 }
817 // We shall never fail to find a free caller-save register, as
818 // there are more than two core caller-save registers on ARM64
819 // (meaning it is possible to find one which is different from
820 // `ref` and `obj`).
821 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
822 LOG(FATAL) << "Could not find a free register";
823 UNREACHABLE();
824 }
825
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000826 const Location out_;
827 const Location ref_;
828 const Location obj_;
829 const uint32_t offset_;
830 // An additional location containing an index to an array.
831 // Only used for HArrayGet and the UnsafeGetObject &
832 // UnsafeGetObjectVolatile intrinsics.
833 const Location index_;
834
835 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
836};
837
838// Slow path generating a read barrier for a GC root.
839class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
840 public:
841 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000842 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000843 DCHECK(kEmitCompilerReadBarrier);
844 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000845
846 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
847 LocationSummary* locations = instruction_->GetLocations();
848 Primitive::Type type = Primitive::kPrimNot;
849 DCHECK(locations->CanCall());
850 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000851 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
852 << "Unexpected instruction in read barrier for GC root slow path: "
853 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000854
855 __ Bind(GetEntryLabel());
856 SaveLiveRegisters(codegen, locations);
857
858 InvokeRuntimeCallingConvention calling_convention;
859 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
860 // The argument of the ReadBarrierForRootSlow is not a managed
861 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
862 // thus we need a 64-bit move here, and we cannot use
863 //
864 // arm64_codegen->MoveLocation(
865 // LocationFrom(calling_convention.GetRegisterAt(0)),
866 // root_,
867 // type);
868 //
869 // which would emit a 32-bit move, as `type` is a (32-bit wide)
870 // reference type (`Primitive::kPrimNot`).
871 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
872 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
873 instruction_,
874 instruction_->GetDexPc(),
875 this);
876 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
877 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
878
879 RestoreLiveRegisters(codegen, locations);
880 __ B(GetExitLabel());
881 }
882
883 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
884
885 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000886 const Location out_;
887 const Location root_;
888
889 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
890};
891
Alexandre Rames5319def2014-10-23 10:03:10 +0100892#undef __
893
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100894Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100895 Location next_location;
896 if (type == Primitive::kPrimVoid) {
897 LOG(FATAL) << "Unreachable type " << type;
898 }
899
Alexandre Rames542361f2015-01-29 16:57:31 +0000900 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100901 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
902 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000903 } else if (!Primitive::IsFloatingPointType(type) &&
904 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000905 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
906 } else {
907 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000908 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
909 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100910 }
911
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000912 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000913 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100914 return next_location;
915}
916
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100917Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100918 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100919}
920
Serban Constantinescu579885a2015-02-22 20:51:33 +0000921CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
922 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100923 const CompilerOptions& compiler_options,
924 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100925 : CodeGenerator(graph,
926 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000927 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000928 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100929 callee_saved_core_registers.GetList(),
930 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100931 compiler_options,
932 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100933 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800934 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100935 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000936 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000937 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100938 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000939 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000940 uint32_literals_(std::less<uint32_t>(),
941 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100942 uint64_literals_(std::less<uint64_t>(),
943 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
944 method_patches_(MethodReferenceComparator(),
945 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
946 call_patches_(MethodReferenceComparator(),
947 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
948 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000949 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
950 boot_image_string_patches_(StringReferenceValueComparator(),
951 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
952 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100953 boot_image_type_patches_(TypeReferenceValueComparator(),
954 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
955 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000956 boot_image_address_patches_(std::less<uint32_t>(),
957 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000958 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000959 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000960}
Alexandre Rames5319def2014-10-23 10:03:10 +0100961
Alexandre Rames67555f72014-11-18 10:55:16 +0000962#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100963
Zheng Xu3927c8b2015-11-18 17:46:25 +0800964void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100965 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800966 jump_table->EmitTable(this);
967 }
968}
969
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000970void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800971 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000972 // Ensure we emit the literal pool.
973 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000974
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000975 CodeGenerator::Finalize(allocator);
976}
977
Zheng Xuad4450e2015-04-17 18:48:56 +0800978void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
979 // Note: There are 6 kinds of moves:
980 // 1. constant -> GPR/FPR (non-cycle)
981 // 2. constant -> stack (non-cycle)
982 // 3. GPR/FPR -> GPR/FPR
983 // 4. GPR/FPR -> stack
984 // 5. stack -> GPR/FPR
985 // 6. stack -> stack (non-cycle)
986 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
987 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
988 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
989 // dependency.
990 vixl_temps_.Open(GetVIXLAssembler());
991}
992
993void ParallelMoveResolverARM64::FinishEmitNativeCode() {
994 vixl_temps_.Close();
995}
996
997Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
998 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
999 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1000 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1001 Location scratch = GetScratchLocation(kind);
1002 if (!scratch.Equals(Location::NoLocation())) {
1003 return scratch;
1004 }
1005 // Allocate from VIXL temp registers.
1006 if (kind == Location::kRegister) {
1007 scratch = LocationFrom(vixl_temps_.AcquireX());
1008 } else {
1009 DCHECK(kind == Location::kFpuRegister);
1010 scratch = LocationFrom(vixl_temps_.AcquireD());
1011 }
1012 AddScratchLocation(scratch);
1013 return scratch;
1014}
1015
1016void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1017 if (loc.IsRegister()) {
1018 vixl_temps_.Release(XRegisterFrom(loc));
1019 } else {
1020 DCHECK(loc.IsFpuRegister());
1021 vixl_temps_.Release(DRegisterFrom(loc));
1022 }
1023 RemoveScratchLocation(loc);
1024}
1025
Alexandre Rames3e69f162014-12-10 10:36:50 +00001026void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001027 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001028 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001029}
1030
Alexandre Rames5319def2014-10-23 10:03:10 +01001031void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001032 MacroAssembler* masm = GetVIXLAssembler();
1033 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001034 __ Bind(&frame_entry_label_);
1035
Serban Constantinescu02164b32014-11-13 14:05:07 +00001036 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1037 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001038 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001039 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001040 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001041 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001042 __ Ldr(wzr, MemOperand(temp, 0));
1043 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001044 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001045
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001046 if (!HasEmptyFrame()) {
1047 int frame_size = GetFrameSize();
1048 // Stack layout:
1049 // sp[frame_size - 8] : lr.
1050 // ... : other preserved core registers.
1051 // ... : other preserved fp registers.
1052 // ... : reserved frame space.
1053 // sp[0] : current method.
1054 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001055 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001056 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1057 frame_size - GetCoreSpillSize());
1058 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1059 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001060 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001061}
1062
1063void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001064 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001065 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001066 if (!HasEmptyFrame()) {
1067 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001068 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1069 frame_size - FrameEntrySpillSize());
1070 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1071 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001072 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001074 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001075 __ Ret();
1076 GetAssembler()->cfi().RestoreState();
1077 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001078}
1079
Scott Wakeling97c72b72016-06-24 16:19:36 +01001080CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001081 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001082 return CPURegList(CPURegister::kRegister, kXRegSize,
1083 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001084}
1085
Scott Wakeling97c72b72016-06-24 16:19:36 +01001086CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001087 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1088 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001089 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1090 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001091}
1092
Alexandre Rames5319def2014-10-23 10:03:10 +01001093void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1094 __ Bind(GetLabelOf(block));
1095}
1096
Calin Juravle175dc732015-08-25 15:42:32 +01001097void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1098 DCHECK(location.IsRegister());
1099 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1100}
1101
Calin Juravlee460d1d2015-09-29 04:52:17 +01001102void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1103 if (location.IsRegister()) {
1104 locations->AddTemp(location);
1105 } else {
1106 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1107 }
1108}
1109
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001110void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001111 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001112 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001113 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001114 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001115 if (value_can_be_null) {
1116 __ Cbz(value, &done);
1117 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1119 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001120 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001121 if (value_can_be_null) {
1122 __ Bind(&done);
1123 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001124}
1125
David Brazdil58282f42016-01-14 12:45:10 +00001126void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001127 // Blocked core registers:
1128 // lr : Runtime reserved.
1129 // tr : Runtime reserved.
1130 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1131 // ip1 : VIXL core temp.
1132 // ip0 : VIXL core temp.
1133 //
1134 // Blocked fp registers:
1135 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001136 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1137 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001138 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001139 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001140 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001141
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001142 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001143 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001144 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001145 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001146
David Brazdil58282f42016-01-14 12:45:10 +00001147 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001148 // Stubs do not save callee-save floating point registers. If the graph
1149 // is debuggable, we need to deal with these registers differently. For
1150 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001151 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1152 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001153 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001154 }
1155 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001156}
1157
Alexandre Rames3e69f162014-12-10 10:36:50 +00001158size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1159 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1160 __ Str(reg, MemOperand(sp, stack_index));
1161 return kArm64WordSize;
1162}
1163
1164size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1165 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1166 __ Ldr(reg, MemOperand(sp, stack_index));
1167 return kArm64WordSize;
1168}
1169
1170size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1171 FPRegister reg = FPRegister(reg_id, kDRegSize);
1172 __ Str(reg, MemOperand(sp, stack_index));
1173 return kArm64WordSize;
1174}
1175
1176size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1177 FPRegister reg = FPRegister(reg_id, kDRegSize);
1178 __ Ldr(reg, MemOperand(sp, stack_index));
1179 return kArm64WordSize;
1180}
1181
Alexandre Rames5319def2014-10-23 10:03:10 +01001182void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001183 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001184}
1185
1186void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001187 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001188}
1189
Alexandre Rames67555f72014-11-18 10:55:16 +00001190void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001191 if (constant->IsIntConstant()) {
1192 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1193 } else if (constant->IsLongConstant()) {
1194 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1195 } else if (constant->IsNullConstant()) {
1196 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001197 } else if (constant->IsFloatConstant()) {
1198 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1199 } else {
1200 DCHECK(constant->IsDoubleConstant());
1201 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1202 }
1203}
1204
Alexandre Rames3e69f162014-12-10 10:36:50 +00001205
1206static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1207 DCHECK(constant.IsConstant());
1208 HConstant* cst = constant.GetConstant();
1209 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001210 // Null is mapped to a core W register, which we associate with kPrimInt.
1211 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1213 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1214 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1215}
1216
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217void CodeGeneratorARM64::MoveLocation(Location destination,
1218 Location source,
1219 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001220 if (source.Equals(destination)) {
1221 return;
1222 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001223
1224 // A valid move can always be inferred from the destination and source
1225 // locations. When moving from and to a register, the argument type can be
1226 // used to generate 32bit instead of 64bit moves. In debug mode we also
1227 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229
1230 if (destination.IsRegister() || destination.IsFpuRegister()) {
1231 if (unspecified_type) {
1232 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1233 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001234 (src_cst != nullptr && (src_cst->IsIntConstant()
1235 || src_cst->IsFloatConstant()
1236 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001237 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001238 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001239 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 // If the source is a double stack slot or a 64bit constant, a 64bit
1241 // type is appropriate. Else the source is a register, and since the
1242 // type has not been specified, we chose a 64bit type to force a 64bit
1243 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001245 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001246 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1248 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1249 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001250 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1251 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1252 __ Ldr(dst, StackOperandFrom(source));
1253 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001255 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001256 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001257 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001259 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001260 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1262 ? Primitive::kPrimLong
1263 : Primitive::kPrimInt;
1264 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1265 }
1266 } else {
1267 DCHECK(source.IsFpuRegister());
1268 if (destination.IsRegister()) {
1269 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1270 ? Primitive::kPrimDouble
1271 : Primitive::kPrimFloat;
1272 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1273 } else {
1274 DCHECK(destination.IsFpuRegister());
1275 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001276 }
1277 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001278 } else { // The destination is not a register. It must be a stack slot.
1279 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1280 if (source.IsRegister() || source.IsFpuRegister()) {
1281 if (unspecified_type) {
1282 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001284 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001285 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001286 }
1287 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001288 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1289 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1290 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001291 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001292 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1293 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001294 UseScratchRegisterScope temps(GetVIXLAssembler());
1295 HConstant* src_cst = source.GetConstant();
1296 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001297 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001298 temp = temps.AcquireW();
1299 } else if (src_cst->IsLongConstant()) {
1300 temp = temps.AcquireX();
1301 } else if (src_cst->IsFloatConstant()) {
1302 temp = temps.AcquireS();
1303 } else {
1304 DCHECK(src_cst->IsDoubleConstant());
1305 temp = temps.AcquireD();
1306 }
1307 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001308 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001309 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001310 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001311 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001312 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001313 // There is generally less pressure on FP registers.
1314 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001315 __ Ldr(temp, StackOperandFrom(source));
1316 __ Str(temp, StackOperandFrom(destination));
1317 }
1318 }
1319}
1320
1321void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001322 CPURegister dst,
1323 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001324 switch (type) {
1325 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 break;
1328 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001329 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001330 break;
1331 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001332 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001333 break;
1334 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001335 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001336 break;
1337 case Primitive::kPrimInt:
1338 case Primitive::kPrimNot:
1339 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001340 case Primitive::kPrimFloat:
1341 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001342 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001343 __ Ldr(dst, src);
1344 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001345 case Primitive::kPrimVoid:
1346 LOG(FATAL) << "Unreachable type " << type;
1347 }
1348}
1349
Calin Juravle77520bc2015-01-12 18:45:46 +00001350void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001351 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001352 const MemOperand& src,
1353 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001354 MacroAssembler* masm = GetVIXLAssembler();
1355 BlockPoolsScope block_pools(masm);
1356 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001357 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001358 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001359
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001360 DCHECK(!src.IsPreIndex());
1361 DCHECK(!src.IsPostIndex());
1362
1363 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001364 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001365 MemOperand base = MemOperand(temp_base);
1366 switch (type) {
1367 case Primitive::kPrimBoolean:
1368 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001369 if (needs_null_check) {
1370 MaybeRecordImplicitNullCheck(instruction);
1371 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001372 break;
1373 case Primitive::kPrimByte:
1374 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001375 if (needs_null_check) {
1376 MaybeRecordImplicitNullCheck(instruction);
1377 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001378 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1379 break;
1380 case Primitive::kPrimChar:
1381 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001382 if (needs_null_check) {
1383 MaybeRecordImplicitNullCheck(instruction);
1384 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001385 break;
1386 case Primitive::kPrimShort:
1387 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001388 if (needs_null_check) {
1389 MaybeRecordImplicitNullCheck(instruction);
1390 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1392 break;
1393 case Primitive::kPrimInt:
1394 case Primitive::kPrimNot:
1395 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001396 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001397 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001398 if (needs_null_check) {
1399 MaybeRecordImplicitNullCheck(instruction);
1400 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001401 break;
1402 case Primitive::kPrimFloat:
1403 case Primitive::kPrimDouble: {
1404 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001405 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406
1407 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1408 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001409 if (needs_null_check) {
1410 MaybeRecordImplicitNullCheck(instruction);
1411 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001412 __ Fmov(FPRegister(dst), temp);
1413 break;
1414 }
1415 case Primitive::kPrimVoid:
1416 LOG(FATAL) << "Unreachable type " << type;
1417 }
1418}
1419
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001420void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421 CPURegister src,
1422 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001423 switch (type) {
1424 case Primitive::kPrimBoolean:
1425 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001426 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001427 break;
1428 case Primitive::kPrimChar:
1429 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001430 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001431 break;
1432 case Primitive::kPrimInt:
1433 case Primitive::kPrimNot:
1434 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001435 case Primitive::kPrimFloat:
1436 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001437 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001438 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001439 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001440 case Primitive::kPrimVoid:
1441 LOG(FATAL) << "Unreachable type " << type;
1442 }
1443}
1444
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001445void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1446 CPURegister src,
1447 const MemOperand& dst) {
1448 UseScratchRegisterScope temps(GetVIXLAssembler());
1449 Register temp_base = temps.AcquireX();
1450
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001451 DCHECK(!dst.IsPreIndex());
1452 DCHECK(!dst.IsPostIndex());
1453
1454 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001455 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001456 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001457 MemOperand base = MemOperand(temp_base);
1458 switch (type) {
1459 case Primitive::kPrimBoolean:
1460 case Primitive::kPrimByte:
1461 __ Stlrb(Register(src), base);
1462 break;
1463 case Primitive::kPrimChar:
1464 case Primitive::kPrimShort:
1465 __ Stlrh(Register(src), base);
1466 break;
1467 case Primitive::kPrimInt:
1468 case Primitive::kPrimNot:
1469 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001470 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001471 __ Stlr(Register(src), base);
1472 break;
1473 case Primitive::kPrimFloat:
1474 case Primitive::kPrimDouble: {
1475 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001476 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001477
1478 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1479 __ Fmov(temp, FPRegister(src));
1480 __ Stlr(temp, base);
1481 break;
1482 }
1483 case Primitive::kPrimVoid:
1484 LOG(FATAL) << "Unreachable type " << type;
1485 }
1486}
1487
Calin Juravle175dc732015-08-25 15:42:32 +01001488void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1489 HInstruction* instruction,
1490 uint32_t dex_pc,
1491 SlowPathCode* slow_path) {
1492 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1493 instruction,
1494 dex_pc,
1495 slow_path);
1496}
1497
Alexandre Rames67555f72014-11-18 10:55:16 +00001498void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1499 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001500 uint32_t dex_pc,
1501 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001502 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001503 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1505 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001506 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001507}
1508
1509void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001510 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001511 UseScratchRegisterScope temps(GetVIXLAssembler());
1512 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001513 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1514
Serban Constantinescu02164b32014-11-13 14:05:07 +00001515 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001516 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1517 __ Add(temp, class_reg, status_offset);
1518 __ Ldar(temp, HeapOperand(temp));
1519 __ Cmp(temp, mirror::Class::kStatusInitialized);
1520 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001521 __ Bind(slow_path->GetExitLabel());
1522}
Alexandre Rames5319def2014-10-23 10:03:10 +01001523
Roland Levillain44015862016-01-22 11:47:17 +00001524void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001525 BarrierType type = BarrierAll;
1526
1527 switch (kind) {
1528 case MemBarrierKind::kAnyAny:
1529 case MemBarrierKind::kAnyStore: {
1530 type = BarrierAll;
1531 break;
1532 }
1533 case MemBarrierKind::kLoadAny: {
1534 type = BarrierReads;
1535 break;
1536 }
1537 case MemBarrierKind::kStoreStore: {
1538 type = BarrierWrites;
1539 break;
1540 }
1541 default:
1542 LOG(FATAL) << "Unexpected memory barrier " << kind;
1543 }
1544 __ Dmb(InnerShareable, type);
1545}
1546
Serban Constantinescu02164b32014-11-13 14:05:07 +00001547void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1548 HBasicBlock* successor) {
1549 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001550 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1551 if (slow_path == nullptr) {
1552 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1553 instruction->SetSlowPath(slow_path);
1554 codegen_->AddSlowPath(slow_path);
1555 if (successor != nullptr) {
1556 DCHECK(successor->IsLoopHeader());
1557 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1558 }
1559 } else {
1560 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1561 }
1562
Serban Constantinescu02164b32014-11-13 14:05:07 +00001563 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1564 Register temp = temps.AcquireW();
1565
1566 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1567 if (successor == nullptr) {
1568 __ Cbnz(temp, slow_path->GetEntryLabel());
1569 __ Bind(slow_path->GetReturnLabel());
1570 } else {
1571 __ Cbz(temp, codegen_->GetLabelOf(successor));
1572 __ B(slow_path->GetEntryLabel());
1573 // slow_path will return to GetLabelOf(successor).
1574 }
1575}
1576
Alexandre Rames5319def2014-10-23 10:03:10 +01001577InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1578 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001579 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001580 assembler_(codegen->GetAssembler()),
1581 codegen_(codegen) {}
1582
1583#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001584 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001585
1586#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1587
1588enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001589 // Using a base helps identify when we hit such breakpoints.
1590 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001591#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1592 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1593#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1594};
1595
1596#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001597 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001598 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1599 } \
1600 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1602 locations->SetOut(Location::Any()); \
1603 }
1604 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1605#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1606
1607#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001608#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001609
Alexandre Rames67555f72014-11-18 10:55:16 +00001610void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001611 DCHECK_EQ(instr->InputCount(), 2U);
1612 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1613 Primitive::Type type = instr->GetResultType();
1614 switch (type) {
1615 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001616 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001617 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001618 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001620 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001621
1622 case Primitive::kPrimFloat:
1623 case Primitive::kPrimDouble:
1624 locations->SetInAt(0, Location::RequiresFpuRegister());
1625 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001626 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001627 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001628
Alexandre Rames5319def2014-10-23 10:03:10 +01001629 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001630 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001631 }
1632}
1633
Alexandre Rames09a99962015-04-15 11:47:56 +01001634void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001635 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1636
1637 bool object_field_get_with_read_barrier =
1638 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001639 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001640 new (GetGraph()->GetArena()) LocationSummary(instruction,
1641 object_field_get_with_read_barrier ?
1642 LocationSummary::kCallOnSlowPath :
1643 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001644 locations->SetInAt(0, Location::RequiresRegister());
1645 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1646 locations->SetOut(Location::RequiresFpuRegister());
1647 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001648 // The output overlaps for an object field get when read barriers
1649 // are enabled: we do not want the load to overwrite the object's
1650 // location, as we need it to emit the read barrier.
1651 locations->SetOut(
1652 Location::RequiresRegister(),
1653 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001654 }
1655}
1656
1657void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1658 const FieldInfo& field_info) {
1659 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001660 LocationSummary* locations = instruction->GetLocations();
1661 Location base_loc = locations->InAt(0);
1662 Location out = locations->Out();
1663 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001664 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001665 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001666 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001667
Roland Levillain44015862016-01-22 11:47:17 +00001668 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1669 // Object FieldGet with Baker's read barrier case.
1670 MacroAssembler* masm = GetVIXLAssembler();
1671 UseScratchRegisterScope temps(masm);
1672 // /* HeapReference<Object> */ out = *(base + offset)
1673 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1674 Register temp = temps.AcquireW();
1675 // Note that potential implicit null checks are handled in this
1676 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1677 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1678 instruction,
1679 out,
1680 base,
1681 offset,
1682 temp,
1683 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001684 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001685 } else {
1686 // General case.
1687 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001688 // Note that a potential implicit null check is handled in this
1689 // CodeGeneratorARM64::LoadAcquire call.
1690 // NB: LoadAcquire will record the pc info if needed.
1691 codegen_->LoadAcquire(
1692 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001693 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001694 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001695 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001696 }
Roland Levillain44015862016-01-22 11:47:17 +00001697 if (field_type == Primitive::kPrimNot) {
1698 // If read barriers are enabled, emit read barriers other than
1699 // Baker's using a slow path (and also unpoison the loaded
1700 // reference, if heap poisoning is enabled).
1701 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1702 }
Roland Levillain4d027112015-07-01 15:41:14 +01001703 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001704}
1705
1706void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1707 LocationSummary* locations =
1708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1709 locations->SetInAt(0, Location::RequiresRegister());
1710 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1711 locations->SetInAt(1, Location::RequiresFpuRegister());
1712 } else {
1713 locations->SetInAt(1, Location::RequiresRegister());
1714 }
1715}
1716
1717void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001718 const FieldInfo& field_info,
1719 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001720 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001721 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001722
1723 Register obj = InputRegisterAt(instruction, 0);
1724 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001725 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001726 Offset offset = field_info.GetFieldOffset();
1727 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001728
Roland Levillain4d027112015-07-01 15:41:14 +01001729 {
1730 // We use a block to end the scratch scope before the write barrier, thus
1731 // freeing the temporary registers so they can be used in `MarkGCCard`.
1732 UseScratchRegisterScope temps(GetVIXLAssembler());
1733
1734 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1735 DCHECK(value.IsW());
1736 Register temp = temps.AcquireW();
1737 __ Mov(temp, value.W());
1738 GetAssembler()->PoisonHeapReference(temp.W());
1739 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001740 }
Roland Levillain4d027112015-07-01 15:41:14 +01001741
1742 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001743 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1744 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001745 } else {
1746 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1747 codegen_->MaybeRecordImplicitNullCheck(instruction);
1748 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001749 }
1750
1751 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001752 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001753 }
1754}
1755
Alexandre Rames67555f72014-11-18 10:55:16 +00001756void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001757 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001758
1759 switch (type) {
1760 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001761 case Primitive::kPrimLong: {
1762 Register dst = OutputRegister(instr);
1763 Register lhs = InputRegisterAt(instr, 0);
1764 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001765 if (instr->IsAdd()) {
1766 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001767 } else if (instr->IsAnd()) {
1768 __ And(dst, lhs, rhs);
1769 } else if (instr->IsOr()) {
1770 __ Orr(dst, lhs, rhs);
1771 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001772 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001773 } else if (instr->IsRor()) {
1774 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001775 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001776 __ Ror(dst, lhs, shift);
1777 } else {
1778 // Ensure shift distance is in the same size register as the result. If
1779 // we are rotating a long and the shift comes in a w register originally,
1780 // we don't need to sxtw for use as an x since the shift distances are
1781 // all & reg_bits - 1.
1782 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1783 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001784 } else {
1785 DCHECK(instr->IsXor());
1786 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001787 }
1788 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001789 }
1790 case Primitive::kPrimFloat:
1791 case Primitive::kPrimDouble: {
1792 FPRegister dst = OutputFPRegister(instr);
1793 FPRegister lhs = InputFPRegisterAt(instr, 0);
1794 FPRegister rhs = InputFPRegisterAt(instr, 1);
1795 if (instr->IsAdd()) {
1796 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001797 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001798 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001799 } else {
1800 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001801 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001802 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001803 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001804 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001805 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001806 }
1807}
1808
Serban Constantinescu02164b32014-11-13 14:05:07 +00001809void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1810 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1811
1812 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1813 Primitive::Type type = instr->GetResultType();
1814 switch (type) {
1815 case Primitive::kPrimInt:
1816 case Primitive::kPrimLong: {
1817 locations->SetInAt(0, Location::RequiresRegister());
1818 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1819 locations->SetOut(Location::RequiresRegister());
1820 break;
1821 }
1822 default:
1823 LOG(FATAL) << "Unexpected shift type " << type;
1824 }
1825}
1826
1827void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1828 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1829
1830 Primitive::Type type = instr->GetType();
1831 switch (type) {
1832 case Primitive::kPrimInt:
1833 case Primitive::kPrimLong: {
1834 Register dst = OutputRegister(instr);
1835 Register lhs = InputRegisterAt(instr, 0);
1836 Operand rhs = InputOperandAt(instr, 1);
1837 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001838 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001839 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001840 if (instr->IsShl()) {
1841 __ Lsl(dst, lhs, shift_value);
1842 } else if (instr->IsShr()) {
1843 __ Asr(dst, lhs, shift_value);
1844 } else {
1845 __ Lsr(dst, lhs, shift_value);
1846 }
1847 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001848 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001849
1850 if (instr->IsShl()) {
1851 __ Lsl(dst, lhs, rhs_reg);
1852 } else if (instr->IsShr()) {
1853 __ Asr(dst, lhs, rhs_reg);
1854 } else {
1855 __ Lsr(dst, lhs, rhs_reg);
1856 }
1857 }
1858 break;
1859 }
1860 default:
1861 LOG(FATAL) << "Unexpected shift operation type " << type;
1862 }
1863}
1864
Alexandre Rames5319def2014-10-23 10:03:10 +01001865void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001866 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001867}
1868
1869void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001870 HandleBinaryOp(instruction);
1871}
1872
1873void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1874 HandleBinaryOp(instruction);
1875}
1876
1877void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1878 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001879}
1880
Artem Serov7fc63502016-02-09 17:15:29 +00001881void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001882 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1883 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1884 locations->SetInAt(0, Location::RequiresRegister());
1885 // There is no immediate variant of negated bitwise instructions in AArch64.
1886 locations->SetInAt(1, Location::RequiresRegister());
1887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1888}
1889
Artem Serov7fc63502016-02-09 17:15:29 +00001890void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001891 Register dst = OutputRegister(instr);
1892 Register lhs = InputRegisterAt(instr, 0);
1893 Register rhs = InputRegisterAt(instr, 1);
1894
1895 switch (instr->GetOpKind()) {
1896 case HInstruction::kAnd:
1897 __ Bic(dst, lhs, rhs);
1898 break;
1899 case HInstruction::kOr:
1900 __ Orn(dst, lhs, rhs);
1901 break;
1902 case HInstruction::kXor:
1903 __ Eon(dst, lhs, rhs);
1904 break;
1905 default:
1906 LOG(FATAL) << "Unreachable";
1907 }
1908}
1909
Alexandre Rames8626b742015-11-25 16:28:08 +00001910void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1911 HArm64DataProcWithShifterOp* instruction) {
1912 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1913 instruction->GetType() == Primitive::kPrimLong);
1914 LocationSummary* locations =
1915 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1916 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1917 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1918 } else {
1919 locations->SetInAt(0, Location::RequiresRegister());
1920 }
1921 locations->SetInAt(1, Location::RequiresRegister());
1922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1923}
1924
1925void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1926 HArm64DataProcWithShifterOp* instruction) {
1927 Primitive::Type type = instruction->GetType();
1928 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1929 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1930 Register out = OutputRegister(instruction);
1931 Register left;
1932 if (kind != HInstruction::kNeg) {
1933 left = InputRegisterAt(instruction, 0);
1934 }
1935 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1936 // shifter operand operation, the IR generating `right_reg` (input to the type
1937 // conversion) can have a different type from the current instruction's type,
1938 // so we manually indicate the type.
1939 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001940 int64_t shift_amount = instruction->GetShiftAmount() &
1941 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001942
1943 Operand right_operand(0);
1944
1945 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1946 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1947 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1948 } else {
1949 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1950 }
1951
1952 // Logical binary operations do not support extension operations in the
1953 // operand. Note that VIXL would still manage if it was passed by generating
1954 // the extension as a separate instruction.
1955 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1956 DCHECK(!right_operand.IsExtendedRegister() ||
1957 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1958 kind != HInstruction::kNeg));
1959 switch (kind) {
1960 case HInstruction::kAdd:
1961 __ Add(out, left, right_operand);
1962 break;
1963 case HInstruction::kAnd:
1964 __ And(out, left, right_operand);
1965 break;
1966 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001967 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001968 __ Neg(out, right_operand);
1969 break;
1970 case HInstruction::kOr:
1971 __ Orr(out, left, right_operand);
1972 break;
1973 case HInstruction::kSub:
1974 __ Sub(out, left, right_operand);
1975 break;
1976 case HInstruction::kXor:
1977 __ Eor(out, left, right_operand);
1978 break;
1979 default:
1980 LOG(FATAL) << "Unexpected operation kind: " << kind;
1981 UNREACHABLE();
1982 }
1983}
1984
Artem Serov328429f2016-07-06 16:23:04 +01001985void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
1986 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001987 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1990 locations->SetInAt(0, Location::RequiresRegister());
1991 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1992 locations->SetOut(Location::RequiresRegister());
1993}
1994
Artem Serov328429f2016-07-06 16:23:04 +01001995void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1996 HIntermediateAddress* instruction) {
1997 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001998 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001999 __ Add(OutputRegister(instruction),
2000 InputRegisterAt(instruction, 0),
2001 Operand(InputOperandAt(instruction, 1)));
2002}
2003
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002004void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002005 LocationSummary* locations =
2006 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002007 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2008 if (instr->GetOpKind() == HInstruction::kSub &&
2009 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002010 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002011 // Don't allocate register for Mneg instruction.
2012 } else {
2013 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2014 Location::RequiresRegister());
2015 }
2016 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2017 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2019}
2020
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002021void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002022 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002023 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2024 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002025
2026 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2027 // This fixup should be carried out for all multiply-accumulate instructions:
2028 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2029 if (instr->GetType() == Primitive::kPrimLong &&
2030 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2031 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002032 vixl::aarch64::Instruction* prev =
2033 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002034 if (prev->IsLoadOrStore()) {
2035 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002036 vixl::aarch64::CodeBufferCheckScope scope(masm,
2037 kInstructionSize,
2038 vixl::aarch64::CodeBufferCheckScope::kCheck,
2039 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002040 __ nop();
2041 }
2042 }
2043
2044 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002045 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002046 __ Madd(res, mul_left, mul_right, accumulator);
2047 } else {
2048 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002049 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002050 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002051 __ Mneg(res, mul_left, mul_right);
2052 } else {
2053 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2054 __ Msub(res, mul_left, mul_right, accumulator);
2055 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002056 }
2057}
2058
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002059void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002060 bool object_array_get_with_read_barrier =
2061 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002062 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002063 new (GetGraph()->GetArena()) LocationSummary(instruction,
2064 object_array_get_with_read_barrier ?
2065 LocationSummary::kCallOnSlowPath :
2066 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002067 locations->SetInAt(0, Location::RequiresRegister());
2068 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002069 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2070 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2071 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002072 // The output overlaps in the case of an object array get with
2073 // read barriers enabled: we do not want the move to overwrite the
2074 // array's location, as we need it to emit the read barrier.
2075 locations->SetOut(
2076 Location::RequiresRegister(),
2077 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002078 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002079}
2080
2081void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002082 Primitive::Type type = instruction->GetType();
2083 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002084 LocationSummary* locations = instruction->GetLocations();
2085 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002086 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002087 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002088
Alexandre Ramesd921d642015-04-16 15:07:16 +01002089 MacroAssembler* masm = GetVIXLAssembler();
2090 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002091 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002092 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002093
Roland Levillain44015862016-01-22 11:47:17 +00002094 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2095 // Object ArrayGet with Baker's read barrier case.
2096 Register temp = temps.AcquireW();
Artem Serov328429f2016-07-06 16:23:04 +01002097 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2098 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002099 // Note that a potential implicit null check is handled in the
2100 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2101 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2102 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002103 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002104 // General case.
2105 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002106 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002107 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2108 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002109 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002110 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002111 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002112 // The read barrier instrumentation does not support the
Artem Serov328429f2016-07-06 16:23:04 +01002113 // HIntermediateAddress instruction yet.
Roland Levillain44015862016-01-22 11:47:17 +00002114 DCHECK(!kEmitCompilerReadBarrier);
2115 // We do not need to compute the intermediate address from the array: the
2116 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002117 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002118 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002119 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002120 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2121 }
2122 temp = obj;
2123 } else {
2124 __ Add(temp, obj, offset);
2125 }
2126 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2127 }
2128
2129 codegen_->Load(type, OutputCPURegister(instruction), source);
2130 codegen_->MaybeRecordImplicitNullCheck(instruction);
2131
2132 if (type == Primitive::kPrimNot) {
2133 static_assert(
2134 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2135 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2136 Location obj_loc = locations->InAt(0);
2137 if (index.IsConstant()) {
2138 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2139 } else {
2140 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2141 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002142 }
Roland Levillain4d027112015-07-01 15:41:14 +01002143 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002144}
2145
Alexandre Rames5319def2014-10-23 10:03:10 +01002146void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2148 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002150}
2151
2152void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002153 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002154 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002155 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002156 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002157}
2158
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002159void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002160 Primitive::Type value_type = instruction->GetComponentType();
2161
2162 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2163 bool object_array_set_with_read_barrier =
2164 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2166 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002167 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2168 LocationSummary::kCallOnSlowPath :
2169 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002170 locations->SetInAt(0, Location::RequiresRegister());
2171 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002172 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002173 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002174 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002175 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002176 }
2177}
2178
2179void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2180 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002181 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002182 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002183 bool needs_write_barrier =
2184 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002185
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002186 Register array = InputRegisterAt(instruction, 0);
2187 CPURegister value = InputCPURegisterAt(instruction, 2);
2188 CPURegister source = value;
2189 Location index = locations->InAt(1);
2190 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2191 MemOperand destination = HeapOperand(array);
2192 MacroAssembler* masm = GetVIXLAssembler();
2193 BlockPoolsScope block_pools(masm);
2194
2195 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002196 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002197 if (index.IsConstant()) {
2198 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2199 destination = HeapOperand(array, offset);
2200 } else {
2201 UseScratchRegisterScope temps(masm);
2202 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002203 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002204 // The read barrier instrumentation does not support the
Artem Serov328429f2016-07-06 16:23:04 +01002205 // HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002206 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002207 // We do not need to compute the intermediate address from the array: the
2208 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002209 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002210 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002211 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002212 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2213 }
2214 temp = array;
2215 } else {
2216 __ Add(temp, array, offset);
2217 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002218 destination = HeapOperand(temp,
2219 XRegisterFrom(index),
2220 LSL,
2221 Primitive::ComponentSizeShift(value_type));
2222 }
2223 codegen_->Store(value_type, value, destination);
2224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002225 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002226 DCHECK(needs_write_barrier);
Artem Serov328429f2016-07-06 16:23:04 +01002227 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002228 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002229 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002230 {
2231 // We use a block to end the scratch scope before the write barrier, thus
2232 // freeing the temporary registers so they can be used in `MarkGCCard`.
2233 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002234 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002235 if (index.IsConstant()) {
2236 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002237 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002238 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002239 destination = HeapOperand(temp,
2240 XRegisterFrom(index),
2241 LSL,
2242 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002243 }
2244
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002245 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2246 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2247 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2248
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002249 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002250 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2251 codegen_->AddSlowPath(slow_path);
2252 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002253 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002254 __ Cbnz(Register(value), &non_zero);
2255 if (!index.IsConstant()) {
2256 __ Add(temp, array, offset);
2257 }
2258 __ Str(wzr, destination);
2259 codegen_->MaybeRecordImplicitNullCheck(instruction);
2260 __ B(&done);
2261 __ Bind(&non_zero);
2262 }
2263
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002264 if (kEmitCompilerReadBarrier) {
2265 // When read barriers are enabled, the type checking
2266 // instrumentation requires two read barriers:
2267 //
2268 // __ Mov(temp2, temp);
2269 // // /* HeapReference<Class> */ temp = temp->component_type_
2270 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002271 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002272 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2273 //
2274 // // /* HeapReference<Class> */ temp2 = value->klass_
2275 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002276 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002277 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2278 //
2279 // __ Cmp(temp, temp2);
2280 //
2281 // However, the second read barrier may trash `temp`, as it
2282 // is a temporary register, and as such would not be saved
2283 // along with live registers before calling the runtime (nor
2284 // restored afterwards). So in this case, we bail out and
2285 // delegate the work to the array set slow path.
2286 //
2287 // TODO: Extend the register allocator to support a new
2288 // "(locally) live temp" location so as to avoid always
2289 // going into the slow path when read barriers are enabled.
2290 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002291 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002292 Register temp2 = temps.AcquireSameSizeAs(array);
2293 // /* HeapReference<Class> */ temp = array->klass_
2294 __ Ldr(temp, HeapOperand(array, class_offset));
2295 codegen_->MaybeRecordImplicitNullCheck(instruction);
2296 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2297
2298 // /* HeapReference<Class> */ temp = temp->component_type_
2299 __ Ldr(temp, HeapOperand(temp, component_offset));
2300 // /* HeapReference<Class> */ temp2 = value->klass_
2301 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2302 // If heap poisoning is enabled, no need to unpoison `temp`
2303 // nor `temp2`, as we are comparing two poisoned references.
2304 __ Cmp(temp, temp2);
2305
2306 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002307 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002308 __ B(eq, &do_put);
2309 // If heap poisoning is enabled, the `temp` reference has
2310 // not been unpoisoned yet; unpoison it now.
2311 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2312
2313 // /* HeapReference<Class> */ temp = temp->super_class_
2314 __ Ldr(temp, HeapOperand(temp, super_offset));
2315 // If heap poisoning is enabled, no need to unpoison
2316 // `temp`, as we are comparing against null below.
2317 __ Cbnz(temp, slow_path->GetEntryLabel());
2318 __ Bind(&do_put);
2319 } else {
2320 __ B(ne, slow_path->GetEntryLabel());
2321 }
2322 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002323 }
2324 }
2325
2326 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002327 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002328 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002329 __ Mov(temp2, value.W());
2330 GetAssembler()->PoisonHeapReference(temp2);
2331 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002332 }
2333
2334 if (!index.IsConstant()) {
2335 __ Add(temp, array, offset);
2336 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002337 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002338
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002339 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002340 codegen_->MaybeRecordImplicitNullCheck(instruction);
2341 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002342 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002343
2344 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2345
2346 if (done.IsLinked()) {
2347 __ Bind(&done);
2348 }
2349
2350 if (slow_path != nullptr) {
2351 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002352 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002353 }
2354}
2355
Alexandre Rames67555f72014-11-18 10:55:16 +00002356void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002357 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2358 ? LocationSummary::kCallOnSlowPath
2359 : LocationSummary::kNoCall;
2360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002361 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002362 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002363 if (instruction->HasUses()) {
2364 locations->SetOut(Location::SameAsFirstInput());
2365 }
2366}
2367
2368void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002369 BoundsCheckSlowPathARM64* slow_path =
2370 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002371 codegen_->AddSlowPath(slow_path);
2372
2373 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2374 __ B(slow_path->GetEntryLabel(), hs);
2375}
2376
Alexandre Rames67555f72014-11-18 10:55:16 +00002377void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2378 LocationSummary* locations =
2379 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2380 locations->SetInAt(0, Location::RequiresRegister());
2381 if (check->HasUses()) {
2382 locations->SetOut(Location::SameAsFirstInput());
2383 }
2384}
2385
2386void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2387 // We assume the class is not null.
2388 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2389 check->GetLoadClass(), check, check->GetDexPc(), true);
2390 codegen_->AddSlowPath(slow_path);
2391 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2392}
2393
Roland Levillain1a653882016-03-18 18:05:57 +00002394static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2395 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2396 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2397}
2398
2399void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2400 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2401 Location rhs_loc = instruction->GetLocations()->InAt(1);
2402 if (rhs_loc.IsConstant()) {
2403 // 0.0 is the only immediate that can be encoded directly in
2404 // an FCMP instruction.
2405 //
2406 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2407 // specify that in a floating-point comparison, positive zero
2408 // and negative zero are considered equal, so we can use the
2409 // literal 0.0 for both cases here.
2410 //
2411 // Note however that some methods (Float.equal, Float.compare,
2412 // Float.compareTo, Double.equal, Double.compare,
2413 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2414 // StrictMath.min) consider 0.0 to be (strictly) greater than
2415 // -0.0. So if we ever translate calls to these methods into a
2416 // HCompare instruction, we must handle the -0.0 case with
2417 // care here.
2418 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2419 __ Fcmp(lhs_reg, 0.0);
2420 } else {
2421 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2422 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002423}
2424
Serban Constantinescu02164b32014-11-13 14:05:07 +00002425void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002426 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002427 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2428 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002429 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002430 case Primitive::kPrimBoolean:
2431 case Primitive::kPrimByte:
2432 case Primitive::kPrimShort:
2433 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002434 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002435 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002436 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002437 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2439 break;
2440 }
2441 case Primitive::kPrimFloat:
2442 case Primitive::kPrimDouble: {
2443 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002444 locations->SetInAt(1,
2445 IsFloatingPointZeroConstant(compare->InputAt(1))
2446 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2447 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002448 locations->SetOut(Location::RequiresRegister());
2449 break;
2450 }
2451 default:
2452 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2453 }
2454}
2455
2456void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2457 Primitive::Type in_type = compare->InputAt(0)->GetType();
2458
2459 // 0 if: left == right
2460 // 1 if: left > right
2461 // -1 if: left < right
2462 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002463 case Primitive::kPrimBoolean:
2464 case Primitive::kPrimByte:
2465 case Primitive::kPrimShort:
2466 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002467 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002468 case Primitive::kPrimLong: {
2469 Register result = OutputRegister(compare);
2470 Register left = InputRegisterAt(compare, 0);
2471 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002472 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002473 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2474 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002475 break;
2476 }
2477 case Primitive::kPrimFloat:
2478 case Primitive::kPrimDouble: {
2479 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002480 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002481 __ Cset(result, ne);
2482 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002483 break;
2484 }
2485 default:
2486 LOG(FATAL) << "Unimplemented compare type " << in_type;
2487 }
2488}
2489
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002490void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002491 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002492
2493 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2494 locations->SetInAt(0, Location::RequiresFpuRegister());
2495 locations->SetInAt(1,
2496 IsFloatingPointZeroConstant(instruction->InputAt(1))
2497 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2498 : Location::RequiresFpuRegister());
2499 } else {
2500 // Integer cases.
2501 locations->SetInAt(0, Location::RequiresRegister());
2502 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2503 }
2504
David Brazdilb3e773e2016-01-26 11:28:37 +00002505 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002507 }
2508}
2509
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002510void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002511 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002512 return;
2513 }
2514
2515 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002516 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002517 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002518
Roland Levillain7f63c522015-07-13 15:54:55 +00002519 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002520 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002521 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002522 } else {
2523 // Integer cases.
2524 Register lhs = InputRegisterAt(instruction, 0);
2525 Operand rhs = InputOperandAt(instruction, 1);
2526 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002527 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002528 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002529}
2530
2531#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2532 M(Equal) \
2533 M(NotEqual) \
2534 M(LessThan) \
2535 M(LessThanOrEqual) \
2536 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002537 M(GreaterThanOrEqual) \
2538 M(Below) \
2539 M(BelowOrEqual) \
2540 M(Above) \
2541 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002542#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002543void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2544void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002545FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002546#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002547#undef FOR_EACH_CONDITION_INSTRUCTION
2548
Zheng Xuc6667102015-05-15 16:08:45 +08002549void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2550 DCHECK(instruction->IsDiv() || instruction->IsRem());
2551
2552 LocationSummary* locations = instruction->GetLocations();
2553 Location second = locations->InAt(1);
2554 DCHECK(second.IsConstant());
2555
2556 Register out = OutputRegister(instruction);
2557 Register dividend = InputRegisterAt(instruction, 0);
2558 int64_t imm = Int64FromConstant(second.GetConstant());
2559 DCHECK(imm == 1 || imm == -1);
2560
2561 if (instruction->IsRem()) {
2562 __ Mov(out, 0);
2563 } else {
2564 if (imm == 1) {
2565 __ Mov(out, dividend);
2566 } else {
2567 __ Neg(out, dividend);
2568 }
2569 }
2570}
2571
2572void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2573 DCHECK(instruction->IsDiv() || instruction->IsRem());
2574
2575 LocationSummary* locations = instruction->GetLocations();
2576 Location second = locations->InAt(1);
2577 DCHECK(second.IsConstant());
2578
2579 Register out = OutputRegister(instruction);
2580 Register dividend = InputRegisterAt(instruction, 0);
2581 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002582 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002583 int ctz_imm = CTZ(abs_imm);
2584
2585 UseScratchRegisterScope temps(GetVIXLAssembler());
2586 Register temp = temps.AcquireSameSizeAs(out);
2587
2588 if (instruction->IsDiv()) {
2589 __ Add(temp, dividend, abs_imm - 1);
2590 __ Cmp(dividend, 0);
2591 __ Csel(out, temp, dividend, lt);
2592 if (imm > 0) {
2593 __ Asr(out, out, ctz_imm);
2594 } else {
2595 __ Neg(out, Operand(out, ASR, ctz_imm));
2596 }
2597 } else {
2598 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2599 __ Asr(temp, dividend, bits - 1);
2600 __ Lsr(temp, temp, bits - ctz_imm);
2601 __ Add(out, dividend, temp);
2602 __ And(out, out, abs_imm - 1);
2603 __ Sub(out, out, temp);
2604 }
2605}
2606
2607void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2608 DCHECK(instruction->IsDiv() || instruction->IsRem());
2609
2610 LocationSummary* locations = instruction->GetLocations();
2611 Location second = locations->InAt(1);
2612 DCHECK(second.IsConstant());
2613
2614 Register out = OutputRegister(instruction);
2615 Register dividend = InputRegisterAt(instruction, 0);
2616 int64_t imm = Int64FromConstant(second.GetConstant());
2617
2618 Primitive::Type type = instruction->GetResultType();
2619 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2620
2621 int64_t magic;
2622 int shift;
2623 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2624
2625 UseScratchRegisterScope temps(GetVIXLAssembler());
2626 Register temp = temps.AcquireSameSizeAs(out);
2627
2628 // temp = get_high(dividend * magic)
2629 __ Mov(temp, magic);
2630 if (type == Primitive::kPrimLong) {
2631 __ Smulh(temp, dividend, temp);
2632 } else {
2633 __ Smull(temp.X(), dividend, temp);
2634 __ Lsr(temp.X(), temp.X(), 32);
2635 }
2636
2637 if (imm > 0 && magic < 0) {
2638 __ Add(temp, temp, dividend);
2639 } else if (imm < 0 && magic > 0) {
2640 __ Sub(temp, temp, dividend);
2641 }
2642
2643 if (shift != 0) {
2644 __ Asr(temp, temp, shift);
2645 }
2646
2647 if (instruction->IsDiv()) {
2648 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2649 } else {
2650 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2651 // TODO: Strength reduction for msub.
2652 Register temp_imm = temps.AcquireSameSizeAs(out);
2653 __ Mov(temp_imm, imm);
2654 __ Msub(out, temp, temp_imm, dividend);
2655 }
2656}
2657
2658void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2659 DCHECK(instruction->IsDiv() || instruction->IsRem());
2660 Primitive::Type type = instruction->GetResultType();
2661 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2662
2663 LocationSummary* locations = instruction->GetLocations();
2664 Register out = OutputRegister(instruction);
2665 Location second = locations->InAt(1);
2666
2667 if (second.IsConstant()) {
2668 int64_t imm = Int64FromConstant(second.GetConstant());
2669
2670 if (imm == 0) {
2671 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2672 } else if (imm == 1 || imm == -1) {
2673 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002674 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002675 DivRemByPowerOfTwo(instruction);
2676 } else {
2677 DCHECK(imm <= -2 || imm >= 2);
2678 GenerateDivRemWithAnyConstant(instruction);
2679 }
2680 } else {
2681 Register dividend = InputRegisterAt(instruction, 0);
2682 Register divisor = InputRegisterAt(instruction, 1);
2683 if (instruction->IsDiv()) {
2684 __ Sdiv(out, dividend, divisor);
2685 } else {
2686 UseScratchRegisterScope temps(GetVIXLAssembler());
2687 Register temp = temps.AcquireSameSizeAs(out);
2688 __ Sdiv(temp, dividend, divisor);
2689 __ Msub(out, temp, divisor, dividend);
2690 }
2691 }
2692}
2693
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002694void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2697 switch (div->GetResultType()) {
2698 case Primitive::kPrimInt:
2699 case Primitive::kPrimLong:
2700 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002701 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002702 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2703 break;
2704
2705 case Primitive::kPrimFloat:
2706 case Primitive::kPrimDouble:
2707 locations->SetInAt(0, Location::RequiresFpuRegister());
2708 locations->SetInAt(1, Location::RequiresFpuRegister());
2709 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2710 break;
2711
2712 default:
2713 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2714 }
2715}
2716
2717void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2718 Primitive::Type type = div->GetResultType();
2719 switch (type) {
2720 case Primitive::kPrimInt:
2721 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002722 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002723 break;
2724
2725 case Primitive::kPrimFloat:
2726 case Primitive::kPrimDouble:
2727 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2728 break;
2729
2730 default:
2731 LOG(FATAL) << "Unexpected div type " << type;
2732 }
2733}
2734
Alexandre Rames67555f72014-11-18 10:55:16 +00002735void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002736 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2737 ? LocationSummary::kCallOnSlowPath
2738 : LocationSummary::kNoCall;
2739 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002740 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2741 if (instruction->HasUses()) {
2742 locations->SetOut(Location::SameAsFirstInput());
2743 }
2744}
2745
2746void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2747 SlowPathCodeARM64* slow_path =
2748 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2749 codegen_->AddSlowPath(slow_path);
2750 Location value = instruction->GetLocations()->InAt(0);
2751
Alexandre Rames3e69f162014-12-10 10:36:50 +00002752 Primitive::Type type = instruction->GetType();
2753
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002754 if (!Primitive::IsIntegralType(type)) {
2755 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002756 return;
2757 }
2758
Alexandre Rames67555f72014-11-18 10:55:16 +00002759 if (value.IsConstant()) {
2760 int64_t divisor = Int64ConstantFrom(value);
2761 if (divisor == 0) {
2762 __ B(slow_path->GetEntryLabel());
2763 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002764 // A division by a non-null constant is valid. We don't need to perform
2765 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002766 }
2767 } else {
2768 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2769 }
2770}
2771
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002772void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2773 LocationSummary* locations =
2774 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2775 locations->SetOut(Location::ConstantLocation(constant));
2776}
2777
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002778void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2779 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002780 // Will be generated at use site.
2781}
2782
Alexandre Rames5319def2014-10-23 10:03:10 +01002783void LocationsBuilderARM64::VisitExit(HExit* exit) {
2784 exit->SetLocations(nullptr);
2785}
2786
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002787void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002788}
2789
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002790void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2791 LocationSummary* locations =
2792 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2793 locations->SetOut(Location::ConstantLocation(constant));
2794}
2795
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002796void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002797 // Will be generated at use site.
2798}
2799
David Brazdilfc6a86a2015-06-26 10:33:45 +00002800void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002801 DCHECK(!successor->IsExitBlock());
2802 HBasicBlock* block = got->GetBlock();
2803 HInstruction* previous = got->GetPrevious();
2804 HLoopInformation* info = block->GetLoopInformation();
2805
David Brazdil46e2a392015-03-16 17:31:52 +00002806 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002807 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2808 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2809 return;
2810 }
2811 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2812 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2813 }
2814 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002815 __ B(codegen_->GetLabelOf(successor));
2816 }
2817}
2818
David Brazdilfc6a86a2015-06-26 10:33:45 +00002819void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2820 got->SetLocations(nullptr);
2821}
2822
2823void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2824 HandleGoto(got, got->GetSuccessor());
2825}
2826
2827void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2828 try_boundary->SetLocations(nullptr);
2829}
2830
2831void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2832 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2833 if (!successor->IsExitBlock()) {
2834 HandleGoto(try_boundary, successor);
2835 }
2836}
2837
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002838void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002839 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002840 vixl::aarch64::Label* true_target,
2841 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002842 // FP branching requires both targets to be explicit. If either of the targets
2843 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002844 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002845 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002846
David Brazdil0debae72015-11-12 18:37:00 +00002847 if (true_target == nullptr && false_target == nullptr) {
2848 // Nothing to do. The code always falls through.
2849 return;
2850 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002851 // Constant condition, statically compared against "true" (integer value 1).
2852 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002853 if (true_target != nullptr) {
2854 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002855 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002856 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002857 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002858 if (false_target != nullptr) {
2859 __ B(false_target);
2860 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002861 }
David Brazdil0debae72015-11-12 18:37:00 +00002862 return;
2863 }
2864
2865 // The following code generates these patterns:
2866 // (1) true_target == nullptr && false_target != nullptr
2867 // - opposite condition true => branch to false_target
2868 // (2) true_target != nullptr && false_target == nullptr
2869 // - condition true => branch to true_target
2870 // (3) true_target != nullptr && false_target != nullptr
2871 // - condition true => branch to true_target
2872 // - branch to false_target
2873 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002874 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002875 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002876 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002877 if (true_target == nullptr) {
2878 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2879 } else {
2880 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2881 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002882 } else {
2883 // The condition instruction has not been materialized, use its inputs as
2884 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002885 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002886
David Brazdil0debae72015-11-12 18:37:00 +00002887 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002888 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002889 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002890 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002891 IfCondition opposite_condition = condition->GetOppositeCondition();
2892 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002893 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002894 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002895 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002896 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002897 // Integer cases.
2898 Register lhs = InputRegisterAt(condition, 0);
2899 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002900
2901 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002902 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002903 if (true_target == nullptr) {
2904 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2905 non_fallthrough_target = false_target;
2906 } else {
2907 arm64_cond = ARM64Condition(condition->GetCondition());
2908 non_fallthrough_target = true_target;
2909 }
2910
Aart Bik086d27e2016-01-20 17:02:00 -08002911 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002912 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002913 switch (arm64_cond) {
2914 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002915 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002916 break;
2917 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002918 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002919 break;
2920 case lt:
2921 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002922 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002923 break;
2924 case ge:
2925 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002926 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002927 break;
2928 default:
2929 // Without the `static_cast` the compiler throws an error for
2930 // `-Werror=sign-promo`.
2931 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2932 }
2933 } else {
2934 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002935 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002936 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002937 }
2938 }
David Brazdil0debae72015-11-12 18:37:00 +00002939
2940 // If neither branch falls through (case 3), the conditional branch to `true_target`
2941 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2942 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002943 __ B(false_target);
2944 }
David Brazdil0debae72015-11-12 18:37:00 +00002945
2946 if (fallthrough_target.IsLinked()) {
2947 __ Bind(&fallthrough_target);
2948 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002949}
2950
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002951void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2952 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002953 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002954 locations->SetInAt(0, Location::RequiresRegister());
2955 }
2956}
2957
2958void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002959 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2960 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002961 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2962 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2963 true_target = nullptr;
2964 }
2965 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2966 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2967 false_target = nullptr;
2968 }
David Brazdil0debae72015-11-12 18:37:00 +00002969 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002970}
2971
2972void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2973 LocationSummary* locations = new (GetGraph()->GetArena())
2974 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002975 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002976 locations->SetInAt(0, Location::RequiresRegister());
2977 }
2978}
2979
2980void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002981 SlowPathCodeARM64* slow_path =
2982 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002983 GenerateTestAndBranch(deoptimize,
2984 /* condition_input_index */ 0,
2985 slow_path->GetEntryLabel(),
2986 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002987}
2988
David Brazdilc0b601b2016-02-08 14:20:45 +00002989static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2990 return condition->IsCondition() &&
2991 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2992}
2993
Alexandre Rames880f1192016-06-13 16:04:50 +01002994static inline Condition GetConditionForSelect(HCondition* condition) {
2995 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002996 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2997 : ARM64Condition(cond);
2998}
2999
David Brazdil74eb1b22015-12-14 11:44:01 +00003000void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003002 if (Primitive::IsFloatingPointType(select->GetType())) {
3003 locations->SetInAt(0, Location::RequiresFpuRegister());
3004 locations->SetInAt(1, Location::RequiresFpuRegister());
3005 locations->SetOut(Location::RequiresFpuRegister());
3006 } else {
3007 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3008 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3009 bool is_true_value_constant = cst_true_value != nullptr;
3010 bool is_false_value_constant = cst_false_value != nullptr;
3011 // Ask VIXL whether we should synthesize constants in registers.
3012 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3013 Operand true_op = is_true_value_constant ?
3014 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3015 Operand false_op = is_false_value_constant ?
3016 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3017 bool true_value_in_register = false;
3018 bool false_value_in_register = false;
3019 MacroAssembler::GetCselSynthesisInformation(
3020 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3021 true_value_in_register |= !is_true_value_constant;
3022 false_value_in_register |= !is_false_value_constant;
3023
3024 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3025 : Location::ConstantLocation(cst_true_value));
3026 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3027 : Location::ConstantLocation(cst_false_value));
3028 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003029 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003030
David Brazdil74eb1b22015-12-14 11:44:01 +00003031 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3032 locations->SetInAt(2, Location::RequiresRegister());
3033 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003034}
3035
3036void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003037 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003038 Condition csel_cond;
3039
3040 if (IsBooleanValueOrMaterializedCondition(cond)) {
3041 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003042 // Use the condition flags set by the previous instruction.
3043 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003044 } else {
3045 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003046 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003047 }
3048 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003049 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003050 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003051 } else {
3052 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003053 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003054 }
3055
Alexandre Rames880f1192016-06-13 16:04:50 +01003056 if (Primitive::IsFloatingPointType(select->GetType())) {
3057 __ Fcsel(OutputFPRegister(select),
3058 InputFPRegisterAt(select, 1),
3059 InputFPRegisterAt(select, 0),
3060 csel_cond);
3061 } else {
3062 __ Csel(OutputRegister(select),
3063 InputOperandAt(select, 1),
3064 InputOperandAt(select, 0),
3065 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003066 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003067}
3068
David Srbecky0cf44932015-12-09 14:09:59 +00003069void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3070 new (GetGraph()->GetArena()) LocationSummary(info);
3071}
3072
David Srbeckyd28f4a02016-03-14 17:14:24 +00003073void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3074 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003075}
3076
3077void CodeGeneratorARM64::GenerateNop() {
3078 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003079}
3080
Alexandre Rames5319def2014-10-23 10:03:10 +01003081void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003082 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003083}
3084
3085void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003086 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003087}
3088
3089void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003090 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003091}
3092
3093void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003094 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003095}
3096
Roland Levillain44015862016-01-22 11:47:17 +00003097static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3098 return kEmitCompilerReadBarrier &&
3099 (kUseBakerReadBarrier ||
3100 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3101 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3102 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3103}
3104
Alexandre Rames67555f72014-11-18 10:55:16 +00003105void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003106 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003107 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3108 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003109 case TypeCheckKind::kExactCheck:
3110 case TypeCheckKind::kAbstractClassCheck:
3111 case TypeCheckKind::kClassHierarchyCheck:
3112 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003113 call_kind =
3114 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003115 break;
3116 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003117 case TypeCheckKind::kUnresolvedCheck:
3118 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003119 call_kind = LocationSummary::kCallOnSlowPath;
3120 break;
3121 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003122
Alexandre Rames67555f72014-11-18 10:55:16 +00003123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003124 locations->SetInAt(0, Location::RequiresRegister());
3125 locations->SetInAt(1, Location::RequiresRegister());
3126 // The "out" register is used as a temporary, so it overlaps with the inputs.
3127 // Note that TypeCheckSlowPathARM64 uses this register too.
3128 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3129 // When read barriers are enabled, we need a temporary register for
3130 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003131 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003132 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003133 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003134}
3135
3136void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003137 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003138 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003139 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003140 Register obj = InputRegisterAt(instruction, 0);
3141 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003142 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003143 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003144 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3145 locations->GetTemp(0) :
3146 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003147 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3148 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3149 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3150 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003151
Scott Wakeling97c72b72016-06-24 16:19:36 +01003152 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003153 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003154
3155 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003156 // Avoid null check if we know `obj` is not null.
3157 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003158 __ Cbz(obj, &zero);
3159 }
3160
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003161 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003162 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003163
Roland Levillain44015862016-01-22 11:47:17 +00003164 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003165 case TypeCheckKind::kExactCheck: {
3166 __ Cmp(out, cls);
3167 __ Cset(out, eq);
3168 if (zero.IsLinked()) {
3169 __ B(&done);
3170 }
3171 break;
3172 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003173
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003174 case TypeCheckKind::kAbstractClassCheck: {
3175 // If the class is abstract, we eagerly fetch the super class of the
3176 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003177 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003179 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003180 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003181 // If `out` is null, we use it for the result, and jump to `done`.
3182 __ Cbz(out, &done);
3183 __ Cmp(out, cls);
3184 __ B(ne, &loop);
3185 __ Mov(out, 1);
3186 if (zero.IsLinked()) {
3187 __ B(&done);
3188 }
3189 break;
3190 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003192 case TypeCheckKind::kClassHierarchyCheck: {
3193 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003194 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003195 __ Bind(&loop);
3196 __ Cmp(out, cls);
3197 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003198 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003199 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200 __ Cbnz(out, &loop);
3201 // If `out` is null, we use it for the result, and jump to `done`.
3202 __ B(&done);
3203 __ Bind(&success);
3204 __ Mov(out, 1);
3205 if (zero.IsLinked()) {
3206 __ B(&done);
3207 }
3208 break;
3209 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003211 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003212 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003213 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003214 __ Cmp(out, cls);
3215 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003216 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003217 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003218 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003219 // If `out` is null, we use it for the result, and jump to `done`.
3220 __ Cbz(out, &done);
3221 __ Ldrh(out, HeapOperand(out, primitive_offset));
3222 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3223 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003224 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003225 __ Mov(out, 1);
3226 __ B(&done);
3227 break;
3228 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003230 case TypeCheckKind::kArrayCheck: {
3231 __ Cmp(out, cls);
3232 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003233 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3234 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003235 codegen_->AddSlowPath(slow_path);
3236 __ B(ne, slow_path->GetEntryLabel());
3237 __ Mov(out, 1);
3238 if (zero.IsLinked()) {
3239 __ B(&done);
3240 }
3241 break;
3242 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003243
Calin Juravle98893e12015-10-02 21:05:03 +01003244 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003245 case TypeCheckKind::kInterfaceCheck: {
3246 // Note that we indeed only call on slow path, but we always go
3247 // into the slow path for the unresolved and interface check
3248 // cases.
3249 //
3250 // We cannot directly call the InstanceofNonTrivial runtime
3251 // entry point without resorting to a type checking slow path
3252 // here (i.e. by calling InvokeRuntime directly), as it would
3253 // require to assign fixed registers for the inputs of this
3254 // HInstanceOf instruction (following the runtime calling
3255 // convention), which might be cluttered by the potential first
3256 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003257 //
3258 // TODO: Introduce a new runtime entry point taking the object
3259 // to test (instead of its class) as argument, and let it deal
3260 // with the read barrier issues. This will let us refactor this
3261 // case of the `switch` code as it was previously (with a direct
3262 // call to the runtime not using a type checking slow path).
3263 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003264 DCHECK(locations->OnlyCallsOnSlowPath());
3265 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3266 /* is_fatal */ false);
3267 codegen_->AddSlowPath(slow_path);
3268 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003269 if (zero.IsLinked()) {
3270 __ B(&done);
3271 }
3272 break;
3273 }
3274 }
3275
3276 if (zero.IsLinked()) {
3277 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003278 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003279 }
3280
3281 if (done.IsLinked()) {
3282 __ Bind(&done);
3283 }
3284
3285 if (slow_path != nullptr) {
3286 __ Bind(slow_path->GetExitLabel());
3287 }
3288}
3289
3290void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3291 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3292 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3293
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003294 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3295 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003296 case TypeCheckKind::kExactCheck:
3297 case TypeCheckKind::kAbstractClassCheck:
3298 case TypeCheckKind::kClassHierarchyCheck:
3299 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003300 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3301 LocationSummary::kCallOnSlowPath :
3302 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003303 break;
3304 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003305 case TypeCheckKind::kUnresolvedCheck:
3306 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003307 call_kind = LocationSummary::kCallOnSlowPath;
3308 break;
3309 }
3310
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003311 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3312 locations->SetInAt(0, Location::RequiresRegister());
3313 locations->SetInAt(1, Location::RequiresRegister());
3314 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3315 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003316 // When read barriers are enabled, we need an additional temporary
3317 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003318 if (TypeCheckNeedsATemporary(type_check_kind)) {
3319 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003320 }
3321}
3322
3323void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003324 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003325 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003326 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003327 Register obj = InputRegisterAt(instruction, 0);
3328 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003329 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003330 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3331 locations->GetTemp(1) :
3332 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003333 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003334 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3335 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3336 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3337 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003338
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003339 bool is_type_check_slow_path_fatal =
3340 (type_check_kind == TypeCheckKind::kExactCheck ||
3341 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3342 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3343 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3344 !instruction->CanThrowIntoCatchBlock();
3345 SlowPathCodeARM64* type_check_slow_path =
3346 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3347 is_type_check_slow_path_fatal);
3348 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003349
Scott Wakeling97c72b72016-06-24 16:19:36 +01003350 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003351 // Avoid null check if we know obj is not null.
3352 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003353 __ Cbz(obj, &done);
3354 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003355
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003356 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003357 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003358
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003359 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003360 case TypeCheckKind::kExactCheck:
3361 case TypeCheckKind::kArrayCheck: {
3362 __ Cmp(temp, cls);
3363 // Jump to slow path for throwing the exception or doing a
3364 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003365 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003366 break;
3367 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 case TypeCheckKind::kAbstractClassCheck: {
3370 // If the class is abstract, we eagerly fetch the super class of the
3371 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003372 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003373 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003374 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003375 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376
3377 // If the class reference currently in `temp` is not null, jump
3378 // to the `compare_classes` label to compare it with the checked
3379 // class.
3380 __ Cbnz(temp, &compare_classes);
3381 // Otherwise, jump to the slow path to throw the exception.
3382 //
3383 // But before, move back the object's class into `temp` before
3384 // going into the slow path, as it has been overwritten in the
3385 // meantime.
3386 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003387 GenerateReferenceLoadTwoRegisters(
3388 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389 __ B(type_check_slow_path->GetEntryLabel());
3390
3391 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003392 __ Cmp(temp, cls);
3393 __ B(ne, &loop);
3394 break;
3395 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003397 case TypeCheckKind::kClassHierarchyCheck: {
3398 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003399 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003400 __ Bind(&loop);
3401 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003402 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003403
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003404 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003405 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003406
3407 // If the class reference currently in `temp` is not null, jump
3408 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003409 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 // Otherwise, jump to the slow path to throw the exception.
3411 //
3412 // But before, move back the object's class into `temp` before
3413 // going into the slow path, as it has been overwritten in the
3414 // meantime.
3415 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003416 GenerateReferenceLoadTwoRegisters(
3417 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003418 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003419 break;
3420 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003421
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003422 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003423 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003424 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003425 __ Cmp(temp, cls);
3426 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003427
3428 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003429 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003430 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431
3432 // If the component type is not null (i.e. the object is indeed
3433 // an array), jump to label `check_non_primitive_component_type`
3434 // to further check that this component type is not a primitive
3435 // type.
3436 __ Cbnz(temp, &check_non_primitive_component_type);
3437 // Otherwise, jump to the slow path to throw the exception.
3438 //
3439 // But before, move back the object's class into `temp` before
3440 // going into the slow path, as it has been overwritten in the
3441 // meantime.
3442 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003443 GenerateReferenceLoadTwoRegisters(
3444 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003445 __ B(type_check_slow_path->GetEntryLabel());
3446
3447 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003448 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3449 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003450 __ Cbz(temp, &done);
3451 // Same comment as above regarding `temp` and the slow path.
3452 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003453 GenerateReferenceLoadTwoRegisters(
3454 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003455 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003456 break;
3457 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003458
Calin Juravle98893e12015-10-02 21:05:03 +01003459 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003460 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003461 // We always go into the type check slow path for the unresolved
3462 // and interface check cases.
3463 //
3464 // We cannot directly call the CheckCast runtime entry point
3465 // without resorting to a type checking slow path here (i.e. by
3466 // calling InvokeRuntime directly), as it would require to
3467 // assign fixed registers for the inputs of this HInstanceOf
3468 // instruction (following the runtime calling convention), which
3469 // might be cluttered by the potential first read barrier
3470 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003471 //
3472 // TODO: Introduce a new runtime entry point taking the object
3473 // to test (instead of its class) as argument, and let it deal
3474 // with the read barrier issues. This will let us refactor this
3475 // case of the `switch` code as it was previously (with a direct
3476 // call to the runtime not using a type checking slow path).
3477 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003478 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003479 break;
3480 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003481 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003482
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003483 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003484}
3485
Alexandre Rames5319def2014-10-23 10:03:10 +01003486void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3487 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3488 locations->SetOut(Location::ConstantLocation(constant));
3489}
3490
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003491void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003492 // Will be generated at use site.
3493}
3494
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003495void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3497 locations->SetOut(Location::ConstantLocation(constant));
3498}
3499
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003500void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003501 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003502}
3503
Calin Juravle175dc732015-08-25 15:42:32 +01003504void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3505 // The trampoline uses the same calling convention as dex calling conventions,
3506 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3507 // the method_idx.
3508 HandleInvoke(invoke);
3509}
3510
3511void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3512 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3513}
3514
Alexandre Rames5319def2014-10-23 10:03:10 +01003515void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003516 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003517 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003518}
3519
Alexandre Rames67555f72014-11-18 10:55:16 +00003520void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3521 HandleInvoke(invoke);
3522}
3523
3524void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3525 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003526 LocationSummary* locations = invoke->GetLocations();
3527 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003528 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003529 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003530 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003531
3532 // The register ip1 is required to be used for the hidden argument in
3533 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003534 MacroAssembler* masm = GetVIXLAssembler();
3535 UseScratchRegisterScope scratch_scope(masm);
3536 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003537 scratch_scope.Exclude(ip1);
3538 __ Mov(ip1, invoke->GetDexMethodIndex());
3539
Alexandre Rames67555f72014-11-18 10:55:16 +00003540 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003541 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003542 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003543 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003544 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003545 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003546 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003547 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003548 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003549 // Instead of simply (possibly) unpoisoning `temp` here, we should
3550 // emit a read barrier for the previous class reference load.
3551 // However this is not required in practice, as this is an
3552 // intermediate/temporary reference and because the current
3553 // concurrent copying collector keeps the from-space memory
3554 // intact/accessible until the end of the marking phase (the
3555 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003556 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003557 __ Ldr(temp,
3558 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3559 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3560 invoke->GetImtIndex() % ImTable::kSize, kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003561 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003562 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003563 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003564 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003565 // lr();
3566 __ Blr(lr);
3567 DCHECK(!codegen_->IsLeafMethod());
3568 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3569}
3570
3571void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003572 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3573 if (intrinsic.TryDispatch(invoke)) {
3574 return;
3575 }
3576
Alexandre Rames67555f72014-11-18 10:55:16 +00003577 HandleInvoke(invoke);
3578}
3579
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003580void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003581 // Explicit clinit checks triggered by static invokes must have been pruned by
3582 // art::PrepareForRegisterAllocation.
3583 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003584
Andreas Gampe878d58c2015-01-15 23:24:00 -08003585 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3586 if (intrinsic.TryDispatch(invoke)) {
3587 return;
3588 }
3589
Alexandre Rames67555f72014-11-18 10:55:16 +00003590 HandleInvoke(invoke);
3591}
3592
Andreas Gampe878d58c2015-01-15 23:24:00 -08003593static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3594 if (invoke->GetLocations()->Intrinsified()) {
3595 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3596 intrinsic.Dispatch(invoke);
3597 return true;
3598 }
3599 return false;
3600}
3601
Vladimir Markodc151b22015-10-15 18:02:30 +01003602HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3603 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3604 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003605 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003606 return desired_dispatch_info;
3607}
3608
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003609void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003610 // For better instruction scheduling we load the direct code pointer before the method pointer.
3611 bool direct_code_loaded = false;
3612 switch (invoke->GetCodePtrLocation()) {
3613 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3614 // LR = code address from literal pool with link-time patch.
3615 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3616 direct_code_loaded = true;
3617 break;
3618 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3619 // LR = invoke->GetDirectCodePtr();
3620 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3621 direct_code_loaded = true;
3622 break;
3623 default:
3624 break;
3625 }
3626
Andreas Gampe878d58c2015-01-15 23:24:00 -08003627 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003628 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3629 switch (invoke->GetMethodLoadKind()) {
3630 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3631 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003632 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003633 break;
3634 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003635 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003636 break;
3637 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3638 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003639 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003640 break;
3641 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3642 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003643 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003644 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3645 break;
3646 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3647 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003648 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3649 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003650 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003651 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003652 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003653 __ Bind(adrp_label);
3654 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003655 }
Vladimir Marko58155012015-08-19 12:49:41 +00003656 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003657 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003658 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003659 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003660 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003661 __ Bind(ldr_label);
3662 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003663 }
Vladimir Marko58155012015-08-19 12:49:41 +00003664 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003665 }
Vladimir Marko58155012015-08-19 12:49:41 +00003666 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003667 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003668 Register reg = XRegisterFrom(temp);
3669 Register method_reg;
3670 if (current_method.IsRegister()) {
3671 method_reg = XRegisterFrom(current_method);
3672 } else {
3673 DCHECK(invoke->GetLocations()->Intrinsified());
3674 DCHECK(!current_method.IsValid());
3675 method_reg = reg;
3676 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3677 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003678
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003679 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003680 __ Ldr(reg.X(),
3681 MemOperand(method_reg.X(),
3682 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003683 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003684 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3685 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003686 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3687 break;
3688 }
3689 }
3690
3691 switch (invoke->GetCodePtrLocation()) {
3692 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3693 __ Bl(&frame_entry_label_);
3694 break;
3695 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3696 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003697 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3698 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003699 __ Bind(label);
3700 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003701 break;
3702 }
3703 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3704 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3705 // LR prepared above for better instruction scheduling.
3706 DCHECK(direct_code_loaded);
3707 // lr()
3708 __ Blr(lr);
3709 break;
3710 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3711 // LR = callee_method->entry_point_from_quick_compiled_code_;
3712 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003713 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003714 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3715 // lr()
3716 __ Blr(lr);
3717 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003718 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003719
Andreas Gampe878d58c2015-01-15 23:24:00 -08003720 DCHECK(!IsLeafMethod());
3721}
3722
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003723void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003724 // Use the calling convention instead of the location of the receiver, as
3725 // intrinsics may have put the receiver in a different register. In the intrinsics
3726 // slow path, the arguments have been moved to the right place, so here we are
3727 // guaranteed that the receiver is the first register of the calling convention.
3728 InvokeDexCallingConvention calling_convention;
3729 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003730 Register temp = XRegisterFrom(temp_in);
3731 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3732 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3733 Offset class_offset = mirror::Object::ClassOffset();
3734 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3735
3736 BlockPoolsScope block_pools(GetVIXLAssembler());
3737
3738 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003739 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003740 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003741 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003742 // Instead of simply (possibly) unpoisoning `temp` here, we should
3743 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003744 // intermediate/temporary reference and because the current
3745 // concurrent copying collector keeps the from-space memory
3746 // intact/accessible until the end of the marking phase (the
3747 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003748 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3749 // temp = temp->GetMethodAt(method_offset);
3750 __ Ldr(temp, MemOperand(temp, method_offset));
3751 // lr = temp->GetEntryPoint();
3752 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3753 // lr();
3754 __ Blr(lr);
3755}
3756
Scott Wakeling97c72b72016-06-24 16:19:36 +01003757vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3758 const DexFile& dex_file,
3759 uint32_t string_index,
3760 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003761 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3762}
3763
Scott Wakeling97c72b72016-06-24 16:19:36 +01003764vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3765 const DexFile& dex_file,
3766 uint32_t type_index,
3767 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003768 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3769}
3770
Scott Wakeling97c72b72016-06-24 16:19:36 +01003771vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3772 const DexFile& dex_file,
3773 uint32_t element_offset,
3774 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003775 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3776}
3777
Scott Wakeling97c72b72016-06-24 16:19:36 +01003778vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3779 const DexFile& dex_file,
3780 uint32_t offset_or_index,
3781 vixl::aarch64::Label* adrp_label,
3782 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003783 // Add a patch entry and return the label.
3784 patches->emplace_back(dex_file, offset_or_index);
3785 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003786 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003787 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3788 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3789 return label;
3790}
3791
Scott Wakeling97c72b72016-06-24 16:19:36 +01003792vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003793 const DexFile& dex_file, uint32_t string_index) {
3794 return boot_image_string_patches_.GetOrCreate(
3795 StringReference(&dex_file, string_index),
3796 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3797}
3798
Scott Wakeling97c72b72016-06-24 16:19:36 +01003799vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003800 const DexFile& dex_file, uint32_t type_index) {
3801 return boot_image_type_patches_.GetOrCreate(
3802 TypeReference(&dex_file, type_index),
3803 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3804}
3805
Scott Wakeling97c72b72016-06-24 16:19:36 +01003806vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3807 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003808 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3809 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3810 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3811}
3812
Scott Wakeling97c72b72016-06-24 16:19:36 +01003813vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3814 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003815 return DeduplicateUint64Literal(address);
3816}
3817
Vladimir Marko58155012015-08-19 12:49:41 +00003818void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3819 DCHECK(linker_patches->empty());
3820 size_t size =
3821 method_patches_.size() +
3822 call_patches_.size() +
3823 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003824 pc_relative_dex_cache_patches_.size() +
3825 boot_image_string_patches_.size() +
3826 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003827 boot_image_type_patches_.size() +
3828 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003829 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003830 linker_patches->reserve(size);
3831 for (const auto& entry : method_patches_) {
3832 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003833 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3834 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003835 target_method.dex_file,
3836 target_method.dex_method_index));
3837 }
3838 for (const auto& entry : call_patches_) {
3839 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003840 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3841 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003842 target_method.dex_file,
3843 target_method.dex_method_index));
3844 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003845 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3846 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003847 info.target_method.dex_file,
3848 info.target_method.dex_method_index));
3849 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003850 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003851 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003852 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003853 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003854 info.offset_or_index));
3855 }
3856 for (const auto& entry : boot_image_string_patches_) {
3857 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003858 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3859 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003860 target_string.dex_file,
3861 target_string.string_index));
3862 }
3863 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003864 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003865 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003866 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003867 info.offset_or_index));
3868 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003869 for (const auto& entry : boot_image_type_patches_) {
3870 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003871 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3872 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003873 target_type.dex_file,
3874 target_type.type_index));
3875 }
3876 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003877 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003878 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003879 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003880 info.offset_or_index));
3881 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003882 for (const auto& entry : boot_image_address_patches_) {
3883 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003884 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3885 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003886 }
3887}
3888
Scott Wakeling97c72b72016-06-24 16:19:36 +01003889vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003890 Uint32ToLiteralMap* map) {
3891 return map->GetOrCreate(
3892 value,
3893 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3894}
3895
Scott Wakeling97c72b72016-06-24 16:19:36 +01003896vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003897 return uint64_literals_.GetOrCreate(
3898 value,
3899 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003900}
3901
Scott Wakeling97c72b72016-06-24 16:19:36 +01003902vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003903 MethodReference target_method,
3904 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003905 return map->GetOrCreate(
3906 target_method,
3907 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003908}
3909
Scott Wakeling97c72b72016-06-24 16:19:36 +01003910vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003911 MethodReference target_method) {
3912 return DeduplicateMethodLiteral(target_method, &method_patches_);
3913}
3914
Scott Wakeling97c72b72016-06-24 16:19:36 +01003915vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003916 MethodReference target_method) {
3917 return DeduplicateMethodLiteral(target_method, &call_patches_);
3918}
3919
3920
Andreas Gampe878d58c2015-01-15 23:24:00 -08003921void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003922 // Explicit clinit checks triggered by static invokes must have been pruned by
3923 // art::PrepareForRegisterAllocation.
3924 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003925
Andreas Gampe878d58c2015-01-15 23:24:00 -08003926 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3927 return;
3928 }
3929
Alexandre Ramesd921d642015-04-16 15:07:16 +01003930 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003931 LocationSummary* locations = invoke->GetLocations();
3932 codegen_->GenerateStaticOrDirectCall(
3933 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003934 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003935}
3936
3937void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003938 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3939 return;
3940 }
3941
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003942 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003943 DCHECK(!codegen_->IsLeafMethod());
3944 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3945}
3946
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003947HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3948 HLoadClass::LoadKind desired_class_load_kind) {
3949 if (kEmitCompilerReadBarrier) {
3950 switch (desired_class_load_kind) {
3951 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3952 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3953 case HLoadClass::LoadKind::kBootImageAddress:
3954 // TODO: Implement for read barrier.
3955 return HLoadClass::LoadKind::kDexCacheViaMethod;
3956 default:
3957 break;
3958 }
3959 }
3960 switch (desired_class_load_kind) {
3961 case HLoadClass::LoadKind::kReferrersClass:
3962 break;
3963 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3964 DCHECK(!GetCompilerOptions().GetCompilePic());
3965 break;
3966 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3967 DCHECK(GetCompilerOptions().GetCompilePic());
3968 break;
3969 case HLoadClass::LoadKind::kBootImageAddress:
3970 break;
3971 case HLoadClass::LoadKind::kDexCacheAddress:
3972 DCHECK(Runtime::Current()->UseJitCompilation());
3973 break;
3974 case HLoadClass::LoadKind::kDexCachePcRelative:
3975 DCHECK(!Runtime::Current()->UseJitCompilation());
3976 break;
3977 case HLoadClass::LoadKind::kDexCacheViaMethod:
3978 break;
3979 }
3980 return desired_class_load_kind;
3981}
3982
Alexandre Rames67555f72014-11-18 10:55:16 +00003983void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003984 if (cls->NeedsAccessCheck()) {
3985 InvokeRuntimeCallingConvention calling_convention;
3986 CodeGenerator::CreateLoadClassLocationSummary(
3987 cls,
3988 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003989 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003990 /* code_generator_supports_read_barrier */ true);
3991 return;
3992 }
3993
3994 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3995 ? LocationSummary::kCallOnSlowPath
3996 : LocationSummary::kNoCall;
3997 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3998 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3999 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4000 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4001 locations->SetInAt(0, Location::RequiresRegister());
4002 }
4003 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004004}
4005
4006void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004007 if (cls->NeedsAccessCheck()) {
4008 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
4009 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4010 cls,
4011 cls->GetDexPc(),
4012 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004013 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004014 return;
4015 }
4016
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004017 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004018 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004019
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004020 bool generate_null_check = false;
4021 switch (cls->GetLoadKind()) {
4022 case HLoadClass::LoadKind::kReferrersClass: {
4023 DCHECK(!cls->CanCallRuntime());
4024 DCHECK(!cls->MustGenerateClinitCheck());
4025 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4026 Register current_method = InputRegisterAt(cls, 0);
4027 GenerateGcRootFieldLoad(
4028 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4029 break;
4030 }
4031 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4032 DCHECK(!kEmitCompilerReadBarrier);
4033 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4034 cls->GetTypeIndex()));
4035 break;
4036 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4037 DCHECK(!kEmitCompilerReadBarrier);
4038 // Add ADRP with its PC-relative type patch.
4039 const DexFile& dex_file = cls->GetDexFile();
4040 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004041 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004042 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004043 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004044 __ Bind(adrp_label);
4045 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004046 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004047 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004048 vixl::aarch64::Label* add_label =
4049 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004050 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004051 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004052 __ Bind(add_label);
4053 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004054 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004055 break;
4056 }
4057 case HLoadClass::LoadKind::kBootImageAddress: {
4058 DCHECK(!kEmitCompilerReadBarrier);
4059 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4060 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4061 break;
4062 }
4063 case HLoadClass::LoadKind::kDexCacheAddress: {
4064 DCHECK_NE(cls->GetAddress(), 0u);
4065 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4066 // that gives a 16KiB range. To try and reduce the number of literals if we load
4067 // multiple types, simply split the dex cache address to a 16KiB aligned base
4068 // loaded from a literal and the remaining offset embedded in the load.
4069 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4070 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4071 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4072 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4073 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4074 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4075 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4076 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4077 generate_null_check = !cls->IsInDexCache();
4078 break;
4079 }
4080 case HLoadClass::LoadKind::kDexCachePcRelative: {
4081 // Add ADRP with its PC-relative DexCache access patch.
4082 const DexFile& dex_file = cls->GetDexFile();
4083 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004084 vixl::aarch64::Label* adrp_label =
4085 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004086 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004087 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004088 __ Bind(adrp_label);
4089 __ adrp(out.X(), /* offset placeholder */ 0);
4090 }
4091 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004092 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004093 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4094 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4095 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4096 generate_null_check = !cls->IsInDexCache();
4097 break;
4098 }
4099 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4100 MemberOffset resolved_types_offset =
4101 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4102 // /* GcRoot<mirror::Class>[] */ out =
4103 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4104 Register current_method = InputRegisterAt(cls, 0);
4105 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4106 // /* GcRoot<mirror::Class> */ out = out[type_index]
4107 GenerateGcRootFieldLoad(
4108 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4109 generate_null_check = !cls->IsInDexCache();
4110 break;
4111 }
4112 }
4113
4114 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4115 DCHECK(cls->CanCallRuntime());
4116 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4117 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4118 codegen_->AddSlowPath(slow_path);
4119 if (generate_null_check) {
4120 __ Cbz(out, slow_path->GetEntryLabel());
4121 }
4122 if (cls->MustGenerateClinitCheck()) {
4123 GenerateClassInitializationCheck(slow_path, out);
4124 } else {
4125 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004126 }
4127 }
4128}
4129
David Brazdilcb1c0552015-08-04 16:22:25 +01004130static MemOperand GetExceptionTlsAddress() {
4131 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
4132}
4133
Alexandre Rames67555f72014-11-18 10:55:16 +00004134void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4135 LocationSummary* locations =
4136 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4137 locations->SetOut(Location::RequiresRegister());
4138}
4139
4140void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004141 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4142}
4143
4144void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4145 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4146}
4147
4148void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4149 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004150}
4151
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004152HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4153 HLoadString::LoadKind desired_string_load_kind) {
4154 if (kEmitCompilerReadBarrier) {
4155 switch (desired_string_load_kind) {
4156 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4157 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4158 case HLoadString::LoadKind::kBootImageAddress:
4159 // TODO: Implement for read barrier.
4160 return HLoadString::LoadKind::kDexCacheViaMethod;
4161 default:
4162 break;
4163 }
4164 }
4165 switch (desired_string_load_kind) {
4166 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4167 DCHECK(!GetCompilerOptions().GetCompilePic());
4168 break;
4169 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4170 DCHECK(GetCompilerOptions().GetCompilePic());
4171 break;
4172 case HLoadString::LoadKind::kBootImageAddress:
4173 break;
4174 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004175 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004176 break;
4177 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004178 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004179 break;
4180 case HLoadString::LoadKind::kDexCacheViaMethod:
4181 break;
4182 }
4183 return desired_string_load_kind;
4184}
4185
Alexandre Rames67555f72014-11-18 10:55:16 +00004186void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004187 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004188 ? LocationSummary::kCallOnSlowPath
4189 : LocationSummary::kNoCall;
4190 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004191 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4192 locations->SetInAt(0, Location::RequiresRegister());
4193 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004194 locations->SetOut(Location::RequiresRegister());
4195}
4196
4197void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004198 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004199 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004200
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004201 switch (load->GetLoadKind()) {
4202 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4203 DCHECK(!kEmitCompilerReadBarrier);
4204 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4205 load->GetStringIndex()));
4206 return; // No dex cache slow path.
4207 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4208 DCHECK(!kEmitCompilerReadBarrier);
4209 // Add ADRP with its PC-relative String patch.
4210 const DexFile& dex_file = load->GetDexFile();
4211 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004212 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004213 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004214 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004215 __ Bind(adrp_label);
4216 __ adrp(out.X(), /* offset placeholder */ 0);
4217 }
4218 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004219 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004220 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4221 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004222 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004223 __ Bind(add_label);
4224 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4225 }
4226 return; // No dex cache slow path.
4227 }
4228 case HLoadString::LoadKind::kBootImageAddress: {
4229 DCHECK(!kEmitCompilerReadBarrier);
4230 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4231 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4232 return; // No dex cache slow path.
4233 }
4234 case HLoadString::LoadKind::kDexCacheAddress: {
4235 DCHECK_NE(load->GetAddress(), 0u);
4236 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4237 // that gives a 16KiB range. To try and reduce the number of literals if we load
4238 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4239 // loaded from a literal and the remaining offset embedded in the load.
4240 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4241 DCHECK_ALIGNED(load->GetAddress(), 4u);
4242 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4243 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4244 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4245 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004246 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004247 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4248 break;
4249 }
4250 case HLoadString::LoadKind::kDexCachePcRelative: {
4251 // Add ADRP with its PC-relative DexCache access patch.
4252 const DexFile& dex_file = load->GetDexFile();
4253 uint32_t element_offset = load->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004254 vixl::aarch64::Label* adrp_label =
4255 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004256 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004257 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004258 __ Bind(adrp_label);
4259 __ adrp(out.X(), /* offset placeholder */ 0);
4260 }
4261 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004262 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004263 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004264 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004265 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4266 break;
4267 }
4268 case HLoadString::LoadKind::kDexCacheViaMethod: {
4269 Register current_method = InputRegisterAt(load, 0);
4270 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4271 GenerateGcRootFieldLoad(
4272 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4273 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4274 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4275 // /* GcRoot<mirror::String> */ out = out[string_index]
4276 GenerateGcRootFieldLoad(
4277 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4278 break;
4279 }
4280 default:
4281 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4282 UNREACHABLE();
4283 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004284
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004285 if (!load->IsInDexCache()) {
4286 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4287 codegen_->AddSlowPath(slow_path);
4288 __ Cbz(out, slow_path->GetEntryLabel());
4289 __ Bind(slow_path->GetExitLabel());
4290 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004291}
4292
Alexandre Rames5319def2014-10-23 10:03:10 +01004293void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4294 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4295 locations->SetOut(Location::ConstantLocation(constant));
4296}
4297
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004298void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004299 // Will be generated at use site.
4300}
4301
Alexandre Rames67555f72014-11-18 10:55:16 +00004302void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4303 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004305 InvokeRuntimeCallingConvention calling_convention;
4306 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4307}
4308
4309void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4310 codegen_->InvokeRuntime(instruction->IsEnter()
4311 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4312 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004313 instruction->GetDexPc(),
4314 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004315 if (instruction->IsEnter()) {
4316 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4317 } else {
4318 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4319 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004320}
4321
Alexandre Rames42d641b2014-10-27 14:00:51 +00004322void LocationsBuilderARM64::VisitMul(HMul* mul) {
4323 LocationSummary* locations =
4324 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4325 switch (mul->GetResultType()) {
4326 case Primitive::kPrimInt:
4327 case Primitive::kPrimLong:
4328 locations->SetInAt(0, Location::RequiresRegister());
4329 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004331 break;
4332
4333 case Primitive::kPrimFloat:
4334 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004335 locations->SetInAt(0, Location::RequiresFpuRegister());
4336 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004337 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004338 break;
4339
4340 default:
4341 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4342 }
4343}
4344
4345void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4346 switch (mul->GetResultType()) {
4347 case Primitive::kPrimInt:
4348 case Primitive::kPrimLong:
4349 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4350 break;
4351
4352 case Primitive::kPrimFloat:
4353 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004354 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004355 break;
4356
4357 default:
4358 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4359 }
4360}
4361
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004362void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4363 LocationSummary* locations =
4364 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4365 switch (neg->GetResultType()) {
4366 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004367 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004368 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004370 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004371
4372 case Primitive::kPrimFloat:
4373 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004374 locations->SetInAt(0, Location::RequiresFpuRegister());
4375 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004376 break;
4377
4378 default:
4379 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4380 }
4381}
4382
4383void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4384 switch (neg->GetResultType()) {
4385 case Primitive::kPrimInt:
4386 case Primitive::kPrimLong:
4387 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4388 break;
4389
4390 case Primitive::kPrimFloat:
4391 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004392 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004393 break;
4394
4395 default:
4396 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4397 }
4398}
4399
4400void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4401 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004402 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004403 InvokeRuntimeCallingConvention calling_convention;
4404 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004405 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004406 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004407 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004408}
4409
4410void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4411 LocationSummary* locations = instruction->GetLocations();
4412 InvokeRuntimeCallingConvention calling_convention;
4413 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4414 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004415 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004416 // Note: if heap poisoning is enabled, the entry point takes cares
4417 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004418 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4419 instruction,
4420 instruction->GetDexPc(),
4421 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004422 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004423}
4424
Alexandre Rames5319def2014-10-23 10:03:10 +01004425void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4426 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004427 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004428 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004429 if (instruction->IsStringAlloc()) {
4430 locations->AddTemp(LocationFrom(kArtMethodRegister));
4431 } else {
4432 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4433 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4434 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004435 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4436}
4437
4438void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004439 // Note: if heap poisoning is enabled, the entry point takes cares
4440 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004441 if (instruction->IsStringAlloc()) {
4442 // String is allocated through StringFactory. Call NewEmptyString entry point.
4443 Location temp = instruction->GetLocations()->GetTemp(0);
4444 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4445 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4446 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4447 __ Blr(lr);
4448 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4449 } else {
4450 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4451 instruction,
4452 instruction->GetDexPc(),
4453 nullptr);
4454 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4455 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004456}
4457
4458void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004460 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004462}
4463
4464void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004465 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004466 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004467 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004468 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004469 break;
4470
4471 default:
4472 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4473 }
4474}
4475
David Brazdil66d126e2015-04-03 16:02:44 +01004476void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4478 locations->SetInAt(0, Location::RequiresRegister());
4479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4480}
4481
4482void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004483 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004484}
4485
Alexandre Rames5319def2014-10-23 10:03:10 +01004486void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004487 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4488 ? LocationSummary::kCallOnSlowPath
4489 : LocationSummary::kNoCall;
4490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004491 locations->SetInAt(0, Location::RequiresRegister());
4492 if (instruction->HasUses()) {
4493 locations->SetOut(Location::SameAsFirstInput());
4494 }
4495}
4496
Calin Juravle2ae48182016-03-16 14:05:09 +00004497void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4498 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004499 return;
4500 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004501
Alexandre Ramesd921d642015-04-16 15:07:16 +01004502 BlockPoolsScope block_pools(GetVIXLAssembler());
4503 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004504 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004505 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004506}
4507
Calin Juravle2ae48182016-03-16 14:05:09 +00004508void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004509 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004510 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004511
4512 LocationSummary* locations = instruction->GetLocations();
4513 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004514
4515 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004516}
4517
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004518void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004519 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004520}
4521
Alexandre Rames67555f72014-11-18 10:55:16 +00004522void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4523 HandleBinaryOp(instruction);
4524}
4525
4526void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4527 HandleBinaryOp(instruction);
4528}
4529
Alexandre Rames3e69f162014-12-10 10:36:50 +00004530void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4531 LOG(FATAL) << "Unreachable";
4532}
4533
4534void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4535 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4536}
4537
Alexandre Rames5319def2014-10-23 10:03:10 +01004538void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4540 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4541 if (location.IsStackSlot()) {
4542 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4543 } else if (location.IsDoubleStackSlot()) {
4544 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4545 }
4546 locations->SetOut(location);
4547}
4548
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004549void InstructionCodeGeneratorARM64::VisitParameterValue(
4550 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004551 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004552}
4553
4554void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4555 LocationSummary* locations =
4556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004557 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004558}
4559
4560void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4561 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4562 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004563}
4564
4565void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004567 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004568 locations->SetInAt(i, Location::Any());
4569 }
4570 locations->SetOut(Location::Any());
4571}
4572
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004573void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004574 LOG(FATAL) << "Unreachable";
4575}
4576
Serban Constantinescu02164b32014-11-13 14:05:07 +00004577void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004578 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004579 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004580 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4581 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4583
4584 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004585 case Primitive::kPrimInt:
4586 case Primitive::kPrimLong:
4587 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004588 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4590 break;
4591
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004592 case Primitive::kPrimFloat:
4593 case Primitive::kPrimDouble: {
4594 InvokeRuntimeCallingConvention calling_convention;
4595 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4596 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4597 locations->SetOut(calling_convention.GetReturnLocation(type));
4598
4599 break;
4600 }
4601
Serban Constantinescu02164b32014-11-13 14:05:07 +00004602 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004603 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004604 }
4605}
4606
4607void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4608 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004609
Serban Constantinescu02164b32014-11-13 14:05:07 +00004610 switch (type) {
4611 case Primitive::kPrimInt:
4612 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004613 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004614 break;
4615 }
4616
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004617 case Primitive::kPrimFloat:
4618 case Primitive::kPrimDouble: {
4619 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4620 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004621 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004622 if (type == Primitive::kPrimFloat) {
4623 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4624 } else {
4625 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4626 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004627 break;
4628 }
4629
Serban Constantinescu02164b32014-11-13 14:05:07 +00004630 default:
4631 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004632 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004633 }
4634}
4635
Calin Juravle27df7582015-04-17 19:12:31 +01004636void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4637 memory_barrier->SetLocations(nullptr);
4638}
4639
4640void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004641 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004642}
4643
Alexandre Rames5319def2014-10-23 10:03:10 +01004644void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4646 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004647 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004648}
4649
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004650void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004651 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004652}
4653
4654void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4655 instruction->SetLocations(nullptr);
4656}
4657
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004658void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004659 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004660}
4661
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004662void LocationsBuilderARM64::VisitRor(HRor* ror) {
4663 HandleBinaryOp(ror);
4664}
4665
4666void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4667 HandleBinaryOp(ror);
4668}
4669
Serban Constantinescu02164b32014-11-13 14:05:07 +00004670void LocationsBuilderARM64::VisitShl(HShl* shl) {
4671 HandleShift(shl);
4672}
4673
4674void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4675 HandleShift(shl);
4676}
4677
4678void LocationsBuilderARM64::VisitShr(HShr* shr) {
4679 HandleShift(shr);
4680}
4681
4682void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4683 HandleShift(shr);
4684}
4685
Alexandre Rames5319def2014-10-23 10:03:10 +01004686void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004687 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004688}
4689
4690void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004691 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004692}
4693
Alexandre Rames67555f72014-11-18 10:55:16 +00004694void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004695 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004696}
4697
4698void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004699 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004700}
4701
4702void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004703 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004704}
4705
Alexandre Rames67555f72014-11-18 10:55:16 +00004706void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004707 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004708}
4709
Calin Juravlee460d1d2015-09-29 04:52:17 +01004710void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4711 HUnresolvedInstanceFieldGet* instruction) {
4712 FieldAccessCallingConventionARM64 calling_convention;
4713 codegen_->CreateUnresolvedFieldLocationSummary(
4714 instruction, instruction->GetFieldType(), calling_convention);
4715}
4716
4717void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4718 HUnresolvedInstanceFieldGet* instruction) {
4719 FieldAccessCallingConventionARM64 calling_convention;
4720 codegen_->GenerateUnresolvedFieldAccess(instruction,
4721 instruction->GetFieldType(),
4722 instruction->GetFieldIndex(),
4723 instruction->GetDexPc(),
4724 calling_convention);
4725}
4726
4727void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4728 HUnresolvedInstanceFieldSet* instruction) {
4729 FieldAccessCallingConventionARM64 calling_convention;
4730 codegen_->CreateUnresolvedFieldLocationSummary(
4731 instruction, instruction->GetFieldType(), calling_convention);
4732}
4733
4734void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4735 HUnresolvedInstanceFieldSet* instruction) {
4736 FieldAccessCallingConventionARM64 calling_convention;
4737 codegen_->GenerateUnresolvedFieldAccess(instruction,
4738 instruction->GetFieldType(),
4739 instruction->GetFieldIndex(),
4740 instruction->GetDexPc(),
4741 calling_convention);
4742}
4743
4744void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4745 HUnresolvedStaticFieldGet* instruction) {
4746 FieldAccessCallingConventionARM64 calling_convention;
4747 codegen_->CreateUnresolvedFieldLocationSummary(
4748 instruction, instruction->GetFieldType(), calling_convention);
4749}
4750
4751void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4752 HUnresolvedStaticFieldGet* instruction) {
4753 FieldAccessCallingConventionARM64 calling_convention;
4754 codegen_->GenerateUnresolvedFieldAccess(instruction,
4755 instruction->GetFieldType(),
4756 instruction->GetFieldIndex(),
4757 instruction->GetDexPc(),
4758 calling_convention);
4759}
4760
4761void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4762 HUnresolvedStaticFieldSet* instruction) {
4763 FieldAccessCallingConventionARM64 calling_convention;
4764 codegen_->CreateUnresolvedFieldLocationSummary(
4765 instruction, instruction->GetFieldType(), calling_convention);
4766}
4767
4768void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4769 HUnresolvedStaticFieldSet* instruction) {
4770 FieldAccessCallingConventionARM64 calling_convention;
4771 codegen_->GenerateUnresolvedFieldAccess(instruction,
4772 instruction->GetFieldType(),
4773 instruction->GetFieldIndex(),
4774 instruction->GetDexPc(),
4775 calling_convention);
4776}
4777
Alexandre Rames5319def2014-10-23 10:03:10 +01004778void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4779 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4780}
4781
4782void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004783 HBasicBlock* block = instruction->GetBlock();
4784 if (block->GetLoopInformation() != nullptr) {
4785 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4786 // The back edge will generate the suspend check.
4787 return;
4788 }
4789 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4790 // The goto will generate the suspend check.
4791 return;
4792 }
4793 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004794}
4795
Alexandre Rames67555f72014-11-18 10:55:16 +00004796void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4797 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004799 InvokeRuntimeCallingConvention calling_convention;
4800 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4801}
4802
4803void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4804 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004805 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004806 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004807}
4808
4809void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4810 LocationSummary* locations =
4811 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4812 Primitive::Type input_type = conversion->GetInputType();
4813 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004814 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004815 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4816 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4817 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4818 }
4819
Alexandre Rames542361f2015-01-29 16:57:31 +00004820 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004821 locations->SetInAt(0, Location::RequiresFpuRegister());
4822 } else {
4823 locations->SetInAt(0, Location::RequiresRegister());
4824 }
4825
Alexandre Rames542361f2015-01-29 16:57:31 +00004826 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004827 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4828 } else {
4829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4830 }
4831}
4832
4833void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4834 Primitive::Type result_type = conversion->GetResultType();
4835 Primitive::Type input_type = conversion->GetInputType();
4836
4837 DCHECK_NE(input_type, result_type);
4838
Alexandre Rames542361f2015-01-29 16:57:31 +00004839 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004840 int result_size = Primitive::ComponentSize(result_type);
4841 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004842 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004843 Register output = OutputRegister(conversion);
4844 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004845 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004846 // 'int' values are used directly as W registers, discarding the top
4847 // bits, so we don't need to sign-extend and can just perform a move.
4848 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4849 // top 32 bits of the target register. We theoretically could leave those
4850 // bits unchanged, but we would have to make sure that no code uses a
4851 // 32bit input value as a 64bit value assuming that the top 32 bits are
4852 // zero.
4853 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004854 } else if (result_type == Primitive::kPrimChar ||
4855 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4856 __ Ubfx(output,
4857 output.IsX() ? source.X() : source.W(),
4858 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004859 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004860 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004861 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004862 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004863 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004864 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004865 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4866 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004867 } else if (Primitive::IsFloatingPointType(result_type) &&
4868 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004869 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4870 } else {
4871 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4872 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004873 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004874}
Alexandre Rames67555f72014-11-18 10:55:16 +00004875
Serban Constantinescu02164b32014-11-13 14:05:07 +00004876void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4877 HandleShift(ushr);
4878}
4879
4880void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4881 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004882}
4883
4884void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4885 HandleBinaryOp(instruction);
4886}
4887
4888void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4889 HandleBinaryOp(instruction);
4890}
4891
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004892void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004893 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004894 LOG(FATAL) << "Unreachable";
4895}
4896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004897void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004898 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004899 LOG(FATAL) << "Unreachable";
4900}
4901
Mark Mendellfe57faa2015-09-18 09:26:15 -04004902// Simple implementation of packed switch - generate cascaded compare/jumps.
4903void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4904 LocationSummary* locations =
4905 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4906 locations->SetInAt(0, Location::RequiresRegister());
4907}
4908
4909void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4910 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004911 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004912 Register value_reg = InputRegisterAt(switch_instr, 0);
4913 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4914
Zheng Xu3927c8b2015-11-18 17:46:25 +08004915 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004916 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004917 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4918 // make sure we don't emit it if the target may run out of range.
4919 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4920 // ranges and emit the tables only as required.
4921 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004922
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004923 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004924 // Current instruction id is an upper bound of the number of HIRs in the graph.
4925 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4926 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004927 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4928 Register temp = temps.AcquireW();
4929 __ Subs(temp, value_reg, Operand(lower_bound));
4930
Zheng Xu3927c8b2015-11-18 17:46:25 +08004931 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004932 // Jump to successors[0] if value == lower_bound.
4933 __ B(eq, codegen_->GetLabelOf(successors[0]));
4934 int32_t last_index = 0;
4935 for (; num_entries - last_index > 2; last_index += 2) {
4936 __ Subs(temp, temp, Operand(2));
4937 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4938 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4939 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4940 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4941 }
4942 if (num_entries - last_index == 2) {
4943 // The last missing case_value.
4944 __ Cmp(temp, Operand(1));
4945 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004946 }
4947
4948 // And the default for any other value.
4949 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4950 __ B(codegen_->GetLabelOf(default_block));
4951 }
4952 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004953 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004954
4955 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4956
4957 // Below instructions should use at most one blocked register. Since there are two blocked
4958 // registers, we are free to block one.
4959 Register temp_w = temps.AcquireW();
4960 Register index;
4961 // Remove the bias.
4962 if (lower_bound != 0) {
4963 index = temp_w;
4964 __ Sub(index, value_reg, Operand(lower_bound));
4965 } else {
4966 index = value_reg;
4967 }
4968
4969 // Jump to default block if index is out of the range.
4970 __ Cmp(index, Operand(num_entries));
4971 __ B(hs, codegen_->GetLabelOf(default_block));
4972
4973 // In current VIXL implementation, it won't require any blocked registers to encode the
4974 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4975 // register pressure.
4976 Register table_base = temps.AcquireX();
4977 // Load jump offset from the table.
4978 __ Adr(table_base, jump_table->GetTableStartLabel());
4979 Register jump_offset = temp_w;
4980 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4981
4982 // Jump to target block by branching to table_base(pc related) + offset.
4983 Register target_address = table_base;
4984 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4985 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004986 }
4987}
4988
Roland Levillain44015862016-01-22 11:47:17 +00004989void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4990 Location out,
4991 uint32_t offset,
4992 Location maybe_temp) {
4993 Primitive::Type type = Primitive::kPrimNot;
4994 Register out_reg = RegisterFrom(out, type);
4995 if (kEmitCompilerReadBarrier) {
4996 Register temp_reg = RegisterFrom(maybe_temp, type);
4997 if (kUseBakerReadBarrier) {
4998 // Load with fast path based Baker's read barrier.
4999 // /* HeapReference<Object> */ out = *(out + offset)
5000 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5001 out,
5002 out_reg,
5003 offset,
5004 temp_reg,
5005 /* needs_null_check */ false,
5006 /* use_load_acquire */ false);
5007 } else {
5008 // Load with slow path based read barrier.
5009 // Save the value of `out` into `maybe_temp` before overwriting it
5010 // in the following move operation, as we will need it for the
5011 // read barrier below.
5012 __ Mov(temp_reg, out_reg);
5013 // /* HeapReference<Object> */ out = *(out + offset)
5014 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5015 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5016 }
5017 } else {
5018 // Plain load with no read barrier.
5019 // /* HeapReference<Object> */ out = *(out + offset)
5020 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5021 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5022 }
5023}
5024
5025void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5026 Location out,
5027 Location obj,
5028 uint32_t offset,
5029 Location maybe_temp) {
5030 Primitive::Type type = Primitive::kPrimNot;
5031 Register out_reg = RegisterFrom(out, type);
5032 Register obj_reg = RegisterFrom(obj, type);
5033 if (kEmitCompilerReadBarrier) {
5034 if (kUseBakerReadBarrier) {
5035 // Load with fast path based Baker's read barrier.
5036 Register temp_reg = RegisterFrom(maybe_temp, type);
5037 // /* HeapReference<Object> */ out = *(obj + offset)
5038 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5039 out,
5040 obj_reg,
5041 offset,
5042 temp_reg,
5043 /* needs_null_check */ false,
5044 /* use_load_acquire */ false);
5045 } else {
5046 // Load with slow path based read barrier.
5047 // /* HeapReference<Object> */ out = *(obj + offset)
5048 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5049 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5050 }
5051 } else {
5052 // Plain load with no read barrier.
5053 // /* HeapReference<Object> */ out = *(obj + offset)
5054 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5055 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5056 }
5057}
5058
5059void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5060 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005061 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005062 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005063 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005064 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5065 if (kEmitCompilerReadBarrier) {
5066 if (kUseBakerReadBarrier) {
5067 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5068 // Baker's read barrier are used:
5069 //
5070 // root = obj.field;
5071 // if (Thread::Current()->GetIsGcMarking()) {
5072 // root = ReadBarrier::Mark(root)
5073 // }
5074
5075 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 if (fixup_label == nullptr) {
5077 __ Ldr(root_reg, MemOperand(obj, offset));
5078 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005079 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005080 __ Bind(fixup_label);
5081 __ ldr(root_reg, MemOperand(obj, offset));
5082 }
Roland Levillain44015862016-01-22 11:47:17 +00005083 static_assert(
5084 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5085 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5086 "have different sizes.");
5087 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5088 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5089 "have different sizes.");
5090
5091 // Slow path used to mark the GC root `root`.
5092 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005093 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005094 codegen_->AddSlowPath(slow_path);
5095
5096 MacroAssembler* masm = GetVIXLAssembler();
5097 UseScratchRegisterScope temps(masm);
5098 Register temp = temps.AcquireW();
5099 // temp = Thread::Current()->GetIsGcMarking()
5100 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
5101 __ Cbnz(temp, slow_path->GetEntryLabel());
5102 __ Bind(slow_path->GetExitLabel());
5103 } else {
5104 // GC root loaded through a slow path for read barriers other
5105 // than Baker's.
5106 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005107 if (fixup_label == nullptr) {
5108 __ Add(root_reg.X(), obj.X(), offset);
5109 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005110 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005111 __ Bind(fixup_label);
5112 __ add(root_reg.X(), obj.X(), offset);
5113 }
Roland Levillain44015862016-01-22 11:47:17 +00005114 // /* mirror::Object* */ root = root->Read()
5115 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5116 }
5117 } else {
5118 // Plain GC root load with no read barrier.
5119 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005120 if (fixup_label == nullptr) {
5121 __ Ldr(root_reg, MemOperand(obj, offset));
5122 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005123 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005124 __ Bind(fixup_label);
5125 __ ldr(root_reg, MemOperand(obj, offset));
5126 }
Roland Levillain44015862016-01-22 11:47:17 +00005127 // Note that GC roots are not affected by heap poisoning, thus we
5128 // do not have to unpoison `root_reg` here.
5129 }
5130}
5131
5132void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5133 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005134 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005135 uint32_t offset,
5136 Register temp,
5137 bool needs_null_check,
5138 bool use_load_acquire) {
5139 DCHECK(kEmitCompilerReadBarrier);
5140 DCHECK(kUseBakerReadBarrier);
5141
5142 // /* HeapReference<Object> */ ref = *(obj + offset)
5143 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005144 size_t no_scale_factor = 0U;
5145 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5146 ref,
5147 obj,
5148 offset,
5149 no_index,
5150 no_scale_factor,
5151 temp,
5152 needs_null_check,
5153 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005154}
5155
5156void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5157 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005158 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005159 uint32_t data_offset,
5160 Location index,
5161 Register temp,
5162 bool needs_null_check) {
5163 DCHECK(kEmitCompilerReadBarrier);
5164 DCHECK(kUseBakerReadBarrier);
5165
5166 // Array cells are never volatile variables, therefore array loads
5167 // never use Load-Acquire instructions on ARM64.
5168 const bool use_load_acquire = false;
5169
Roland Levillainbfea3352016-06-23 13:48:47 +01005170 static_assert(
5171 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5172 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005173 // /* HeapReference<Object> */ ref =
5174 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005175 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5176 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5177 ref,
5178 obj,
5179 data_offset,
5180 index,
5181 scale_factor,
5182 temp,
5183 needs_null_check,
5184 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005185}
5186
5187void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5188 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005189 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005190 uint32_t offset,
5191 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005192 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005193 Register temp,
5194 bool needs_null_check,
5195 bool use_load_acquire) {
5196 DCHECK(kEmitCompilerReadBarrier);
5197 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005198 // If we are emitting an array load, we should not be using a
5199 // Load Acquire instruction. In other words:
5200 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5201 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005202
5203 MacroAssembler* masm = GetVIXLAssembler();
5204 UseScratchRegisterScope temps(masm);
5205
5206 // In slow path based read barriers, the read barrier call is
5207 // inserted after the original load. However, in fast path based
5208 // Baker's read barriers, we need to perform the load of
5209 // mirror::Object::monitor_ *before* the original reference load.
5210 // This load-load ordering is required by the read barrier.
5211 // The fast path/slow path (for Baker's algorithm) should look like:
5212 //
5213 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5214 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5215 // HeapReference<Object> ref = *src; // Original reference load.
5216 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5217 // if (is_gray) {
5218 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5219 // }
5220 //
5221 // Note: the original implementation in ReadBarrier::Barrier is
5222 // slightly more complex as it performs additional checks that we do
5223 // not do here for performance reasons.
5224
5225 Primitive::Type type = Primitive::kPrimNot;
5226 Register ref_reg = RegisterFrom(ref, type);
5227 DCHECK(obj.IsW());
5228 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5229
5230 // /* int32_t */ monitor = obj->monitor_
5231 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5232 if (needs_null_check) {
5233 MaybeRecordImplicitNullCheck(instruction);
5234 }
5235 // /* LockWord */ lock_word = LockWord(monitor)
5236 static_assert(sizeof(LockWord) == sizeof(int32_t),
5237 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005238
Vladimir Marko877a0332016-07-11 19:30:56 +01005239 // Introduce a dependency on the lock_word including rb_state,
5240 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005241 // a memory barrier (which would be more expensive).
Vladimir Marko877a0332016-07-11 19:30:56 +01005242 // obj is unchanged by this operation, but its value now depends on temp.
5243 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005244
5245 // The actual reference load.
5246 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005247 // Load types involving an "index".
5248 if (use_load_acquire) {
5249 // UnsafeGetObjectVolatile intrinsic case.
5250 // Register `index` is not an index in an object array, but an
5251 // offset to an object reference field within object `obj`.
5252 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5253 DCHECK(instruction->GetLocations()->Intrinsified());
5254 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5255 << instruction->AsInvoke()->GetIntrinsic();
5256 DCHECK_EQ(offset, 0U);
5257 DCHECK_EQ(scale_factor, 0U);
5258 DCHECK_EQ(needs_null_check, 0U);
5259 // /* HeapReference<Object> */ ref = *(obj + index)
5260 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5261 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005262 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005263 // ArrayGet and UnsafeGetObject intrinsics cases.
5264 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5265 if (index.IsConstant()) {
5266 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5267 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5268 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005269 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005270 __ Add(temp2, obj, offset);
5271 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5272 temps.Release(temp2);
5273 }
Roland Levillain44015862016-01-22 11:47:17 +00005274 }
Roland Levillain44015862016-01-22 11:47:17 +00005275 } else {
5276 // /* HeapReference<Object> */ ref = *(obj + offset)
5277 MemOperand field = HeapOperand(obj, offset);
5278 if (use_load_acquire) {
5279 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5280 } else {
5281 Load(type, ref_reg, field);
5282 }
5283 }
5284
5285 // Object* ref = ref_addr->AsMirrorPtr()
5286 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5287
5288 // Slow path used to mark the object `ref` when it is gray.
5289 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005290 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005291 AddSlowPath(slow_path);
5292
5293 // if (rb_state == ReadBarrier::gray_ptr_)
5294 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005295 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5296 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5297 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5298 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5299 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005300 __ Bind(slow_path->GetExitLabel());
5301}
5302
5303void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5304 Location out,
5305 Location ref,
5306 Location obj,
5307 uint32_t offset,
5308 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005309 DCHECK(kEmitCompilerReadBarrier);
5310
Roland Levillain44015862016-01-22 11:47:17 +00005311 // Insert a slow path based read barrier *after* the reference load.
5312 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005313 // If heap poisoning is enabled, the unpoisoning of the loaded
5314 // reference will be carried out by the runtime within the slow
5315 // path.
5316 //
5317 // Note that `ref` currently does not get unpoisoned (when heap
5318 // poisoning is enabled), which is alright as the `ref` argument is
5319 // not used by the artReadBarrierSlow entry point.
5320 //
5321 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5322 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5323 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5324 AddSlowPath(slow_path);
5325
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005326 __ B(slow_path->GetEntryLabel());
5327 __ Bind(slow_path->GetExitLabel());
5328}
5329
Roland Levillain44015862016-01-22 11:47:17 +00005330void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5331 Location out,
5332 Location ref,
5333 Location obj,
5334 uint32_t offset,
5335 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005336 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005337 // Baker's read barriers shall be handled by the fast path
5338 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5339 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005340 // If heap poisoning is enabled, unpoisoning will be taken care of
5341 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005342 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005343 } else if (kPoisonHeapReferences) {
5344 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5345 }
5346}
5347
Roland Levillain44015862016-01-22 11:47:17 +00005348void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5349 Location out,
5350 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005351 DCHECK(kEmitCompilerReadBarrier);
5352
Roland Levillain44015862016-01-22 11:47:17 +00005353 // Insert a slow path based read barrier *after* the GC root load.
5354 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005355 // Note that GC roots are not affected by heap poisoning, so we do
5356 // not need to do anything special for this here.
5357 SlowPathCodeARM64* slow_path =
5358 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5359 AddSlowPath(slow_path);
5360
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005361 __ B(slow_path->GetEntryLabel());
5362 __ Bind(slow_path->GetExitLabel());
5363}
5364
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005365void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5366 LocationSummary* locations =
5367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5368 locations->SetInAt(0, Location::RequiresRegister());
5369 locations->SetOut(Location::RequiresRegister());
5370}
5371
5372void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5373 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005374 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005375 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005376 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005377 __ Ldr(XRegisterFrom(locations->Out()),
5378 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005379 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005380 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
5381 instruction->GetIndex() % ImTable::kSize, kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005382 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5383 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005384 __ Ldr(XRegisterFrom(locations->Out()),
5385 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005386 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005387}
5388
5389
5390
Alexandre Rames67555f72014-11-18 10:55:16 +00005391#undef __
5392#undef QUICK_ENTRY_POINT
5393
Alexandre Rames5319def2014-10-23 10:03:10 +01005394} // namespace arm64
5395} // namespace art