blob: eaffcee6dc2fa135b4b4e5a976b5fdd0f6e1498d [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
Andreas Gampe542451c2016-07-26 09:02:02 -0700136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, 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);
Andreas Gampe67409972016-07-19 22:34:53 -0700465 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000466 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() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100600 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000601 << "Unexpected instruction in read barrier marking slow path: "
602 << instruction_->DebugName();
603
604 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100605 // No need to save live registers; it's taken care of by the
606 // entrypoint. Also, there is no need to update the stack mask,
607 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000608 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100609 DCHECK_NE(obj_.reg(), LR);
610 DCHECK_NE(obj_.reg(), WSP);
611 DCHECK_NE(obj_.reg(), WZR);
612 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
613 // "Compact" slow path, saving two moves.
614 //
615 // Instead of using the standard runtime calling convention (input
616 // and output in W0):
617 //
618 // W0 <- obj
619 // W0 <- ReadBarrierMark(W0)
620 // obj <- W0
621 //
622 // we just use rX (the register holding `obj`) as input and output
623 // of a dedicated entrypoint:
624 //
625 // rX <- ReadBarrierMarkRegX(rX)
626 //
627 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700628 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100629 // This runtime call does not require a stack map.
630 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000631 __ B(GetExitLabel());
632 }
633
634 private:
Roland Levillain44015862016-01-22 11:47:17 +0000635 const Location obj_;
636
637 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
638};
639
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000640// Slow path generating a read barrier for a heap reference.
641class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
642 public:
643 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
644 Location out,
645 Location ref,
646 Location obj,
647 uint32_t offset,
648 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000649 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000650 out_(out),
651 ref_(ref),
652 obj_(obj),
653 offset_(offset),
654 index_(index) {
655 DCHECK(kEmitCompilerReadBarrier);
656 // If `obj` is equal to `out` or `ref`, it means the initial object
657 // has been overwritten by (or after) the heap object reference load
658 // to be instrumented, e.g.:
659 //
660 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000661 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000662 //
663 // In that case, we have lost the information about the original
664 // object, and the emitted read barrier cannot work properly.
665 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
666 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
667 }
668
669 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
670 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
671 LocationSummary* locations = instruction_->GetLocations();
672 Primitive::Type type = Primitive::kPrimNot;
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100675 DCHECK(instruction_->IsInstanceFieldGet() ||
676 instruction_->IsStaticFieldGet() ||
677 instruction_->IsArrayGet() ||
678 instruction_->IsInstanceOf() ||
679 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100680 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000681 << "Unexpected instruction in read barrier for heap reference slow path: "
682 << instruction_->DebugName();
Artem Serov328429f2016-07-06 16:23:04 +0100683 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000684 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100685 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000686
687 __ Bind(GetEntryLabel());
688
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000689 SaveLiveRegisters(codegen, locations);
690
691 // We may have to change the index's value, but as `index_` is a
692 // constant member (like other "inputs" of this slow path),
693 // introduce a copy of it, `index`.
694 Location index = index_;
695 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100696 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000697 if (instruction_->IsArrayGet()) {
698 // Compute the actual memory offset and store it in `index`.
699 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
700 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
701 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
702 // We are about to change the value of `index_reg` (see the
703 // calls to vixl::MacroAssembler::Lsl and
704 // vixl::MacroAssembler::Mov below), but it has
705 // not been saved by the previous call to
706 // art::SlowPathCode::SaveLiveRegisters, as it is a
707 // callee-save register --
708 // art::SlowPathCode::SaveLiveRegisters does not consider
709 // callee-save registers, as it has been designed with the
710 // assumption that callee-save registers are supposed to be
711 // handled by the called function. So, as a callee-save
712 // register, `index_reg` _would_ eventually be saved onto
713 // the stack, but it would be too late: we would have
714 // changed its value earlier. Therefore, we manually save
715 // it here into another freely available register,
716 // `free_reg`, chosen of course among the caller-save
717 // registers (as a callee-save `free_reg` register would
718 // exhibit the same problem).
719 //
720 // Note we could have requested a temporary register from
721 // the register allocator instead; but we prefer not to, as
722 // this is a slow path, and we know we can find a
723 // caller-save register that is available.
724 Register free_reg = FindAvailableCallerSaveRegister(codegen);
725 __ Mov(free_reg.W(), index_reg);
726 index_reg = free_reg;
727 index = LocationFrom(index_reg);
728 } else {
729 // The initial register stored in `index_` has already been
730 // saved in the call to art::SlowPathCode::SaveLiveRegisters
731 // (as it is not a callee-save register), so we can freely
732 // use it.
733 }
734 // Shifting the index value contained in `index_reg` by the scale
735 // factor (2) cannot overflow in practice, as the runtime is
736 // unable to allocate object arrays with a size larger than
737 // 2^26 - 1 (that is, 2^28 - 4 bytes).
738 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
739 static_assert(
740 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
741 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
742 __ Add(index_reg, index_reg, Operand(offset_));
743 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100744 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
745 // intrinsics, `index_` is not shifted by a scale factor of 2
746 // (as in the case of ArrayGet), as it is actually an offset
747 // to an object field within an object.
748 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000749 DCHECK(instruction_->GetLocations()->Intrinsified());
750 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
751 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
752 << instruction_->AsInvoke()->GetIntrinsic();
753 DCHECK_EQ(offset_, 0U);
754 DCHECK(index_.IsRegisterPair());
755 // UnsafeGet's offset location is a register pair, the low
756 // part contains the correct offset.
757 index = index_.ToLow();
758 }
759 }
760
761 // We're moving two or three locations to locations that could
762 // overlap, so we need a parallel move resolver.
763 InvokeRuntimeCallingConvention calling_convention;
764 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
765 parallel_move.AddMove(ref_,
766 LocationFrom(calling_convention.GetRegisterAt(0)),
767 type,
768 nullptr);
769 parallel_move.AddMove(obj_,
770 LocationFrom(calling_convention.GetRegisterAt(1)),
771 type,
772 nullptr);
773 if (index.IsValid()) {
774 parallel_move.AddMove(index,
775 LocationFrom(calling_convention.GetRegisterAt(2)),
776 Primitive::kPrimInt,
777 nullptr);
778 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
779 } else {
780 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
781 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
782 }
783 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
784 instruction_,
785 instruction_->GetDexPc(),
786 this);
787 CheckEntrypointTypes<
788 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
789 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
790
791 RestoreLiveRegisters(codegen, locations);
792
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000793 __ B(GetExitLabel());
794 }
795
796 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
797
798 private:
799 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100800 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
801 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000802 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
803 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
804 return Register(VIXLRegCodeFromART(i), kXRegSize);
805 }
806 }
807 // We shall never fail to find a free caller-save register, as
808 // there are more than two core caller-save registers on ARM64
809 // (meaning it is possible to find one which is different from
810 // `ref` and `obj`).
811 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
812 LOG(FATAL) << "Could not find a free register";
813 UNREACHABLE();
814 }
815
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000816 const Location out_;
817 const Location ref_;
818 const Location obj_;
819 const uint32_t offset_;
820 // An additional location containing an index to an array.
821 // Only used for HArrayGet and the UnsafeGetObject &
822 // UnsafeGetObjectVolatile intrinsics.
823 const Location index_;
824
825 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
826};
827
828// Slow path generating a read barrier for a GC root.
829class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
830 public:
831 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000832 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000833 DCHECK(kEmitCompilerReadBarrier);
834 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000835
836 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
837 LocationSummary* locations = instruction_->GetLocations();
838 Primitive::Type type = Primitive::kPrimNot;
839 DCHECK(locations->CanCall());
840 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000841 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
842 << "Unexpected instruction in read barrier for GC root slow path: "
843 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000844
845 __ Bind(GetEntryLabel());
846 SaveLiveRegisters(codegen, locations);
847
848 InvokeRuntimeCallingConvention calling_convention;
849 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
850 // The argument of the ReadBarrierForRootSlow is not a managed
851 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
852 // thus we need a 64-bit move here, and we cannot use
853 //
854 // arm64_codegen->MoveLocation(
855 // LocationFrom(calling_convention.GetRegisterAt(0)),
856 // root_,
857 // type);
858 //
859 // which would emit a 32-bit move, as `type` is a (32-bit wide)
860 // reference type (`Primitive::kPrimNot`).
861 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
862 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
863 instruction_,
864 instruction_->GetDexPc(),
865 this);
866 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
867 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
868
869 RestoreLiveRegisters(codegen, locations);
870 __ B(GetExitLabel());
871 }
872
873 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
874
875 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000876 const Location out_;
877 const Location root_;
878
879 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
880};
881
Alexandre Rames5319def2014-10-23 10:03:10 +0100882#undef __
883
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100884Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100885 Location next_location;
886 if (type == Primitive::kPrimVoid) {
887 LOG(FATAL) << "Unreachable type " << type;
888 }
889
Alexandre Rames542361f2015-01-29 16:57:31 +0000890 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100891 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
892 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000893 } else if (!Primitive::IsFloatingPointType(type) &&
894 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
896 } else {
897 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000898 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
899 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100900 }
901
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000902 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000903 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100904 return next_location;
905}
906
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100907Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100908 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100909}
910
Serban Constantinescu579885a2015-02-22 20:51:33 +0000911CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
912 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100913 const CompilerOptions& compiler_options,
914 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100915 : CodeGenerator(graph,
916 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000917 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000918 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100919 callee_saved_core_registers.GetList(),
920 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100921 compiler_options,
922 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100923 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800924 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100925 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000926 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000927 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100928 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000929 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000930 uint32_literals_(std::less<uint32_t>(),
931 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100932 uint64_literals_(std::less<uint64_t>(),
933 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
934 method_patches_(MethodReferenceComparator(),
935 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
936 call_patches_(MethodReferenceComparator(),
937 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
938 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
940 boot_image_string_patches_(StringReferenceValueComparator(),
941 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
942 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100943 boot_image_type_patches_(TypeReferenceValueComparator(),
944 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
945 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000946 boot_image_address_patches_(std::less<uint32_t>(),
947 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000948 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000949 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000950}
Alexandre Rames5319def2014-10-23 10:03:10 +0100951
Alexandre Rames67555f72014-11-18 10:55:16 +0000952#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100953
Zheng Xu3927c8b2015-11-18 17:46:25 +0800954void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100955 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800956 jump_table->EmitTable(this);
957 }
958}
959
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000960void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800961 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000962 // Ensure we emit the literal pool.
963 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000964
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000965 CodeGenerator::Finalize(allocator);
966}
967
Zheng Xuad4450e2015-04-17 18:48:56 +0800968void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
969 // Note: There are 6 kinds of moves:
970 // 1. constant -> GPR/FPR (non-cycle)
971 // 2. constant -> stack (non-cycle)
972 // 3. GPR/FPR -> GPR/FPR
973 // 4. GPR/FPR -> stack
974 // 5. stack -> GPR/FPR
975 // 6. stack -> stack (non-cycle)
976 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
977 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
978 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
979 // dependency.
980 vixl_temps_.Open(GetVIXLAssembler());
981}
982
983void ParallelMoveResolverARM64::FinishEmitNativeCode() {
984 vixl_temps_.Close();
985}
986
987Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
988 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
989 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
990 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
991 Location scratch = GetScratchLocation(kind);
992 if (!scratch.Equals(Location::NoLocation())) {
993 return scratch;
994 }
995 // Allocate from VIXL temp registers.
996 if (kind == Location::kRegister) {
997 scratch = LocationFrom(vixl_temps_.AcquireX());
998 } else {
999 DCHECK(kind == Location::kFpuRegister);
1000 scratch = LocationFrom(vixl_temps_.AcquireD());
1001 }
1002 AddScratchLocation(scratch);
1003 return scratch;
1004}
1005
1006void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1007 if (loc.IsRegister()) {
1008 vixl_temps_.Release(XRegisterFrom(loc));
1009 } else {
1010 DCHECK(loc.IsFpuRegister());
1011 vixl_temps_.Release(DRegisterFrom(loc));
1012 }
1013 RemoveScratchLocation(loc);
1014}
1015
Alexandre Rames3e69f162014-12-10 10:36:50 +00001016void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001017 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001018 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001019}
1020
Alexandre Rames5319def2014-10-23 10:03:10 +01001021void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001022 MacroAssembler* masm = GetVIXLAssembler();
1023 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001024 __ Bind(&frame_entry_label_);
1025
Serban Constantinescu02164b32014-11-13 14:05:07 +00001026 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1027 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001028 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001029 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001030 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001031 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001032 __ Ldr(wzr, MemOperand(temp, 0));
1033 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001034 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001035
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001036 if (!HasEmptyFrame()) {
1037 int frame_size = GetFrameSize();
1038 // Stack layout:
1039 // sp[frame_size - 8] : lr.
1040 // ... : other preserved core registers.
1041 // ... : other preserved fp registers.
1042 // ... : reserved frame space.
1043 // sp[0] : current method.
1044 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001045 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001046 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1047 frame_size - GetCoreSpillSize());
1048 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1049 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001050 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001051}
1052
1053void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001054 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001055 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001056 if (!HasEmptyFrame()) {
1057 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001058 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1059 frame_size - FrameEntrySpillSize());
1060 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1061 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001062 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001064 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001065 __ Ret();
1066 GetAssembler()->cfi().RestoreState();
1067 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001068}
1069
Scott Wakeling97c72b72016-06-24 16:19:36 +01001070CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001071 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001072 return CPURegList(CPURegister::kRegister, kXRegSize,
1073 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001074}
1075
Scott Wakeling97c72b72016-06-24 16:19:36 +01001076CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001077 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1078 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001079 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1080 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001081}
1082
Alexandre Rames5319def2014-10-23 10:03:10 +01001083void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1084 __ Bind(GetLabelOf(block));
1085}
1086
Calin Juravle175dc732015-08-25 15:42:32 +01001087void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1088 DCHECK(location.IsRegister());
1089 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1090}
1091
Calin Juravlee460d1d2015-09-29 04:52:17 +01001092void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1093 if (location.IsRegister()) {
1094 locations->AddTemp(location);
1095 } else {
1096 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1097 }
1098}
1099
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001100void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001104 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001105 if (value_can_be_null) {
1106 __ Cbz(value, &done);
1107 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001108 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001109 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001110 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001111 if (value_can_be_null) {
1112 __ Bind(&done);
1113 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001114}
1115
David Brazdil58282f42016-01-14 12:45:10 +00001116void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001117 // Blocked core registers:
1118 // lr : Runtime reserved.
1119 // tr : Runtime reserved.
1120 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1121 // ip1 : VIXL core temp.
1122 // ip0 : VIXL core temp.
1123 //
1124 // Blocked fp registers:
1125 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1127 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001128 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001129 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001130 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001131
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001132 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001133 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001134 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001135 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001136
David Brazdil58282f42016-01-14 12:45:10 +00001137 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001138 // Stubs do not save callee-save floating point registers. If the graph
1139 // is debuggable, we need to deal with these registers differently. For
1140 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001141 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1142 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001143 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001144 }
1145 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001146}
1147
Alexandre Rames3e69f162014-12-10 10:36:50 +00001148size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1149 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1150 __ Str(reg, MemOperand(sp, stack_index));
1151 return kArm64WordSize;
1152}
1153
1154size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1155 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1156 __ Ldr(reg, MemOperand(sp, stack_index));
1157 return kArm64WordSize;
1158}
1159
1160size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1161 FPRegister reg = FPRegister(reg_id, kDRegSize);
1162 __ Str(reg, MemOperand(sp, stack_index));
1163 return kArm64WordSize;
1164}
1165
1166size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1167 FPRegister reg = FPRegister(reg_id, kDRegSize);
1168 __ Ldr(reg, MemOperand(sp, stack_index));
1169 return kArm64WordSize;
1170}
1171
Alexandre Rames5319def2014-10-23 10:03:10 +01001172void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001173 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001174}
1175
1176void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001177 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001178}
1179
Alexandre Rames67555f72014-11-18 10:55:16 +00001180void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001181 if (constant->IsIntConstant()) {
1182 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1183 } else if (constant->IsLongConstant()) {
1184 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1185 } else if (constant->IsNullConstant()) {
1186 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001187 } else if (constant->IsFloatConstant()) {
1188 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1189 } else {
1190 DCHECK(constant->IsDoubleConstant());
1191 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1192 }
1193}
1194
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195
1196static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1197 DCHECK(constant.IsConstant());
1198 HConstant* cst = constant.GetConstant();
1199 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 // Null is mapped to a core W register, which we associate with kPrimInt.
1201 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001202 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1203 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1204 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1205}
1206
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207void CodeGeneratorARM64::MoveLocation(Location destination,
1208 Location source,
1209 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001210 if (source.Equals(destination)) {
1211 return;
1212 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213
1214 // A valid move can always be inferred from the destination and source
1215 // locations. When moving from and to a register, the argument type can be
1216 // used to generate 32bit instead of 64bit moves. In debug mode we also
1217 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001219
1220 if (destination.IsRegister() || destination.IsFpuRegister()) {
1221 if (unspecified_type) {
1222 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1223 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001224 (src_cst != nullptr && (src_cst->IsIntConstant()
1225 || src_cst->IsFloatConstant()
1226 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001227 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001229 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230 // If the source is a double stack slot or a 64bit constant, a 64bit
1231 // type is appropriate. Else the source is a register, and since the
1232 // type has not been specified, we chose a 64bit type to force a 64bit
1233 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001235 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001236 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1238 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1239 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1241 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1242 __ Ldr(dst, StackOperandFrom(source));
1243 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001246 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001247 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001248 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001250 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1252 ? Primitive::kPrimLong
1253 : Primitive::kPrimInt;
1254 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1255 }
1256 } else {
1257 DCHECK(source.IsFpuRegister());
1258 if (destination.IsRegister()) {
1259 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1260 ? Primitive::kPrimDouble
1261 : Primitive::kPrimFloat;
1262 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1263 } else {
1264 DCHECK(destination.IsFpuRegister());
1265 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001266 }
1267 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001268 } else { // The destination is not a register. It must be a stack slot.
1269 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1270 if (source.IsRegister() || source.IsFpuRegister()) {
1271 if (unspecified_type) {
1272 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001276 }
1277 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1279 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1280 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1283 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001284 UseScratchRegisterScope temps(GetVIXLAssembler());
1285 HConstant* src_cst = source.GetConstant();
1286 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001287 if (src_cst->IsZeroBitPattern()) {
1288 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001289 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001290 if (src_cst->IsIntConstant()) {
1291 temp = temps.AcquireW();
1292 } else if (src_cst->IsLongConstant()) {
1293 temp = temps.AcquireX();
1294 } else if (src_cst->IsFloatConstant()) {
1295 temp = temps.AcquireS();
1296 } else {
1297 DCHECK(src_cst->IsDoubleConstant());
1298 temp = temps.AcquireD();
1299 }
1300 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001301 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001303 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001304 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001305 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001306 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001307 // There is generally less pressure on FP registers.
1308 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001309 __ Ldr(temp, StackOperandFrom(source));
1310 __ Str(temp, StackOperandFrom(destination));
1311 }
1312 }
1313}
1314
1315void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001316 CPURegister dst,
1317 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001318 switch (type) {
1319 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001320 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001321 break;
1322 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001324 break;
1325 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 break;
1328 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001329 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001330 break;
1331 case Primitive::kPrimInt:
1332 case Primitive::kPrimNot:
1333 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001334 case Primitive::kPrimFloat:
1335 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001336 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001337 __ Ldr(dst, src);
1338 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 case Primitive::kPrimVoid:
1340 LOG(FATAL) << "Unreachable type " << type;
1341 }
1342}
1343
Calin Juravle77520bc2015-01-12 18:45:46 +00001344void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001346 const MemOperand& src,
1347 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001348 MacroAssembler* masm = GetVIXLAssembler();
1349 BlockPoolsScope block_pools(masm);
1350 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001351 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001352 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001353
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001354 DCHECK(!src.IsPreIndex());
1355 DCHECK(!src.IsPostIndex());
1356
1357 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001358 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001359 MemOperand base = MemOperand(temp_base);
1360 switch (type) {
1361 case Primitive::kPrimBoolean:
1362 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001363 if (needs_null_check) {
1364 MaybeRecordImplicitNullCheck(instruction);
1365 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001366 break;
1367 case Primitive::kPrimByte:
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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1373 break;
1374 case Primitive::kPrimChar:
1375 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001376 if (needs_null_check) {
1377 MaybeRecordImplicitNullCheck(instruction);
1378 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001379 break;
1380 case Primitive::kPrimShort:
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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1386 break;
1387 case Primitive::kPrimInt:
1388 case Primitive::kPrimNot:
1389 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001390 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001392 if (needs_null_check) {
1393 MaybeRecordImplicitNullCheck(instruction);
1394 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001395 break;
1396 case Primitive::kPrimFloat:
1397 case Primitive::kPrimDouble: {
1398 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001399 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001400
1401 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1402 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001403 if (needs_null_check) {
1404 MaybeRecordImplicitNullCheck(instruction);
1405 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 __ Fmov(FPRegister(dst), temp);
1407 break;
1408 }
1409 case Primitive::kPrimVoid:
1410 LOG(FATAL) << "Unreachable type " << type;
1411 }
1412}
1413
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001415 CPURegister src,
1416 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001417 switch (type) {
1418 case Primitive::kPrimBoolean:
1419 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001420 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421 break;
1422 case Primitive::kPrimChar:
1423 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001424 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001425 break;
1426 case Primitive::kPrimInt:
1427 case Primitive::kPrimNot:
1428 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001429 case Primitive::kPrimFloat:
1430 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001431 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001432 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001433 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001434 case Primitive::kPrimVoid:
1435 LOG(FATAL) << "Unreachable type " << type;
1436 }
1437}
1438
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001439void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1440 CPURegister src,
1441 const MemOperand& dst) {
1442 UseScratchRegisterScope temps(GetVIXLAssembler());
1443 Register temp_base = temps.AcquireX();
1444
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001445 DCHECK(!dst.IsPreIndex());
1446 DCHECK(!dst.IsPostIndex());
1447
1448 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001449 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001450 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001451 MemOperand base = MemOperand(temp_base);
1452 switch (type) {
1453 case Primitive::kPrimBoolean:
1454 case Primitive::kPrimByte:
1455 __ Stlrb(Register(src), base);
1456 break;
1457 case Primitive::kPrimChar:
1458 case Primitive::kPrimShort:
1459 __ Stlrh(Register(src), base);
1460 break;
1461 case Primitive::kPrimInt:
1462 case Primitive::kPrimNot:
1463 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001464 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001465 __ Stlr(Register(src), base);
1466 break;
1467 case Primitive::kPrimFloat:
1468 case Primitive::kPrimDouble: {
1469 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001470 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001471
1472 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1473 __ Fmov(temp, FPRegister(src));
1474 __ Stlr(temp, base);
1475 break;
1476 }
1477 case Primitive::kPrimVoid:
1478 LOG(FATAL) << "Unreachable type " << type;
1479 }
1480}
1481
Calin Juravle175dc732015-08-25 15:42:32 +01001482void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1483 HInstruction* instruction,
1484 uint32_t dex_pc,
1485 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001486 InvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001487 instruction,
1488 dex_pc,
1489 slow_path);
1490}
1491
Alexandre Rames67555f72014-11-18 10:55:16 +00001492void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1493 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001494 uint32_t dex_pc,
1495 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001496 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001497 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001498 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1499 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001500 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001501}
1502
Roland Levillaindec8f632016-07-22 17:10:06 +01001503void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1504 HInstruction* instruction,
1505 SlowPathCode* slow_path) {
1506 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1507 BlockPoolsScope block_pools(GetVIXLAssembler());
1508 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1509 __ Blr(lr);
1510}
1511
Alexandre Rames67555f72014-11-18 10:55:16 +00001512void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001513 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001514 UseScratchRegisterScope temps(GetVIXLAssembler());
1515 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001516 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1517
Serban Constantinescu02164b32014-11-13 14:05:07 +00001518 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001519 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1520 __ Add(temp, class_reg, status_offset);
1521 __ Ldar(temp, HeapOperand(temp));
1522 __ Cmp(temp, mirror::Class::kStatusInitialized);
1523 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001524 __ Bind(slow_path->GetExitLabel());
1525}
Alexandre Rames5319def2014-10-23 10:03:10 +01001526
Roland Levillain44015862016-01-22 11:47:17 +00001527void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001528 BarrierType type = BarrierAll;
1529
1530 switch (kind) {
1531 case MemBarrierKind::kAnyAny:
1532 case MemBarrierKind::kAnyStore: {
1533 type = BarrierAll;
1534 break;
1535 }
1536 case MemBarrierKind::kLoadAny: {
1537 type = BarrierReads;
1538 break;
1539 }
1540 case MemBarrierKind::kStoreStore: {
1541 type = BarrierWrites;
1542 break;
1543 }
1544 default:
1545 LOG(FATAL) << "Unexpected memory barrier " << kind;
1546 }
1547 __ Dmb(InnerShareable, type);
1548}
1549
Serban Constantinescu02164b32014-11-13 14:05:07 +00001550void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1551 HBasicBlock* successor) {
1552 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001553 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1554 if (slow_path == nullptr) {
1555 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1556 instruction->SetSlowPath(slow_path);
1557 codegen_->AddSlowPath(slow_path);
1558 if (successor != nullptr) {
1559 DCHECK(successor->IsLoopHeader());
1560 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1561 }
1562 } else {
1563 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1564 }
1565
Serban Constantinescu02164b32014-11-13 14:05:07 +00001566 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1567 Register temp = temps.AcquireW();
1568
Andreas Gampe542451c2016-07-26 09:02:02 -07001569 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001570 if (successor == nullptr) {
1571 __ Cbnz(temp, slow_path->GetEntryLabel());
1572 __ Bind(slow_path->GetReturnLabel());
1573 } else {
1574 __ Cbz(temp, codegen_->GetLabelOf(successor));
1575 __ B(slow_path->GetEntryLabel());
1576 // slow_path will return to GetLabelOf(successor).
1577 }
1578}
1579
Alexandre Rames5319def2014-10-23 10:03:10 +01001580InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1581 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001582 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001583 assembler_(codegen->GetAssembler()),
1584 codegen_(codegen) {}
1585
1586#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001587 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001588
1589#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1590
1591enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 // Using a base helps identify when we hit such breakpoints.
1593 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001594#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1595 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1596#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1597};
1598
1599#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001600 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1602 } \
1603 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1605 locations->SetOut(Location::Any()); \
1606 }
1607 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1608#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1609
1610#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001611#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001612
Alexandre Rames67555f72014-11-18 10:55:16 +00001613void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001614 DCHECK_EQ(instr->InputCount(), 2U);
1615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1616 Primitive::Type type = instr->GetResultType();
1617 switch (type) {
1618 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001619 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001620 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001621 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001623 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001624
1625 case Primitive::kPrimFloat:
1626 case Primitive::kPrimDouble:
1627 locations->SetInAt(0, Location::RequiresFpuRegister());
1628 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001630 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001631
Alexandre Rames5319def2014-10-23 10:03:10 +01001632 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001633 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001634 }
1635}
1636
Alexandre Rames09a99962015-04-15 11:47:56 +01001637void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001638 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1639
1640 bool object_field_get_with_read_barrier =
1641 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001642 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001643 new (GetGraph()->GetArena()) LocationSummary(instruction,
1644 object_field_get_with_read_barrier ?
1645 LocationSummary::kCallOnSlowPath :
1646 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001647 locations->SetInAt(0, Location::RequiresRegister());
1648 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1649 locations->SetOut(Location::RequiresFpuRegister());
1650 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001651 // The output overlaps for an object field get when read barriers
1652 // are enabled: we do not want the load to overwrite the object's
1653 // location, as we need it to emit the read barrier.
1654 locations->SetOut(
1655 Location::RequiresRegister(),
1656 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001657 }
1658}
1659
1660void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1661 const FieldInfo& field_info) {
1662 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001663 LocationSummary* locations = instruction->GetLocations();
1664 Location base_loc = locations->InAt(0);
1665 Location out = locations->Out();
1666 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001667 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001668 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001669 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001670
Roland Levillain44015862016-01-22 11:47:17 +00001671 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1672 // Object FieldGet with Baker's read barrier case.
1673 MacroAssembler* masm = GetVIXLAssembler();
1674 UseScratchRegisterScope temps(masm);
1675 // /* HeapReference<Object> */ out = *(base + offset)
1676 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1677 Register temp = temps.AcquireW();
1678 // Note that potential implicit null checks are handled in this
1679 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1680 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1681 instruction,
1682 out,
1683 base,
1684 offset,
1685 temp,
1686 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001687 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001688 } else {
1689 // General case.
1690 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001691 // Note that a potential implicit null check is handled in this
1692 // CodeGeneratorARM64::LoadAcquire call.
1693 // NB: LoadAcquire will record the pc info if needed.
1694 codegen_->LoadAcquire(
1695 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001696 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001697 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001698 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001699 }
Roland Levillain44015862016-01-22 11:47:17 +00001700 if (field_type == Primitive::kPrimNot) {
1701 // If read barriers are enabled, emit read barriers other than
1702 // Baker's using a slow path (and also unpoison the loaded
1703 // reference, if heap poisoning is enabled).
1704 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1705 }
Roland Levillain4d027112015-07-01 15:41:14 +01001706 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001707}
1708
1709void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1710 LocationSummary* locations =
1711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1712 locations->SetInAt(0, Location::RequiresRegister());
1713 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1714 locations->SetInAt(1, Location::RequiresFpuRegister());
1715 } else {
1716 locations->SetInAt(1, Location::RequiresRegister());
1717 }
1718}
1719
1720void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001721 const FieldInfo& field_info,
1722 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001723 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001724 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001725
1726 Register obj = InputRegisterAt(instruction, 0);
1727 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001728 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001729 Offset offset = field_info.GetFieldOffset();
1730 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001731
Roland Levillain4d027112015-07-01 15:41:14 +01001732 {
1733 // We use a block to end the scratch scope before the write barrier, thus
1734 // freeing the temporary registers so they can be used in `MarkGCCard`.
1735 UseScratchRegisterScope temps(GetVIXLAssembler());
1736
1737 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1738 DCHECK(value.IsW());
1739 Register temp = temps.AcquireW();
1740 __ Mov(temp, value.W());
1741 GetAssembler()->PoisonHeapReference(temp.W());
1742 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001743 }
Roland Levillain4d027112015-07-01 15:41:14 +01001744
1745 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001746 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001748 } else {
1749 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1750 codegen_->MaybeRecordImplicitNullCheck(instruction);
1751 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001752 }
1753
1754 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001755 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001756 }
1757}
1758
Alexandre Rames67555f72014-11-18 10:55:16 +00001759void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001760 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001761
1762 switch (type) {
1763 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001764 case Primitive::kPrimLong: {
1765 Register dst = OutputRegister(instr);
1766 Register lhs = InputRegisterAt(instr, 0);
1767 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001768 if (instr->IsAdd()) {
1769 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001770 } else if (instr->IsAnd()) {
1771 __ And(dst, lhs, rhs);
1772 } else if (instr->IsOr()) {
1773 __ Orr(dst, lhs, rhs);
1774 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001775 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001776 } else if (instr->IsRor()) {
1777 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001778 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001779 __ Ror(dst, lhs, shift);
1780 } else {
1781 // Ensure shift distance is in the same size register as the result. If
1782 // we are rotating a long and the shift comes in a w register originally,
1783 // we don't need to sxtw for use as an x since the shift distances are
1784 // all & reg_bits - 1.
1785 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1786 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 } else {
1788 DCHECK(instr->IsXor());
1789 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001790 }
1791 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001792 }
1793 case Primitive::kPrimFloat:
1794 case Primitive::kPrimDouble: {
1795 FPRegister dst = OutputFPRegister(instr);
1796 FPRegister lhs = InputFPRegisterAt(instr, 0);
1797 FPRegister rhs = InputFPRegisterAt(instr, 1);
1798 if (instr->IsAdd()) {
1799 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001800 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001801 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001802 } else {
1803 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001804 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001806 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001807 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001808 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001809 }
1810}
1811
Serban Constantinescu02164b32014-11-13 14:05:07 +00001812void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1813 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1814
1815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1816 Primitive::Type type = instr->GetResultType();
1817 switch (type) {
1818 case Primitive::kPrimInt:
1819 case Primitive::kPrimLong: {
1820 locations->SetInAt(0, Location::RequiresRegister());
1821 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1822 locations->SetOut(Location::RequiresRegister());
1823 break;
1824 }
1825 default:
1826 LOG(FATAL) << "Unexpected shift type " << type;
1827 }
1828}
1829
1830void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1831 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1832
1833 Primitive::Type type = instr->GetType();
1834 switch (type) {
1835 case Primitive::kPrimInt:
1836 case Primitive::kPrimLong: {
1837 Register dst = OutputRegister(instr);
1838 Register lhs = InputRegisterAt(instr, 0);
1839 Operand rhs = InputOperandAt(instr, 1);
1840 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001841 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001842 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001843 if (instr->IsShl()) {
1844 __ Lsl(dst, lhs, shift_value);
1845 } else if (instr->IsShr()) {
1846 __ Asr(dst, lhs, shift_value);
1847 } else {
1848 __ Lsr(dst, lhs, shift_value);
1849 }
1850 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001851 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001852
1853 if (instr->IsShl()) {
1854 __ Lsl(dst, lhs, rhs_reg);
1855 } else if (instr->IsShr()) {
1856 __ Asr(dst, lhs, rhs_reg);
1857 } else {
1858 __ Lsr(dst, lhs, rhs_reg);
1859 }
1860 }
1861 break;
1862 }
1863 default:
1864 LOG(FATAL) << "Unexpected shift operation type " << type;
1865 }
1866}
1867
Alexandre Rames5319def2014-10-23 10:03:10 +01001868void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001869 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001870}
1871
1872void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001873 HandleBinaryOp(instruction);
1874}
1875
1876void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1877 HandleBinaryOp(instruction);
1878}
1879
1880void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1881 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001882}
1883
Artem Serov7fc63502016-02-09 17:15:29 +00001884void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001885 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1887 locations->SetInAt(0, Location::RequiresRegister());
1888 // There is no immediate variant of negated bitwise instructions in AArch64.
1889 locations->SetInAt(1, Location::RequiresRegister());
1890 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1891}
1892
Artem Serov7fc63502016-02-09 17:15:29 +00001893void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001894 Register dst = OutputRegister(instr);
1895 Register lhs = InputRegisterAt(instr, 0);
1896 Register rhs = InputRegisterAt(instr, 1);
1897
1898 switch (instr->GetOpKind()) {
1899 case HInstruction::kAnd:
1900 __ Bic(dst, lhs, rhs);
1901 break;
1902 case HInstruction::kOr:
1903 __ Orn(dst, lhs, rhs);
1904 break;
1905 case HInstruction::kXor:
1906 __ Eon(dst, lhs, rhs);
1907 break;
1908 default:
1909 LOG(FATAL) << "Unreachable";
1910 }
1911}
1912
Alexandre Rames8626b742015-11-25 16:28:08 +00001913void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1914 HArm64DataProcWithShifterOp* instruction) {
1915 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1916 instruction->GetType() == Primitive::kPrimLong);
1917 LocationSummary* locations =
1918 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1919 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1920 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1921 } else {
1922 locations->SetInAt(0, Location::RequiresRegister());
1923 }
1924 locations->SetInAt(1, Location::RequiresRegister());
1925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1926}
1927
1928void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1929 HArm64DataProcWithShifterOp* instruction) {
1930 Primitive::Type type = instruction->GetType();
1931 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1932 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1933 Register out = OutputRegister(instruction);
1934 Register left;
1935 if (kind != HInstruction::kNeg) {
1936 left = InputRegisterAt(instruction, 0);
1937 }
1938 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1939 // shifter operand operation, the IR generating `right_reg` (input to the type
1940 // conversion) can have a different type from the current instruction's type,
1941 // so we manually indicate the type.
1942 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001943 int64_t shift_amount = instruction->GetShiftAmount() &
1944 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001945
1946 Operand right_operand(0);
1947
1948 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1949 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1950 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1951 } else {
1952 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1953 }
1954
1955 // Logical binary operations do not support extension operations in the
1956 // operand. Note that VIXL would still manage if it was passed by generating
1957 // the extension as a separate instruction.
1958 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1959 DCHECK(!right_operand.IsExtendedRegister() ||
1960 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1961 kind != HInstruction::kNeg));
1962 switch (kind) {
1963 case HInstruction::kAdd:
1964 __ Add(out, left, right_operand);
1965 break;
1966 case HInstruction::kAnd:
1967 __ And(out, left, right_operand);
1968 break;
1969 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001970 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001971 __ Neg(out, right_operand);
1972 break;
1973 case HInstruction::kOr:
1974 __ Orr(out, left, right_operand);
1975 break;
1976 case HInstruction::kSub:
1977 __ Sub(out, left, right_operand);
1978 break;
1979 case HInstruction::kXor:
1980 __ Eor(out, left, right_operand);
1981 break;
1982 default:
1983 LOG(FATAL) << "Unexpected operation kind: " << kind;
1984 UNREACHABLE();
1985 }
1986}
1987
Artem Serov328429f2016-07-06 16:23:04 +01001988void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
1989 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001990 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1993 locations->SetInAt(0, Location::RequiresRegister());
1994 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1995 locations->SetOut(Location::RequiresRegister());
1996}
1997
Artem Serov328429f2016-07-06 16:23:04 +01001998void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1999 HIntermediateAddress* instruction) {
2000 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002001 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002002 __ Add(OutputRegister(instruction),
2003 InputRegisterAt(instruction, 0),
2004 Operand(InputOperandAt(instruction, 1)));
2005}
2006
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002007void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002008 LocationSummary* locations =
2009 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002010 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2011 if (instr->GetOpKind() == HInstruction::kSub &&
2012 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002013 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002014 // Don't allocate register for Mneg instruction.
2015 } else {
2016 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2017 Location::RequiresRegister());
2018 }
2019 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2020 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2022}
2023
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002024void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002025 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002026 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2027 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002028
2029 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2030 // This fixup should be carried out for all multiply-accumulate instructions:
2031 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2032 if (instr->GetType() == Primitive::kPrimLong &&
2033 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2034 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002035 vixl::aarch64::Instruction* prev =
2036 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002037 if (prev->IsLoadOrStore()) {
2038 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002039 vixl::aarch64::CodeBufferCheckScope scope(masm,
2040 kInstructionSize,
2041 vixl::aarch64::CodeBufferCheckScope::kCheck,
2042 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002043 __ nop();
2044 }
2045 }
2046
2047 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002048 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002049 __ Madd(res, mul_left, mul_right, accumulator);
2050 } else {
2051 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002052 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002053 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002054 __ Mneg(res, mul_left, mul_right);
2055 } else {
2056 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2057 __ Msub(res, mul_left, mul_right, accumulator);
2058 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002059 }
2060}
2061
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002062void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002063 bool object_array_get_with_read_barrier =
2064 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002065 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002066 new (GetGraph()->GetArena()) LocationSummary(instruction,
2067 object_array_get_with_read_barrier ?
2068 LocationSummary::kCallOnSlowPath :
2069 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002070 locations->SetInAt(0, Location::RequiresRegister());
2071 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002072 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2073 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2074 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002075 // The output overlaps in the case of an object array get with
2076 // read barriers enabled: we do not want the move to overwrite the
2077 // array's location, as we need it to emit the read barrier.
2078 locations->SetOut(
2079 Location::RequiresRegister(),
2080 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002081 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002082}
2083
2084void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002085 Primitive::Type type = instruction->GetType();
2086 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002087 LocationSummary* locations = instruction->GetLocations();
2088 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002089 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002090 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002091
Alexandre Ramesd921d642015-04-16 15:07:16 +01002092 MacroAssembler* masm = GetVIXLAssembler();
2093 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002094 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002095 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002096
Roland Levillain44015862016-01-22 11:47:17 +00002097 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2098 // Object ArrayGet with Baker's read barrier case.
2099 Register temp = temps.AcquireW();
Artem Serov328429f2016-07-06 16:23:04 +01002100 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2101 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002102 // Note that a potential implicit null check is handled in the
2103 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2104 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2105 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002106 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002107 // General case.
2108 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002109 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002110 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2111 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002112 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002113 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002114 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002115 // The read barrier instrumentation does not support the
Artem Serov328429f2016-07-06 16:23:04 +01002116 // HIntermediateAddress instruction yet.
Roland Levillain44015862016-01-22 11:47:17 +00002117 DCHECK(!kEmitCompilerReadBarrier);
2118 // We do not need to compute the intermediate address from the array: the
2119 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002120 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002121 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002122 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002123 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2124 }
2125 temp = obj;
2126 } else {
2127 __ Add(temp, obj, offset);
2128 }
2129 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2130 }
2131
2132 codegen_->Load(type, OutputCPURegister(instruction), source);
2133 codegen_->MaybeRecordImplicitNullCheck(instruction);
2134
2135 if (type == Primitive::kPrimNot) {
2136 static_assert(
2137 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2138 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2139 Location obj_loc = locations->InAt(0);
2140 if (index.IsConstant()) {
2141 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2142 } else {
2143 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2144 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002145 }
Roland Levillain4d027112015-07-01 15:41:14 +01002146 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002147}
2148
Alexandre Rames5319def2014-10-23 10:03:10 +01002149void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2151 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002153}
2154
2155void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002156 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002157 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002158 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002159 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002160}
2161
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002162void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002163 Primitive::Type value_type = instruction->GetComponentType();
2164
2165 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2166 bool object_array_set_with_read_barrier =
2167 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2169 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002170 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2171 LocationSummary::kCallOnSlowPath :
2172 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002173 locations->SetInAt(0, Location::RequiresRegister());
2174 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002175 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002176 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002177 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002178 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002179 }
2180}
2181
2182void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2183 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002184 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002185 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002186 bool needs_write_barrier =
2187 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002188
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002189 Register array = InputRegisterAt(instruction, 0);
2190 CPURegister value = InputCPURegisterAt(instruction, 2);
2191 CPURegister source = value;
2192 Location index = locations->InAt(1);
2193 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2194 MemOperand destination = HeapOperand(array);
2195 MacroAssembler* masm = GetVIXLAssembler();
2196 BlockPoolsScope block_pools(masm);
2197
2198 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002199 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002200 if (index.IsConstant()) {
2201 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2202 destination = HeapOperand(array, offset);
2203 } else {
2204 UseScratchRegisterScope temps(masm);
2205 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002206 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002207 // The read barrier instrumentation does not support the
Artem Serov328429f2016-07-06 16:23:04 +01002208 // HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002209 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002210 // We do not need to compute the intermediate address from the array: the
2211 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002212 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002213 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002214 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002215 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2216 }
2217 temp = array;
2218 } else {
2219 __ Add(temp, array, offset);
2220 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002221 destination = HeapOperand(temp,
2222 XRegisterFrom(index),
2223 LSL,
2224 Primitive::ComponentSizeShift(value_type));
2225 }
2226 codegen_->Store(value_type, value, destination);
2227 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002228 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002229 DCHECK(needs_write_barrier);
Artem Serov328429f2016-07-06 16:23:04 +01002230 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002231 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002232 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002233 {
2234 // We use a block to end the scratch scope before the write barrier, thus
2235 // freeing the temporary registers so they can be used in `MarkGCCard`.
2236 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002237 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002238 if (index.IsConstant()) {
2239 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002240 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002241 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002242 destination = HeapOperand(temp,
2243 XRegisterFrom(index),
2244 LSL,
2245 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002246 }
2247
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002248 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2249 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2250 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2251
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002252 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002253 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2254 codegen_->AddSlowPath(slow_path);
2255 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002256 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002257 __ Cbnz(Register(value), &non_zero);
2258 if (!index.IsConstant()) {
2259 __ Add(temp, array, offset);
2260 }
2261 __ Str(wzr, destination);
2262 codegen_->MaybeRecordImplicitNullCheck(instruction);
2263 __ B(&done);
2264 __ Bind(&non_zero);
2265 }
2266
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002267 if (kEmitCompilerReadBarrier) {
2268 // When read barriers are enabled, the type checking
2269 // instrumentation requires two read barriers:
2270 //
2271 // __ Mov(temp2, temp);
2272 // // /* HeapReference<Class> */ temp = temp->component_type_
2273 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002274 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002275 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2276 //
2277 // // /* HeapReference<Class> */ temp2 = value->klass_
2278 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002279 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002280 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2281 //
2282 // __ Cmp(temp, temp2);
2283 //
2284 // However, the second read barrier may trash `temp`, as it
2285 // is a temporary register, and as such would not be saved
2286 // along with live registers before calling the runtime (nor
2287 // restored afterwards). So in this case, we bail out and
2288 // delegate the work to the array set slow path.
2289 //
2290 // TODO: Extend the register allocator to support a new
2291 // "(locally) live temp" location so as to avoid always
2292 // going into the slow path when read barriers are enabled.
2293 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002294 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002295 Register temp2 = temps.AcquireSameSizeAs(array);
2296 // /* HeapReference<Class> */ temp = array->klass_
2297 __ Ldr(temp, HeapOperand(array, class_offset));
2298 codegen_->MaybeRecordImplicitNullCheck(instruction);
2299 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2300
2301 // /* HeapReference<Class> */ temp = temp->component_type_
2302 __ Ldr(temp, HeapOperand(temp, component_offset));
2303 // /* HeapReference<Class> */ temp2 = value->klass_
2304 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2305 // If heap poisoning is enabled, no need to unpoison `temp`
2306 // nor `temp2`, as we are comparing two poisoned references.
2307 __ Cmp(temp, temp2);
2308
2309 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002310 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002311 __ B(eq, &do_put);
2312 // If heap poisoning is enabled, the `temp` reference has
2313 // not been unpoisoned yet; unpoison it now.
2314 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2315
2316 // /* HeapReference<Class> */ temp = temp->super_class_
2317 __ Ldr(temp, HeapOperand(temp, super_offset));
2318 // If heap poisoning is enabled, no need to unpoison
2319 // `temp`, as we are comparing against null below.
2320 __ Cbnz(temp, slow_path->GetEntryLabel());
2321 __ Bind(&do_put);
2322 } else {
2323 __ B(ne, slow_path->GetEntryLabel());
2324 }
2325 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002326 }
2327 }
2328
2329 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002330 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002331 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002332 __ Mov(temp2, value.W());
2333 GetAssembler()->PoisonHeapReference(temp2);
2334 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002335 }
2336
2337 if (!index.IsConstant()) {
2338 __ Add(temp, array, offset);
2339 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002340 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002341
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002342 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002343 codegen_->MaybeRecordImplicitNullCheck(instruction);
2344 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002345 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002346
2347 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2348
2349 if (done.IsLinked()) {
2350 __ Bind(&done);
2351 }
2352
2353 if (slow_path != nullptr) {
2354 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002355 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002356 }
2357}
2358
Alexandre Rames67555f72014-11-18 10:55:16 +00002359void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002360 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2361 ? LocationSummary::kCallOnSlowPath
2362 : LocationSummary::kNoCall;
2363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002364 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002365 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002366 if (instruction->HasUses()) {
2367 locations->SetOut(Location::SameAsFirstInput());
2368 }
2369}
2370
2371void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002372 BoundsCheckSlowPathARM64* slow_path =
2373 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002374 codegen_->AddSlowPath(slow_path);
2375
2376 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2377 __ B(slow_path->GetEntryLabel(), hs);
2378}
2379
Alexandre Rames67555f72014-11-18 10:55:16 +00002380void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2381 LocationSummary* locations =
2382 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2383 locations->SetInAt(0, Location::RequiresRegister());
2384 if (check->HasUses()) {
2385 locations->SetOut(Location::SameAsFirstInput());
2386 }
2387}
2388
2389void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2390 // We assume the class is not null.
2391 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2392 check->GetLoadClass(), check, check->GetDexPc(), true);
2393 codegen_->AddSlowPath(slow_path);
2394 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2395}
2396
Roland Levillain1a653882016-03-18 18:05:57 +00002397static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2398 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2399 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2400}
2401
2402void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2403 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2404 Location rhs_loc = instruction->GetLocations()->InAt(1);
2405 if (rhs_loc.IsConstant()) {
2406 // 0.0 is the only immediate that can be encoded directly in
2407 // an FCMP instruction.
2408 //
2409 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2410 // specify that in a floating-point comparison, positive zero
2411 // and negative zero are considered equal, so we can use the
2412 // literal 0.0 for both cases here.
2413 //
2414 // Note however that some methods (Float.equal, Float.compare,
2415 // Float.compareTo, Double.equal, Double.compare,
2416 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2417 // StrictMath.min) consider 0.0 to be (strictly) greater than
2418 // -0.0. So if we ever translate calls to these methods into a
2419 // HCompare instruction, we must handle the -0.0 case with
2420 // care here.
2421 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2422 __ Fcmp(lhs_reg, 0.0);
2423 } else {
2424 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2425 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002426}
2427
Serban Constantinescu02164b32014-11-13 14:05:07 +00002428void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002429 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002430 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2431 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002432 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002433 case Primitive::kPrimBoolean:
2434 case Primitive::kPrimByte:
2435 case Primitive::kPrimShort:
2436 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002437 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002438 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002439 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002440 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2442 break;
2443 }
2444 case Primitive::kPrimFloat:
2445 case Primitive::kPrimDouble: {
2446 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002447 locations->SetInAt(1,
2448 IsFloatingPointZeroConstant(compare->InputAt(1))
2449 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2450 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002451 locations->SetOut(Location::RequiresRegister());
2452 break;
2453 }
2454 default:
2455 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2456 }
2457}
2458
2459void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2460 Primitive::Type in_type = compare->InputAt(0)->GetType();
2461
2462 // 0 if: left == right
2463 // 1 if: left > right
2464 // -1 if: left < right
2465 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002466 case Primitive::kPrimBoolean:
2467 case Primitive::kPrimByte:
2468 case Primitive::kPrimShort:
2469 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002470 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002471 case Primitive::kPrimLong: {
2472 Register result = OutputRegister(compare);
2473 Register left = InputRegisterAt(compare, 0);
2474 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002475 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002476 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2477 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002478 break;
2479 }
2480 case Primitive::kPrimFloat:
2481 case Primitive::kPrimDouble: {
2482 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002483 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002484 __ Cset(result, ne);
2485 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002486 break;
2487 }
2488 default:
2489 LOG(FATAL) << "Unimplemented compare type " << in_type;
2490 }
2491}
2492
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002493void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002494 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002495
2496 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2497 locations->SetInAt(0, Location::RequiresFpuRegister());
2498 locations->SetInAt(1,
2499 IsFloatingPointZeroConstant(instruction->InputAt(1))
2500 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2501 : Location::RequiresFpuRegister());
2502 } else {
2503 // Integer cases.
2504 locations->SetInAt(0, Location::RequiresRegister());
2505 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2506 }
2507
David Brazdilb3e773e2016-01-26 11:28:37 +00002508 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002510 }
2511}
2512
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002513void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002514 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002515 return;
2516 }
2517
2518 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002519 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002520 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002521
Roland Levillain7f63c522015-07-13 15:54:55 +00002522 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002523 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002524 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002525 } else {
2526 // Integer cases.
2527 Register lhs = InputRegisterAt(instruction, 0);
2528 Operand rhs = InputOperandAt(instruction, 1);
2529 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002530 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002531 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002532}
2533
2534#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2535 M(Equal) \
2536 M(NotEqual) \
2537 M(LessThan) \
2538 M(LessThanOrEqual) \
2539 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002540 M(GreaterThanOrEqual) \
2541 M(Below) \
2542 M(BelowOrEqual) \
2543 M(Above) \
2544 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002545#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002546void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2547void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002548FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002549#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002550#undef FOR_EACH_CONDITION_INSTRUCTION
2551
Zheng Xuc6667102015-05-15 16:08:45 +08002552void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2553 DCHECK(instruction->IsDiv() || instruction->IsRem());
2554
2555 LocationSummary* locations = instruction->GetLocations();
2556 Location second = locations->InAt(1);
2557 DCHECK(second.IsConstant());
2558
2559 Register out = OutputRegister(instruction);
2560 Register dividend = InputRegisterAt(instruction, 0);
2561 int64_t imm = Int64FromConstant(second.GetConstant());
2562 DCHECK(imm == 1 || imm == -1);
2563
2564 if (instruction->IsRem()) {
2565 __ Mov(out, 0);
2566 } else {
2567 if (imm == 1) {
2568 __ Mov(out, dividend);
2569 } else {
2570 __ Neg(out, dividend);
2571 }
2572 }
2573}
2574
2575void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2576 DCHECK(instruction->IsDiv() || instruction->IsRem());
2577
2578 LocationSummary* locations = instruction->GetLocations();
2579 Location second = locations->InAt(1);
2580 DCHECK(second.IsConstant());
2581
2582 Register out = OutputRegister(instruction);
2583 Register dividend = InputRegisterAt(instruction, 0);
2584 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002585 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002586 int ctz_imm = CTZ(abs_imm);
2587
2588 UseScratchRegisterScope temps(GetVIXLAssembler());
2589 Register temp = temps.AcquireSameSizeAs(out);
2590
2591 if (instruction->IsDiv()) {
2592 __ Add(temp, dividend, abs_imm - 1);
2593 __ Cmp(dividend, 0);
2594 __ Csel(out, temp, dividend, lt);
2595 if (imm > 0) {
2596 __ Asr(out, out, ctz_imm);
2597 } else {
2598 __ Neg(out, Operand(out, ASR, ctz_imm));
2599 }
2600 } else {
2601 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2602 __ Asr(temp, dividend, bits - 1);
2603 __ Lsr(temp, temp, bits - ctz_imm);
2604 __ Add(out, dividend, temp);
2605 __ And(out, out, abs_imm - 1);
2606 __ Sub(out, out, temp);
2607 }
2608}
2609
2610void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2611 DCHECK(instruction->IsDiv() || instruction->IsRem());
2612
2613 LocationSummary* locations = instruction->GetLocations();
2614 Location second = locations->InAt(1);
2615 DCHECK(second.IsConstant());
2616
2617 Register out = OutputRegister(instruction);
2618 Register dividend = InputRegisterAt(instruction, 0);
2619 int64_t imm = Int64FromConstant(second.GetConstant());
2620
2621 Primitive::Type type = instruction->GetResultType();
2622 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2623
2624 int64_t magic;
2625 int shift;
2626 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2627
2628 UseScratchRegisterScope temps(GetVIXLAssembler());
2629 Register temp = temps.AcquireSameSizeAs(out);
2630
2631 // temp = get_high(dividend * magic)
2632 __ Mov(temp, magic);
2633 if (type == Primitive::kPrimLong) {
2634 __ Smulh(temp, dividend, temp);
2635 } else {
2636 __ Smull(temp.X(), dividend, temp);
2637 __ Lsr(temp.X(), temp.X(), 32);
2638 }
2639
2640 if (imm > 0 && magic < 0) {
2641 __ Add(temp, temp, dividend);
2642 } else if (imm < 0 && magic > 0) {
2643 __ Sub(temp, temp, dividend);
2644 }
2645
2646 if (shift != 0) {
2647 __ Asr(temp, temp, shift);
2648 }
2649
2650 if (instruction->IsDiv()) {
2651 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2652 } else {
2653 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2654 // TODO: Strength reduction for msub.
2655 Register temp_imm = temps.AcquireSameSizeAs(out);
2656 __ Mov(temp_imm, imm);
2657 __ Msub(out, temp, temp_imm, dividend);
2658 }
2659}
2660
2661void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2662 DCHECK(instruction->IsDiv() || instruction->IsRem());
2663 Primitive::Type type = instruction->GetResultType();
2664 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2665
2666 LocationSummary* locations = instruction->GetLocations();
2667 Register out = OutputRegister(instruction);
2668 Location second = locations->InAt(1);
2669
2670 if (second.IsConstant()) {
2671 int64_t imm = Int64FromConstant(second.GetConstant());
2672
2673 if (imm == 0) {
2674 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2675 } else if (imm == 1 || imm == -1) {
2676 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002677 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002678 DivRemByPowerOfTwo(instruction);
2679 } else {
2680 DCHECK(imm <= -2 || imm >= 2);
2681 GenerateDivRemWithAnyConstant(instruction);
2682 }
2683 } else {
2684 Register dividend = InputRegisterAt(instruction, 0);
2685 Register divisor = InputRegisterAt(instruction, 1);
2686 if (instruction->IsDiv()) {
2687 __ Sdiv(out, dividend, divisor);
2688 } else {
2689 UseScratchRegisterScope temps(GetVIXLAssembler());
2690 Register temp = temps.AcquireSameSizeAs(out);
2691 __ Sdiv(temp, dividend, divisor);
2692 __ Msub(out, temp, divisor, dividend);
2693 }
2694 }
2695}
2696
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002697void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2698 LocationSummary* locations =
2699 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2700 switch (div->GetResultType()) {
2701 case Primitive::kPrimInt:
2702 case Primitive::kPrimLong:
2703 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002704 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2706 break;
2707
2708 case Primitive::kPrimFloat:
2709 case Primitive::kPrimDouble:
2710 locations->SetInAt(0, Location::RequiresFpuRegister());
2711 locations->SetInAt(1, Location::RequiresFpuRegister());
2712 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2713 break;
2714
2715 default:
2716 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2717 }
2718}
2719
2720void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2721 Primitive::Type type = div->GetResultType();
2722 switch (type) {
2723 case Primitive::kPrimInt:
2724 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002725 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002726 break;
2727
2728 case Primitive::kPrimFloat:
2729 case Primitive::kPrimDouble:
2730 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2731 break;
2732
2733 default:
2734 LOG(FATAL) << "Unexpected div type " << type;
2735 }
2736}
2737
Alexandre Rames67555f72014-11-18 10:55:16 +00002738void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002739 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2740 ? LocationSummary::kCallOnSlowPath
2741 : LocationSummary::kNoCall;
2742 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002743 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2744 if (instruction->HasUses()) {
2745 locations->SetOut(Location::SameAsFirstInput());
2746 }
2747}
2748
2749void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2750 SlowPathCodeARM64* slow_path =
2751 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2752 codegen_->AddSlowPath(slow_path);
2753 Location value = instruction->GetLocations()->InAt(0);
2754
Alexandre Rames3e69f162014-12-10 10:36:50 +00002755 Primitive::Type type = instruction->GetType();
2756
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002757 if (!Primitive::IsIntegralType(type)) {
2758 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002759 return;
2760 }
2761
Alexandre Rames67555f72014-11-18 10:55:16 +00002762 if (value.IsConstant()) {
2763 int64_t divisor = Int64ConstantFrom(value);
2764 if (divisor == 0) {
2765 __ B(slow_path->GetEntryLabel());
2766 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002767 // A division by a non-null constant is valid. We don't need to perform
2768 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002769 }
2770 } else {
2771 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2772 }
2773}
2774
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002775void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2776 LocationSummary* locations =
2777 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2778 locations->SetOut(Location::ConstantLocation(constant));
2779}
2780
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002781void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2782 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002783 // Will be generated at use site.
2784}
2785
Alexandre Rames5319def2014-10-23 10:03:10 +01002786void LocationsBuilderARM64::VisitExit(HExit* exit) {
2787 exit->SetLocations(nullptr);
2788}
2789
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002790void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002791}
2792
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002793void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2794 LocationSummary* locations =
2795 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2796 locations->SetOut(Location::ConstantLocation(constant));
2797}
2798
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002799void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002800 // Will be generated at use site.
2801}
2802
David Brazdilfc6a86a2015-06-26 10:33:45 +00002803void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002804 DCHECK(!successor->IsExitBlock());
2805 HBasicBlock* block = got->GetBlock();
2806 HInstruction* previous = got->GetPrevious();
2807 HLoopInformation* info = block->GetLoopInformation();
2808
David Brazdil46e2a392015-03-16 17:31:52 +00002809 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002810 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2811 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2812 return;
2813 }
2814 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2815 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2816 }
2817 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002818 __ B(codegen_->GetLabelOf(successor));
2819 }
2820}
2821
David Brazdilfc6a86a2015-06-26 10:33:45 +00002822void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2823 got->SetLocations(nullptr);
2824}
2825
2826void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2827 HandleGoto(got, got->GetSuccessor());
2828}
2829
2830void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2831 try_boundary->SetLocations(nullptr);
2832}
2833
2834void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2835 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2836 if (!successor->IsExitBlock()) {
2837 HandleGoto(try_boundary, successor);
2838 }
2839}
2840
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002841void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002842 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002843 vixl::aarch64::Label* true_target,
2844 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002845 // FP branching requires both targets to be explicit. If either of the targets
2846 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002847 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002848 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002849
David Brazdil0debae72015-11-12 18:37:00 +00002850 if (true_target == nullptr && false_target == nullptr) {
2851 // Nothing to do. The code always falls through.
2852 return;
2853 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002854 // Constant condition, statically compared against "true" (integer value 1).
2855 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002856 if (true_target != nullptr) {
2857 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002858 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002859 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002860 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002861 if (false_target != nullptr) {
2862 __ B(false_target);
2863 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002864 }
David Brazdil0debae72015-11-12 18:37:00 +00002865 return;
2866 }
2867
2868 // The following code generates these patterns:
2869 // (1) true_target == nullptr && false_target != nullptr
2870 // - opposite condition true => branch to false_target
2871 // (2) true_target != nullptr && false_target == nullptr
2872 // - condition true => branch to true_target
2873 // (3) true_target != nullptr && false_target != nullptr
2874 // - condition true => branch to true_target
2875 // - branch to false_target
2876 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002877 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002878 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002879 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002880 if (true_target == nullptr) {
2881 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2882 } else {
2883 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2884 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002885 } else {
2886 // The condition instruction has not been materialized, use its inputs as
2887 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002888 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002889
David Brazdil0debae72015-11-12 18:37:00 +00002890 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002891 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002892 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002893 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002894 IfCondition opposite_condition = condition->GetOppositeCondition();
2895 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002896 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002897 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002898 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002899 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002900 // Integer cases.
2901 Register lhs = InputRegisterAt(condition, 0);
2902 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002903
2904 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002905 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002906 if (true_target == nullptr) {
2907 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2908 non_fallthrough_target = false_target;
2909 } else {
2910 arm64_cond = ARM64Condition(condition->GetCondition());
2911 non_fallthrough_target = true_target;
2912 }
2913
Aart Bik086d27e2016-01-20 17:02:00 -08002914 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002915 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002916 switch (arm64_cond) {
2917 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002918 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002919 break;
2920 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002921 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002922 break;
2923 case lt:
2924 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002925 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002926 break;
2927 case ge:
2928 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002929 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002930 break;
2931 default:
2932 // Without the `static_cast` the compiler throws an error for
2933 // `-Werror=sign-promo`.
2934 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2935 }
2936 } else {
2937 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002938 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002939 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002940 }
2941 }
David Brazdil0debae72015-11-12 18:37:00 +00002942
2943 // If neither branch falls through (case 3), the conditional branch to `true_target`
2944 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2945 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002946 __ B(false_target);
2947 }
David Brazdil0debae72015-11-12 18:37:00 +00002948
2949 if (fallthrough_target.IsLinked()) {
2950 __ Bind(&fallthrough_target);
2951 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002952}
2953
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002954void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2955 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002956 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002957 locations->SetInAt(0, Location::RequiresRegister());
2958 }
2959}
2960
2961void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002962 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2963 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002964 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2965 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2966 true_target = nullptr;
2967 }
2968 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2969 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2970 false_target = nullptr;
2971 }
David Brazdil0debae72015-11-12 18:37:00 +00002972 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002973}
2974
2975void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2976 LocationSummary* locations = new (GetGraph()->GetArena())
2977 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002978 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002979 locations->SetInAt(0, Location::RequiresRegister());
2980 }
2981}
2982
2983void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002984 SlowPathCodeARM64* slow_path =
2985 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002986 GenerateTestAndBranch(deoptimize,
2987 /* condition_input_index */ 0,
2988 slow_path->GetEntryLabel(),
2989 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002990}
2991
David Brazdilc0b601b2016-02-08 14:20:45 +00002992static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2993 return condition->IsCondition() &&
2994 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2995}
2996
Alexandre Rames880f1192016-06-13 16:04:50 +01002997static inline Condition GetConditionForSelect(HCondition* condition) {
2998 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002999 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3000 : ARM64Condition(cond);
3001}
3002
David Brazdil74eb1b22015-12-14 11:44:01 +00003003void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3004 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003005 if (Primitive::IsFloatingPointType(select->GetType())) {
3006 locations->SetInAt(0, Location::RequiresFpuRegister());
3007 locations->SetInAt(1, Location::RequiresFpuRegister());
3008 locations->SetOut(Location::RequiresFpuRegister());
3009 } else {
3010 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3011 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3012 bool is_true_value_constant = cst_true_value != nullptr;
3013 bool is_false_value_constant = cst_false_value != nullptr;
3014 // Ask VIXL whether we should synthesize constants in registers.
3015 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3016 Operand true_op = is_true_value_constant ?
3017 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3018 Operand false_op = is_false_value_constant ?
3019 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3020 bool true_value_in_register = false;
3021 bool false_value_in_register = false;
3022 MacroAssembler::GetCselSynthesisInformation(
3023 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3024 true_value_in_register |= !is_true_value_constant;
3025 false_value_in_register |= !is_false_value_constant;
3026
3027 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3028 : Location::ConstantLocation(cst_true_value));
3029 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3030 : Location::ConstantLocation(cst_false_value));
3031 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003032 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003033
David Brazdil74eb1b22015-12-14 11:44:01 +00003034 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3035 locations->SetInAt(2, Location::RequiresRegister());
3036 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003037}
3038
3039void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003040 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003041 Condition csel_cond;
3042
3043 if (IsBooleanValueOrMaterializedCondition(cond)) {
3044 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003045 // Use the condition flags set by the previous instruction.
3046 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003047 } else {
3048 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003049 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003050 }
3051 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003052 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003053 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003054 } else {
3055 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003056 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003057 }
3058
Alexandre Rames880f1192016-06-13 16:04:50 +01003059 if (Primitive::IsFloatingPointType(select->GetType())) {
3060 __ Fcsel(OutputFPRegister(select),
3061 InputFPRegisterAt(select, 1),
3062 InputFPRegisterAt(select, 0),
3063 csel_cond);
3064 } else {
3065 __ Csel(OutputRegister(select),
3066 InputOperandAt(select, 1),
3067 InputOperandAt(select, 0),
3068 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003069 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003070}
3071
David Srbecky0cf44932015-12-09 14:09:59 +00003072void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3073 new (GetGraph()->GetArena()) LocationSummary(info);
3074}
3075
David Srbeckyd28f4a02016-03-14 17:14:24 +00003076void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3077 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003078}
3079
3080void CodeGeneratorARM64::GenerateNop() {
3081 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003082}
3083
Alexandre Rames5319def2014-10-23 10:03:10 +01003084void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003085 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003086}
3087
3088void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003089 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003090}
3091
3092void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003093 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003094}
3095
3096void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003097 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003098}
3099
Roland Levillain44015862016-01-22 11:47:17 +00003100static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3101 return kEmitCompilerReadBarrier &&
3102 (kUseBakerReadBarrier ||
3103 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3104 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3105 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3106}
3107
Alexandre Rames67555f72014-11-18 10:55:16 +00003108void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003109 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003110 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3111 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003112 case TypeCheckKind::kExactCheck:
3113 case TypeCheckKind::kAbstractClassCheck:
3114 case TypeCheckKind::kClassHierarchyCheck:
3115 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003116 call_kind =
3117 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003118 break;
3119 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003120 case TypeCheckKind::kUnresolvedCheck:
3121 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003122 call_kind = LocationSummary::kCallOnSlowPath;
3123 break;
3124 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003125
Alexandre Rames67555f72014-11-18 10:55:16 +00003126 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003127 locations->SetInAt(0, Location::RequiresRegister());
3128 locations->SetInAt(1, Location::RequiresRegister());
3129 // The "out" register is used as a temporary, so it overlaps with the inputs.
3130 // Note that TypeCheckSlowPathARM64 uses this register too.
3131 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3132 // When read barriers are enabled, we need a temporary register for
3133 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003134 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003135 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003136 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003137}
3138
3139void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003140 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003141 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003142 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003143 Register obj = InputRegisterAt(instruction, 0);
3144 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003145 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003146 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003147 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3148 locations->GetTemp(0) :
3149 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003150 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3151 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3152 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3153 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003154
Scott Wakeling97c72b72016-06-24 16:19:36 +01003155 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003156 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003157
3158 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003159 // Avoid null check if we know `obj` is not null.
3160 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003161 __ Cbz(obj, &zero);
3162 }
3163
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003164 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003165 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003166
Roland Levillain44015862016-01-22 11:47:17 +00003167 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003168 case TypeCheckKind::kExactCheck: {
3169 __ Cmp(out, cls);
3170 __ Cset(out, eq);
3171 if (zero.IsLinked()) {
3172 __ B(&done);
3173 }
3174 break;
3175 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003176
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003177 case TypeCheckKind::kAbstractClassCheck: {
3178 // If the class is abstract, we eagerly fetch the super class of the
3179 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003180 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003181 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003182 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003183 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 // If `out` is null, we use it for the result, and jump to `done`.
3185 __ Cbz(out, &done);
3186 __ Cmp(out, cls);
3187 __ B(ne, &loop);
3188 __ Mov(out, 1);
3189 if (zero.IsLinked()) {
3190 __ B(&done);
3191 }
3192 break;
3193 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003195 case TypeCheckKind::kClassHierarchyCheck: {
3196 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003197 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003198 __ Bind(&loop);
3199 __ Cmp(out, cls);
3200 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003201 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003202 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003203 __ Cbnz(out, &loop);
3204 // If `out` is null, we use it for the result, and jump to `done`.
3205 __ B(&done);
3206 __ Bind(&success);
3207 __ Mov(out, 1);
3208 if (zero.IsLinked()) {
3209 __ B(&done);
3210 }
3211 break;
3212 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003213
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003214 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003215 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003216 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003217 __ Cmp(out, cls);
3218 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003219 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003220 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003221 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003222 // If `out` is null, we use it for the result, and jump to `done`.
3223 __ Cbz(out, &done);
3224 __ Ldrh(out, HeapOperand(out, primitive_offset));
3225 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3226 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003227 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003228 __ Mov(out, 1);
3229 __ B(&done);
3230 break;
3231 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003233 case TypeCheckKind::kArrayCheck: {
3234 __ Cmp(out, cls);
3235 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003236 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3237 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003238 codegen_->AddSlowPath(slow_path);
3239 __ B(ne, slow_path->GetEntryLabel());
3240 __ Mov(out, 1);
3241 if (zero.IsLinked()) {
3242 __ B(&done);
3243 }
3244 break;
3245 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003246
Calin Juravle98893e12015-10-02 21:05:03 +01003247 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003248 case TypeCheckKind::kInterfaceCheck: {
3249 // Note that we indeed only call on slow path, but we always go
3250 // into the slow path for the unresolved and interface check
3251 // cases.
3252 //
3253 // We cannot directly call the InstanceofNonTrivial runtime
3254 // entry point without resorting to a type checking slow path
3255 // here (i.e. by calling InvokeRuntime directly), as it would
3256 // require to assign fixed registers for the inputs of this
3257 // HInstanceOf instruction (following the runtime calling
3258 // convention), which might be cluttered by the potential first
3259 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003260 //
3261 // TODO: Introduce a new runtime entry point taking the object
3262 // to test (instead of its class) as argument, and let it deal
3263 // with the read barrier issues. This will let us refactor this
3264 // case of the `switch` code as it was previously (with a direct
3265 // call to the runtime not using a type checking slow path).
3266 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003267 DCHECK(locations->OnlyCallsOnSlowPath());
3268 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3269 /* is_fatal */ false);
3270 codegen_->AddSlowPath(slow_path);
3271 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003272 if (zero.IsLinked()) {
3273 __ B(&done);
3274 }
3275 break;
3276 }
3277 }
3278
3279 if (zero.IsLinked()) {
3280 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003281 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003282 }
3283
3284 if (done.IsLinked()) {
3285 __ Bind(&done);
3286 }
3287
3288 if (slow_path != nullptr) {
3289 __ Bind(slow_path->GetExitLabel());
3290 }
3291}
3292
3293void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3294 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3295 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3296
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003297 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3298 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003299 case TypeCheckKind::kExactCheck:
3300 case TypeCheckKind::kAbstractClassCheck:
3301 case TypeCheckKind::kClassHierarchyCheck:
3302 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3304 LocationSummary::kCallOnSlowPath :
3305 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003306 break;
3307 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003308 case TypeCheckKind::kUnresolvedCheck:
3309 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003310 call_kind = LocationSummary::kCallOnSlowPath;
3311 break;
3312 }
3313
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003314 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3315 locations->SetInAt(0, Location::RequiresRegister());
3316 locations->SetInAt(1, Location::RequiresRegister());
3317 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3318 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003319 // When read barriers are enabled, we need an additional temporary
3320 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003321 if (TypeCheckNeedsATemporary(type_check_kind)) {
3322 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003323 }
3324}
3325
3326void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003327 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003328 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003329 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003330 Register obj = InputRegisterAt(instruction, 0);
3331 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003332 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003333 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3334 locations->GetTemp(1) :
3335 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003336 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003337 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3338 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3339 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3340 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003341
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003342 bool is_type_check_slow_path_fatal =
3343 (type_check_kind == TypeCheckKind::kExactCheck ||
3344 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3345 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3346 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3347 !instruction->CanThrowIntoCatchBlock();
3348 SlowPathCodeARM64* type_check_slow_path =
3349 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3350 is_type_check_slow_path_fatal);
3351 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003352
Scott Wakeling97c72b72016-06-24 16:19:36 +01003353 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003354 // Avoid null check if we know obj is not null.
3355 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003356 __ Cbz(obj, &done);
3357 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003358
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003359 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003360 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003361
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003362 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003363 case TypeCheckKind::kExactCheck:
3364 case TypeCheckKind::kArrayCheck: {
3365 __ Cmp(temp, cls);
3366 // Jump to slow path for throwing the exception or doing a
3367 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 break;
3370 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003372 case TypeCheckKind::kAbstractClassCheck: {
3373 // If the class is abstract, we eagerly fetch the super class of the
3374 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003375 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003376 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003377 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003378 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003379
3380 // If the class reference currently in `temp` is not null, jump
3381 // to the `compare_classes` label to compare it with the checked
3382 // class.
3383 __ Cbnz(temp, &compare_classes);
3384 // Otherwise, jump to the slow path to throw the exception.
3385 //
3386 // But before, move back the object's class into `temp` before
3387 // going into the slow path, as it has been overwritten in the
3388 // meantime.
3389 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003390 GenerateReferenceLoadTwoRegisters(
3391 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392 __ B(type_check_slow_path->GetEntryLabel());
3393
3394 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003395 __ Cmp(temp, cls);
3396 __ B(ne, &loop);
3397 break;
3398 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003399
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003400 case TypeCheckKind::kClassHierarchyCheck: {
3401 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003402 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003403 __ Bind(&loop);
3404 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003405 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003406
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003407 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003408 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003409
3410 // If the class reference currently in `temp` is not null, jump
3411 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003412 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 // Otherwise, jump to the slow path to throw the exception.
3414 //
3415 // But before, move back the object's class into `temp` before
3416 // going into the slow path, as it has been overwritten in the
3417 // meantime.
3418 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003419 GenerateReferenceLoadTwoRegisters(
3420 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003421 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003422 break;
3423 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003424
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003425 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003426 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003427 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003428 __ Cmp(temp, cls);
3429 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430
3431 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003432 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003433 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003434
3435 // If the component type is not null (i.e. the object is indeed
3436 // an array), jump to label `check_non_primitive_component_type`
3437 // to further check that this component type is not a primitive
3438 // type.
3439 __ Cbnz(temp, &check_non_primitive_component_type);
3440 // Otherwise, jump to the slow path to throw the exception.
3441 //
3442 // But before, move back the object's class into `temp` before
3443 // going into the slow path, as it has been overwritten in the
3444 // meantime.
3445 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003446 GenerateReferenceLoadTwoRegisters(
3447 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003448 __ B(type_check_slow_path->GetEntryLabel());
3449
3450 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003451 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3452 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453 __ Cbz(temp, &done);
3454 // Same comment as above regarding `temp` and the slow path.
3455 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003456 GenerateReferenceLoadTwoRegisters(
3457 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003458 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 break;
3460 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003461
Calin Juravle98893e12015-10-02 21:05:03 +01003462 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003463 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003464 // We always go into the type check slow path for the unresolved
3465 // and interface check cases.
3466 //
3467 // We cannot directly call the CheckCast runtime entry point
3468 // without resorting to a type checking slow path here (i.e. by
3469 // calling InvokeRuntime directly), as it would require to
3470 // assign fixed registers for the inputs of this HInstanceOf
3471 // instruction (following the runtime calling convention), which
3472 // might be cluttered by the potential first read barrier
3473 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003474 //
3475 // TODO: Introduce a new runtime entry point taking the object
3476 // to test (instead of its class) as argument, and let it deal
3477 // with the read barrier issues. This will let us refactor this
3478 // case of the `switch` code as it was previously (with a direct
3479 // call to the runtime not using a type checking slow path).
3480 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003481 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003482 break;
3483 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003484 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003485
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003486 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003487}
3488
Alexandre Rames5319def2014-10-23 10:03:10 +01003489void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3491 locations->SetOut(Location::ConstantLocation(constant));
3492}
3493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003494void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003495 // Will be generated at use site.
3496}
3497
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003498void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3500 locations->SetOut(Location::ConstantLocation(constant));
3501}
3502
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003503void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003504 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003505}
3506
Calin Juravle175dc732015-08-25 15:42:32 +01003507void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3508 // The trampoline uses the same calling convention as dex calling conventions,
3509 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3510 // the method_idx.
3511 HandleInvoke(invoke);
3512}
3513
3514void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3515 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3516}
3517
Alexandre Rames5319def2014-10-23 10:03:10 +01003518void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003519 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003520 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003521}
3522
Alexandre Rames67555f72014-11-18 10:55:16 +00003523void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3524 HandleInvoke(invoke);
3525}
3526
3527void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3528 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003529 LocationSummary* locations = invoke->GetLocations();
3530 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003531 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003532 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003533 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003534
3535 // The register ip1 is required to be used for the hidden argument in
3536 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003537 MacroAssembler* masm = GetVIXLAssembler();
3538 UseScratchRegisterScope scratch_scope(masm);
3539 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003540 scratch_scope.Exclude(ip1);
3541 __ Mov(ip1, invoke->GetDexMethodIndex());
3542
Alexandre Rames67555f72014-11-18 10:55:16 +00003543 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003544 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003545 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003546 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003547 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003548 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003549 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003550 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003551 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003552 // Instead of simply (possibly) unpoisoning `temp` here, we should
3553 // emit a read barrier for the previous class reference load.
3554 // However this is not required in practice, as this is an
3555 // intermediate/temporary reference and because the current
3556 // concurrent copying collector keeps the from-space memory
3557 // intact/accessible until the end of the marking phase (the
3558 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003559 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003560 __ Ldr(temp,
3561 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3562 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003563 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003564 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003565 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003566 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003567 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003568 // lr();
3569 __ Blr(lr);
3570 DCHECK(!codegen_->IsLeafMethod());
3571 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3572}
3573
3574void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003575 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3576 if (intrinsic.TryDispatch(invoke)) {
3577 return;
3578 }
3579
Alexandre Rames67555f72014-11-18 10:55:16 +00003580 HandleInvoke(invoke);
3581}
3582
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003583void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003584 // Explicit clinit checks triggered by static invokes must have been pruned by
3585 // art::PrepareForRegisterAllocation.
3586 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003587
Andreas Gampe878d58c2015-01-15 23:24:00 -08003588 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3589 if (intrinsic.TryDispatch(invoke)) {
3590 return;
3591 }
3592
Alexandre Rames67555f72014-11-18 10:55:16 +00003593 HandleInvoke(invoke);
3594}
3595
Andreas Gampe878d58c2015-01-15 23:24:00 -08003596static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3597 if (invoke->GetLocations()->Intrinsified()) {
3598 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3599 intrinsic.Dispatch(invoke);
3600 return true;
3601 }
3602 return false;
3603}
3604
Vladimir Markodc151b22015-10-15 18:02:30 +01003605HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3606 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3607 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003608 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003609 return desired_dispatch_info;
3610}
3611
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003612void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003613 // For better instruction scheduling we load the direct code pointer before the method pointer.
3614 bool direct_code_loaded = false;
3615 switch (invoke->GetCodePtrLocation()) {
3616 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3617 // LR = code address from literal pool with link-time patch.
3618 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3619 direct_code_loaded = true;
3620 break;
3621 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3622 // LR = invoke->GetDirectCodePtr();
3623 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3624 direct_code_loaded = true;
3625 break;
3626 default:
3627 break;
3628 }
3629
Andreas Gampe878d58c2015-01-15 23:24:00 -08003630 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003631 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3632 switch (invoke->GetMethodLoadKind()) {
3633 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3634 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003635 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003636 break;
3637 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003638 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003639 break;
3640 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3641 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003642 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003643 break;
3644 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3645 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003646 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003647 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3648 break;
3649 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3650 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003651 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3652 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003653 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003654 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003655 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003656 __ Bind(adrp_label);
3657 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003658 }
Vladimir Marko58155012015-08-19 12:49:41 +00003659 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003660 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003661 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003662 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003663 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003664 __ Bind(ldr_label);
3665 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003666 }
Vladimir Marko58155012015-08-19 12:49:41 +00003667 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003668 }
Vladimir Marko58155012015-08-19 12:49:41 +00003669 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003670 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003671 Register reg = XRegisterFrom(temp);
3672 Register method_reg;
3673 if (current_method.IsRegister()) {
3674 method_reg = XRegisterFrom(current_method);
3675 } else {
3676 DCHECK(invoke->GetLocations()->Intrinsified());
3677 DCHECK(!current_method.IsValid());
3678 method_reg = reg;
3679 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3680 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003681
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003682 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003683 __ Ldr(reg.X(),
3684 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003685 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003686 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003687 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3688 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003689 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3690 break;
3691 }
3692 }
3693
3694 switch (invoke->GetCodePtrLocation()) {
3695 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3696 __ Bl(&frame_entry_label_);
3697 break;
3698 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3699 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003700 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3701 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003702 __ Bind(label);
3703 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003704 break;
3705 }
3706 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3707 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3708 // LR prepared above for better instruction scheduling.
3709 DCHECK(direct_code_loaded);
3710 // lr()
3711 __ Blr(lr);
3712 break;
3713 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3714 // LR = callee_method->entry_point_from_quick_compiled_code_;
3715 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003716 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003717 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003718 // lr()
3719 __ Blr(lr);
3720 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003721 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003722
Andreas Gampe878d58c2015-01-15 23:24:00 -08003723 DCHECK(!IsLeafMethod());
3724}
3725
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003726void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003727 // Use the calling convention instead of the location of the receiver, as
3728 // intrinsics may have put the receiver in a different register. In the intrinsics
3729 // slow path, the arguments have been moved to the right place, so here we are
3730 // guaranteed that the receiver is the first register of the calling convention.
3731 InvokeDexCallingConvention calling_convention;
3732 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003733 Register temp = XRegisterFrom(temp_in);
3734 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3735 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3736 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003737 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003738
3739 BlockPoolsScope block_pools(GetVIXLAssembler());
3740
3741 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003742 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003743 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003744 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003745 // Instead of simply (possibly) unpoisoning `temp` here, we should
3746 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003747 // intermediate/temporary reference and because the current
3748 // concurrent copying collector keeps the from-space memory
3749 // intact/accessible until the end of the marking phase (the
3750 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003751 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3752 // temp = temp->GetMethodAt(method_offset);
3753 __ Ldr(temp, MemOperand(temp, method_offset));
3754 // lr = temp->GetEntryPoint();
3755 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3756 // lr();
3757 __ Blr(lr);
3758}
3759
Scott Wakeling97c72b72016-06-24 16:19:36 +01003760vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3761 const DexFile& dex_file,
3762 uint32_t string_index,
3763 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003764 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3765}
3766
Scott Wakeling97c72b72016-06-24 16:19:36 +01003767vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3768 const DexFile& dex_file,
3769 uint32_t type_index,
3770 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003771 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3772}
3773
Scott Wakeling97c72b72016-06-24 16:19:36 +01003774vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3775 const DexFile& dex_file,
3776 uint32_t element_offset,
3777 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003778 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3779}
3780
Scott Wakeling97c72b72016-06-24 16:19:36 +01003781vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3782 const DexFile& dex_file,
3783 uint32_t offset_or_index,
3784 vixl::aarch64::Label* adrp_label,
3785 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003786 // Add a patch entry and return the label.
3787 patches->emplace_back(dex_file, offset_or_index);
3788 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003789 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003790 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3791 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3792 return label;
3793}
3794
Scott Wakeling97c72b72016-06-24 16:19:36 +01003795vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003796 const DexFile& dex_file, uint32_t string_index) {
3797 return boot_image_string_patches_.GetOrCreate(
3798 StringReference(&dex_file, string_index),
3799 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3800}
3801
Scott Wakeling97c72b72016-06-24 16:19:36 +01003802vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003803 const DexFile& dex_file, uint32_t type_index) {
3804 return boot_image_type_patches_.GetOrCreate(
3805 TypeReference(&dex_file, type_index),
3806 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3807}
3808
Scott Wakeling97c72b72016-06-24 16:19:36 +01003809vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3810 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003811 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3812 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3813 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3814}
3815
Scott Wakeling97c72b72016-06-24 16:19:36 +01003816vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3817 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003818 return DeduplicateUint64Literal(address);
3819}
3820
Vladimir Marko58155012015-08-19 12:49:41 +00003821void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3822 DCHECK(linker_patches->empty());
3823 size_t size =
3824 method_patches_.size() +
3825 call_patches_.size() +
3826 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003827 pc_relative_dex_cache_patches_.size() +
3828 boot_image_string_patches_.size() +
3829 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003830 boot_image_type_patches_.size() +
3831 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003832 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003833 linker_patches->reserve(size);
3834 for (const auto& entry : method_patches_) {
3835 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003836 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3837 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003838 target_method.dex_file,
3839 target_method.dex_method_index));
3840 }
3841 for (const auto& entry : call_patches_) {
3842 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003843 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3844 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003845 target_method.dex_file,
3846 target_method.dex_method_index));
3847 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003848 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3849 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003850 info.target_method.dex_file,
3851 info.target_method.dex_method_index));
3852 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003853 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003854 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003855 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003856 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003857 info.offset_or_index));
3858 }
3859 for (const auto& entry : boot_image_string_patches_) {
3860 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003861 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3862 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003863 target_string.dex_file,
3864 target_string.string_index));
3865 }
3866 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003867 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003868 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003869 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003870 info.offset_or_index));
3871 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003872 for (const auto& entry : boot_image_type_patches_) {
3873 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003874 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3875 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003876 target_type.dex_file,
3877 target_type.type_index));
3878 }
3879 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003880 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003881 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003882 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003883 info.offset_or_index));
3884 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003885 for (const auto& entry : boot_image_address_patches_) {
3886 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003887 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3888 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003889 }
3890}
3891
Scott Wakeling97c72b72016-06-24 16:19:36 +01003892vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003893 Uint32ToLiteralMap* map) {
3894 return map->GetOrCreate(
3895 value,
3896 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3897}
3898
Scott Wakeling97c72b72016-06-24 16:19:36 +01003899vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003900 return uint64_literals_.GetOrCreate(
3901 value,
3902 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003903}
3904
Scott Wakeling97c72b72016-06-24 16:19:36 +01003905vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003906 MethodReference target_method,
3907 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003908 return map->GetOrCreate(
3909 target_method,
3910 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003911}
3912
Scott Wakeling97c72b72016-06-24 16:19:36 +01003913vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003914 MethodReference target_method) {
3915 return DeduplicateMethodLiteral(target_method, &method_patches_);
3916}
3917
Scott Wakeling97c72b72016-06-24 16:19:36 +01003918vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003919 MethodReference target_method) {
3920 return DeduplicateMethodLiteral(target_method, &call_patches_);
3921}
3922
3923
Andreas Gampe878d58c2015-01-15 23:24:00 -08003924void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003925 // Explicit clinit checks triggered by static invokes must have been pruned by
3926 // art::PrepareForRegisterAllocation.
3927 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003928
Andreas Gampe878d58c2015-01-15 23:24:00 -08003929 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3930 return;
3931 }
3932
Alexandre Ramesd921d642015-04-16 15:07:16 +01003933 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003934 LocationSummary* locations = invoke->GetLocations();
3935 codegen_->GenerateStaticOrDirectCall(
3936 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003937 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003938}
3939
3940void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003941 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3942 return;
3943 }
3944
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003945 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003946 DCHECK(!codegen_->IsLeafMethod());
3947 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3948}
3949
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003950HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3951 HLoadClass::LoadKind desired_class_load_kind) {
3952 if (kEmitCompilerReadBarrier) {
3953 switch (desired_class_load_kind) {
3954 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3955 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3956 case HLoadClass::LoadKind::kBootImageAddress:
3957 // TODO: Implement for read barrier.
3958 return HLoadClass::LoadKind::kDexCacheViaMethod;
3959 default:
3960 break;
3961 }
3962 }
3963 switch (desired_class_load_kind) {
3964 case HLoadClass::LoadKind::kReferrersClass:
3965 break;
3966 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3967 DCHECK(!GetCompilerOptions().GetCompilePic());
3968 break;
3969 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3970 DCHECK(GetCompilerOptions().GetCompilePic());
3971 break;
3972 case HLoadClass::LoadKind::kBootImageAddress:
3973 break;
3974 case HLoadClass::LoadKind::kDexCacheAddress:
3975 DCHECK(Runtime::Current()->UseJitCompilation());
3976 break;
3977 case HLoadClass::LoadKind::kDexCachePcRelative:
3978 DCHECK(!Runtime::Current()->UseJitCompilation());
3979 break;
3980 case HLoadClass::LoadKind::kDexCacheViaMethod:
3981 break;
3982 }
3983 return desired_class_load_kind;
3984}
3985
Alexandre Rames67555f72014-11-18 10:55:16 +00003986void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003987 if (cls->NeedsAccessCheck()) {
3988 InvokeRuntimeCallingConvention calling_convention;
3989 CodeGenerator::CreateLoadClassLocationSummary(
3990 cls,
3991 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003992 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003993 /* code_generator_supports_read_barrier */ true);
3994 return;
3995 }
3996
3997 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3998 ? LocationSummary::kCallOnSlowPath
3999 : LocationSummary::kNoCall;
4000 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4001 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4002 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4003 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4004 locations->SetInAt(0, Location::RequiresRegister());
4005 }
4006 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004007}
4008
4009void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004010 if (cls->NeedsAccessCheck()) {
4011 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
4012 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4013 cls,
4014 cls->GetDexPc(),
4015 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004016 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004017 return;
4018 }
4019
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004020 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004021 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004022
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004023 bool generate_null_check = false;
4024 switch (cls->GetLoadKind()) {
4025 case HLoadClass::LoadKind::kReferrersClass: {
4026 DCHECK(!cls->CanCallRuntime());
4027 DCHECK(!cls->MustGenerateClinitCheck());
4028 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4029 Register current_method = InputRegisterAt(cls, 0);
4030 GenerateGcRootFieldLoad(
4031 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4032 break;
4033 }
4034 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4035 DCHECK(!kEmitCompilerReadBarrier);
4036 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4037 cls->GetTypeIndex()));
4038 break;
4039 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4040 DCHECK(!kEmitCompilerReadBarrier);
4041 // Add ADRP with its PC-relative type patch.
4042 const DexFile& dex_file = cls->GetDexFile();
4043 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004044 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004045 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004046 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004047 __ Bind(adrp_label);
4048 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004049 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004050 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004051 vixl::aarch64::Label* add_label =
4052 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004053 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004054 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004055 __ Bind(add_label);
4056 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004057 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004058 break;
4059 }
4060 case HLoadClass::LoadKind::kBootImageAddress: {
4061 DCHECK(!kEmitCompilerReadBarrier);
4062 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4063 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4064 break;
4065 }
4066 case HLoadClass::LoadKind::kDexCacheAddress: {
4067 DCHECK_NE(cls->GetAddress(), 0u);
4068 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4069 // that gives a 16KiB range. To try and reduce the number of literals if we load
4070 // multiple types, simply split the dex cache address to a 16KiB aligned base
4071 // loaded from a literal and the remaining offset embedded in the load.
4072 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4073 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4074 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4075 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4076 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4077 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4078 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4079 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4080 generate_null_check = !cls->IsInDexCache();
4081 break;
4082 }
4083 case HLoadClass::LoadKind::kDexCachePcRelative: {
4084 // Add ADRP with its PC-relative DexCache access patch.
4085 const DexFile& dex_file = cls->GetDexFile();
4086 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004087 vixl::aarch64::Label* adrp_label =
4088 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004089 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004090 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004091 __ Bind(adrp_label);
4092 __ adrp(out.X(), /* offset placeholder */ 0);
4093 }
4094 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004095 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004096 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4097 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4098 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4099 generate_null_check = !cls->IsInDexCache();
4100 break;
4101 }
4102 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4103 MemberOffset resolved_types_offset =
4104 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4105 // /* GcRoot<mirror::Class>[] */ out =
4106 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4107 Register current_method = InputRegisterAt(cls, 0);
4108 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4109 // /* GcRoot<mirror::Class> */ out = out[type_index]
4110 GenerateGcRootFieldLoad(
4111 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4112 generate_null_check = !cls->IsInDexCache();
4113 break;
4114 }
4115 }
4116
4117 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4118 DCHECK(cls->CanCallRuntime());
4119 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4120 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4121 codegen_->AddSlowPath(slow_path);
4122 if (generate_null_check) {
4123 __ Cbz(out, slow_path->GetEntryLabel());
4124 }
4125 if (cls->MustGenerateClinitCheck()) {
4126 GenerateClassInitializationCheck(slow_path, out);
4127 } else {
4128 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004129 }
4130 }
4131}
4132
David Brazdilcb1c0552015-08-04 16:22:25 +01004133static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004134 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004135}
4136
Alexandre Rames67555f72014-11-18 10:55:16 +00004137void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4138 LocationSummary* locations =
4139 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4140 locations->SetOut(Location::RequiresRegister());
4141}
4142
4143void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004144 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4145}
4146
4147void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4148 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4149}
4150
4151void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4152 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004153}
4154
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004155HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4156 HLoadString::LoadKind desired_string_load_kind) {
4157 if (kEmitCompilerReadBarrier) {
4158 switch (desired_string_load_kind) {
4159 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4160 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4161 case HLoadString::LoadKind::kBootImageAddress:
4162 // TODO: Implement for read barrier.
4163 return HLoadString::LoadKind::kDexCacheViaMethod;
4164 default:
4165 break;
4166 }
4167 }
4168 switch (desired_string_load_kind) {
4169 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4170 DCHECK(!GetCompilerOptions().GetCompilePic());
4171 break;
4172 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4173 DCHECK(GetCompilerOptions().GetCompilePic());
4174 break;
4175 case HLoadString::LoadKind::kBootImageAddress:
4176 break;
4177 case HLoadString::LoadKind::kDexCacheAddress:
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::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004181 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004182 break;
4183 case HLoadString::LoadKind::kDexCacheViaMethod:
4184 break;
4185 }
4186 return desired_string_load_kind;
4187}
4188
Alexandre Rames67555f72014-11-18 10:55:16 +00004189void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004190 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004191 ? LocationSummary::kCallOnSlowPath
4192 : LocationSummary::kNoCall;
4193 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004194 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4195 locations->SetInAt(0, Location::RequiresRegister());
4196 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004197 locations->SetOut(Location::RequiresRegister());
4198}
4199
4200void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004202 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004203
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004204 switch (load->GetLoadKind()) {
4205 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4206 DCHECK(!kEmitCompilerReadBarrier);
4207 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4208 load->GetStringIndex()));
4209 return; // No dex cache slow path.
4210 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4211 DCHECK(!kEmitCompilerReadBarrier);
4212 // Add ADRP with its PC-relative String patch.
4213 const DexFile& dex_file = load->GetDexFile();
4214 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004215 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004216 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004217 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004218 __ Bind(adrp_label);
4219 __ adrp(out.X(), /* offset placeholder */ 0);
4220 }
4221 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004222 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004223 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4224 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004225 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004226 __ Bind(add_label);
4227 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4228 }
4229 return; // No dex cache slow path.
4230 }
4231 case HLoadString::LoadKind::kBootImageAddress: {
4232 DCHECK(!kEmitCompilerReadBarrier);
4233 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4234 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4235 return; // No dex cache slow path.
4236 }
4237 case HLoadString::LoadKind::kDexCacheAddress: {
4238 DCHECK_NE(load->GetAddress(), 0u);
4239 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4240 // that gives a 16KiB range. To try and reduce the number of literals if we load
4241 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4242 // loaded from a literal and the remaining offset embedded in the load.
4243 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4244 DCHECK_ALIGNED(load->GetAddress(), 4u);
4245 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4246 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4247 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4248 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004249 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004250 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4251 break;
4252 }
4253 case HLoadString::LoadKind::kDexCachePcRelative: {
4254 // Add ADRP with its PC-relative DexCache access patch.
4255 const DexFile& dex_file = load->GetDexFile();
4256 uint32_t element_offset = load->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004257 vixl::aarch64::Label* adrp_label =
4258 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004259 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004260 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004261 __ Bind(adrp_label);
4262 __ adrp(out.X(), /* offset placeholder */ 0);
4263 }
4264 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004265 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004266 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004267 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004268 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4269 break;
4270 }
4271 case HLoadString::LoadKind::kDexCacheViaMethod: {
4272 Register current_method = InputRegisterAt(load, 0);
4273 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4274 GenerateGcRootFieldLoad(
4275 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4276 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4277 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4278 // /* GcRoot<mirror::String> */ out = out[string_index]
4279 GenerateGcRootFieldLoad(
4280 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4281 break;
4282 }
4283 default:
4284 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4285 UNREACHABLE();
4286 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004287
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004288 if (!load->IsInDexCache()) {
4289 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4290 codegen_->AddSlowPath(slow_path);
4291 __ Cbz(out, slow_path->GetEntryLabel());
4292 __ Bind(slow_path->GetExitLabel());
4293 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004294}
4295
Alexandre Rames5319def2014-10-23 10:03:10 +01004296void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4298 locations->SetOut(Location::ConstantLocation(constant));
4299}
4300
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004301void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004302 // Will be generated at use site.
4303}
4304
Alexandre Rames67555f72014-11-18 10:55:16 +00004305void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4306 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004308 InvokeRuntimeCallingConvention calling_convention;
4309 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4310}
4311
4312void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4313 codegen_->InvokeRuntime(instruction->IsEnter()
4314 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4315 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004316 instruction->GetDexPc(),
4317 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004318 if (instruction->IsEnter()) {
4319 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4320 } else {
4321 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4322 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004323}
4324
Alexandre Rames42d641b2014-10-27 14:00:51 +00004325void LocationsBuilderARM64::VisitMul(HMul* mul) {
4326 LocationSummary* locations =
4327 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4328 switch (mul->GetResultType()) {
4329 case Primitive::kPrimInt:
4330 case Primitive::kPrimLong:
4331 locations->SetInAt(0, Location::RequiresRegister());
4332 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004334 break;
4335
4336 case Primitive::kPrimFloat:
4337 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004338 locations->SetInAt(0, Location::RequiresFpuRegister());
4339 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004340 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004341 break;
4342
4343 default:
4344 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4345 }
4346}
4347
4348void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4349 switch (mul->GetResultType()) {
4350 case Primitive::kPrimInt:
4351 case Primitive::kPrimLong:
4352 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4353 break;
4354
4355 case Primitive::kPrimFloat:
4356 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004357 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004358 break;
4359
4360 default:
4361 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4362 }
4363}
4364
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004365void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4366 LocationSummary* locations =
4367 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4368 switch (neg->GetResultType()) {
4369 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004370 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004371 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004373 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004374
4375 case Primitive::kPrimFloat:
4376 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004377 locations->SetInAt(0, Location::RequiresFpuRegister());
4378 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004379 break;
4380
4381 default:
4382 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4383 }
4384}
4385
4386void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4387 switch (neg->GetResultType()) {
4388 case Primitive::kPrimInt:
4389 case Primitive::kPrimLong:
4390 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4391 break;
4392
4393 case Primitive::kPrimFloat:
4394 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004395 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004396 break;
4397
4398 default:
4399 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4400 }
4401}
4402
4403void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4404 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004406 InvokeRuntimeCallingConvention calling_convention;
4407 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004408 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004409 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004410 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004411}
4412
4413void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4414 LocationSummary* locations = instruction->GetLocations();
4415 InvokeRuntimeCallingConvention calling_convention;
4416 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4417 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004418 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004419 // Note: if heap poisoning is enabled, the entry point takes cares
4420 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004421 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4422 instruction,
4423 instruction->GetDexPc(),
4424 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004425 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004426}
4427
Alexandre Rames5319def2014-10-23 10:03:10 +01004428void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4429 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004430 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004431 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004432 if (instruction->IsStringAlloc()) {
4433 locations->AddTemp(LocationFrom(kArtMethodRegister));
4434 } else {
4435 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4436 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4437 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004438 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4439}
4440
4441void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004442 // Note: if heap poisoning is enabled, the entry point takes cares
4443 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004444 if (instruction->IsStringAlloc()) {
4445 // String is allocated through StringFactory. Call NewEmptyString entry point.
4446 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004447 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004448 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4449 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4450 __ Blr(lr);
4451 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4452 } else {
4453 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4454 instruction,
4455 instruction->GetDexPc(),
4456 nullptr);
4457 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4458 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004459}
4460
4461void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004463 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004465}
4466
4467void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004468 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004469 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004470 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004471 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004472 break;
4473
4474 default:
4475 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4476 }
4477}
4478
David Brazdil66d126e2015-04-03 16:02:44 +01004479void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4480 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4481 locations->SetInAt(0, Location::RequiresRegister());
4482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4483}
4484
4485void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004486 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004487}
4488
Alexandre Rames5319def2014-10-23 10:03:10 +01004489void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004490 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4491 ? LocationSummary::kCallOnSlowPath
4492 : LocationSummary::kNoCall;
4493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004494 locations->SetInAt(0, Location::RequiresRegister());
4495 if (instruction->HasUses()) {
4496 locations->SetOut(Location::SameAsFirstInput());
4497 }
4498}
4499
Calin Juravle2ae48182016-03-16 14:05:09 +00004500void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4501 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004502 return;
4503 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004504
Alexandre Ramesd921d642015-04-16 15:07:16 +01004505 BlockPoolsScope block_pools(GetVIXLAssembler());
4506 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004507 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004508 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004509}
4510
Calin Juravle2ae48182016-03-16 14:05:09 +00004511void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004512 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004513 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004514
4515 LocationSummary* locations = instruction->GetLocations();
4516 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004517
4518 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004519}
4520
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004521void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004522 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004523}
4524
Alexandre Rames67555f72014-11-18 10:55:16 +00004525void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4526 HandleBinaryOp(instruction);
4527}
4528
4529void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4530 HandleBinaryOp(instruction);
4531}
4532
Alexandre Rames3e69f162014-12-10 10:36:50 +00004533void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4534 LOG(FATAL) << "Unreachable";
4535}
4536
4537void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4538 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4539}
4540
Alexandre Rames5319def2014-10-23 10:03:10 +01004541void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4543 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4544 if (location.IsStackSlot()) {
4545 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4546 } else if (location.IsDoubleStackSlot()) {
4547 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4548 }
4549 locations->SetOut(location);
4550}
4551
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004552void InstructionCodeGeneratorARM64::VisitParameterValue(
4553 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004554 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004555}
4556
4557void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4558 LocationSummary* locations =
4559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004560 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004561}
4562
4563void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4564 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4565 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004566}
4567
4568void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004570 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004571 locations->SetInAt(i, Location::Any());
4572 }
4573 locations->SetOut(Location::Any());
4574}
4575
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004576void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004577 LOG(FATAL) << "Unreachable";
4578}
4579
Serban Constantinescu02164b32014-11-13 14:05:07 +00004580void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004581 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004582 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004583 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4584 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4586
4587 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004588 case Primitive::kPrimInt:
4589 case Primitive::kPrimLong:
4590 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004591 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4593 break;
4594
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004595 case Primitive::kPrimFloat:
4596 case Primitive::kPrimDouble: {
4597 InvokeRuntimeCallingConvention calling_convention;
4598 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4599 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4600 locations->SetOut(calling_convention.GetReturnLocation(type));
4601
4602 break;
4603 }
4604
Serban Constantinescu02164b32014-11-13 14:05:07 +00004605 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004606 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004607 }
4608}
4609
4610void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4611 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004612
Serban Constantinescu02164b32014-11-13 14:05:07 +00004613 switch (type) {
4614 case Primitive::kPrimInt:
4615 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004616 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004617 break;
4618 }
4619
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004620 case Primitive::kPrimFloat:
4621 case Primitive::kPrimDouble: {
4622 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4623 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004624 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004625 if (type == Primitive::kPrimFloat) {
4626 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4627 } else {
4628 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4629 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004630 break;
4631 }
4632
Serban Constantinescu02164b32014-11-13 14:05:07 +00004633 default:
4634 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004635 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004636 }
4637}
4638
Calin Juravle27df7582015-04-17 19:12:31 +01004639void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4640 memory_barrier->SetLocations(nullptr);
4641}
4642
4643void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004644 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004645}
4646
Alexandre Rames5319def2014-10-23 10:03:10 +01004647void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4648 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4649 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004650 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004651}
4652
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004653void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004654 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004655}
4656
4657void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4658 instruction->SetLocations(nullptr);
4659}
4660
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004661void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004662 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004663}
4664
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004665void LocationsBuilderARM64::VisitRor(HRor* ror) {
4666 HandleBinaryOp(ror);
4667}
4668
4669void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4670 HandleBinaryOp(ror);
4671}
4672
Serban Constantinescu02164b32014-11-13 14:05:07 +00004673void LocationsBuilderARM64::VisitShl(HShl* shl) {
4674 HandleShift(shl);
4675}
4676
4677void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4678 HandleShift(shl);
4679}
4680
4681void LocationsBuilderARM64::VisitShr(HShr* shr) {
4682 HandleShift(shr);
4683}
4684
4685void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4686 HandleShift(shr);
4687}
4688
Alexandre Rames5319def2014-10-23 10:03:10 +01004689void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004690 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004691}
4692
4693void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004694 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004695}
4696
Alexandre Rames67555f72014-11-18 10:55:16 +00004697void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004698 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004699}
4700
4701void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004702 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004703}
4704
4705void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004706 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004707}
4708
Alexandre Rames67555f72014-11-18 10:55:16 +00004709void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004710 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004711}
4712
Calin Juravlee460d1d2015-09-29 04:52:17 +01004713void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4714 HUnresolvedInstanceFieldGet* instruction) {
4715 FieldAccessCallingConventionARM64 calling_convention;
4716 codegen_->CreateUnresolvedFieldLocationSummary(
4717 instruction, instruction->GetFieldType(), calling_convention);
4718}
4719
4720void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4721 HUnresolvedInstanceFieldGet* instruction) {
4722 FieldAccessCallingConventionARM64 calling_convention;
4723 codegen_->GenerateUnresolvedFieldAccess(instruction,
4724 instruction->GetFieldType(),
4725 instruction->GetFieldIndex(),
4726 instruction->GetDexPc(),
4727 calling_convention);
4728}
4729
4730void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4731 HUnresolvedInstanceFieldSet* instruction) {
4732 FieldAccessCallingConventionARM64 calling_convention;
4733 codegen_->CreateUnresolvedFieldLocationSummary(
4734 instruction, instruction->GetFieldType(), calling_convention);
4735}
4736
4737void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4738 HUnresolvedInstanceFieldSet* instruction) {
4739 FieldAccessCallingConventionARM64 calling_convention;
4740 codegen_->GenerateUnresolvedFieldAccess(instruction,
4741 instruction->GetFieldType(),
4742 instruction->GetFieldIndex(),
4743 instruction->GetDexPc(),
4744 calling_convention);
4745}
4746
4747void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4748 HUnresolvedStaticFieldGet* instruction) {
4749 FieldAccessCallingConventionARM64 calling_convention;
4750 codegen_->CreateUnresolvedFieldLocationSummary(
4751 instruction, instruction->GetFieldType(), calling_convention);
4752}
4753
4754void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4755 HUnresolvedStaticFieldGet* instruction) {
4756 FieldAccessCallingConventionARM64 calling_convention;
4757 codegen_->GenerateUnresolvedFieldAccess(instruction,
4758 instruction->GetFieldType(),
4759 instruction->GetFieldIndex(),
4760 instruction->GetDexPc(),
4761 calling_convention);
4762}
4763
4764void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4765 HUnresolvedStaticFieldSet* instruction) {
4766 FieldAccessCallingConventionARM64 calling_convention;
4767 codegen_->CreateUnresolvedFieldLocationSummary(
4768 instruction, instruction->GetFieldType(), calling_convention);
4769}
4770
4771void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4772 HUnresolvedStaticFieldSet* instruction) {
4773 FieldAccessCallingConventionARM64 calling_convention;
4774 codegen_->GenerateUnresolvedFieldAccess(instruction,
4775 instruction->GetFieldType(),
4776 instruction->GetFieldIndex(),
4777 instruction->GetDexPc(),
4778 calling_convention);
4779}
4780
Alexandre Rames5319def2014-10-23 10:03:10 +01004781void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4783}
4784
4785void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004786 HBasicBlock* block = instruction->GetBlock();
4787 if (block->GetLoopInformation() != nullptr) {
4788 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4789 // The back edge will generate the suspend check.
4790 return;
4791 }
4792 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4793 // The goto will generate the suspend check.
4794 return;
4795 }
4796 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004797}
4798
Alexandre Rames67555f72014-11-18 10:55:16 +00004799void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4800 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004801 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004802 InvokeRuntimeCallingConvention calling_convention;
4803 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4804}
4805
4806void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4807 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004808 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004809 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004810}
4811
4812void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4813 LocationSummary* locations =
4814 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4815 Primitive::Type input_type = conversion->GetInputType();
4816 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004817 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004818 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4819 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4820 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4821 }
4822
Alexandre Rames542361f2015-01-29 16:57:31 +00004823 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004824 locations->SetInAt(0, Location::RequiresFpuRegister());
4825 } else {
4826 locations->SetInAt(0, Location::RequiresRegister());
4827 }
4828
Alexandre Rames542361f2015-01-29 16:57:31 +00004829 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004830 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4831 } else {
4832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4833 }
4834}
4835
4836void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4837 Primitive::Type result_type = conversion->GetResultType();
4838 Primitive::Type input_type = conversion->GetInputType();
4839
4840 DCHECK_NE(input_type, result_type);
4841
Alexandre Rames542361f2015-01-29 16:57:31 +00004842 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004843 int result_size = Primitive::ComponentSize(result_type);
4844 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004845 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004846 Register output = OutputRegister(conversion);
4847 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004848 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004849 // 'int' values are used directly as W registers, discarding the top
4850 // bits, so we don't need to sign-extend and can just perform a move.
4851 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4852 // top 32 bits of the target register. We theoretically could leave those
4853 // bits unchanged, but we would have to make sure that no code uses a
4854 // 32bit input value as a 64bit value assuming that the top 32 bits are
4855 // zero.
4856 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004857 } else if (result_type == Primitive::kPrimChar ||
4858 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4859 __ Ubfx(output,
4860 output.IsX() ? source.X() : source.W(),
4861 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004862 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004863 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004864 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004865 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004866 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004867 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004868 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4869 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004870 } else if (Primitive::IsFloatingPointType(result_type) &&
4871 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004872 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4873 } else {
4874 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4875 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004876 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004877}
Alexandre Rames67555f72014-11-18 10:55:16 +00004878
Serban Constantinescu02164b32014-11-13 14:05:07 +00004879void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4880 HandleShift(ushr);
4881}
4882
4883void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4884 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004885}
4886
4887void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4888 HandleBinaryOp(instruction);
4889}
4890
4891void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4892 HandleBinaryOp(instruction);
4893}
4894
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004895void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004896 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004897 LOG(FATAL) << "Unreachable";
4898}
4899
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004900void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004901 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004902 LOG(FATAL) << "Unreachable";
4903}
4904
Mark Mendellfe57faa2015-09-18 09:26:15 -04004905// Simple implementation of packed switch - generate cascaded compare/jumps.
4906void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4907 LocationSummary* locations =
4908 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4909 locations->SetInAt(0, Location::RequiresRegister());
4910}
4911
4912void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4913 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004914 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004915 Register value_reg = InputRegisterAt(switch_instr, 0);
4916 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4917
Zheng Xu3927c8b2015-11-18 17:46:25 +08004918 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004919 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004920 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4921 // make sure we don't emit it if the target may run out of range.
4922 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4923 // ranges and emit the tables only as required.
4924 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004925
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004926 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004927 // Current instruction id is an upper bound of the number of HIRs in the graph.
4928 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4929 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004930 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4931 Register temp = temps.AcquireW();
4932 __ Subs(temp, value_reg, Operand(lower_bound));
4933
Zheng Xu3927c8b2015-11-18 17:46:25 +08004934 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004935 // Jump to successors[0] if value == lower_bound.
4936 __ B(eq, codegen_->GetLabelOf(successors[0]));
4937 int32_t last_index = 0;
4938 for (; num_entries - last_index > 2; last_index += 2) {
4939 __ Subs(temp, temp, Operand(2));
4940 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4941 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4942 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4943 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4944 }
4945 if (num_entries - last_index == 2) {
4946 // The last missing case_value.
4947 __ Cmp(temp, Operand(1));
4948 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004949 }
4950
4951 // And the default for any other value.
4952 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4953 __ B(codegen_->GetLabelOf(default_block));
4954 }
4955 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004956 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004957
4958 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4959
4960 // Below instructions should use at most one blocked register. Since there are two blocked
4961 // registers, we are free to block one.
4962 Register temp_w = temps.AcquireW();
4963 Register index;
4964 // Remove the bias.
4965 if (lower_bound != 0) {
4966 index = temp_w;
4967 __ Sub(index, value_reg, Operand(lower_bound));
4968 } else {
4969 index = value_reg;
4970 }
4971
4972 // Jump to default block if index is out of the range.
4973 __ Cmp(index, Operand(num_entries));
4974 __ B(hs, codegen_->GetLabelOf(default_block));
4975
4976 // In current VIXL implementation, it won't require any blocked registers to encode the
4977 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4978 // register pressure.
4979 Register table_base = temps.AcquireX();
4980 // Load jump offset from the table.
4981 __ Adr(table_base, jump_table->GetTableStartLabel());
4982 Register jump_offset = temp_w;
4983 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4984
4985 // Jump to target block by branching to table_base(pc related) + offset.
4986 Register target_address = table_base;
4987 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4988 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004989 }
4990}
4991
Roland Levillain44015862016-01-22 11:47:17 +00004992void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4993 Location out,
4994 uint32_t offset,
4995 Location maybe_temp) {
4996 Primitive::Type type = Primitive::kPrimNot;
4997 Register out_reg = RegisterFrom(out, type);
4998 if (kEmitCompilerReadBarrier) {
4999 Register temp_reg = RegisterFrom(maybe_temp, type);
5000 if (kUseBakerReadBarrier) {
5001 // Load with fast path based Baker's read barrier.
5002 // /* HeapReference<Object> */ out = *(out + offset)
5003 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5004 out,
5005 out_reg,
5006 offset,
5007 temp_reg,
5008 /* needs_null_check */ false,
5009 /* use_load_acquire */ false);
5010 } else {
5011 // Load with slow path based read barrier.
5012 // Save the value of `out` into `maybe_temp` before overwriting it
5013 // in the following move operation, as we will need it for the
5014 // read barrier below.
5015 __ Mov(temp_reg, out_reg);
5016 // /* HeapReference<Object> */ out = *(out + offset)
5017 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5018 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5019 }
5020 } else {
5021 // Plain load with no read barrier.
5022 // /* HeapReference<Object> */ out = *(out + offset)
5023 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5024 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5025 }
5026}
5027
5028void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5029 Location out,
5030 Location obj,
5031 uint32_t offset,
5032 Location maybe_temp) {
5033 Primitive::Type type = Primitive::kPrimNot;
5034 Register out_reg = RegisterFrom(out, type);
5035 Register obj_reg = RegisterFrom(obj, type);
5036 if (kEmitCompilerReadBarrier) {
5037 if (kUseBakerReadBarrier) {
5038 // Load with fast path based Baker's read barrier.
5039 Register temp_reg = RegisterFrom(maybe_temp, type);
5040 // /* HeapReference<Object> */ out = *(obj + offset)
5041 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5042 out,
5043 obj_reg,
5044 offset,
5045 temp_reg,
5046 /* needs_null_check */ false,
5047 /* use_load_acquire */ false);
5048 } else {
5049 // Load with slow path based read barrier.
5050 // /* HeapReference<Object> */ out = *(obj + offset)
5051 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5052 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5053 }
5054 } else {
5055 // Plain load with no read barrier.
5056 // /* HeapReference<Object> */ out = *(obj + offset)
5057 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5058 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5059 }
5060}
5061
5062void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5063 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005064 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005065 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005066 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005067 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5068 if (kEmitCompilerReadBarrier) {
5069 if (kUseBakerReadBarrier) {
5070 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5071 // Baker's read barrier are used:
5072 //
5073 // root = obj.field;
5074 // if (Thread::Current()->GetIsGcMarking()) {
5075 // root = ReadBarrier::Mark(root)
5076 // }
5077
5078 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 if (fixup_label == nullptr) {
5080 __ Ldr(root_reg, MemOperand(obj, offset));
5081 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005082 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005083 __ Bind(fixup_label);
5084 __ ldr(root_reg, MemOperand(obj, offset));
5085 }
Roland Levillain44015862016-01-22 11:47:17 +00005086 static_assert(
5087 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5088 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5089 "have different sizes.");
5090 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5091 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5092 "have different sizes.");
5093
5094 // Slow path used to mark the GC root `root`.
5095 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005096 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005097 codegen_->AddSlowPath(slow_path);
5098
5099 MacroAssembler* masm = GetVIXLAssembler();
5100 UseScratchRegisterScope temps(masm);
5101 Register temp = temps.AcquireW();
5102 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005103 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005104 __ Cbnz(temp, slow_path->GetEntryLabel());
5105 __ Bind(slow_path->GetExitLabel());
5106 } else {
5107 // GC root loaded through a slow path for read barriers other
5108 // than Baker's.
5109 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005110 if (fixup_label == nullptr) {
5111 __ Add(root_reg.X(), obj.X(), offset);
5112 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005113 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005114 __ Bind(fixup_label);
5115 __ add(root_reg.X(), obj.X(), offset);
5116 }
Roland Levillain44015862016-01-22 11:47:17 +00005117 // /* mirror::Object* */ root = root->Read()
5118 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5119 }
5120 } else {
5121 // Plain GC root load with no read barrier.
5122 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005123 if (fixup_label == nullptr) {
5124 __ Ldr(root_reg, MemOperand(obj, offset));
5125 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005126 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005127 __ Bind(fixup_label);
5128 __ ldr(root_reg, MemOperand(obj, offset));
5129 }
Roland Levillain44015862016-01-22 11:47:17 +00005130 // Note that GC roots are not affected by heap poisoning, thus we
5131 // do not have to unpoison `root_reg` here.
5132 }
5133}
5134
5135void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5136 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005137 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005138 uint32_t offset,
5139 Register temp,
5140 bool needs_null_check,
5141 bool use_load_acquire) {
5142 DCHECK(kEmitCompilerReadBarrier);
5143 DCHECK(kUseBakerReadBarrier);
5144
5145 // /* HeapReference<Object> */ ref = *(obj + offset)
5146 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005147 size_t no_scale_factor = 0U;
5148 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5149 ref,
5150 obj,
5151 offset,
5152 no_index,
5153 no_scale_factor,
5154 temp,
5155 needs_null_check,
5156 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005157}
5158
5159void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5160 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005161 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005162 uint32_t data_offset,
5163 Location index,
5164 Register temp,
5165 bool needs_null_check) {
5166 DCHECK(kEmitCompilerReadBarrier);
5167 DCHECK(kUseBakerReadBarrier);
5168
5169 // Array cells are never volatile variables, therefore array loads
5170 // never use Load-Acquire instructions on ARM64.
5171 const bool use_load_acquire = false;
5172
Roland Levillainbfea3352016-06-23 13:48:47 +01005173 static_assert(
5174 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5175 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005176 // /* HeapReference<Object> */ ref =
5177 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005178 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5179 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5180 ref,
5181 obj,
5182 data_offset,
5183 index,
5184 scale_factor,
5185 temp,
5186 needs_null_check,
5187 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005188}
5189
5190void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5191 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005192 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005193 uint32_t offset,
5194 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005195 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005196 Register temp,
5197 bool needs_null_check,
5198 bool use_load_acquire) {
5199 DCHECK(kEmitCompilerReadBarrier);
5200 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005201 // If we are emitting an array load, we should not be using a
5202 // Load Acquire instruction. In other words:
5203 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5204 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005205
5206 MacroAssembler* masm = GetVIXLAssembler();
5207 UseScratchRegisterScope temps(masm);
5208
5209 // In slow path based read barriers, the read barrier call is
5210 // inserted after the original load. However, in fast path based
5211 // Baker's read barriers, we need to perform the load of
5212 // mirror::Object::monitor_ *before* the original reference load.
5213 // This load-load ordering is required by the read barrier.
5214 // The fast path/slow path (for Baker's algorithm) should look like:
5215 //
5216 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5217 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5218 // HeapReference<Object> ref = *src; // Original reference load.
5219 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5220 // if (is_gray) {
5221 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5222 // }
5223 //
5224 // Note: the original implementation in ReadBarrier::Barrier is
5225 // slightly more complex as it performs additional checks that we do
5226 // not do here for performance reasons.
5227
5228 Primitive::Type type = Primitive::kPrimNot;
5229 Register ref_reg = RegisterFrom(ref, type);
5230 DCHECK(obj.IsW());
5231 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5232
5233 // /* int32_t */ monitor = obj->monitor_
5234 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5235 if (needs_null_check) {
5236 MaybeRecordImplicitNullCheck(instruction);
5237 }
5238 // /* LockWord */ lock_word = LockWord(monitor)
5239 static_assert(sizeof(LockWord) == sizeof(int32_t),
5240 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005241
Vladimir Marko877a0332016-07-11 19:30:56 +01005242 // Introduce a dependency on the lock_word including rb_state,
5243 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005244 // a memory barrier (which would be more expensive).
Vladimir Marko877a0332016-07-11 19:30:56 +01005245 // obj is unchanged by this operation, but its value now depends on temp.
5246 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005247
5248 // The actual reference load.
5249 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005250 // Load types involving an "index".
5251 if (use_load_acquire) {
5252 // UnsafeGetObjectVolatile intrinsic case.
5253 // Register `index` is not an index in an object array, but an
5254 // offset to an object reference field within object `obj`.
5255 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5256 DCHECK(instruction->GetLocations()->Intrinsified());
5257 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5258 << instruction->AsInvoke()->GetIntrinsic();
5259 DCHECK_EQ(offset, 0U);
5260 DCHECK_EQ(scale_factor, 0U);
5261 DCHECK_EQ(needs_null_check, 0U);
5262 // /* HeapReference<Object> */ ref = *(obj + index)
5263 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5264 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005265 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005266 // ArrayGet and UnsafeGetObject intrinsics cases.
5267 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5268 if (index.IsConstant()) {
5269 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5270 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5271 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005272 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005273 __ Add(temp2, obj, offset);
5274 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5275 temps.Release(temp2);
5276 }
Roland Levillain44015862016-01-22 11:47:17 +00005277 }
Roland Levillain44015862016-01-22 11:47:17 +00005278 } else {
5279 // /* HeapReference<Object> */ ref = *(obj + offset)
5280 MemOperand field = HeapOperand(obj, offset);
5281 if (use_load_acquire) {
5282 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5283 } else {
5284 Load(type, ref_reg, field);
5285 }
5286 }
5287
5288 // Object* ref = ref_addr->AsMirrorPtr()
5289 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5290
5291 // Slow path used to mark the object `ref` when it is gray.
5292 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005293 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005294 AddSlowPath(slow_path);
5295
5296 // if (rb_state == ReadBarrier::gray_ptr_)
5297 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005298 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5299 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5300 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5301 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5302 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005303 __ Bind(slow_path->GetExitLabel());
5304}
5305
5306void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5307 Location out,
5308 Location ref,
5309 Location obj,
5310 uint32_t offset,
5311 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005312 DCHECK(kEmitCompilerReadBarrier);
5313
Roland Levillain44015862016-01-22 11:47:17 +00005314 // Insert a slow path based read barrier *after* the reference load.
5315 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005316 // If heap poisoning is enabled, the unpoisoning of the loaded
5317 // reference will be carried out by the runtime within the slow
5318 // path.
5319 //
5320 // Note that `ref` currently does not get unpoisoned (when heap
5321 // poisoning is enabled), which is alright as the `ref` argument is
5322 // not used by the artReadBarrierSlow entry point.
5323 //
5324 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5325 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5326 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5327 AddSlowPath(slow_path);
5328
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005329 __ B(slow_path->GetEntryLabel());
5330 __ Bind(slow_path->GetExitLabel());
5331}
5332
Roland Levillain44015862016-01-22 11:47:17 +00005333void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5334 Location out,
5335 Location ref,
5336 Location obj,
5337 uint32_t offset,
5338 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005339 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005340 // Baker's read barriers shall be handled by the fast path
5341 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5342 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005343 // If heap poisoning is enabled, unpoisoning will be taken care of
5344 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005345 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005346 } else if (kPoisonHeapReferences) {
5347 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5348 }
5349}
5350
Roland Levillain44015862016-01-22 11:47:17 +00005351void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5352 Location out,
5353 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005354 DCHECK(kEmitCompilerReadBarrier);
5355
Roland Levillain44015862016-01-22 11:47:17 +00005356 // Insert a slow path based read barrier *after* the GC root load.
5357 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005358 // Note that GC roots are not affected by heap poisoning, so we do
5359 // not need to do anything special for this here.
5360 SlowPathCodeARM64* slow_path =
5361 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5362 AddSlowPath(slow_path);
5363
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005364 __ B(slow_path->GetEntryLabel());
5365 __ Bind(slow_path->GetExitLabel());
5366}
5367
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005368void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5369 LocationSummary* locations =
5370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5371 locations->SetInAt(0, Location::RequiresRegister());
5372 locations->SetOut(Location::RequiresRegister());
5373}
5374
5375void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5376 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005377 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005378 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005379 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005380 __ Ldr(XRegisterFrom(locations->Out()),
5381 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005382 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005383 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005384 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005385 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5386 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005387 __ Ldr(XRegisterFrom(locations->Out()),
5388 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005389 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005390}
5391
5392
5393
Alexandre Rames67555f72014-11-18 10:55:16 +00005394#undef __
5395#undef QUICK_ENTRY_POINT
5396
Alexandre Rames5319def2014-10-23 10:03:10 +01005397} // namespace arm64
5398} // namespace art