blob: cc8985d0b0b029a554378498f71df268ac219f80 [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
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100134// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) 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());
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000402 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800403 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000404 if (successor_ == nullptr) {
405 __ B(GetReturnLabel());
406 } else {
407 __ B(arm64_codegen->GetLabelOf(successor_));
408 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 }
410
Scott Wakeling97c72b72016-06-24 16:19:36 +0100411 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100412 DCHECK(successor_ == nullptr);
413 return &return_label_;
414 }
415
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100416 HBasicBlock* GetSuccessor() const {
417 return successor_;
418 }
419
Alexandre Rames9931f312015-06-19 14:47:01 +0100420 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
421
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100423 // If not null, the block to branch to after the suspend check.
424 HBasicBlock* const successor_;
425
426 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100427 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100428
429 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
430};
431
Alexandre Rames67555f72014-11-18 10:55:16 +0000432class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
433 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000434 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000435 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000436
437 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100439 Location class_to_check = locations->InAt(1);
440 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
441 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442 DCHECK(instruction_->IsCheckCast()
443 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
444 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000449 if (!is_fatal_) {
450 SaveLiveRegisters(codegen, locations);
451 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452
453 // We're moving two locations to locations that could overlap, so we need a parallel
454 // move resolver.
455 InvokeRuntimeCallingConvention calling_convention;
456 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100457 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
458 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000459
460 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000461 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100462 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700463 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000464 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 Primitive::Type ret_type = instruction_->GetType();
466 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
467 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
468 } else {
469 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100470 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800471 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 }
473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000474 if (!is_fatal_) {
475 RestoreLiveRegisters(codegen, locations);
476 __ B(GetExitLabel());
477 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000478 }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100482
Alexandre Rames67555f72014-11-18 10:55:16 +0000483 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
487};
488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700489class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
490 public:
Aart Bik42249c32016-01-07 15:33:50 -0800491 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000492 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493
494 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 __ Bind(GetEntryLabel());
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800498 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
499 instruction_,
500 instruction_->GetDexPc(),
501 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000502 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 }
504
Alexandre Rames9931f312015-06-19 14:47:01 +0100505 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
506
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700507 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700508 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
509};
510
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100511class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
512 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000513 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
516 LocationSummary* locations = instruction_->GetLocations();
517 __ Bind(GetEntryLabel());
518 SaveLiveRegisters(codegen, locations);
519
520 InvokeRuntimeCallingConvention calling_convention;
521 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
522 parallel_move.AddMove(
523 locations->InAt(0),
524 LocationFrom(calling_convention.GetRegisterAt(0)),
525 Primitive::kPrimNot,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(1),
529 LocationFrom(calling_convention.GetRegisterAt(1)),
530 Primitive::kPrimInt,
531 nullptr);
532 parallel_move.AddMove(
533 locations->InAt(2),
534 LocationFrom(calling_convention.GetRegisterAt(2)),
535 Primitive::kPrimNot,
536 nullptr);
537 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
538
539 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
540 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
541 instruction_,
542 instruction_->GetDexPc(),
543 this);
544 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
545 RestoreLiveRegisters(codegen, locations);
546 __ B(GetExitLabel());
547 }
548
549 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
550
551 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100552 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
553};
554
Zheng Xu3927c8b2015-11-18 17:46:25 +0800555void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
556 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000557 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800558
559 // We are about to use the assembler to place literals directly. Make sure we have enough
560 // underlying code buffer and we have generated the jump table with right size.
561 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
562 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
563
564 __ Bind(&table_start_);
565 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
566 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100567 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800568 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100569 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800570 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
571 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
572 Literal<int32_t> literal(jump_offset);
573 __ place(&literal);
574 }
575}
576
Roland Levillain44015862016-01-22 11:47:17 +0000577// Slow path marking an object during a read barrier.
578class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
579 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100580 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
581 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000582 DCHECK(kEmitCompilerReadBarrier);
583 }
584
585 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
588 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000589 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100590 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000591 DCHECK(instruction_->IsInstanceFieldGet() ||
592 instruction_->IsStaticFieldGet() ||
593 instruction_->IsArrayGet() ||
594 instruction_->IsLoadClass() ||
595 instruction_->IsLoadString() ||
596 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100597 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100598 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
599 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000600 << "Unexpected instruction in read barrier marking slow path: "
601 << instruction_->DebugName();
602
603 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100604 // No need to save live registers; it's taken care of by the
605 // entrypoint. Also, there is no need to update the stack mask,
606 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000607 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100608 DCHECK_NE(obj_.reg(), LR);
609 DCHECK_NE(obj_.reg(), WSP);
610 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100611 // IP0 is used internally by the ReadBarrierMarkRegX entry point
612 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700613 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100614 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
615 // "Compact" slow path, saving two moves.
616 //
617 // Instead of using the standard runtime calling convention (input
618 // and output in W0):
619 //
620 // W0 <- obj
621 // W0 <- ReadBarrierMark(W0)
622 // obj <- W0
623 //
624 // we just use rX (the register holding `obj`) as input and output
625 // of a dedicated entrypoint:
626 //
627 // rX <- ReadBarrierMarkRegX(rX)
628 //
629 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700630 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100631 // This runtime call does not require a stack map.
632 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000633 __ B(GetExitLabel());
634 }
635
636 private:
Roland Levillain44015862016-01-22 11:47:17 +0000637 const Location obj_;
638
639 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
640};
641
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000642// Slow path generating a read barrier for a heap reference.
643class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
644 public:
645 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
646 Location out,
647 Location ref,
648 Location obj,
649 uint32_t offset,
650 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000651 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000652 out_(out),
653 ref_(ref),
654 obj_(obj),
655 offset_(offset),
656 index_(index) {
657 DCHECK(kEmitCompilerReadBarrier);
658 // If `obj` is equal to `out` or `ref`, it means the initial object
659 // has been overwritten by (or after) the heap object reference load
660 // to be instrumented, e.g.:
661 //
662 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000663 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000664 //
665 // In that case, we have lost the information about the original
666 // object, and the emitted read barrier cannot work properly.
667 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
668 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
669 }
670
671 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
672 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
673 LocationSummary* locations = instruction_->GetLocations();
674 Primitive::Type type = Primitive::kPrimNot;
675 DCHECK(locations->CanCall());
676 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100677 DCHECK(instruction_->IsInstanceFieldGet() ||
678 instruction_->IsStaticFieldGet() ||
679 instruction_->IsArrayGet() ||
680 instruction_->IsInstanceOf() ||
681 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100682 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000683 << "Unexpected instruction in read barrier for heap reference slow path: "
684 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000685 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000686 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100687 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000688
689 __ Bind(GetEntryLabel());
690
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000691 SaveLiveRegisters(codegen, locations);
692
693 // We may have to change the index's value, but as `index_` is a
694 // constant member (like other "inputs" of this slow path),
695 // introduce a copy of it, `index`.
696 Location index = index_;
697 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100698 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000699 if (instruction_->IsArrayGet()) {
700 // Compute the actual memory offset and store it in `index`.
701 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
702 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
703 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
704 // We are about to change the value of `index_reg` (see the
705 // calls to vixl::MacroAssembler::Lsl and
706 // vixl::MacroAssembler::Mov below), but it has
707 // not been saved by the previous call to
708 // art::SlowPathCode::SaveLiveRegisters, as it is a
709 // callee-save register --
710 // art::SlowPathCode::SaveLiveRegisters does not consider
711 // callee-save registers, as it has been designed with the
712 // assumption that callee-save registers are supposed to be
713 // handled by the called function. So, as a callee-save
714 // register, `index_reg` _would_ eventually be saved onto
715 // the stack, but it would be too late: we would have
716 // changed its value earlier. Therefore, we manually save
717 // it here into another freely available register,
718 // `free_reg`, chosen of course among the caller-save
719 // registers (as a callee-save `free_reg` register would
720 // exhibit the same problem).
721 //
722 // Note we could have requested a temporary register from
723 // the register allocator instead; but we prefer not to, as
724 // this is a slow path, and we know we can find a
725 // caller-save register that is available.
726 Register free_reg = FindAvailableCallerSaveRegister(codegen);
727 __ Mov(free_reg.W(), index_reg);
728 index_reg = free_reg;
729 index = LocationFrom(index_reg);
730 } else {
731 // The initial register stored in `index_` has already been
732 // saved in the call to art::SlowPathCode::SaveLiveRegisters
733 // (as it is not a callee-save register), so we can freely
734 // use it.
735 }
736 // Shifting the index value contained in `index_reg` by the scale
737 // factor (2) cannot overflow in practice, as the runtime is
738 // unable to allocate object arrays with a size larger than
739 // 2^26 - 1 (that is, 2^28 - 4 bytes).
740 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
741 static_assert(
742 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
743 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
744 __ Add(index_reg, index_reg, Operand(offset_));
745 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100746 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
747 // intrinsics, `index_` is not shifted by a scale factor of 2
748 // (as in the case of ArrayGet), as it is actually an offset
749 // to an object field within an object.
750 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000751 DCHECK(instruction_->GetLocations()->Intrinsified());
752 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
753 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
754 << instruction_->AsInvoke()->GetIntrinsic();
755 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100756 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000757 }
758 }
759
760 // We're moving two or three locations to locations that could
761 // overlap, so we need a parallel move resolver.
762 InvokeRuntimeCallingConvention calling_convention;
763 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
764 parallel_move.AddMove(ref_,
765 LocationFrom(calling_convention.GetRegisterAt(0)),
766 type,
767 nullptr);
768 parallel_move.AddMove(obj_,
769 LocationFrom(calling_convention.GetRegisterAt(1)),
770 type,
771 nullptr);
772 if (index.IsValid()) {
773 parallel_move.AddMove(index,
774 LocationFrom(calling_convention.GetRegisterAt(2)),
775 Primitive::kPrimInt,
776 nullptr);
777 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
778 } else {
779 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
780 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
781 }
782 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
783 instruction_,
784 instruction_->GetDexPc(),
785 this);
786 CheckEntrypointTypes<
787 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
788 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
789
790 RestoreLiveRegisters(codegen, locations);
791
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000792 __ B(GetExitLabel());
793 }
794
795 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
796
797 private:
798 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100799 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
800 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000801 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
802 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
803 return Register(VIXLRegCodeFromART(i), kXRegSize);
804 }
805 }
806 // We shall never fail to find a free caller-save register, as
807 // there are more than two core caller-save registers on ARM64
808 // (meaning it is possible to find one which is different from
809 // `ref` and `obj`).
810 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
811 LOG(FATAL) << "Could not find a free register";
812 UNREACHABLE();
813 }
814
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000815 const Location out_;
816 const Location ref_;
817 const Location obj_;
818 const uint32_t offset_;
819 // An additional location containing an index to an array.
820 // Only used for HArrayGet and the UnsafeGetObject &
821 // UnsafeGetObjectVolatile intrinsics.
822 const Location index_;
823
824 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
825};
826
827// Slow path generating a read barrier for a GC root.
828class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
829 public:
830 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000831 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000832 DCHECK(kEmitCompilerReadBarrier);
833 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000834
835 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
836 LocationSummary* locations = instruction_->GetLocations();
837 Primitive::Type type = Primitive::kPrimNot;
838 DCHECK(locations->CanCall());
839 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000840 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
841 << "Unexpected instruction in read barrier for GC root slow path: "
842 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000843
844 __ Bind(GetEntryLabel());
845 SaveLiveRegisters(codegen, locations);
846
847 InvokeRuntimeCallingConvention calling_convention;
848 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
849 // The argument of the ReadBarrierForRootSlow is not a managed
850 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
851 // thus we need a 64-bit move here, and we cannot use
852 //
853 // arm64_codegen->MoveLocation(
854 // LocationFrom(calling_convention.GetRegisterAt(0)),
855 // root_,
856 // type);
857 //
858 // which would emit a 32-bit move, as `type` is a (32-bit wide)
859 // reference type (`Primitive::kPrimNot`).
860 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
861 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
862 instruction_,
863 instruction_->GetDexPc(),
864 this);
865 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
866 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
867
868 RestoreLiveRegisters(codegen, locations);
869 __ B(GetExitLabel());
870 }
871
872 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
873
874 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000875 const Location out_;
876 const Location root_;
877
878 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
879};
880
Alexandre Rames5319def2014-10-23 10:03:10 +0100881#undef __
882
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100883Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100884 Location next_location;
885 if (type == Primitive::kPrimVoid) {
886 LOG(FATAL) << "Unreachable type " << type;
887 }
888
Alexandre Rames542361f2015-01-29 16:57:31 +0000889 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100890 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
891 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000892 } else if (!Primitive::IsFloatingPointType(type) &&
893 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000894 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
895 } else {
896 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000897 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
898 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100899 }
900
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000901 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000902 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100903 return next_location;
904}
905
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100906Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100907 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100908}
909
Serban Constantinescu579885a2015-02-22 20:51:33 +0000910CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
911 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100912 const CompilerOptions& compiler_options,
913 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100914 : CodeGenerator(graph,
915 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000916 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000917 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100918 callee_saved_core_registers.GetList(),
919 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100920 compiler_options,
921 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100922 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800923 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100924 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000925 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000926 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100927 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000928 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000929 uint32_literals_(std::less<uint32_t>(),
930 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100931 uint64_literals_(std::less<uint64_t>(),
932 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
933 method_patches_(MethodReferenceComparator(),
934 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
935 call_patches_(MethodReferenceComparator(),
936 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
937 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000938 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
939 boot_image_string_patches_(StringReferenceValueComparator(),
940 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
941 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100942 boot_image_type_patches_(TypeReferenceValueComparator(),
943 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
944 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000945 boot_image_address_patches_(std::less<uint32_t>(),
946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000947 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000948 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000949}
Alexandre Rames5319def2014-10-23 10:03:10 +0100950
Alexandre Rames67555f72014-11-18 10:55:16 +0000951#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100952
Zheng Xu3927c8b2015-11-18 17:46:25 +0800953void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100954 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800955 jump_table->EmitTable(this);
956 }
957}
958
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000959void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800960 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000961 // Ensure we emit the literal pool.
962 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000963
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000964 CodeGenerator::Finalize(allocator);
965}
966
Zheng Xuad4450e2015-04-17 18:48:56 +0800967void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
968 // Note: There are 6 kinds of moves:
969 // 1. constant -> GPR/FPR (non-cycle)
970 // 2. constant -> stack (non-cycle)
971 // 3. GPR/FPR -> GPR/FPR
972 // 4. GPR/FPR -> stack
973 // 5. stack -> GPR/FPR
974 // 6. stack -> stack (non-cycle)
975 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
976 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
977 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
978 // dependency.
979 vixl_temps_.Open(GetVIXLAssembler());
980}
981
982void ParallelMoveResolverARM64::FinishEmitNativeCode() {
983 vixl_temps_.Close();
984}
985
986Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
987 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
988 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
989 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
990 Location scratch = GetScratchLocation(kind);
991 if (!scratch.Equals(Location::NoLocation())) {
992 return scratch;
993 }
994 // Allocate from VIXL temp registers.
995 if (kind == Location::kRegister) {
996 scratch = LocationFrom(vixl_temps_.AcquireX());
997 } else {
998 DCHECK(kind == Location::kFpuRegister);
999 scratch = LocationFrom(vixl_temps_.AcquireD());
1000 }
1001 AddScratchLocation(scratch);
1002 return scratch;
1003}
1004
1005void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1006 if (loc.IsRegister()) {
1007 vixl_temps_.Release(XRegisterFrom(loc));
1008 } else {
1009 DCHECK(loc.IsFpuRegister());
1010 vixl_temps_.Release(DRegisterFrom(loc));
1011 }
1012 RemoveScratchLocation(loc);
1013}
1014
Alexandre Rames3e69f162014-12-10 10:36:50 +00001015void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001016 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001017 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001018}
1019
Alexandre Rames5319def2014-10-23 10:03:10 +01001020void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001021 MacroAssembler* masm = GetVIXLAssembler();
1022 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001023 __ Bind(&frame_entry_label_);
1024
Serban Constantinescu02164b32014-11-13 14:05:07 +00001025 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1026 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001027 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001028 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001029 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001030 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001031 __ Ldr(wzr, MemOperand(temp, 0));
1032 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001033 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001034
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001035 if (!HasEmptyFrame()) {
1036 int frame_size = GetFrameSize();
1037 // Stack layout:
1038 // sp[frame_size - 8] : lr.
1039 // ... : other preserved core registers.
1040 // ... : other preserved fp registers.
1041 // ... : reserved frame space.
1042 // sp[0] : current method.
1043 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001044 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001045 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1046 frame_size - GetCoreSpillSize());
1047 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1048 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001049 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001050}
1051
1052void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001053 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001054 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001055 if (!HasEmptyFrame()) {
1056 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001057 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1058 frame_size - FrameEntrySpillSize());
1059 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1060 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001061 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001063 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001064 __ Ret();
1065 GetAssembler()->cfi().RestoreState();
1066 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001067}
1068
Scott Wakeling97c72b72016-06-24 16:19:36 +01001069CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001070 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001071 return CPURegList(CPURegister::kRegister, kXRegSize,
1072 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001073}
1074
Scott Wakeling97c72b72016-06-24 16:19:36 +01001075CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001076 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1077 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001078 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1079 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001080}
1081
Alexandre Rames5319def2014-10-23 10:03:10 +01001082void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1083 __ Bind(GetLabelOf(block));
1084}
1085
Calin Juravle175dc732015-08-25 15:42:32 +01001086void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1087 DCHECK(location.IsRegister());
1088 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1089}
1090
Calin Juravlee460d1d2015-09-29 04:52:17 +01001091void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1092 if (location.IsRegister()) {
1093 locations->AddTemp(location);
1094 } else {
1095 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1096 }
1097}
1098
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001099void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001100 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001102 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001103 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001104 if (value_can_be_null) {
1105 __ Cbz(value, &done);
1106 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001107 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001108 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001109 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001110 if (value_can_be_null) {
1111 __ Bind(&done);
1112 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001113}
1114
David Brazdil58282f42016-01-14 12:45:10 +00001115void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001116 // Blocked core registers:
1117 // lr : Runtime reserved.
1118 // tr : Runtime reserved.
1119 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1120 // ip1 : VIXL core temp.
1121 // ip0 : VIXL core temp.
1122 //
1123 // Blocked fp registers:
1124 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001125 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1126 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001127 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001128 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001129 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001130
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001132 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001133 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001134 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001135
David Brazdil58282f42016-01-14 12:45:10 +00001136 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001137 // Stubs do not save callee-save floating point registers. If the graph
1138 // is debuggable, we need to deal with these registers differently. For
1139 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001140 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1141 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001142 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001143 }
1144 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001145}
1146
Alexandre Rames3e69f162014-12-10 10:36:50 +00001147size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1148 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1149 __ Str(reg, MemOperand(sp, stack_index));
1150 return kArm64WordSize;
1151}
1152
1153size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1155 __ Ldr(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 FPRegister reg = FPRegister(reg_id, kDRegSize);
1161 __ Str(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
1165size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1166 FPRegister reg = FPRegister(reg_id, kDRegSize);
1167 __ Ldr(reg, MemOperand(sp, stack_index));
1168 return kArm64WordSize;
1169}
1170
Alexandre Rames5319def2014-10-23 10:03:10 +01001171void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001172 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001173}
1174
1175void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001176 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001177}
1178
Alexandre Rames67555f72014-11-18 10:55:16 +00001179void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001180 if (constant->IsIntConstant()) {
1181 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1182 } else if (constant->IsLongConstant()) {
1183 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1184 } else if (constant->IsNullConstant()) {
1185 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001186 } else if (constant->IsFloatConstant()) {
1187 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1188 } else {
1189 DCHECK(constant->IsDoubleConstant());
1190 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1191 }
1192}
1193
Alexandre Rames3e69f162014-12-10 10:36:50 +00001194
1195static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1196 DCHECK(constant.IsConstant());
1197 HConstant* cst = constant.GetConstant();
1198 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001199 // Null is mapped to a core W register, which we associate with kPrimInt.
1200 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001201 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1202 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1203 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1204}
1205
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206void CodeGeneratorARM64::MoveLocation(Location destination,
1207 Location source,
1208 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001209 if (source.Equals(destination)) {
1210 return;
1211 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212
1213 // A valid move can always be inferred from the destination and source
1214 // locations. When moving from and to a register, the argument type can be
1215 // used to generate 32bit instead of 64bit moves. In debug mode we also
1216 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001218
1219 if (destination.IsRegister() || destination.IsFpuRegister()) {
1220 if (unspecified_type) {
1221 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1222 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001223 (src_cst != nullptr && (src_cst->IsIntConstant()
1224 || src_cst->IsFloatConstant()
1225 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001226 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 // If the source is a double stack slot or a 64bit constant, a 64bit
1230 // type is appropriate. Else the source is a register, and since the
1231 // type has not been specified, we chose a 64bit type to force a 64bit
1232 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001235 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1237 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1238 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001239 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1240 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1241 __ Ldr(dst, StackOperandFrom(source));
1242 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001243 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001244 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001246 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001248 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001249 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1251 ? Primitive::kPrimLong
1252 : Primitive::kPrimInt;
1253 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1254 }
1255 } else {
1256 DCHECK(source.IsFpuRegister());
1257 if (destination.IsRegister()) {
1258 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1259 ? Primitive::kPrimDouble
1260 : Primitive::kPrimFloat;
1261 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1262 } else {
1263 DCHECK(destination.IsFpuRegister());
1264 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001265 }
1266 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 } else { // The destination is not a register. It must be a stack slot.
1268 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1269 if (source.IsRegister() || source.IsFpuRegister()) {
1270 if (unspecified_type) {
1271 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001273 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001275 }
1276 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1278 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1279 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1282 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001283 UseScratchRegisterScope temps(GetVIXLAssembler());
1284 HConstant* src_cst = source.GetConstant();
1285 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001286 if (src_cst->IsZeroBitPattern()) {
1287 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001288 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001289 if (src_cst->IsIntConstant()) {
1290 temp = temps.AcquireW();
1291 } else if (src_cst->IsLongConstant()) {
1292 temp = temps.AcquireX();
1293 } else if (src_cst->IsFloatConstant()) {
1294 temp = temps.AcquireS();
1295 } else {
1296 DCHECK(src_cst->IsDoubleConstant());
1297 temp = temps.AcquireD();
1298 }
1299 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001300 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001301 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001302 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001303 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001304 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001305 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001306 // There is generally less pressure on FP registers.
1307 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001308 __ Ldr(temp, StackOperandFrom(source));
1309 __ Str(temp, StackOperandFrom(destination));
1310 }
1311 }
1312}
1313
1314void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001315 CPURegister dst,
1316 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001317 switch (type) {
1318 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001320 break;
1321 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 break;
1324 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001325 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001326 break;
1327 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001328 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001329 break;
1330 case Primitive::kPrimInt:
1331 case Primitive::kPrimNot:
1332 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001333 case Primitive::kPrimFloat:
1334 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001335 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001336 __ Ldr(dst, src);
1337 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001338 case Primitive::kPrimVoid:
1339 LOG(FATAL) << "Unreachable type " << type;
1340 }
1341}
1342
Calin Juravle77520bc2015-01-12 18:45:46 +00001343void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001344 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001345 const MemOperand& src,
1346 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001347 MacroAssembler* masm = GetVIXLAssembler();
1348 BlockPoolsScope block_pools(masm);
1349 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001350 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001351 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001352
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001353 DCHECK(!src.IsPreIndex());
1354 DCHECK(!src.IsPostIndex());
1355
1356 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001357 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 MemOperand base = MemOperand(temp_base);
1359 switch (type) {
1360 case Primitive::kPrimBoolean:
1361 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001362 if (needs_null_check) {
1363 MaybeRecordImplicitNullCheck(instruction);
1364 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001365 break;
1366 case Primitive::kPrimByte:
1367 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001368 if (needs_null_check) {
1369 MaybeRecordImplicitNullCheck(instruction);
1370 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001371 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1372 break;
1373 case Primitive::kPrimChar:
1374 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001375 if (needs_null_check) {
1376 MaybeRecordImplicitNullCheck(instruction);
1377 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001378 break;
1379 case Primitive::kPrimShort:
1380 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001381 if (needs_null_check) {
1382 MaybeRecordImplicitNullCheck(instruction);
1383 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1385 break;
1386 case Primitive::kPrimInt:
1387 case Primitive::kPrimNot:
1388 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001389 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001390 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001391 if (needs_null_check) {
1392 MaybeRecordImplicitNullCheck(instruction);
1393 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001394 break;
1395 case Primitive::kPrimFloat:
1396 case Primitive::kPrimDouble: {
1397 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001398 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001399
1400 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1401 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001402 if (needs_null_check) {
1403 MaybeRecordImplicitNullCheck(instruction);
1404 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001405 __ Fmov(FPRegister(dst), temp);
1406 break;
1407 }
1408 case Primitive::kPrimVoid:
1409 LOG(FATAL) << "Unreachable type " << type;
1410 }
1411}
1412
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001413void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001414 CPURegister src,
1415 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001416 switch (type) {
1417 case Primitive::kPrimBoolean:
1418 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001419 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001420 break;
1421 case Primitive::kPrimChar:
1422 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001423 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001424 break;
1425 case Primitive::kPrimInt:
1426 case Primitive::kPrimNot:
1427 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001428 case Primitive::kPrimFloat:
1429 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001430 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001431 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001432 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001433 case Primitive::kPrimVoid:
1434 LOG(FATAL) << "Unreachable type " << type;
1435 }
1436}
1437
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001438void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1439 CPURegister src,
1440 const MemOperand& dst) {
1441 UseScratchRegisterScope temps(GetVIXLAssembler());
1442 Register temp_base = temps.AcquireX();
1443
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444 DCHECK(!dst.IsPreIndex());
1445 DCHECK(!dst.IsPostIndex());
1446
1447 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001448 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001449 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001450 MemOperand base = MemOperand(temp_base);
1451 switch (type) {
1452 case Primitive::kPrimBoolean:
1453 case Primitive::kPrimByte:
1454 __ Stlrb(Register(src), base);
1455 break;
1456 case Primitive::kPrimChar:
1457 case Primitive::kPrimShort:
1458 __ Stlrh(Register(src), base);
1459 break;
1460 case Primitive::kPrimInt:
1461 case Primitive::kPrimNot:
1462 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001463 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001464 __ Stlr(Register(src), base);
1465 break;
1466 case Primitive::kPrimFloat:
1467 case Primitive::kPrimDouble: {
1468 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001469 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001470
1471 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1472 __ Fmov(temp, FPRegister(src));
1473 __ Stlr(temp, base);
1474 break;
1475 }
1476 case Primitive::kPrimVoid:
1477 LOG(FATAL) << "Unreachable type " << type;
1478 }
1479}
1480
Calin Juravle175dc732015-08-25 15:42:32 +01001481void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1482 HInstruction* instruction,
1483 uint32_t dex_pc,
1484 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001485 InvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001486 instruction,
1487 dex_pc,
1488 slow_path);
1489}
1490
Alexandre Rames67555f72014-11-18 10:55:16 +00001491void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1492 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001493 uint32_t dex_pc,
1494 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001495 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001496 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001497 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1498 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001499 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001500}
1501
Roland Levillaindec8f632016-07-22 17:10:06 +01001502void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1503 HInstruction* instruction,
1504 SlowPathCode* slow_path) {
1505 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1506 BlockPoolsScope block_pools(GetVIXLAssembler());
1507 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1508 __ Blr(lr);
1509}
1510
Alexandre Rames67555f72014-11-18 10:55:16 +00001511void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001512 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001513 UseScratchRegisterScope temps(GetVIXLAssembler());
1514 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001515 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1516
Serban Constantinescu02164b32014-11-13 14:05:07 +00001517 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001518 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1519 __ Add(temp, class_reg, status_offset);
1520 __ Ldar(temp, HeapOperand(temp));
1521 __ Cmp(temp, mirror::Class::kStatusInitialized);
1522 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001523 __ Bind(slow_path->GetExitLabel());
1524}
Alexandre Rames5319def2014-10-23 10:03:10 +01001525
Roland Levillain44015862016-01-22 11:47:17 +00001526void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001527 BarrierType type = BarrierAll;
1528
1529 switch (kind) {
1530 case MemBarrierKind::kAnyAny:
1531 case MemBarrierKind::kAnyStore: {
1532 type = BarrierAll;
1533 break;
1534 }
1535 case MemBarrierKind::kLoadAny: {
1536 type = BarrierReads;
1537 break;
1538 }
1539 case MemBarrierKind::kStoreStore: {
1540 type = BarrierWrites;
1541 break;
1542 }
1543 default:
1544 LOG(FATAL) << "Unexpected memory barrier " << kind;
1545 }
1546 __ Dmb(InnerShareable, type);
1547}
1548
Serban Constantinescu02164b32014-11-13 14:05:07 +00001549void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1550 HBasicBlock* successor) {
1551 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001552 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1553 if (slow_path == nullptr) {
1554 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1555 instruction->SetSlowPath(slow_path);
1556 codegen_->AddSlowPath(slow_path);
1557 if (successor != nullptr) {
1558 DCHECK(successor->IsLoopHeader());
1559 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1560 }
1561 } else {
1562 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1563 }
1564
Serban Constantinescu02164b32014-11-13 14:05:07 +00001565 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1566 Register temp = temps.AcquireW();
1567
Andreas Gampe542451c2016-07-26 09:02:02 -07001568 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001569 if (successor == nullptr) {
1570 __ Cbnz(temp, slow_path->GetEntryLabel());
1571 __ Bind(slow_path->GetReturnLabel());
1572 } else {
1573 __ Cbz(temp, codegen_->GetLabelOf(successor));
1574 __ B(slow_path->GetEntryLabel());
1575 // slow_path will return to GetLabelOf(successor).
1576 }
1577}
1578
Alexandre Rames5319def2014-10-23 10:03:10 +01001579InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1580 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001581 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001582 assembler_(codegen->GetAssembler()),
1583 codegen_(codegen) {}
1584
1585#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001586 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001587
1588#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1589
1590enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001591 // Using a base helps identify when we hit such breakpoints.
1592 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001593#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1594 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1595#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1596};
1597
1598#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001599 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1601 } \
1602 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1604 locations->SetOut(Location::Any()); \
1605 }
1606 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1607#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1608
1609#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001610#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001611
Alexandre Rames67555f72014-11-18 10:55:16 +00001612void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001613 DCHECK_EQ(instr->InputCount(), 2U);
1614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1615 Primitive::Type type = instr->GetResultType();
1616 switch (type) {
1617 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001618 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001619 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001620 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001622 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001623
1624 case Primitive::kPrimFloat:
1625 case Primitive::kPrimDouble:
1626 locations->SetInAt(0, Location::RequiresFpuRegister());
1627 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001629 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001630
Alexandre Rames5319def2014-10-23 10:03:10 +01001631 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001632 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001633 }
1634}
1635
Alexandre Rames09a99962015-04-15 11:47:56 +01001636void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001637 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1638
1639 bool object_field_get_with_read_barrier =
1640 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001641 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001642 new (GetGraph()->GetArena()) LocationSummary(instruction,
1643 object_field_get_with_read_barrier ?
1644 LocationSummary::kCallOnSlowPath :
1645 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001646 locations->SetInAt(0, Location::RequiresRegister());
1647 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1648 locations->SetOut(Location::RequiresFpuRegister());
1649 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001650 // The output overlaps for an object field get when read barriers
1651 // are enabled: we do not want the load to overwrite the object's
1652 // location, as we need it to emit the read barrier.
1653 locations->SetOut(
1654 Location::RequiresRegister(),
1655 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001656 }
1657}
1658
1659void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1660 const FieldInfo& field_info) {
1661 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001662 LocationSummary* locations = instruction->GetLocations();
1663 Location base_loc = locations->InAt(0);
1664 Location out = locations->Out();
1665 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001666 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001667 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001668 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001669
Roland Levillain44015862016-01-22 11:47:17 +00001670 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1671 // Object FieldGet with Baker's read barrier case.
1672 MacroAssembler* masm = GetVIXLAssembler();
1673 UseScratchRegisterScope temps(masm);
1674 // /* HeapReference<Object> */ out = *(base + offset)
1675 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1676 Register temp = temps.AcquireW();
1677 // Note that potential implicit null checks are handled in this
1678 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1679 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1680 instruction,
1681 out,
1682 base,
1683 offset,
1684 temp,
1685 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001686 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001687 } else {
1688 // General case.
1689 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001690 // Note that a potential implicit null check is handled in this
1691 // CodeGeneratorARM64::LoadAcquire call.
1692 // NB: LoadAcquire will record the pc info if needed.
1693 codegen_->LoadAcquire(
1694 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001695 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001696 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001697 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001698 }
Roland Levillain44015862016-01-22 11:47:17 +00001699 if (field_type == Primitive::kPrimNot) {
1700 // If read barriers are enabled, emit read barriers other than
1701 // Baker's using a slow path (and also unpoison the loaded
1702 // reference, if heap poisoning is enabled).
1703 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1704 }
Roland Levillain4d027112015-07-01 15:41:14 +01001705 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001706}
1707
1708void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1709 LocationSummary* locations =
1710 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1711 locations->SetInAt(0, Location::RequiresRegister());
1712 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1713 locations->SetInAt(1, Location::RequiresFpuRegister());
1714 } else {
1715 locations->SetInAt(1, Location::RequiresRegister());
1716 }
1717}
1718
1719void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001720 const FieldInfo& field_info,
1721 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001722 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001723 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001724
1725 Register obj = InputRegisterAt(instruction, 0);
1726 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001727 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001728 Offset offset = field_info.GetFieldOffset();
1729 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001730
Roland Levillain4d027112015-07-01 15:41:14 +01001731 {
1732 // We use a block to end the scratch scope before the write barrier, thus
1733 // freeing the temporary registers so they can be used in `MarkGCCard`.
1734 UseScratchRegisterScope temps(GetVIXLAssembler());
1735
1736 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1737 DCHECK(value.IsW());
1738 Register temp = temps.AcquireW();
1739 __ Mov(temp, value.W());
1740 GetAssembler()->PoisonHeapReference(temp.W());
1741 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001742 }
Roland Levillain4d027112015-07-01 15:41:14 +01001743
1744 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001745 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1746 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001747 } else {
1748 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1749 codegen_->MaybeRecordImplicitNullCheck(instruction);
1750 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001751 }
1752
1753 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001754 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001755 }
1756}
1757
Alexandre Rames67555f72014-11-18 10:55:16 +00001758void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001759 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001760
1761 switch (type) {
1762 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001763 case Primitive::kPrimLong: {
1764 Register dst = OutputRegister(instr);
1765 Register lhs = InputRegisterAt(instr, 0);
1766 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001767 if (instr->IsAdd()) {
1768 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001769 } else if (instr->IsAnd()) {
1770 __ And(dst, lhs, rhs);
1771 } else if (instr->IsOr()) {
1772 __ Orr(dst, lhs, rhs);
1773 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001774 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001775 } else if (instr->IsRor()) {
1776 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001777 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001778 __ Ror(dst, lhs, shift);
1779 } else {
1780 // Ensure shift distance is in the same size register as the result. If
1781 // we are rotating a long and the shift comes in a w register originally,
1782 // we don't need to sxtw for use as an x since the shift distances are
1783 // all & reg_bits - 1.
1784 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1785 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001786 } else {
1787 DCHECK(instr->IsXor());
1788 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001789 }
1790 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001791 }
1792 case Primitive::kPrimFloat:
1793 case Primitive::kPrimDouble: {
1794 FPRegister dst = OutputFPRegister(instr);
1795 FPRegister lhs = InputFPRegisterAt(instr, 0);
1796 FPRegister rhs = InputFPRegisterAt(instr, 1);
1797 if (instr->IsAdd()) {
1798 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001799 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001800 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001801 } else {
1802 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001803 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001804 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001805 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001806 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001807 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001808 }
1809}
1810
Serban Constantinescu02164b32014-11-13 14:05:07 +00001811void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1812 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1813
1814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1815 Primitive::Type type = instr->GetResultType();
1816 switch (type) {
1817 case Primitive::kPrimInt:
1818 case Primitive::kPrimLong: {
1819 locations->SetInAt(0, Location::RequiresRegister());
1820 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1821 locations->SetOut(Location::RequiresRegister());
1822 break;
1823 }
1824 default:
1825 LOG(FATAL) << "Unexpected shift type " << type;
1826 }
1827}
1828
1829void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1830 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1831
1832 Primitive::Type type = instr->GetType();
1833 switch (type) {
1834 case Primitive::kPrimInt:
1835 case Primitive::kPrimLong: {
1836 Register dst = OutputRegister(instr);
1837 Register lhs = InputRegisterAt(instr, 0);
1838 Operand rhs = InputOperandAt(instr, 1);
1839 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001840 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001841 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001842 if (instr->IsShl()) {
1843 __ Lsl(dst, lhs, shift_value);
1844 } else if (instr->IsShr()) {
1845 __ Asr(dst, lhs, shift_value);
1846 } else {
1847 __ Lsr(dst, lhs, shift_value);
1848 }
1849 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001850 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001851
1852 if (instr->IsShl()) {
1853 __ Lsl(dst, lhs, rhs_reg);
1854 } else if (instr->IsShr()) {
1855 __ Asr(dst, lhs, rhs_reg);
1856 } else {
1857 __ Lsr(dst, lhs, rhs_reg);
1858 }
1859 }
1860 break;
1861 }
1862 default:
1863 LOG(FATAL) << "Unexpected shift operation type " << type;
1864 }
1865}
1866
Alexandre Rames5319def2014-10-23 10:03:10 +01001867void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001868 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001869}
1870
1871void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001872 HandleBinaryOp(instruction);
1873}
1874
1875void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1876 HandleBinaryOp(instruction);
1877}
1878
1879void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1880 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001881}
1882
Artem Serov7fc63502016-02-09 17:15:29 +00001883void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001884 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1886 locations->SetInAt(0, Location::RequiresRegister());
1887 // There is no immediate variant of negated bitwise instructions in AArch64.
1888 locations->SetInAt(1, Location::RequiresRegister());
1889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1890}
1891
Artem Serov7fc63502016-02-09 17:15:29 +00001892void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001893 Register dst = OutputRegister(instr);
1894 Register lhs = InputRegisterAt(instr, 0);
1895 Register rhs = InputRegisterAt(instr, 1);
1896
1897 switch (instr->GetOpKind()) {
1898 case HInstruction::kAnd:
1899 __ Bic(dst, lhs, rhs);
1900 break;
1901 case HInstruction::kOr:
1902 __ Orn(dst, lhs, rhs);
1903 break;
1904 case HInstruction::kXor:
1905 __ Eon(dst, lhs, rhs);
1906 break;
1907 default:
1908 LOG(FATAL) << "Unreachable";
1909 }
1910}
1911
Alexandre Rames8626b742015-11-25 16:28:08 +00001912void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1913 HArm64DataProcWithShifterOp* instruction) {
1914 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1915 instruction->GetType() == Primitive::kPrimLong);
1916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1918 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1919 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1920 } else {
1921 locations->SetInAt(0, Location::RequiresRegister());
1922 }
1923 locations->SetInAt(1, Location::RequiresRegister());
1924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1925}
1926
1927void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1928 HArm64DataProcWithShifterOp* instruction) {
1929 Primitive::Type type = instruction->GetType();
1930 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1931 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1932 Register out = OutputRegister(instruction);
1933 Register left;
1934 if (kind != HInstruction::kNeg) {
1935 left = InputRegisterAt(instruction, 0);
1936 }
1937 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1938 // shifter operand operation, the IR generating `right_reg` (input to the type
1939 // conversion) can have a different type from the current instruction's type,
1940 // so we manually indicate the type.
1941 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001942 int64_t shift_amount = instruction->GetShiftAmount() &
1943 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001944
1945 Operand right_operand(0);
1946
1947 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1948 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1949 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1950 } else {
1951 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1952 }
1953
1954 // Logical binary operations do not support extension operations in the
1955 // operand. Note that VIXL would still manage if it was passed by generating
1956 // the extension as a separate instruction.
1957 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1958 DCHECK(!right_operand.IsExtendedRegister() ||
1959 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1960 kind != HInstruction::kNeg));
1961 switch (kind) {
1962 case HInstruction::kAdd:
1963 __ Add(out, left, right_operand);
1964 break;
1965 case HInstruction::kAnd:
1966 __ And(out, left, right_operand);
1967 break;
1968 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001969 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001970 __ Neg(out, right_operand);
1971 break;
1972 case HInstruction::kOr:
1973 __ Orr(out, left, right_operand);
1974 break;
1975 case HInstruction::kSub:
1976 __ Sub(out, left, right_operand);
1977 break;
1978 case HInstruction::kXor:
1979 __ Eor(out, left, right_operand);
1980 break;
1981 default:
1982 LOG(FATAL) << "Unexpected operation kind: " << kind;
1983 UNREACHABLE();
1984 }
1985}
1986
Artem Serov328429f2016-07-06 16:23:04 +01001987void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001988 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1989 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001990 LocationSummary* locations =
1991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1992 locations->SetInAt(0, Location::RequiresRegister());
1993 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1994 locations->SetOut(Location::RequiresRegister());
1995}
1996
Roland Levillain4a3aa572016-08-15 13:17:06 +00001997void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1998 HIntermediateAddress* instruction) {
1999 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2000 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002001 __ Add(OutputRegister(instruction),
2002 InputRegisterAt(instruction, 0),
2003 Operand(InputOperandAt(instruction, 1)));
2004}
2005
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002006void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002007 LocationSummary* locations =
2008 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002009 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2010 if (instr->GetOpKind() == HInstruction::kSub &&
2011 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002012 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002013 // Don't allocate register for Mneg instruction.
2014 } else {
2015 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2016 Location::RequiresRegister());
2017 }
2018 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2019 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2021}
2022
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002023void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002024 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002025 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2026 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002027
2028 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2029 // This fixup should be carried out for all multiply-accumulate instructions:
2030 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2031 if (instr->GetType() == Primitive::kPrimLong &&
2032 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2033 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002034 vixl::aarch64::Instruction* prev =
2035 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002036 if (prev->IsLoadOrStore()) {
2037 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002038 vixl::aarch64::CodeBufferCheckScope scope(masm,
2039 kInstructionSize,
2040 vixl::aarch64::CodeBufferCheckScope::kCheck,
2041 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002042 __ nop();
2043 }
2044 }
2045
2046 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002047 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002048 __ Madd(res, mul_left, mul_right, accumulator);
2049 } else {
2050 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002051 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002052 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002053 __ Mneg(res, mul_left, mul_right);
2054 } else {
2055 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2056 __ Msub(res, mul_left, mul_right, accumulator);
2057 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002058 }
2059}
2060
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002062 bool object_array_get_with_read_barrier =
2063 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002064 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002065 new (GetGraph()->GetArena()) LocationSummary(instruction,
2066 object_array_get_with_read_barrier ?
2067 LocationSummary::kCallOnSlowPath :
2068 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002069 locations->SetInAt(0, Location::RequiresRegister());
2070 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002071 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2072 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2073 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002074 // The output overlaps in the case of an object array get with
2075 // read barriers enabled: we do not want the move to overwrite the
2076 // array's location, as we need it to emit the read barrier.
2077 locations->SetOut(
2078 Location::RequiresRegister(),
2079 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002080 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002081}
2082
2083void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002084 Primitive::Type type = instruction->GetType();
2085 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002086 LocationSummary* locations = instruction->GetLocations();
2087 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002088 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002089 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002090
Alexandre Ramesd921d642015-04-16 15:07:16 +01002091 MacroAssembler* masm = GetVIXLAssembler();
2092 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002093 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002094 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002095
Roland Levillain44015862016-01-22 11:47:17 +00002096 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2097 // Object ArrayGet with Baker's read barrier case.
2098 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002099 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2100 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002101 // Note that a potential implicit null check is handled in the
2102 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2103 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2104 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002105 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002106 // General case.
2107 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002108 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002109 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2110 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002111 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002112 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002113 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002114 // The read barrier instrumentation does not support the
2115 // HIntermediateAddress instruction yet.
2116 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002117 // We do not need to compute the intermediate address from the array: the
2118 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002119 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002120 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002121 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002122 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2123 }
2124 temp = obj;
2125 } else {
2126 __ Add(temp, obj, offset);
2127 }
2128 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2129 }
2130
2131 codegen_->Load(type, OutputCPURegister(instruction), source);
2132 codegen_->MaybeRecordImplicitNullCheck(instruction);
2133
2134 if (type == Primitive::kPrimNot) {
2135 static_assert(
2136 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2137 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2138 Location obj_loc = locations->InAt(0);
2139 if (index.IsConstant()) {
2140 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2141 } else {
2142 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2143 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002144 }
Roland Levillain4d027112015-07-01 15:41:14 +01002145 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002146}
2147
Alexandre Rames5319def2014-10-23 10:03:10 +01002148void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2150 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002151 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002152}
2153
2154void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002155 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002156 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002157 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002158 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002159}
2160
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002161void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002162 Primitive::Type value_type = instruction->GetComponentType();
2163
2164 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2165 bool object_array_set_with_read_barrier =
2166 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002167 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2168 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002169 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2170 LocationSummary::kCallOnSlowPath :
2171 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002172 locations->SetInAt(0, Location::RequiresRegister());
2173 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002174 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002175 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002176 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002177 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002178 }
2179}
2180
2181void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2182 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002183 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002184 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002185 bool needs_write_barrier =
2186 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002187
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002188 Register array = InputRegisterAt(instruction, 0);
2189 CPURegister value = InputCPURegisterAt(instruction, 2);
2190 CPURegister source = value;
2191 Location index = locations->InAt(1);
2192 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2193 MemOperand destination = HeapOperand(array);
2194 MacroAssembler* masm = GetVIXLAssembler();
2195 BlockPoolsScope block_pools(masm);
2196
2197 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002198 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002199 if (index.IsConstant()) {
2200 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2201 destination = HeapOperand(array, offset);
2202 } else {
2203 UseScratchRegisterScope temps(masm);
2204 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002205 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002206 // The read barrier instrumentation does not support the
2207 // HIntermediateAddress instruction yet.
2208 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002209 // We do not need to compute the intermediate address from the array: the
2210 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002211 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002212 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002213 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002214 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2215 }
2216 temp = array;
2217 } else {
2218 __ Add(temp, array, offset);
2219 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002220 destination = HeapOperand(temp,
2221 XRegisterFrom(index),
2222 LSL,
2223 Primitive::ComponentSizeShift(value_type));
2224 }
2225 codegen_->Store(value_type, value, destination);
2226 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002227 } else {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002228 DCHECK(needs_write_barrier);
Artem Serov328429f2016-07-06 16:23:04 +01002229 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002230 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002231 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002232 {
2233 // We use a block to end the scratch scope before the write barrier, thus
2234 // freeing the temporary registers so they can be used in `MarkGCCard`.
2235 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002236 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002237 if (index.IsConstant()) {
2238 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002239 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002240 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002241 destination = HeapOperand(temp,
2242 XRegisterFrom(index),
2243 LSL,
2244 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002245 }
2246
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002247 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2248 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2249 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2250
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002251 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002252 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2253 codegen_->AddSlowPath(slow_path);
2254 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002255 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002256 __ Cbnz(Register(value), &non_zero);
2257 if (!index.IsConstant()) {
2258 __ Add(temp, array, offset);
2259 }
2260 __ Str(wzr, destination);
2261 codegen_->MaybeRecordImplicitNullCheck(instruction);
2262 __ B(&done);
2263 __ Bind(&non_zero);
2264 }
2265
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002266 if (kEmitCompilerReadBarrier) {
2267 // When read barriers are enabled, the type checking
2268 // instrumentation requires two read barriers:
2269 //
2270 // __ Mov(temp2, temp);
2271 // // /* HeapReference<Class> */ temp = temp->component_type_
2272 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002273 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002274 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2275 //
2276 // // /* HeapReference<Class> */ temp2 = value->klass_
2277 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002278 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002279 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2280 //
2281 // __ Cmp(temp, temp2);
2282 //
2283 // However, the second read barrier may trash `temp`, as it
2284 // is a temporary register, and as such would not be saved
2285 // along with live registers before calling the runtime (nor
2286 // restored afterwards). So in this case, we bail out and
2287 // delegate the work to the array set slow path.
2288 //
2289 // TODO: Extend the register allocator to support a new
2290 // "(locally) live temp" location so as to avoid always
2291 // going into the slow path when read barriers are enabled.
2292 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002293 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 Register temp2 = temps.AcquireSameSizeAs(array);
2295 // /* HeapReference<Class> */ temp = array->klass_
2296 __ Ldr(temp, HeapOperand(array, class_offset));
2297 codegen_->MaybeRecordImplicitNullCheck(instruction);
2298 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2299
2300 // /* HeapReference<Class> */ temp = temp->component_type_
2301 __ Ldr(temp, HeapOperand(temp, component_offset));
2302 // /* HeapReference<Class> */ temp2 = value->klass_
2303 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2304 // If heap poisoning is enabled, no need to unpoison `temp`
2305 // nor `temp2`, as we are comparing two poisoned references.
2306 __ Cmp(temp, temp2);
2307
2308 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002309 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002310 __ B(eq, &do_put);
2311 // If heap poisoning is enabled, the `temp` reference has
2312 // not been unpoisoned yet; unpoison it now.
2313 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2314
2315 // /* HeapReference<Class> */ temp = temp->super_class_
2316 __ Ldr(temp, HeapOperand(temp, super_offset));
2317 // If heap poisoning is enabled, no need to unpoison
2318 // `temp`, as we are comparing against null below.
2319 __ Cbnz(temp, slow_path->GetEntryLabel());
2320 __ Bind(&do_put);
2321 } else {
2322 __ B(ne, slow_path->GetEntryLabel());
2323 }
2324 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002325 }
2326 }
2327
2328 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002329 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002330 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002331 __ Mov(temp2, value.W());
2332 GetAssembler()->PoisonHeapReference(temp2);
2333 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002334 }
2335
2336 if (!index.IsConstant()) {
2337 __ Add(temp, array, offset);
2338 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002339 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002340
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002341 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002342 codegen_->MaybeRecordImplicitNullCheck(instruction);
2343 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002344 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002345
2346 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2347
2348 if (done.IsLinked()) {
2349 __ Bind(&done);
2350 }
2351
2352 if (slow_path != nullptr) {
2353 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002354 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002355 }
2356}
2357
Alexandre Rames67555f72014-11-18 10:55:16 +00002358void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002359 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2360 ? LocationSummary::kCallOnSlowPath
2361 : LocationSummary::kNoCall;
2362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002363 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002364 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002365 if (instruction->HasUses()) {
2366 locations->SetOut(Location::SameAsFirstInput());
2367 }
2368}
2369
2370void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002371 BoundsCheckSlowPathARM64* slow_path =
2372 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002373 codegen_->AddSlowPath(slow_path);
2374
2375 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2376 __ B(slow_path->GetEntryLabel(), hs);
2377}
2378
Alexandre Rames67555f72014-11-18 10:55:16 +00002379void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2380 LocationSummary* locations =
2381 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2382 locations->SetInAt(0, Location::RequiresRegister());
2383 if (check->HasUses()) {
2384 locations->SetOut(Location::SameAsFirstInput());
2385 }
2386}
2387
2388void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2389 // We assume the class is not null.
2390 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2391 check->GetLoadClass(), check, check->GetDexPc(), true);
2392 codegen_->AddSlowPath(slow_path);
2393 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2394}
2395
Roland Levillain1a653882016-03-18 18:05:57 +00002396static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2397 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2398 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2399}
2400
2401void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2402 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2403 Location rhs_loc = instruction->GetLocations()->InAt(1);
2404 if (rhs_loc.IsConstant()) {
2405 // 0.0 is the only immediate that can be encoded directly in
2406 // an FCMP instruction.
2407 //
2408 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2409 // specify that in a floating-point comparison, positive zero
2410 // and negative zero are considered equal, so we can use the
2411 // literal 0.0 for both cases here.
2412 //
2413 // Note however that some methods (Float.equal, Float.compare,
2414 // Float.compareTo, Double.equal, Double.compare,
2415 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2416 // StrictMath.min) consider 0.0 to be (strictly) greater than
2417 // -0.0. So if we ever translate calls to these methods into a
2418 // HCompare instruction, we must handle the -0.0 case with
2419 // care here.
2420 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2421 __ Fcmp(lhs_reg, 0.0);
2422 } else {
2423 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2424 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002425}
2426
Serban Constantinescu02164b32014-11-13 14:05:07 +00002427void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002428 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002429 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2430 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002431 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002432 case Primitive::kPrimBoolean:
2433 case Primitive::kPrimByte:
2434 case Primitive::kPrimShort:
2435 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002436 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002437 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002438 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002439 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2441 break;
2442 }
2443 case Primitive::kPrimFloat:
2444 case Primitive::kPrimDouble: {
2445 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002446 locations->SetInAt(1,
2447 IsFloatingPointZeroConstant(compare->InputAt(1))
2448 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2449 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002450 locations->SetOut(Location::RequiresRegister());
2451 break;
2452 }
2453 default:
2454 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2455 }
2456}
2457
2458void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2459 Primitive::Type in_type = compare->InputAt(0)->GetType();
2460
2461 // 0 if: left == right
2462 // 1 if: left > right
2463 // -1 if: left < right
2464 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002465 case Primitive::kPrimBoolean:
2466 case Primitive::kPrimByte:
2467 case Primitive::kPrimShort:
2468 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002469 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002470 case Primitive::kPrimLong: {
2471 Register result = OutputRegister(compare);
2472 Register left = InputRegisterAt(compare, 0);
2473 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002474 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002475 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2476 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002477 break;
2478 }
2479 case Primitive::kPrimFloat:
2480 case Primitive::kPrimDouble: {
2481 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002482 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002483 __ Cset(result, ne);
2484 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002485 break;
2486 }
2487 default:
2488 LOG(FATAL) << "Unimplemented compare type " << in_type;
2489 }
2490}
2491
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002492void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002494
2495 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2496 locations->SetInAt(0, Location::RequiresFpuRegister());
2497 locations->SetInAt(1,
2498 IsFloatingPointZeroConstant(instruction->InputAt(1))
2499 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2500 : Location::RequiresFpuRegister());
2501 } else {
2502 // Integer cases.
2503 locations->SetInAt(0, Location::RequiresRegister());
2504 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2505 }
2506
David Brazdilb3e773e2016-01-26 11:28:37 +00002507 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002509 }
2510}
2511
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002512void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002513 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002514 return;
2515 }
2516
2517 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002518 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002519 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002520
Roland Levillain7f63c522015-07-13 15:54:55 +00002521 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002522 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002523 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002524 } else {
2525 // Integer cases.
2526 Register lhs = InputRegisterAt(instruction, 0);
2527 Operand rhs = InputOperandAt(instruction, 1);
2528 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002529 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002530 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002531}
2532
2533#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2534 M(Equal) \
2535 M(NotEqual) \
2536 M(LessThan) \
2537 M(LessThanOrEqual) \
2538 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002539 M(GreaterThanOrEqual) \
2540 M(Below) \
2541 M(BelowOrEqual) \
2542 M(Above) \
2543 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002544#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002545void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2546void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002547FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002548#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002549#undef FOR_EACH_CONDITION_INSTRUCTION
2550
Zheng Xuc6667102015-05-15 16:08:45 +08002551void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2552 DCHECK(instruction->IsDiv() || instruction->IsRem());
2553
2554 LocationSummary* locations = instruction->GetLocations();
2555 Location second = locations->InAt(1);
2556 DCHECK(second.IsConstant());
2557
2558 Register out = OutputRegister(instruction);
2559 Register dividend = InputRegisterAt(instruction, 0);
2560 int64_t imm = Int64FromConstant(second.GetConstant());
2561 DCHECK(imm == 1 || imm == -1);
2562
2563 if (instruction->IsRem()) {
2564 __ Mov(out, 0);
2565 } else {
2566 if (imm == 1) {
2567 __ Mov(out, dividend);
2568 } else {
2569 __ Neg(out, dividend);
2570 }
2571 }
2572}
2573
2574void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2575 DCHECK(instruction->IsDiv() || instruction->IsRem());
2576
2577 LocationSummary* locations = instruction->GetLocations();
2578 Location second = locations->InAt(1);
2579 DCHECK(second.IsConstant());
2580
2581 Register out = OutputRegister(instruction);
2582 Register dividend = InputRegisterAt(instruction, 0);
2583 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002584 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002585 int ctz_imm = CTZ(abs_imm);
2586
2587 UseScratchRegisterScope temps(GetVIXLAssembler());
2588 Register temp = temps.AcquireSameSizeAs(out);
2589
2590 if (instruction->IsDiv()) {
2591 __ Add(temp, dividend, abs_imm - 1);
2592 __ Cmp(dividend, 0);
2593 __ Csel(out, temp, dividend, lt);
2594 if (imm > 0) {
2595 __ Asr(out, out, ctz_imm);
2596 } else {
2597 __ Neg(out, Operand(out, ASR, ctz_imm));
2598 }
2599 } else {
2600 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2601 __ Asr(temp, dividend, bits - 1);
2602 __ Lsr(temp, temp, bits - ctz_imm);
2603 __ Add(out, dividend, temp);
2604 __ And(out, out, abs_imm - 1);
2605 __ Sub(out, out, temp);
2606 }
2607}
2608
2609void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2610 DCHECK(instruction->IsDiv() || instruction->IsRem());
2611
2612 LocationSummary* locations = instruction->GetLocations();
2613 Location second = locations->InAt(1);
2614 DCHECK(second.IsConstant());
2615
2616 Register out = OutputRegister(instruction);
2617 Register dividend = InputRegisterAt(instruction, 0);
2618 int64_t imm = Int64FromConstant(second.GetConstant());
2619
2620 Primitive::Type type = instruction->GetResultType();
2621 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2622
2623 int64_t magic;
2624 int shift;
2625 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2626
2627 UseScratchRegisterScope temps(GetVIXLAssembler());
2628 Register temp = temps.AcquireSameSizeAs(out);
2629
2630 // temp = get_high(dividend * magic)
2631 __ Mov(temp, magic);
2632 if (type == Primitive::kPrimLong) {
2633 __ Smulh(temp, dividend, temp);
2634 } else {
2635 __ Smull(temp.X(), dividend, temp);
2636 __ Lsr(temp.X(), temp.X(), 32);
2637 }
2638
2639 if (imm > 0 && magic < 0) {
2640 __ Add(temp, temp, dividend);
2641 } else if (imm < 0 && magic > 0) {
2642 __ Sub(temp, temp, dividend);
2643 }
2644
2645 if (shift != 0) {
2646 __ Asr(temp, temp, shift);
2647 }
2648
2649 if (instruction->IsDiv()) {
2650 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2651 } else {
2652 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2653 // TODO: Strength reduction for msub.
2654 Register temp_imm = temps.AcquireSameSizeAs(out);
2655 __ Mov(temp_imm, imm);
2656 __ Msub(out, temp, temp_imm, dividend);
2657 }
2658}
2659
2660void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2661 DCHECK(instruction->IsDiv() || instruction->IsRem());
2662 Primitive::Type type = instruction->GetResultType();
2663 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2664
2665 LocationSummary* locations = instruction->GetLocations();
2666 Register out = OutputRegister(instruction);
2667 Location second = locations->InAt(1);
2668
2669 if (second.IsConstant()) {
2670 int64_t imm = Int64FromConstant(second.GetConstant());
2671
2672 if (imm == 0) {
2673 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2674 } else if (imm == 1 || imm == -1) {
2675 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002676 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002677 DivRemByPowerOfTwo(instruction);
2678 } else {
2679 DCHECK(imm <= -2 || imm >= 2);
2680 GenerateDivRemWithAnyConstant(instruction);
2681 }
2682 } else {
2683 Register dividend = InputRegisterAt(instruction, 0);
2684 Register divisor = InputRegisterAt(instruction, 1);
2685 if (instruction->IsDiv()) {
2686 __ Sdiv(out, dividend, divisor);
2687 } else {
2688 UseScratchRegisterScope temps(GetVIXLAssembler());
2689 Register temp = temps.AcquireSameSizeAs(out);
2690 __ Sdiv(temp, dividend, divisor);
2691 __ Msub(out, temp, divisor, dividend);
2692 }
2693 }
2694}
2695
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002696void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2699 switch (div->GetResultType()) {
2700 case Primitive::kPrimInt:
2701 case Primitive::kPrimLong:
2702 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002703 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2705 break;
2706
2707 case Primitive::kPrimFloat:
2708 case Primitive::kPrimDouble:
2709 locations->SetInAt(0, Location::RequiresFpuRegister());
2710 locations->SetInAt(1, Location::RequiresFpuRegister());
2711 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2712 break;
2713
2714 default:
2715 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2716 }
2717}
2718
2719void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2720 Primitive::Type type = div->GetResultType();
2721 switch (type) {
2722 case Primitive::kPrimInt:
2723 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002724 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002725 break;
2726
2727 case Primitive::kPrimFloat:
2728 case Primitive::kPrimDouble:
2729 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2730 break;
2731
2732 default:
2733 LOG(FATAL) << "Unexpected div type " << type;
2734 }
2735}
2736
Alexandre Rames67555f72014-11-18 10:55:16 +00002737void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002738 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2739 ? LocationSummary::kCallOnSlowPath
2740 : LocationSummary::kNoCall;
2741 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002742 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2743 if (instruction->HasUses()) {
2744 locations->SetOut(Location::SameAsFirstInput());
2745 }
2746}
2747
2748void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2749 SlowPathCodeARM64* slow_path =
2750 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2751 codegen_->AddSlowPath(slow_path);
2752 Location value = instruction->GetLocations()->InAt(0);
2753
Alexandre Rames3e69f162014-12-10 10:36:50 +00002754 Primitive::Type type = instruction->GetType();
2755
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002756 if (!Primitive::IsIntegralType(type)) {
2757 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002758 return;
2759 }
2760
Alexandre Rames67555f72014-11-18 10:55:16 +00002761 if (value.IsConstant()) {
2762 int64_t divisor = Int64ConstantFrom(value);
2763 if (divisor == 0) {
2764 __ B(slow_path->GetEntryLabel());
2765 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002766 // A division by a non-null constant is valid. We don't need to perform
2767 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002768 }
2769 } else {
2770 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2771 }
2772}
2773
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002774void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2775 LocationSummary* locations =
2776 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2777 locations->SetOut(Location::ConstantLocation(constant));
2778}
2779
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002780void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2781 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002782 // Will be generated at use site.
2783}
2784
Alexandre Rames5319def2014-10-23 10:03:10 +01002785void LocationsBuilderARM64::VisitExit(HExit* exit) {
2786 exit->SetLocations(nullptr);
2787}
2788
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002789void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002790}
2791
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002792void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2793 LocationSummary* locations =
2794 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2795 locations->SetOut(Location::ConstantLocation(constant));
2796}
2797
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002798void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002799 // Will be generated at use site.
2800}
2801
David Brazdilfc6a86a2015-06-26 10:33:45 +00002802void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002803 DCHECK(!successor->IsExitBlock());
2804 HBasicBlock* block = got->GetBlock();
2805 HInstruction* previous = got->GetPrevious();
2806 HLoopInformation* info = block->GetLoopInformation();
2807
David Brazdil46e2a392015-03-16 17:31:52 +00002808 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002809 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2810 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2811 return;
2812 }
2813 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2814 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2815 }
2816 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002817 __ B(codegen_->GetLabelOf(successor));
2818 }
2819}
2820
David Brazdilfc6a86a2015-06-26 10:33:45 +00002821void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2822 got->SetLocations(nullptr);
2823}
2824
2825void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2826 HandleGoto(got, got->GetSuccessor());
2827}
2828
2829void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2830 try_boundary->SetLocations(nullptr);
2831}
2832
2833void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2834 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2835 if (!successor->IsExitBlock()) {
2836 HandleGoto(try_boundary, successor);
2837 }
2838}
2839
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002840void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002841 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002842 vixl::aarch64::Label* true_target,
2843 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002844 // FP branching requires both targets to be explicit. If either of the targets
2845 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002846 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002847 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002848
David Brazdil0debae72015-11-12 18:37:00 +00002849 if (true_target == nullptr && false_target == nullptr) {
2850 // Nothing to do. The code always falls through.
2851 return;
2852 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002853 // Constant condition, statically compared against "true" (integer value 1).
2854 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002855 if (true_target != nullptr) {
2856 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002857 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002858 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002859 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002860 if (false_target != nullptr) {
2861 __ B(false_target);
2862 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002863 }
David Brazdil0debae72015-11-12 18:37:00 +00002864 return;
2865 }
2866
2867 // The following code generates these patterns:
2868 // (1) true_target == nullptr && false_target != nullptr
2869 // - opposite condition true => branch to false_target
2870 // (2) true_target != nullptr && false_target == nullptr
2871 // - condition true => branch to true_target
2872 // (3) true_target != nullptr && false_target != nullptr
2873 // - condition true => branch to true_target
2874 // - branch to false_target
2875 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002876 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002877 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002878 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002879 if (true_target == nullptr) {
2880 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2881 } else {
2882 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2883 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002884 } else {
2885 // The condition instruction has not been materialized, use its inputs as
2886 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002887 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002888
David Brazdil0debae72015-11-12 18:37:00 +00002889 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002890 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002891 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002892 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002893 IfCondition opposite_condition = condition->GetOppositeCondition();
2894 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002895 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002896 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002897 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002898 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002899 // Integer cases.
2900 Register lhs = InputRegisterAt(condition, 0);
2901 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002902
2903 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002904 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002905 if (true_target == nullptr) {
2906 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2907 non_fallthrough_target = false_target;
2908 } else {
2909 arm64_cond = ARM64Condition(condition->GetCondition());
2910 non_fallthrough_target = true_target;
2911 }
2912
Aart Bik086d27e2016-01-20 17:02:00 -08002913 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002914 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002915 switch (arm64_cond) {
2916 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002917 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002918 break;
2919 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002920 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002921 break;
2922 case lt:
2923 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002924 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002925 break;
2926 case ge:
2927 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002928 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002929 break;
2930 default:
2931 // Without the `static_cast` the compiler throws an error for
2932 // `-Werror=sign-promo`.
2933 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2934 }
2935 } else {
2936 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002937 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002938 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002939 }
2940 }
David Brazdil0debae72015-11-12 18:37:00 +00002941
2942 // If neither branch falls through (case 3), the conditional branch to `true_target`
2943 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2944 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002945 __ B(false_target);
2946 }
David Brazdil0debae72015-11-12 18:37:00 +00002947
2948 if (fallthrough_target.IsLinked()) {
2949 __ Bind(&fallthrough_target);
2950 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002951}
2952
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002953void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002955 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002956 locations->SetInAt(0, Location::RequiresRegister());
2957 }
2958}
2959
2960void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002961 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2962 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002963 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2964 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2965 true_target = nullptr;
2966 }
2967 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2968 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2969 false_target = nullptr;
2970 }
David Brazdil0debae72015-11-12 18:37:00 +00002971 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002972}
2973
2974void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2975 LocationSummary* locations = new (GetGraph()->GetArena())
2976 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002977 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002978 locations->SetInAt(0, Location::RequiresRegister());
2979 }
2980}
2981
2982void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002983 SlowPathCodeARM64* slow_path =
2984 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002985 GenerateTestAndBranch(deoptimize,
2986 /* condition_input_index */ 0,
2987 slow_path->GetEntryLabel(),
2988 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002989}
2990
David Brazdilc0b601b2016-02-08 14:20:45 +00002991static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2992 return condition->IsCondition() &&
2993 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2994}
2995
Alexandre Rames880f1192016-06-13 16:04:50 +01002996static inline Condition GetConditionForSelect(HCondition* condition) {
2997 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002998 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2999 : ARM64Condition(cond);
3000}
3001
David Brazdil74eb1b22015-12-14 11:44:01 +00003002void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003004 if (Primitive::IsFloatingPointType(select->GetType())) {
3005 locations->SetInAt(0, Location::RequiresFpuRegister());
3006 locations->SetInAt(1, Location::RequiresFpuRegister());
3007 locations->SetOut(Location::RequiresFpuRegister());
3008 } else {
3009 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3010 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3011 bool is_true_value_constant = cst_true_value != nullptr;
3012 bool is_false_value_constant = cst_false_value != nullptr;
3013 // Ask VIXL whether we should synthesize constants in registers.
3014 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3015 Operand true_op = is_true_value_constant ?
3016 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3017 Operand false_op = is_false_value_constant ?
3018 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3019 bool true_value_in_register = false;
3020 bool false_value_in_register = false;
3021 MacroAssembler::GetCselSynthesisInformation(
3022 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3023 true_value_in_register |= !is_true_value_constant;
3024 false_value_in_register |= !is_false_value_constant;
3025
3026 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3027 : Location::ConstantLocation(cst_true_value));
3028 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3029 : Location::ConstantLocation(cst_false_value));
3030 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003031 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003032
David Brazdil74eb1b22015-12-14 11:44:01 +00003033 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3034 locations->SetInAt(2, Location::RequiresRegister());
3035 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003036}
3037
3038void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003039 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003040 Condition csel_cond;
3041
3042 if (IsBooleanValueOrMaterializedCondition(cond)) {
3043 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003044 // Use the condition flags set by the previous instruction.
3045 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003046 } else {
3047 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003048 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003049 }
3050 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003051 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003052 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003053 } else {
3054 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003055 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003056 }
3057
Alexandre Rames880f1192016-06-13 16:04:50 +01003058 if (Primitive::IsFloatingPointType(select->GetType())) {
3059 __ Fcsel(OutputFPRegister(select),
3060 InputFPRegisterAt(select, 1),
3061 InputFPRegisterAt(select, 0),
3062 csel_cond);
3063 } else {
3064 __ Csel(OutputRegister(select),
3065 InputOperandAt(select, 1),
3066 InputOperandAt(select, 0),
3067 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003068 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003069}
3070
David Srbecky0cf44932015-12-09 14:09:59 +00003071void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3072 new (GetGraph()->GetArena()) LocationSummary(info);
3073}
3074
David Srbeckyd28f4a02016-03-14 17:14:24 +00003075void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3076 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003077}
3078
3079void CodeGeneratorARM64::GenerateNop() {
3080 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003081}
3082
Alexandre Rames5319def2014-10-23 10:03:10 +01003083void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003084 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003085}
3086
3087void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003088 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003089}
3090
3091void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003092 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003093}
3094
3095void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003096 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003097}
3098
Roland Levillain44015862016-01-22 11:47:17 +00003099static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3100 return kEmitCompilerReadBarrier &&
3101 (kUseBakerReadBarrier ||
3102 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3103 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3104 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3105}
3106
Alexandre Rames67555f72014-11-18 10:55:16 +00003107void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003108 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003109 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3110 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003111 case TypeCheckKind::kExactCheck:
3112 case TypeCheckKind::kAbstractClassCheck:
3113 case TypeCheckKind::kClassHierarchyCheck:
3114 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003115 call_kind =
3116 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003117 break;
3118 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003119 case TypeCheckKind::kUnresolvedCheck:
3120 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003121 call_kind = LocationSummary::kCallOnSlowPath;
3122 break;
3123 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003124
Alexandre Rames67555f72014-11-18 10:55:16 +00003125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003126 locations->SetInAt(0, Location::RequiresRegister());
3127 locations->SetInAt(1, Location::RequiresRegister());
3128 // The "out" register is used as a temporary, so it overlaps with the inputs.
3129 // Note that TypeCheckSlowPathARM64 uses this register too.
3130 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3131 // When read barriers are enabled, we need a temporary register for
3132 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003133 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003134 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003135 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003136}
3137
3138void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003139 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003140 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003141 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003142 Register obj = InputRegisterAt(instruction, 0);
3143 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003144 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003145 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003146 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3147 locations->GetTemp(0) :
3148 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003149 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3150 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3151 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3152 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003153
Scott Wakeling97c72b72016-06-24 16:19:36 +01003154 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003155 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003156
3157 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003158 // Avoid null check if we know `obj` is not null.
3159 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003160 __ Cbz(obj, &zero);
3161 }
3162
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003163 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003164 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003165
Roland Levillain44015862016-01-22 11:47:17 +00003166 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003167 case TypeCheckKind::kExactCheck: {
3168 __ Cmp(out, cls);
3169 __ Cset(out, eq);
3170 if (zero.IsLinked()) {
3171 __ B(&done);
3172 }
3173 break;
3174 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003175
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003176 case TypeCheckKind::kAbstractClassCheck: {
3177 // If the class is abstract, we eagerly fetch the super class of the
3178 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003179 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003180 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003181 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003182 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003183 // If `out` is null, we use it for the result, and jump to `done`.
3184 __ Cbz(out, &done);
3185 __ Cmp(out, cls);
3186 __ B(ne, &loop);
3187 __ Mov(out, 1);
3188 if (zero.IsLinked()) {
3189 __ B(&done);
3190 }
3191 break;
3192 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003193
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003194 case TypeCheckKind::kClassHierarchyCheck: {
3195 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003196 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003197 __ Bind(&loop);
3198 __ Cmp(out, cls);
3199 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003200 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003201 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003202 __ Cbnz(out, &loop);
3203 // If `out` is null, we use it for the result, and jump to `done`.
3204 __ B(&done);
3205 __ Bind(&success);
3206 __ Mov(out, 1);
3207 if (zero.IsLinked()) {
3208 __ B(&done);
3209 }
3210 break;
3211 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003213 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003214 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003215 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003216 __ Cmp(out, cls);
3217 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003218 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003219 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003220 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003221 // If `out` is null, we use it for the result, and jump to `done`.
3222 __ Cbz(out, &done);
3223 __ Ldrh(out, HeapOperand(out, primitive_offset));
3224 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3225 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003226 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003227 __ Mov(out, 1);
3228 __ B(&done);
3229 break;
3230 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003232 case TypeCheckKind::kArrayCheck: {
3233 __ Cmp(out, cls);
3234 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003235 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3236 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 codegen_->AddSlowPath(slow_path);
3238 __ B(ne, slow_path->GetEntryLabel());
3239 __ Mov(out, 1);
3240 if (zero.IsLinked()) {
3241 __ B(&done);
3242 }
3243 break;
3244 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003245
Calin Juravle98893e12015-10-02 21:05:03 +01003246 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003247 case TypeCheckKind::kInterfaceCheck: {
3248 // Note that we indeed only call on slow path, but we always go
3249 // into the slow path for the unresolved and interface check
3250 // cases.
3251 //
3252 // We cannot directly call the InstanceofNonTrivial runtime
3253 // entry point without resorting to a type checking slow path
3254 // here (i.e. by calling InvokeRuntime directly), as it would
3255 // require to assign fixed registers for the inputs of this
3256 // HInstanceOf instruction (following the runtime calling
3257 // convention), which might be cluttered by the potential first
3258 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003259 //
3260 // TODO: Introduce a new runtime entry point taking the object
3261 // to test (instead of its class) as argument, and let it deal
3262 // with the read barrier issues. This will let us refactor this
3263 // case of the `switch` code as it was previously (with a direct
3264 // call to the runtime not using a type checking slow path).
3265 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003266 DCHECK(locations->OnlyCallsOnSlowPath());
3267 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3268 /* is_fatal */ false);
3269 codegen_->AddSlowPath(slow_path);
3270 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003271 if (zero.IsLinked()) {
3272 __ B(&done);
3273 }
3274 break;
3275 }
3276 }
3277
3278 if (zero.IsLinked()) {
3279 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003280 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003281 }
3282
3283 if (done.IsLinked()) {
3284 __ Bind(&done);
3285 }
3286
3287 if (slow_path != nullptr) {
3288 __ Bind(slow_path->GetExitLabel());
3289 }
3290}
3291
3292void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3293 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3294 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3295
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003296 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3297 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003298 case TypeCheckKind::kExactCheck:
3299 case TypeCheckKind::kAbstractClassCheck:
3300 case TypeCheckKind::kClassHierarchyCheck:
3301 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003302 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3303 LocationSummary::kCallOnSlowPath :
3304 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003305 break;
3306 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003307 case TypeCheckKind::kUnresolvedCheck:
3308 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003309 call_kind = LocationSummary::kCallOnSlowPath;
3310 break;
3311 }
3312
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3314 locations->SetInAt(0, Location::RequiresRegister());
3315 locations->SetInAt(1, Location::RequiresRegister());
3316 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3317 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003318 // When read barriers are enabled, we need an additional temporary
3319 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003320 if (TypeCheckNeedsATemporary(type_check_kind)) {
3321 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003322 }
3323}
3324
3325void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003326 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003327 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003328 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003329 Register obj = InputRegisterAt(instruction, 0);
3330 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003332 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3333 locations->GetTemp(1) :
3334 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003335 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003336 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3337 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3338 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3339 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003340
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003341 bool is_type_check_slow_path_fatal =
3342 (type_check_kind == TypeCheckKind::kExactCheck ||
3343 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3344 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3345 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3346 !instruction->CanThrowIntoCatchBlock();
3347 SlowPathCodeARM64* type_check_slow_path =
3348 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3349 is_type_check_slow_path_fatal);
3350 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003351
Scott Wakeling97c72b72016-06-24 16:19:36 +01003352 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003353 // Avoid null check if we know obj is not null.
3354 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003355 __ Cbz(obj, &done);
3356 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003357
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003358 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003359 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003360
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003361 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362 case TypeCheckKind::kExactCheck:
3363 case TypeCheckKind::kArrayCheck: {
3364 __ Cmp(temp, cls);
3365 // Jump to slow path for throwing the exception or doing a
3366 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003367 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003368 break;
3369 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003371 case TypeCheckKind::kAbstractClassCheck: {
3372 // If the class is abstract, we eagerly fetch the super class of the
3373 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003374 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003375 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003377 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378
3379 // If the class reference currently in `temp` is not null, jump
3380 // to the `compare_classes` label to compare it with the checked
3381 // class.
3382 __ Cbnz(temp, &compare_classes);
3383 // Otherwise, jump to the slow path to throw the exception.
3384 //
3385 // But before, move back the object's class into `temp` before
3386 // going into the slow path, as it has been overwritten in the
3387 // meantime.
3388 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003389 GenerateReferenceLoadTwoRegisters(
3390 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391 __ B(type_check_slow_path->GetEntryLabel());
3392
3393 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003394 __ Cmp(temp, cls);
3395 __ B(ne, &loop);
3396 break;
3397 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003398
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003399 case TypeCheckKind::kClassHierarchyCheck: {
3400 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003401 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003402 __ Bind(&loop);
3403 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003404 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003405
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003406 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003407 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003408
3409 // If the class reference currently in `temp` is not null, jump
3410 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003411 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003412 // Otherwise, jump to the slow path to throw the exception.
3413 //
3414 // But before, move back the object's class into `temp` before
3415 // going into the slow path, as it has been overwritten in the
3416 // meantime.
3417 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003418 GenerateReferenceLoadTwoRegisters(
3419 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003420 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003421 break;
3422 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003424 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003425 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003426 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003427 __ Cmp(temp, cls);
3428 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003429
3430 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003432 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003433
3434 // If the component type is not null (i.e. the object is indeed
3435 // an array), jump to label `check_non_primitive_component_type`
3436 // to further check that this component type is not a primitive
3437 // type.
3438 __ Cbnz(temp, &check_non_primitive_component_type);
3439 // Otherwise, jump to the slow path to throw the exception.
3440 //
3441 // But before, move back the object's class into `temp` before
3442 // going into the slow path, as it has been overwritten in the
3443 // meantime.
3444 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003445 GenerateReferenceLoadTwoRegisters(
3446 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447 __ B(type_check_slow_path->GetEntryLabel());
3448
3449 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3451 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003452 __ Cbz(temp, &done);
3453 // Same comment as above regarding `temp` and the slow path.
3454 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003455 GenerateReferenceLoadTwoRegisters(
3456 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003457 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003458 break;
3459 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003460
Calin Juravle98893e12015-10-02 21:05:03 +01003461 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003462 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003463 // We always go into the type check slow path for the unresolved
3464 // and interface check cases.
3465 //
3466 // We cannot directly call the CheckCast runtime entry point
3467 // without resorting to a type checking slow path here (i.e. by
3468 // calling InvokeRuntime directly), as it would require to
3469 // assign fixed registers for the inputs of this HInstanceOf
3470 // instruction (following the runtime calling convention), which
3471 // might be cluttered by the potential first read barrier
3472 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003473 //
3474 // TODO: Introduce a new runtime entry point taking the object
3475 // to test (instead of its class) as argument, and let it deal
3476 // with the read barrier issues. This will let us refactor this
3477 // case of the `switch` code as it was previously (with a direct
3478 // call to the runtime not using a type checking slow path).
3479 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003480 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003481 break;
3482 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003483 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003484
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003485 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003486}
3487
Alexandre Rames5319def2014-10-23 10:03:10 +01003488void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3490 locations->SetOut(Location::ConstantLocation(constant));
3491}
3492
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003493void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003494 // Will be generated at use site.
3495}
3496
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003497void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3498 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3499 locations->SetOut(Location::ConstantLocation(constant));
3500}
3501
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003502void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003503 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003504}
3505
Calin Juravle175dc732015-08-25 15:42:32 +01003506void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3507 // The trampoline uses the same calling convention as dex calling conventions,
3508 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3509 // the method_idx.
3510 HandleInvoke(invoke);
3511}
3512
3513void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3514 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3515}
3516
Alexandre Rames5319def2014-10-23 10:03:10 +01003517void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003518 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003519 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003520}
3521
Alexandre Rames67555f72014-11-18 10:55:16 +00003522void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3523 HandleInvoke(invoke);
3524}
3525
3526void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3527 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003528 LocationSummary* locations = invoke->GetLocations();
3529 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003530 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003531 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003532 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003533
3534 // The register ip1 is required to be used for the hidden argument in
3535 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003536 MacroAssembler* masm = GetVIXLAssembler();
3537 UseScratchRegisterScope scratch_scope(masm);
3538 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003539 scratch_scope.Exclude(ip1);
3540 __ Mov(ip1, invoke->GetDexMethodIndex());
3541
Alexandre Rames67555f72014-11-18 10:55:16 +00003542 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003543 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003544 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003545 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003546 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003547 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003548 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003549 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003550 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003551 // Instead of simply (possibly) unpoisoning `temp` here, we should
3552 // emit a read barrier for the previous class reference load.
3553 // However this is not required in practice, as this is an
3554 // intermediate/temporary reference and because the current
3555 // concurrent copying collector keeps the from-space memory
3556 // intact/accessible until the end of the marking phase (the
3557 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003558 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003559 __ Ldr(temp,
3560 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3561 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003562 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003563 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003564 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003565 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003566 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003567 // lr();
3568 __ Blr(lr);
3569 DCHECK(!codegen_->IsLeafMethod());
3570 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3571}
3572
3573void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003574 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3575 if (intrinsic.TryDispatch(invoke)) {
3576 return;
3577 }
3578
Alexandre Rames67555f72014-11-18 10:55:16 +00003579 HandleInvoke(invoke);
3580}
3581
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003582void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003583 // Explicit clinit checks triggered by static invokes must have been pruned by
3584 // art::PrepareForRegisterAllocation.
3585 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003586
Andreas Gampe878d58c2015-01-15 23:24:00 -08003587 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3588 if (intrinsic.TryDispatch(invoke)) {
3589 return;
3590 }
3591
Alexandre Rames67555f72014-11-18 10:55:16 +00003592 HandleInvoke(invoke);
3593}
3594
Andreas Gampe878d58c2015-01-15 23:24:00 -08003595static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3596 if (invoke->GetLocations()->Intrinsified()) {
3597 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3598 intrinsic.Dispatch(invoke);
3599 return true;
3600 }
3601 return false;
3602}
3603
Vladimir Markodc151b22015-10-15 18:02:30 +01003604HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3605 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3606 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003607 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003608 return desired_dispatch_info;
3609}
3610
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003611void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003612 // For better instruction scheduling we load the direct code pointer before the method pointer.
3613 bool direct_code_loaded = false;
3614 switch (invoke->GetCodePtrLocation()) {
3615 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3616 // LR = code address from literal pool with link-time patch.
3617 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3618 direct_code_loaded = true;
3619 break;
3620 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3621 // LR = invoke->GetDirectCodePtr();
3622 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3623 direct_code_loaded = true;
3624 break;
3625 default:
3626 break;
3627 }
3628
Andreas Gampe878d58c2015-01-15 23:24:00 -08003629 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003630 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3631 switch (invoke->GetMethodLoadKind()) {
3632 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3633 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003634 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003635 break;
3636 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003637 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003638 break;
3639 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3640 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003641 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003642 break;
3643 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3644 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003645 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003646 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3647 break;
3648 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3649 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003650 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3651 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003652 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003653 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003654 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003655 __ Bind(adrp_label);
3656 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003657 }
Vladimir Marko58155012015-08-19 12:49:41 +00003658 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003659 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003660 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003661 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003662 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003663 __ Bind(ldr_label);
3664 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003665 }
Vladimir Marko58155012015-08-19 12:49:41 +00003666 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003667 }
Vladimir Marko58155012015-08-19 12:49:41 +00003668 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003669 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003670 Register reg = XRegisterFrom(temp);
3671 Register method_reg;
3672 if (current_method.IsRegister()) {
3673 method_reg = XRegisterFrom(current_method);
3674 } else {
3675 DCHECK(invoke->GetLocations()->Intrinsified());
3676 DCHECK(!current_method.IsValid());
3677 method_reg = reg;
3678 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3679 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003680
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003681 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003682 __ Ldr(reg.X(),
3683 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003684 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003685 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003686 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3687 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003688 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3689 break;
3690 }
3691 }
3692
3693 switch (invoke->GetCodePtrLocation()) {
3694 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3695 __ Bl(&frame_entry_label_);
3696 break;
3697 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3698 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003699 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3700 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003701 __ Bind(label);
3702 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003703 break;
3704 }
3705 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3706 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3707 // LR prepared above for better instruction scheduling.
3708 DCHECK(direct_code_loaded);
3709 // lr()
3710 __ Blr(lr);
3711 break;
3712 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3713 // LR = callee_method->entry_point_from_quick_compiled_code_;
3714 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003715 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003716 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003717 // lr()
3718 __ Blr(lr);
3719 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003720 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003721
Andreas Gampe878d58c2015-01-15 23:24:00 -08003722 DCHECK(!IsLeafMethod());
3723}
3724
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003725void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003726 // Use the calling convention instead of the location of the receiver, as
3727 // intrinsics may have put the receiver in a different register. In the intrinsics
3728 // slow path, the arguments have been moved to the right place, so here we are
3729 // guaranteed that the receiver is the first register of the calling convention.
3730 InvokeDexCallingConvention calling_convention;
3731 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003732 Register temp = XRegisterFrom(temp_in);
3733 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3734 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3735 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003736 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003737
3738 BlockPoolsScope block_pools(GetVIXLAssembler());
3739
3740 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003741 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003742 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003743 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003744 // Instead of simply (possibly) unpoisoning `temp` here, we should
3745 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003746 // intermediate/temporary reference and because the current
3747 // concurrent copying collector keeps the from-space memory
3748 // intact/accessible until the end of the marking phase (the
3749 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003750 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3751 // temp = temp->GetMethodAt(method_offset);
3752 __ Ldr(temp, MemOperand(temp, method_offset));
3753 // lr = temp->GetEntryPoint();
3754 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3755 // lr();
3756 __ Blr(lr);
3757}
3758
Scott Wakeling97c72b72016-06-24 16:19:36 +01003759vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3760 const DexFile& dex_file,
3761 uint32_t string_index,
3762 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003763 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3764}
3765
Scott Wakeling97c72b72016-06-24 16:19:36 +01003766vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3767 const DexFile& dex_file,
3768 uint32_t type_index,
3769 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003770 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3771}
3772
Scott Wakeling97c72b72016-06-24 16:19:36 +01003773vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3774 const DexFile& dex_file,
3775 uint32_t element_offset,
3776 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003777 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3778}
3779
Scott Wakeling97c72b72016-06-24 16:19:36 +01003780vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3781 const DexFile& dex_file,
3782 uint32_t offset_or_index,
3783 vixl::aarch64::Label* adrp_label,
3784 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003785 // Add a patch entry and return the label.
3786 patches->emplace_back(dex_file, offset_or_index);
3787 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003788 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003789 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3790 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3791 return label;
3792}
3793
Scott Wakeling97c72b72016-06-24 16:19:36 +01003794vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003795 const DexFile& dex_file, uint32_t string_index) {
3796 return boot_image_string_patches_.GetOrCreate(
3797 StringReference(&dex_file, string_index),
3798 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3799}
3800
Scott Wakeling97c72b72016-06-24 16:19:36 +01003801vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003802 const DexFile& dex_file, uint32_t type_index) {
3803 return boot_image_type_patches_.GetOrCreate(
3804 TypeReference(&dex_file, type_index),
3805 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3806}
3807
Scott Wakeling97c72b72016-06-24 16:19:36 +01003808vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3809 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003810 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3811 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3812 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3813}
3814
Scott Wakeling97c72b72016-06-24 16:19:36 +01003815vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3816 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003817 return DeduplicateUint64Literal(address);
3818}
3819
Vladimir Marko58155012015-08-19 12:49:41 +00003820void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3821 DCHECK(linker_patches->empty());
3822 size_t size =
3823 method_patches_.size() +
3824 call_patches_.size() +
3825 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003826 pc_relative_dex_cache_patches_.size() +
3827 boot_image_string_patches_.size() +
3828 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003829 boot_image_type_patches_.size() +
3830 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003831 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003832 linker_patches->reserve(size);
3833 for (const auto& entry : method_patches_) {
3834 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003835 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3836 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003837 target_method.dex_file,
3838 target_method.dex_method_index));
3839 }
3840 for (const auto& entry : call_patches_) {
3841 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003842 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3843 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003844 target_method.dex_file,
3845 target_method.dex_method_index));
3846 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003847 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3848 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003849 info.target_method.dex_file,
3850 info.target_method.dex_method_index));
3851 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003852 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003853 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003854 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003855 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003856 info.offset_or_index));
3857 }
3858 for (const auto& entry : boot_image_string_patches_) {
3859 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003860 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3861 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003862 target_string.dex_file,
3863 target_string.string_index));
3864 }
3865 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003866 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003867 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003868 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003869 info.offset_or_index));
3870 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003871 for (const auto& entry : boot_image_type_patches_) {
3872 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003873 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3874 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003875 target_type.dex_file,
3876 target_type.type_index));
3877 }
3878 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003879 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003880 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003881 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003882 info.offset_or_index));
3883 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003884 for (const auto& entry : boot_image_address_patches_) {
3885 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003886 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3887 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003888 }
3889}
3890
Scott Wakeling97c72b72016-06-24 16:19:36 +01003891vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003892 Uint32ToLiteralMap* map) {
3893 return map->GetOrCreate(
3894 value,
3895 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3896}
3897
Scott Wakeling97c72b72016-06-24 16:19:36 +01003898vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003899 return uint64_literals_.GetOrCreate(
3900 value,
3901 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003902}
3903
Scott Wakeling97c72b72016-06-24 16:19:36 +01003904vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003905 MethodReference target_method,
3906 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003907 return map->GetOrCreate(
3908 target_method,
3909 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003910}
3911
Scott Wakeling97c72b72016-06-24 16:19:36 +01003912vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003913 MethodReference target_method) {
3914 return DeduplicateMethodLiteral(target_method, &method_patches_);
3915}
3916
Scott Wakeling97c72b72016-06-24 16:19:36 +01003917vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003918 MethodReference target_method) {
3919 return DeduplicateMethodLiteral(target_method, &call_patches_);
3920}
3921
3922
Andreas Gampe878d58c2015-01-15 23:24:00 -08003923void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003924 // Explicit clinit checks triggered by static invokes must have been pruned by
3925 // art::PrepareForRegisterAllocation.
3926 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003927
Andreas Gampe878d58c2015-01-15 23:24:00 -08003928 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3929 return;
3930 }
3931
Alexandre Ramesd921d642015-04-16 15:07:16 +01003932 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003933 LocationSummary* locations = invoke->GetLocations();
3934 codegen_->GenerateStaticOrDirectCall(
3935 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003936 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003937}
3938
3939void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003940 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3941 return;
3942 }
3943
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003944 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003945 DCHECK(!codegen_->IsLeafMethod());
3946 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3947}
3948
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003949HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3950 HLoadClass::LoadKind desired_class_load_kind) {
3951 if (kEmitCompilerReadBarrier) {
3952 switch (desired_class_load_kind) {
3953 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3954 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3955 case HLoadClass::LoadKind::kBootImageAddress:
3956 // TODO: Implement for read barrier.
3957 return HLoadClass::LoadKind::kDexCacheViaMethod;
3958 default:
3959 break;
3960 }
3961 }
3962 switch (desired_class_load_kind) {
3963 case HLoadClass::LoadKind::kReferrersClass:
3964 break;
3965 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3966 DCHECK(!GetCompilerOptions().GetCompilePic());
3967 break;
3968 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3969 DCHECK(GetCompilerOptions().GetCompilePic());
3970 break;
3971 case HLoadClass::LoadKind::kBootImageAddress:
3972 break;
3973 case HLoadClass::LoadKind::kDexCacheAddress:
3974 DCHECK(Runtime::Current()->UseJitCompilation());
3975 break;
3976 case HLoadClass::LoadKind::kDexCachePcRelative:
3977 DCHECK(!Runtime::Current()->UseJitCompilation());
3978 break;
3979 case HLoadClass::LoadKind::kDexCacheViaMethod:
3980 break;
3981 }
3982 return desired_class_load_kind;
3983}
3984
Alexandre Rames67555f72014-11-18 10:55:16 +00003985void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003986 if (cls->NeedsAccessCheck()) {
3987 InvokeRuntimeCallingConvention calling_convention;
3988 CodeGenerator::CreateLoadClassLocationSummary(
3989 cls,
3990 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003991 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003992 /* code_generator_supports_read_barrier */ true);
3993 return;
3994 }
3995
3996 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3997 ? LocationSummary::kCallOnSlowPath
3998 : LocationSummary::kNoCall;
3999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4000 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4001 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4002 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4003 locations->SetInAt(0, Location::RequiresRegister());
4004 }
4005 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004006}
4007
4008void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004009 if (cls->NeedsAccessCheck()) {
4010 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
4011 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4012 cls,
4013 cls->GetDexPc(),
4014 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004015 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004016 return;
4017 }
4018
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004019 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004020 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004021
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004022 bool generate_null_check = false;
4023 switch (cls->GetLoadKind()) {
4024 case HLoadClass::LoadKind::kReferrersClass: {
4025 DCHECK(!cls->CanCallRuntime());
4026 DCHECK(!cls->MustGenerateClinitCheck());
4027 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4028 Register current_method = InputRegisterAt(cls, 0);
4029 GenerateGcRootFieldLoad(
4030 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4031 break;
4032 }
4033 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4034 DCHECK(!kEmitCompilerReadBarrier);
4035 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4036 cls->GetTypeIndex()));
4037 break;
4038 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4039 DCHECK(!kEmitCompilerReadBarrier);
4040 // Add ADRP with its PC-relative type patch.
4041 const DexFile& dex_file = cls->GetDexFile();
4042 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004043 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004044 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004045 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004046 __ Bind(adrp_label);
4047 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004048 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004049 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004050 vixl::aarch64::Label* add_label =
4051 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004052 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004053 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004054 __ Bind(add_label);
4055 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004056 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004057 break;
4058 }
4059 case HLoadClass::LoadKind::kBootImageAddress: {
4060 DCHECK(!kEmitCompilerReadBarrier);
4061 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4062 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4063 break;
4064 }
4065 case HLoadClass::LoadKind::kDexCacheAddress: {
4066 DCHECK_NE(cls->GetAddress(), 0u);
4067 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4068 // that gives a 16KiB range. To try and reduce the number of literals if we load
4069 // multiple types, simply split the dex cache address to a 16KiB aligned base
4070 // loaded from a literal and the remaining offset embedded in the load.
4071 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4072 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4073 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4074 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4075 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4076 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4077 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4078 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4079 generate_null_check = !cls->IsInDexCache();
4080 break;
4081 }
4082 case HLoadClass::LoadKind::kDexCachePcRelative: {
4083 // Add ADRP with its PC-relative DexCache access patch.
4084 const DexFile& dex_file = cls->GetDexFile();
4085 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004086 vixl::aarch64::Label* adrp_label =
4087 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004088 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004089 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004090 __ Bind(adrp_label);
4091 __ adrp(out.X(), /* offset placeholder */ 0);
4092 }
4093 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004094 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004095 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4096 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4097 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4098 generate_null_check = !cls->IsInDexCache();
4099 break;
4100 }
4101 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4102 MemberOffset resolved_types_offset =
4103 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4104 // /* GcRoot<mirror::Class>[] */ out =
4105 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4106 Register current_method = InputRegisterAt(cls, 0);
4107 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4108 // /* GcRoot<mirror::Class> */ out = out[type_index]
4109 GenerateGcRootFieldLoad(
4110 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4111 generate_null_check = !cls->IsInDexCache();
4112 break;
4113 }
4114 }
4115
4116 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4117 DCHECK(cls->CanCallRuntime());
4118 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4119 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4120 codegen_->AddSlowPath(slow_path);
4121 if (generate_null_check) {
4122 __ Cbz(out, slow_path->GetEntryLabel());
4123 }
4124 if (cls->MustGenerateClinitCheck()) {
4125 GenerateClassInitializationCheck(slow_path, out);
4126 } else {
4127 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004128 }
4129 }
4130}
4131
David Brazdilcb1c0552015-08-04 16:22:25 +01004132static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004133 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004134}
4135
Alexandre Rames67555f72014-11-18 10:55:16 +00004136void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4137 LocationSummary* locations =
4138 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4139 locations->SetOut(Location::RequiresRegister());
4140}
4141
4142void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004143 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4144}
4145
4146void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4147 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4148}
4149
4150void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4151 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004152}
4153
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004154HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4155 HLoadString::LoadKind desired_string_load_kind) {
4156 if (kEmitCompilerReadBarrier) {
4157 switch (desired_string_load_kind) {
4158 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4159 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4160 case HLoadString::LoadKind::kBootImageAddress:
4161 // TODO: Implement for read barrier.
4162 return HLoadString::LoadKind::kDexCacheViaMethod;
4163 default:
4164 break;
4165 }
4166 }
4167 switch (desired_string_load_kind) {
4168 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4169 DCHECK(!GetCompilerOptions().GetCompilePic());
4170 break;
4171 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4172 DCHECK(GetCompilerOptions().GetCompilePic());
4173 break;
4174 case HLoadString::LoadKind::kBootImageAddress:
4175 break;
4176 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004177 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004178 break;
4179 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004180 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004181 break;
4182 case HLoadString::LoadKind::kDexCacheViaMethod:
4183 break;
4184 }
4185 return desired_string_load_kind;
4186}
4187
Alexandre Rames67555f72014-11-18 10:55:16 +00004188void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004189 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004190 ? LocationSummary::kCallOnSlowPath
4191 : LocationSummary::kNoCall;
4192 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004193 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4194 locations->SetInAt(0, Location::RequiresRegister());
4195 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004196 locations->SetOut(Location::RequiresRegister());
4197}
4198
4199void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004200 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004202 switch (load->GetLoadKind()) {
4203 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4204 DCHECK(!kEmitCompilerReadBarrier);
4205 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4206 load->GetStringIndex()));
4207 return; // No dex cache slow path.
4208 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4209 DCHECK(!kEmitCompilerReadBarrier);
4210 // Add ADRP with its PC-relative String patch.
4211 const DexFile& dex_file = load->GetDexFile();
4212 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004213 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004214 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004215 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004216 __ Bind(adrp_label);
4217 __ adrp(out.X(), /* offset placeholder */ 0);
4218 }
4219 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004220 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004221 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4222 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004223 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004224 __ Bind(add_label);
4225 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4226 }
4227 return; // No dex cache slow path.
4228 }
4229 case HLoadString::LoadKind::kBootImageAddress: {
4230 DCHECK(!kEmitCompilerReadBarrier);
4231 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4232 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4233 return; // No dex cache slow path.
4234 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004235 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004236 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004237 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004239 // TODO: Re-add the compiler code to do string dex cache lookup again.
4240 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4241 codegen_->AddSlowPath(slow_path);
4242 __ B(slow_path->GetEntryLabel());
4243 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004244}
4245
Alexandre Rames5319def2014-10-23 10:03:10 +01004246void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4248 locations->SetOut(Location::ConstantLocation(constant));
4249}
4250
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004251void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004252 // Will be generated at use site.
4253}
4254
Alexandre Rames67555f72014-11-18 10:55:16 +00004255void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4256 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004257 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004258 InvokeRuntimeCallingConvention calling_convention;
4259 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4260}
4261
4262void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4263 codegen_->InvokeRuntime(instruction->IsEnter()
4264 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4265 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004266 instruction->GetDexPc(),
4267 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004268 if (instruction->IsEnter()) {
4269 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4270 } else {
4271 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4272 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004273}
4274
Alexandre Rames42d641b2014-10-27 14:00:51 +00004275void LocationsBuilderARM64::VisitMul(HMul* mul) {
4276 LocationSummary* locations =
4277 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4278 switch (mul->GetResultType()) {
4279 case Primitive::kPrimInt:
4280 case Primitive::kPrimLong:
4281 locations->SetInAt(0, Location::RequiresRegister());
4282 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004284 break;
4285
4286 case Primitive::kPrimFloat:
4287 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004288 locations->SetInAt(0, Location::RequiresFpuRegister());
4289 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004290 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004291 break;
4292
4293 default:
4294 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4295 }
4296}
4297
4298void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4299 switch (mul->GetResultType()) {
4300 case Primitive::kPrimInt:
4301 case Primitive::kPrimLong:
4302 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4303 break;
4304
4305 case Primitive::kPrimFloat:
4306 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004307 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004308 break;
4309
4310 default:
4311 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4312 }
4313}
4314
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004315void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4316 LocationSummary* locations =
4317 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4318 switch (neg->GetResultType()) {
4319 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004320 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004321 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004323 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004324
4325 case Primitive::kPrimFloat:
4326 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004327 locations->SetInAt(0, Location::RequiresFpuRegister());
4328 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004329 break;
4330
4331 default:
4332 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4333 }
4334}
4335
4336void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4337 switch (neg->GetResultType()) {
4338 case Primitive::kPrimInt:
4339 case Primitive::kPrimLong:
4340 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4341 break;
4342
4343 case Primitive::kPrimFloat:
4344 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004345 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004346 break;
4347
4348 default:
4349 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4350 }
4351}
4352
4353void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4354 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004355 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004356 InvokeRuntimeCallingConvention calling_convention;
4357 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004358 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004359 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004360 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004361}
4362
4363void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4364 LocationSummary* locations = instruction->GetLocations();
4365 InvokeRuntimeCallingConvention calling_convention;
4366 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4367 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004368 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004369 // Note: if heap poisoning is enabled, the entry point takes cares
4370 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004371 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4372 instruction,
4373 instruction->GetDexPc(),
4374 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004375 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004376}
4377
Alexandre Rames5319def2014-10-23 10:03:10 +01004378void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4379 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004381 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004382 if (instruction->IsStringAlloc()) {
4383 locations->AddTemp(LocationFrom(kArtMethodRegister));
4384 } else {
4385 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4386 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4387 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004388 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4389}
4390
4391void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004392 // Note: if heap poisoning is enabled, the entry point takes cares
4393 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004394 if (instruction->IsStringAlloc()) {
4395 // String is allocated through StringFactory. Call NewEmptyString entry point.
4396 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004397 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004398 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4399 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4400 __ Blr(lr);
4401 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4402 } else {
4403 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4404 instruction,
4405 instruction->GetDexPc(),
4406 nullptr);
4407 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4408 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004409}
4410
4411void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004413 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004415}
4416
4417void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004418 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004419 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004420 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004421 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004422 break;
4423
4424 default:
4425 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4426 }
4427}
4428
David Brazdil66d126e2015-04-03 16:02:44 +01004429void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4431 locations->SetInAt(0, Location::RequiresRegister());
4432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4433}
4434
4435void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004436 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004437}
4438
Alexandre Rames5319def2014-10-23 10:03:10 +01004439void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004440 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4441 ? LocationSummary::kCallOnSlowPath
4442 : LocationSummary::kNoCall;
4443 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004444 locations->SetInAt(0, Location::RequiresRegister());
4445 if (instruction->HasUses()) {
4446 locations->SetOut(Location::SameAsFirstInput());
4447 }
4448}
4449
Calin Juravle2ae48182016-03-16 14:05:09 +00004450void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4451 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004452 return;
4453 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004454
Alexandre Ramesd921d642015-04-16 15:07:16 +01004455 BlockPoolsScope block_pools(GetVIXLAssembler());
4456 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004457 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004458 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004459}
4460
Calin Juravle2ae48182016-03-16 14:05:09 +00004461void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004462 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004463 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004464
4465 LocationSummary* locations = instruction->GetLocations();
4466 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004467
4468 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004469}
4470
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004471void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004472 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004473}
4474
Alexandre Rames67555f72014-11-18 10:55:16 +00004475void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4476 HandleBinaryOp(instruction);
4477}
4478
4479void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4480 HandleBinaryOp(instruction);
4481}
4482
Alexandre Rames3e69f162014-12-10 10:36:50 +00004483void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4484 LOG(FATAL) << "Unreachable";
4485}
4486
4487void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4488 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4489}
4490
Alexandre Rames5319def2014-10-23 10:03:10 +01004491void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4493 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4494 if (location.IsStackSlot()) {
4495 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4496 } else if (location.IsDoubleStackSlot()) {
4497 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4498 }
4499 locations->SetOut(location);
4500}
4501
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004502void InstructionCodeGeneratorARM64::VisitParameterValue(
4503 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004504 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004505}
4506
4507void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4508 LocationSummary* locations =
4509 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004510 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004511}
4512
4513void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4514 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4515 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004516}
4517
4518void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004520 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004521 locations->SetInAt(i, Location::Any());
4522 }
4523 locations->SetOut(Location::Any());
4524}
4525
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004526void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004527 LOG(FATAL) << "Unreachable";
4528}
4529
Serban Constantinescu02164b32014-11-13 14:05:07 +00004530void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004531 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004532 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004533 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4534 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4536
4537 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004538 case Primitive::kPrimInt:
4539 case Primitive::kPrimLong:
4540 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004541 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004542 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4543 break;
4544
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004545 case Primitive::kPrimFloat:
4546 case Primitive::kPrimDouble: {
4547 InvokeRuntimeCallingConvention calling_convention;
4548 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4549 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4550 locations->SetOut(calling_convention.GetReturnLocation(type));
4551
4552 break;
4553 }
4554
Serban Constantinescu02164b32014-11-13 14:05:07 +00004555 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004556 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004557 }
4558}
4559
4560void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4561 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004562
Serban Constantinescu02164b32014-11-13 14:05:07 +00004563 switch (type) {
4564 case Primitive::kPrimInt:
4565 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004566 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004567 break;
4568 }
4569
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004570 case Primitive::kPrimFloat:
4571 case Primitive::kPrimDouble: {
4572 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4573 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004574 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004575 if (type == Primitive::kPrimFloat) {
4576 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4577 } else {
4578 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4579 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004580 break;
4581 }
4582
Serban Constantinescu02164b32014-11-13 14:05:07 +00004583 default:
4584 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004585 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004586 }
4587}
4588
Calin Juravle27df7582015-04-17 19:12:31 +01004589void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4590 memory_barrier->SetLocations(nullptr);
4591}
4592
4593void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004594 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004595}
4596
Alexandre Rames5319def2014-10-23 10:03:10 +01004597void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4599 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004600 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004601}
4602
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004603void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004604 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004605}
4606
4607void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4608 instruction->SetLocations(nullptr);
4609}
4610
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004611void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004612 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004613}
4614
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004615void LocationsBuilderARM64::VisitRor(HRor* ror) {
4616 HandleBinaryOp(ror);
4617}
4618
4619void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4620 HandleBinaryOp(ror);
4621}
4622
Serban Constantinescu02164b32014-11-13 14:05:07 +00004623void LocationsBuilderARM64::VisitShl(HShl* shl) {
4624 HandleShift(shl);
4625}
4626
4627void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4628 HandleShift(shl);
4629}
4630
4631void LocationsBuilderARM64::VisitShr(HShr* shr) {
4632 HandleShift(shr);
4633}
4634
4635void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4636 HandleShift(shr);
4637}
4638
Alexandre Rames5319def2014-10-23 10:03:10 +01004639void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004640 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004641}
4642
4643void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004644 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004645}
4646
Alexandre Rames67555f72014-11-18 10:55:16 +00004647void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004648 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004649}
4650
4651void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004652 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004653}
4654
4655void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004656 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004657}
4658
Alexandre Rames67555f72014-11-18 10:55:16 +00004659void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004660 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004661}
4662
Calin Juravlee460d1d2015-09-29 04:52:17 +01004663void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4664 HUnresolvedInstanceFieldGet* instruction) {
4665 FieldAccessCallingConventionARM64 calling_convention;
4666 codegen_->CreateUnresolvedFieldLocationSummary(
4667 instruction, instruction->GetFieldType(), calling_convention);
4668}
4669
4670void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4671 HUnresolvedInstanceFieldGet* instruction) {
4672 FieldAccessCallingConventionARM64 calling_convention;
4673 codegen_->GenerateUnresolvedFieldAccess(instruction,
4674 instruction->GetFieldType(),
4675 instruction->GetFieldIndex(),
4676 instruction->GetDexPc(),
4677 calling_convention);
4678}
4679
4680void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4681 HUnresolvedInstanceFieldSet* instruction) {
4682 FieldAccessCallingConventionARM64 calling_convention;
4683 codegen_->CreateUnresolvedFieldLocationSummary(
4684 instruction, instruction->GetFieldType(), calling_convention);
4685}
4686
4687void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4688 HUnresolvedInstanceFieldSet* instruction) {
4689 FieldAccessCallingConventionARM64 calling_convention;
4690 codegen_->GenerateUnresolvedFieldAccess(instruction,
4691 instruction->GetFieldType(),
4692 instruction->GetFieldIndex(),
4693 instruction->GetDexPc(),
4694 calling_convention);
4695}
4696
4697void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4698 HUnresolvedStaticFieldGet* instruction) {
4699 FieldAccessCallingConventionARM64 calling_convention;
4700 codegen_->CreateUnresolvedFieldLocationSummary(
4701 instruction, instruction->GetFieldType(), calling_convention);
4702}
4703
4704void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4705 HUnresolvedStaticFieldGet* instruction) {
4706 FieldAccessCallingConventionARM64 calling_convention;
4707 codegen_->GenerateUnresolvedFieldAccess(instruction,
4708 instruction->GetFieldType(),
4709 instruction->GetFieldIndex(),
4710 instruction->GetDexPc(),
4711 calling_convention);
4712}
4713
4714void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4715 HUnresolvedStaticFieldSet* instruction) {
4716 FieldAccessCallingConventionARM64 calling_convention;
4717 codegen_->CreateUnresolvedFieldLocationSummary(
4718 instruction, instruction->GetFieldType(), calling_convention);
4719}
4720
4721void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4722 HUnresolvedStaticFieldSet* instruction) {
4723 FieldAccessCallingConventionARM64 calling_convention;
4724 codegen_->GenerateUnresolvedFieldAccess(instruction,
4725 instruction->GetFieldType(),
4726 instruction->GetFieldIndex(),
4727 instruction->GetDexPc(),
4728 calling_convention);
4729}
4730
Alexandre Rames5319def2014-10-23 10:03:10 +01004731void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4733}
4734
4735void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004736 HBasicBlock* block = instruction->GetBlock();
4737 if (block->GetLoopInformation() != nullptr) {
4738 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4739 // The back edge will generate the suspend check.
4740 return;
4741 }
4742 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4743 // The goto will generate the suspend check.
4744 return;
4745 }
4746 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004747}
4748
Alexandre Rames67555f72014-11-18 10:55:16 +00004749void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4750 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004752 InvokeRuntimeCallingConvention calling_convention;
4753 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4754}
4755
4756void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4757 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004758 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004759 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004760}
4761
4762void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4763 LocationSummary* locations =
4764 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4765 Primitive::Type input_type = conversion->GetInputType();
4766 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004767 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004768 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4769 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4770 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4771 }
4772
Alexandre Rames542361f2015-01-29 16:57:31 +00004773 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004774 locations->SetInAt(0, Location::RequiresFpuRegister());
4775 } else {
4776 locations->SetInAt(0, Location::RequiresRegister());
4777 }
4778
Alexandre Rames542361f2015-01-29 16:57:31 +00004779 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004780 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4781 } else {
4782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4783 }
4784}
4785
4786void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4787 Primitive::Type result_type = conversion->GetResultType();
4788 Primitive::Type input_type = conversion->GetInputType();
4789
4790 DCHECK_NE(input_type, result_type);
4791
Alexandre Rames542361f2015-01-29 16:57:31 +00004792 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004793 int result_size = Primitive::ComponentSize(result_type);
4794 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004795 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004796 Register output = OutputRegister(conversion);
4797 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004798 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004799 // 'int' values are used directly as W registers, discarding the top
4800 // bits, so we don't need to sign-extend and can just perform a move.
4801 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4802 // top 32 bits of the target register. We theoretically could leave those
4803 // bits unchanged, but we would have to make sure that no code uses a
4804 // 32bit input value as a 64bit value assuming that the top 32 bits are
4805 // zero.
4806 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004807 } else if (result_type == Primitive::kPrimChar ||
4808 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4809 __ Ubfx(output,
4810 output.IsX() ? source.X() : source.W(),
4811 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004812 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004813 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004814 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004815 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004816 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004817 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004818 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4819 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004820 } else if (Primitive::IsFloatingPointType(result_type) &&
4821 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004822 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4823 } else {
4824 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4825 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004826 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004827}
Alexandre Rames67555f72014-11-18 10:55:16 +00004828
Serban Constantinescu02164b32014-11-13 14:05:07 +00004829void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4830 HandleShift(ushr);
4831}
4832
4833void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4834 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004835}
4836
4837void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4838 HandleBinaryOp(instruction);
4839}
4840
4841void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4842 HandleBinaryOp(instruction);
4843}
4844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004845void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004846 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004847 LOG(FATAL) << "Unreachable";
4848}
4849
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004850void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004851 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004852 LOG(FATAL) << "Unreachable";
4853}
4854
Mark Mendellfe57faa2015-09-18 09:26:15 -04004855// Simple implementation of packed switch - generate cascaded compare/jumps.
4856void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4857 LocationSummary* locations =
4858 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4859 locations->SetInAt(0, Location::RequiresRegister());
4860}
4861
4862void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4863 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004864 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004865 Register value_reg = InputRegisterAt(switch_instr, 0);
4866 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4867
Zheng Xu3927c8b2015-11-18 17:46:25 +08004868 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004869 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004870 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4871 // make sure we don't emit it if the target may run out of range.
4872 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4873 // ranges and emit the tables only as required.
4874 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004875
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004876 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004877 // Current instruction id is an upper bound of the number of HIRs in the graph.
4878 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4879 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004880 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4881 Register temp = temps.AcquireW();
4882 __ Subs(temp, value_reg, Operand(lower_bound));
4883
Zheng Xu3927c8b2015-11-18 17:46:25 +08004884 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004885 // Jump to successors[0] if value == lower_bound.
4886 __ B(eq, codegen_->GetLabelOf(successors[0]));
4887 int32_t last_index = 0;
4888 for (; num_entries - last_index > 2; last_index += 2) {
4889 __ Subs(temp, temp, Operand(2));
4890 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4891 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4892 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4893 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4894 }
4895 if (num_entries - last_index == 2) {
4896 // The last missing case_value.
4897 __ Cmp(temp, Operand(1));
4898 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004899 }
4900
4901 // And the default for any other value.
4902 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4903 __ B(codegen_->GetLabelOf(default_block));
4904 }
4905 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004906 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004907
4908 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4909
4910 // Below instructions should use at most one blocked register. Since there are two blocked
4911 // registers, we are free to block one.
4912 Register temp_w = temps.AcquireW();
4913 Register index;
4914 // Remove the bias.
4915 if (lower_bound != 0) {
4916 index = temp_w;
4917 __ Sub(index, value_reg, Operand(lower_bound));
4918 } else {
4919 index = value_reg;
4920 }
4921
4922 // Jump to default block if index is out of the range.
4923 __ Cmp(index, Operand(num_entries));
4924 __ B(hs, codegen_->GetLabelOf(default_block));
4925
4926 // In current VIXL implementation, it won't require any blocked registers to encode the
4927 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4928 // register pressure.
4929 Register table_base = temps.AcquireX();
4930 // Load jump offset from the table.
4931 __ Adr(table_base, jump_table->GetTableStartLabel());
4932 Register jump_offset = temp_w;
4933 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4934
4935 // Jump to target block by branching to table_base(pc related) + offset.
4936 Register target_address = table_base;
4937 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4938 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004939 }
4940}
4941
Roland Levillain44015862016-01-22 11:47:17 +00004942void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4943 Location out,
4944 uint32_t offset,
4945 Location maybe_temp) {
4946 Primitive::Type type = Primitive::kPrimNot;
4947 Register out_reg = RegisterFrom(out, type);
4948 if (kEmitCompilerReadBarrier) {
4949 Register temp_reg = RegisterFrom(maybe_temp, type);
4950 if (kUseBakerReadBarrier) {
4951 // Load with fast path based Baker's read barrier.
4952 // /* HeapReference<Object> */ out = *(out + offset)
4953 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4954 out,
4955 out_reg,
4956 offset,
4957 temp_reg,
4958 /* needs_null_check */ false,
4959 /* use_load_acquire */ false);
4960 } else {
4961 // Load with slow path based read barrier.
4962 // Save the value of `out` into `maybe_temp` before overwriting it
4963 // in the following move operation, as we will need it for the
4964 // read barrier below.
4965 __ Mov(temp_reg, out_reg);
4966 // /* HeapReference<Object> */ out = *(out + offset)
4967 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4968 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4969 }
4970 } else {
4971 // Plain load with no read barrier.
4972 // /* HeapReference<Object> */ out = *(out + offset)
4973 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4974 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4975 }
4976}
4977
4978void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4979 Location out,
4980 Location obj,
4981 uint32_t offset,
4982 Location maybe_temp) {
4983 Primitive::Type type = Primitive::kPrimNot;
4984 Register out_reg = RegisterFrom(out, type);
4985 Register obj_reg = RegisterFrom(obj, type);
4986 if (kEmitCompilerReadBarrier) {
4987 if (kUseBakerReadBarrier) {
4988 // Load with fast path based Baker's read barrier.
4989 Register temp_reg = RegisterFrom(maybe_temp, type);
4990 // /* HeapReference<Object> */ out = *(obj + offset)
4991 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4992 out,
4993 obj_reg,
4994 offset,
4995 temp_reg,
4996 /* needs_null_check */ false,
4997 /* use_load_acquire */ false);
4998 } else {
4999 // Load with slow path based read barrier.
5000 // /* HeapReference<Object> */ out = *(obj + offset)
5001 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5002 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5003 }
5004 } else {
5005 // Plain load with no read barrier.
5006 // /* HeapReference<Object> */ out = *(obj + offset)
5007 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5008 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5009 }
5010}
5011
5012void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5013 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005014 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005015 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005016 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005017 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5018 if (kEmitCompilerReadBarrier) {
5019 if (kUseBakerReadBarrier) {
5020 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5021 // Baker's read barrier are used:
5022 //
5023 // root = obj.field;
5024 // if (Thread::Current()->GetIsGcMarking()) {
5025 // root = ReadBarrier::Mark(root)
5026 // }
5027
5028 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005029 if (fixup_label == nullptr) {
5030 __ Ldr(root_reg, MemOperand(obj, offset));
5031 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005032 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005033 __ Bind(fixup_label);
5034 __ ldr(root_reg, MemOperand(obj, offset));
5035 }
Roland Levillain44015862016-01-22 11:47:17 +00005036 static_assert(
5037 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5038 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5039 "have different sizes.");
5040 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5041 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5042 "have different sizes.");
5043
Vladimir Marko953437b2016-08-24 08:30:46 +00005044 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005045 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005046 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005047 codegen_->AddSlowPath(slow_path);
5048
5049 MacroAssembler* masm = GetVIXLAssembler();
5050 UseScratchRegisterScope temps(masm);
5051 Register temp = temps.AcquireW();
5052 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005053 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005054 __ Cbnz(temp, slow_path->GetEntryLabel());
5055 __ Bind(slow_path->GetExitLabel());
5056 } else {
5057 // GC root loaded through a slow path for read barriers other
5058 // than Baker's.
5059 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005060 if (fixup_label == nullptr) {
5061 __ Add(root_reg.X(), obj.X(), offset);
5062 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005063 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005064 __ Bind(fixup_label);
5065 __ add(root_reg.X(), obj.X(), offset);
5066 }
Roland Levillain44015862016-01-22 11:47:17 +00005067 // /* mirror::Object* */ root = root->Read()
5068 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5069 }
5070 } else {
5071 // Plain GC root load with no read barrier.
5072 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005073 if (fixup_label == nullptr) {
5074 __ Ldr(root_reg, MemOperand(obj, offset));
5075 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005076 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005077 __ Bind(fixup_label);
5078 __ ldr(root_reg, MemOperand(obj, offset));
5079 }
Roland Levillain44015862016-01-22 11:47:17 +00005080 // Note that GC roots are not affected by heap poisoning, thus we
5081 // do not have to unpoison `root_reg` here.
5082 }
5083}
5084
5085void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5086 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005087 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005088 uint32_t offset,
5089 Register temp,
5090 bool needs_null_check,
5091 bool use_load_acquire) {
5092 DCHECK(kEmitCompilerReadBarrier);
5093 DCHECK(kUseBakerReadBarrier);
5094
5095 // /* HeapReference<Object> */ ref = *(obj + offset)
5096 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005097 size_t no_scale_factor = 0U;
5098 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5099 ref,
5100 obj,
5101 offset,
5102 no_index,
5103 no_scale_factor,
5104 temp,
5105 needs_null_check,
5106 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005107}
5108
5109void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5110 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005111 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005112 uint32_t data_offset,
5113 Location index,
5114 Register temp,
5115 bool needs_null_check) {
5116 DCHECK(kEmitCompilerReadBarrier);
5117 DCHECK(kUseBakerReadBarrier);
5118
5119 // Array cells are never volatile variables, therefore array loads
5120 // never use Load-Acquire instructions on ARM64.
5121 const bool use_load_acquire = false;
5122
Roland Levillainbfea3352016-06-23 13:48:47 +01005123 static_assert(
5124 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5125 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005126 // /* HeapReference<Object> */ ref =
5127 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005128 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5129 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5130 ref,
5131 obj,
5132 data_offset,
5133 index,
5134 scale_factor,
5135 temp,
5136 needs_null_check,
5137 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005138}
5139
5140void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5141 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005142 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005143 uint32_t offset,
5144 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005145 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005146 Register temp,
5147 bool needs_null_check,
5148 bool use_load_acquire) {
5149 DCHECK(kEmitCompilerReadBarrier);
5150 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005151 // If we are emitting an array load, we should not be using a
5152 // Load Acquire instruction. In other words:
5153 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5154 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005155
5156 MacroAssembler* masm = GetVIXLAssembler();
5157 UseScratchRegisterScope temps(masm);
5158
5159 // In slow path based read barriers, the read barrier call is
5160 // inserted after the original load. However, in fast path based
5161 // Baker's read barriers, we need to perform the load of
5162 // mirror::Object::monitor_ *before* the original reference load.
5163 // This load-load ordering is required by the read barrier.
5164 // The fast path/slow path (for Baker's algorithm) should look like:
5165 //
5166 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5167 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5168 // HeapReference<Object> ref = *src; // Original reference load.
5169 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5170 // if (is_gray) {
5171 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5172 // }
5173 //
5174 // Note: the original implementation in ReadBarrier::Barrier is
5175 // slightly more complex as it performs additional checks that we do
5176 // not do here for performance reasons.
5177
5178 Primitive::Type type = Primitive::kPrimNot;
5179 Register ref_reg = RegisterFrom(ref, type);
5180 DCHECK(obj.IsW());
5181 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5182
5183 // /* int32_t */ monitor = obj->monitor_
5184 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5185 if (needs_null_check) {
5186 MaybeRecordImplicitNullCheck(instruction);
5187 }
5188 // /* LockWord */ lock_word = LockWord(monitor)
5189 static_assert(sizeof(LockWord) == sizeof(int32_t),
5190 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005191
Vladimir Marko877a0332016-07-11 19:30:56 +01005192 // Introduce a dependency on the lock_word including rb_state,
5193 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005194 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005195 // `obj` is unchanged by this operation, but its value now depends
5196 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005197 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005198
5199 // The actual reference load.
5200 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005201 // Load types involving an "index".
5202 if (use_load_acquire) {
5203 // UnsafeGetObjectVolatile intrinsic case.
5204 // Register `index` is not an index in an object array, but an
5205 // offset to an object reference field within object `obj`.
5206 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5207 DCHECK(instruction->GetLocations()->Intrinsified());
5208 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5209 << instruction->AsInvoke()->GetIntrinsic();
5210 DCHECK_EQ(offset, 0U);
5211 DCHECK_EQ(scale_factor, 0U);
5212 DCHECK_EQ(needs_null_check, 0U);
5213 // /* HeapReference<Object> */ ref = *(obj + index)
5214 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5215 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005216 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005217 // ArrayGet and UnsafeGetObject intrinsics cases.
5218 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5219 if (index.IsConstant()) {
5220 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5221 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5222 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005223 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005224 __ Add(temp2, obj, offset);
5225 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5226 temps.Release(temp2);
5227 }
Roland Levillain44015862016-01-22 11:47:17 +00005228 }
Roland Levillain44015862016-01-22 11:47:17 +00005229 } else {
5230 // /* HeapReference<Object> */ ref = *(obj + offset)
5231 MemOperand field = HeapOperand(obj, offset);
5232 if (use_load_acquire) {
5233 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5234 } else {
5235 Load(type, ref_reg, field);
5236 }
5237 }
5238
5239 // Object* ref = ref_addr->AsMirrorPtr()
5240 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5241
Vladimir Marko953437b2016-08-24 08:30:46 +00005242 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005243 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005244 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005245 AddSlowPath(slow_path);
5246
5247 // if (rb_state == ReadBarrier::gray_ptr_)
5248 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005249 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5250 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5251 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5252 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5253 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005254 __ Bind(slow_path->GetExitLabel());
5255}
5256
5257void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5258 Location out,
5259 Location ref,
5260 Location obj,
5261 uint32_t offset,
5262 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005263 DCHECK(kEmitCompilerReadBarrier);
5264
Roland Levillain44015862016-01-22 11:47:17 +00005265 // Insert a slow path based read barrier *after* the reference load.
5266 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005267 // If heap poisoning is enabled, the unpoisoning of the loaded
5268 // reference will be carried out by the runtime within the slow
5269 // path.
5270 //
5271 // Note that `ref` currently does not get unpoisoned (when heap
5272 // poisoning is enabled), which is alright as the `ref` argument is
5273 // not used by the artReadBarrierSlow entry point.
5274 //
5275 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5276 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5277 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5278 AddSlowPath(slow_path);
5279
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005280 __ B(slow_path->GetEntryLabel());
5281 __ Bind(slow_path->GetExitLabel());
5282}
5283
Roland Levillain44015862016-01-22 11:47:17 +00005284void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5285 Location out,
5286 Location ref,
5287 Location obj,
5288 uint32_t offset,
5289 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005290 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005291 // Baker's read barriers shall be handled by the fast path
5292 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5293 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005294 // If heap poisoning is enabled, unpoisoning will be taken care of
5295 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005296 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005297 } else if (kPoisonHeapReferences) {
5298 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5299 }
5300}
5301
Roland Levillain44015862016-01-22 11:47:17 +00005302void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5303 Location out,
5304 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005305 DCHECK(kEmitCompilerReadBarrier);
5306
Roland Levillain44015862016-01-22 11:47:17 +00005307 // Insert a slow path based read barrier *after* the GC root load.
5308 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005309 // Note that GC roots are not affected by heap poisoning, so we do
5310 // not need to do anything special for this here.
5311 SlowPathCodeARM64* slow_path =
5312 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5313 AddSlowPath(slow_path);
5314
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005315 __ B(slow_path->GetEntryLabel());
5316 __ Bind(slow_path->GetExitLabel());
5317}
5318
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005319void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5320 LocationSummary* locations =
5321 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5322 locations->SetInAt(0, Location::RequiresRegister());
5323 locations->SetOut(Location::RequiresRegister());
5324}
5325
5326void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5327 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005328 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005329 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005330 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005331 __ Ldr(XRegisterFrom(locations->Out()),
5332 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005333 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005334 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005335 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005336 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5337 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005338 __ Ldr(XRegisterFrom(locations->Out()),
5339 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005340 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005341}
5342
5343
5344
Alexandre Rames67555f72014-11-18 10:55:16 +00005345#undef __
5346#undef QUICK_ENTRY_POINT
5347
Alexandre Rames5319def2014-10-23 10:03:10 +01005348} // namespace arm64
5349} // namespace art