blob: f3ebcf40ddfe6b80f6fd20db439eb579b7e89ef9 [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);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000239 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
240 ? kQuickThrowStringBounds
241 : kQuickThrowArrayBounds;
242 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100243 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 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000266 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 }
269
Alexandre Rames8158f282015-08-07 10:26:17 +0100270 bool IsFatal() const OVERRIDE { return true; }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
273
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
276};
277
278class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
279 public:
280 LoadClassSlowPathARM64(HLoadClass* cls,
281 HInstruction* at,
282 uint32_t dex_pc,
283 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
286 }
287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
289 LocationSummary* locations = at_->GetLocations();
290 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
291
292 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000293 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000294
295 InvokeRuntimeCallingConvention calling_convention;
296 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000297 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
298 : kQuickInitializeType;
299 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800300 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100301 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800302 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100303 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800304 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000305
306 // Move the class to the desired location.
307 Location out = locations->Out();
308 if (out.IsValid()) {
309 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
310 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000311 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 }
313
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000314 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 __ B(GetExitLabel());
316 }
317
Alexandre Rames9931f312015-06-19 14:47:01 +0100318 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
319
Alexandre Rames67555f72014-11-18 10:55:16 +0000320 private:
321 // The class this slow path will load.
322 HLoadClass* const cls_;
323
324 // The instruction where this slow path is happening.
325 // (Might be the load class or an initialization check).
326 HInstruction* const at_;
327
328 // The dex PC of `at_`.
329 const uint32_t dex_pc_;
330
331 // Whether to initialize the class.
332 const bool do_clinit_;
333
334 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
335};
336
337class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
338 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000339 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000340
341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
342 LocationSummary* locations = instruction_->GetLocations();
343 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
344 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
345
346 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000347 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000348
349 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000350 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
351 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000352 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100353 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 __ B(GetExitLabel());
359 }
360
Alexandre Rames9931f312015-06-19 14:47:01 +0100361 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
365};
366
Alexandre Rames5319def2014-10-23 10:03:10 +0100367class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000369 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100370
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
372 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100373 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000374 if (instruction_->CanThrowIntoCatchBlock()) {
375 // Live registers will be restored in the catch block if caught.
376 SaveLiveRegisters(codegen, instruction_->GetLocations());
377 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000378 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
379 instruction_,
380 instruction_->GetDexPc(),
381 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());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000401 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800402 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000403 if (successor_ == nullptr) {
404 __ B(GetReturnLabel());
405 } else {
406 __ B(arm64_codegen->GetLabelOf(successor_));
407 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 }
409
Scott Wakeling97c72b72016-06-24 16:19:36 +0100410 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100411 DCHECK(successor_ == nullptr);
412 return &return_label_;
413 }
414
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100415 HBasicBlock* GetSuccessor() const {
416 return successor_;
417 }
418
Alexandre Rames9931f312015-06-19 14:47:01 +0100419 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
420
Alexandre Rames5319def2014-10-23 10:03:10 +0100421 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 // If not null, the block to branch to after the suspend check.
423 HBasicBlock* const successor_;
424
425 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100426 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100427
428 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
429};
430
Alexandre Rames67555f72014-11-18 10:55:16 +0000431class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
432 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000433 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000434 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000435
436 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000437 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100438 Location class_to_check = locations->InAt(1);
439 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
440 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000441 DCHECK(instruction_->IsCheckCast()
442 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
443 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100444 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445
Alexandre Rames67555f72014-11-18 10:55:16 +0000446 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000447
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000448 if (!is_fatal_) {
449 SaveLiveRegisters(codegen, locations);
450 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451
452 // We're moving two locations to locations that could overlap, so we need a parallel
453 // move resolver.
454 InvokeRuntimeCallingConvention calling_convention;
455 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100456 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
457 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458
459 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000460 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700461 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000462 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000463 Primitive::Type ret_type = instruction_->GetType();
464 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
465 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
466 } else {
467 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000468 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800469 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000470 }
471
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000472 if (!is_fatal_) {
473 RestoreLiveRegisters(codegen, locations);
474 __ B(GetExitLabel());
475 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000476 }
477
Alexandre Rames9931f312015-06-19 14:47:01 +0100478 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000479 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100480
Alexandre Rames67555f72014-11-18 10:55:16 +0000481 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000482 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000483
Alexandre Rames67555f72014-11-18 10:55:16 +0000484 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
485};
486
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700487class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
488 public:
Aart Bik42249c32016-01-07 15:33:50 -0800489 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000490 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700491
492 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800493 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700494 __ Bind(GetEntryLabel());
495 SaveLiveRegisters(codegen, instruction_->GetLocations());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000497 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 }
499
Alexandre Rames9931f312015-06-19 14:47:01 +0100500 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
501
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700502 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
504};
505
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100506class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
507 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000508 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100509
510 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
511 LocationSummary* locations = instruction_->GetLocations();
512 __ Bind(GetEntryLabel());
513 SaveLiveRegisters(codegen, locations);
514
515 InvokeRuntimeCallingConvention calling_convention;
516 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
517 parallel_move.AddMove(
518 locations->InAt(0),
519 LocationFrom(calling_convention.GetRegisterAt(0)),
520 Primitive::kPrimNot,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(1),
524 LocationFrom(calling_convention.GetRegisterAt(1)),
525 Primitive::kPrimInt,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(2),
529 LocationFrom(calling_convention.GetRegisterAt(2)),
530 Primitive::kPrimNot,
531 nullptr);
532 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
533
534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000535 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100536 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
537 RestoreLiveRegisters(codegen, locations);
538 __ B(GetExitLabel());
539 }
540
541 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
542
543 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
545};
546
Zheng Xu3927c8b2015-11-18 17:46:25 +0800547void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
548 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000549 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800550
551 // We are about to use the assembler to place literals directly. Make sure we have enough
552 // underlying code buffer and we have generated the jump table with right size.
553 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
554 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
555
556 __ Bind(&table_start_);
557 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
558 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100559 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800560 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100561 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800562 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
563 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
564 Literal<int32_t> literal(jump_offset);
565 __ place(&literal);
566 }
567}
568
Roland Levillain44015862016-01-22 11:47:17 +0000569// Slow path marking an object during a read barrier.
570class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
571 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100572 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
573 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000574 DCHECK(kEmitCompilerReadBarrier);
575 }
576
577 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000581 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100582 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000583 DCHECK(instruction_->IsInstanceFieldGet() ||
584 instruction_->IsStaticFieldGet() ||
585 instruction_->IsArrayGet() ||
586 instruction_->IsLoadClass() ||
587 instruction_->IsLoadString() ||
588 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100589 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100590 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
591 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000592 << "Unexpected instruction in read barrier marking slow path: "
593 << instruction_->DebugName();
594
595 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100596 // No need to save live registers; it's taken care of by the
597 // entrypoint. Also, there is no need to update the stack mask,
598 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000599 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100600 DCHECK_NE(obj_.reg(), LR);
601 DCHECK_NE(obj_.reg(), WSP);
602 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100603 // IP0 is used internally by the ReadBarrierMarkRegX entry point
604 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700605 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100606 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
607 // "Compact" slow path, saving two moves.
608 //
609 // Instead of using the standard runtime calling convention (input
610 // and output in W0):
611 //
612 // W0 <- obj
613 // W0 <- ReadBarrierMark(W0)
614 // obj <- W0
615 //
616 // we just use rX (the register holding `obj`) as input and output
617 // of a dedicated entrypoint:
618 //
619 // rX <- ReadBarrierMarkRegX(rX)
620 //
621 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700622 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100623 // This runtime call does not require a stack map.
624 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000625 __ B(GetExitLabel());
626 }
627
628 private:
Roland Levillain44015862016-01-22 11:47:17 +0000629 const Location obj_;
630
631 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
632};
633
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000634// Slow path generating a read barrier for a heap reference.
635class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
636 public:
637 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
638 Location out,
639 Location ref,
640 Location obj,
641 uint32_t offset,
642 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000643 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000644 out_(out),
645 ref_(ref),
646 obj_(obj),
647 offset_(offset),
648 index_(index) {
649 DCHECK(kEmitCompilerReadBarrier);
650 // If `obj` is equal to `out` or `ref`, it means the initial object
651 // has been overwritten by (or after) the heap object reference load
652 // to be instrumented, e.g.:
653 //
654 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000655 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000656 //
657 // In that case, we have lost the information about the original
658 // object, and the emitted read barrier cannot work properly.
659 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
660 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
661 }
662
663 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
664 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
665 LocationSummary* locations = instruction_->GetLocations();
666 Primitive::Type type = Primitive::kPrimNot;
667 DCHECK(locations->CanCall());
668 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100669 DCHECK(instruction_->IsInstanceFieldGet() ||
670 instruction_->IsStaticFieldGet() ||
671 instruction_->IsArrayGet() ||
672 instruction_->IsInstanceOf() ||
673 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100674 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000675 << "Unexpected instruction in read barrier for heap reference slow path: "
676 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000677 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000678 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100679 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000680
681 __ Bind(GetEntryLabel());
682
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000683 SaveLiveRegisters(codegen, locations);
684
685 // We may have to change the index's value, but as `index_` is a
686 // constant member (like other "inputs" of this slow path),
687 // introduce a copy of it, `index`.
688 Location index = index_;
689 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100690 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000691 if (instruction_->IsArrayGet()) {
692 // Compute the actual memory offset and store it in `index`.
693 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
694 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
695 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
696 // We are about to change the value of `index_reg` (see the
697 // calls to vixl::MacroAssembler::Lsl and
698 // vixl::MacroAssembler::Mov below), but it has
699 // not been saved by the previous call to
700 // art::SlowPathCode::SaveLiveRegisters, as it is a
701 // callee-save register --
702 // art::SlowPathCode::SaveLiveRegisters does not consider
703 // callee-save registers, as it has been designed with the
704 // assumption that callee-save registers are supposed to be
705 // handled by the called function. So, as a callee-save
706 // register, `index_reg` _would_ eventually be saved onto
707 // the stack, but it would be too late: we would have
708 // changed its value earlier. Therefore, we manually save
709 // it here into another freely available register,
710 // `free_reg`, chosen of course among the caller-save
711 // registers (as a callee-save `free_reg` register would
712 // exhibit the same problem).
713 //
714 // Note we could have requested a temporary register from
715 // the register allocator instead; but we prefer not to, as
716 // this is a slow path, and we know we can find a
717 // caller-save register that is available.
718 Register free_reg = FindAvailableCallerSaveRegister(codegen);
719 __ Mov(free_reg.W(), index_reg);
720 index_reg = free_reg;
721 index = LocationFrom(index_reg);
722 } else {
723 // The initial register stored in `index_` has already been
724 // saved in the call to art::SlowPathCode::SaveLiveRegisters
725 // (as it is not a callee-save register), so we can freely
726 // use it.
727 }
728 // Shifting the index value contained in `index_reg` by the scale
729 // factor (2) cannot overflow in practice, as the runtime is
730 // unable to allocate object arrays with a size larger than
731 // 2^26 - 1 (that is, 2^28 - 4 bytes).
732 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
733 static_assert(
734 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
735 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
736 __ Add(index_reg, index_reg, Operand(offset_));
737 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100738 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
739 // intrinsics, `index_` is not shifted by a scale factor of 2
740 // (as in the case of ArrayGet), as it is actually an offset
741 // to an object field within an object.
742 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000743 DCHECK(instruction_->GetLocations()->Intrinsified());
744 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
745 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
746 << instruction_->AsInvoke()->GetIntrinsic();
747 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100748 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000749 }
750 }
751
752 // We're moving two or three locations to locations that could
753 // overlap, so we need a parallel move resolver.
754 InvokeRuntimeCallingConvention calling_convention;
755 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
756 parallel_move.AddMove(ref_,
757 LocationFrom(calling_convention.GetRegisterAt(0)),
758 type,
759 nullptr);
760 parallel_move.AddMove(obj_,
761 LocationFrom(calling_convention.GetRegisterAt(1)),
762 type,
763 nullptr);
764 if (index.IsValid()) {
765 parallel_move.AddMove(index,
766 LocationFrom(calling_convention.GetRegisterAt(2)),
767 Primitive::kPrimInt,
768 nullptr);
769 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
770 } else {
771 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
772 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
773 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000774 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000775 instruction_,
776 instruction_->GetDexPc(),
777 this);
778 CheckEntrypointTypes<
779 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
780 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
781
782 RestoreLiveRegisters(codegen, locations);
783
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000784 __ B(GetExitLabel());
785 }
786
787 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
788
789 private:
790 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100791 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
792 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000793 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
794 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
795 return Register(VIXLRegCodeFromART(i), kXRegSize);
796 }
797 }
798 // We shall never fail to find a free caller-save register, as
799 // there are more than two core caller-save registers on ARM64
800 // (meaning it is possible to find one which is different from
801 // `ref` and `obj`).
802 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
803 LOG(FATAL) << "Could not find a free register";
804 UNREACHABLE();
805 }
806
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807 const Location out_;
808 const Location ref_;
809 const Location obj_;
810 const uint32_t offset_;
811 // An additional location containing an index to an array.
812 // Only used for HArrayGet and the UnsafeGetObject &
813 // UnsafeGetObjectVolatile intrinsics.
814 const Location index_;
815
816 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
817};
818
819// Slow path generating a read barrier for a GC root.
820class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
821 public:
822 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000823 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000824 DCHECK(kEmitCompilerReadBarrier);
825 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000826
827 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
828 LocationSummary* locations = instruction_->GetLocations();
829 Primitive::Type type = Primitive::kPrimNot;
830 DCHECK(locations->CanCall());
831 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000832 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
833 << "Unexpected instruction in read barrier for GC root slow path: "
834 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000835
836 __ Bind(GetEntryLabel());
837 SaveLiveRegisters(codegen, locations);
838
839 InvokeRuntimeCallingConvention calling_convention;
840 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
841 // The argument of the ReadBarrierForRootSlow is not a managed
842 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
843 // thus we need a 64-bit move here, and we cannot use
844 //
845 // arm64_codegen->MoveLocation(
846 // LocationFrom(calling_convention.GetRegisterAt(0)),
847 // root_,
848 // type);
849 //
850 // which would emit a 32-bit move, as `type` is a (32-bit wide)
851 // reference type (`Primitive::kPrimNot`).
852 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000853 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000854 instruction_,
855 instruction_->GetDexPc(),
856 this);
857 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
858 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
859
860 RestoreLiveRegisters(codegen, locations);
861 __ B(GetExitLabel());
862 }
863
864 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
865
866 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000867 const Location out_;
868 const Location root_;
869
870 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
871};
872
Alexandre Rames5319def2014-10-23 10:03:10 +0100873#undef __
874
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100875Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100876 Location next_location;
877 if (type == Primitive::kPrimVoid) {
878 LOG(FATAL) << "Unreachable type " << type;
879 }
880
Alexandre Rames542361f2015-01-29 16:57:31 +0000881 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100882 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
883 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000884 } else if (!Primitive::IsFloatingPointType(type) &&
885 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000886 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
887 } else {
888 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000889 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
890 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100891 }
892
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000893 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000894 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100895 return next_location;
896}
897
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100898Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100899 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100900}
901
Serban Constantinescu579885a2015-02-22 20:51:33 +0000902CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
903 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100904 const CompilerOptions& compiler_options,
905 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100906 : CodeGenerator(graph,
907 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000908 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000909 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100910 callee_saved_core_registers.GetList(),
911 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100912 compiler_options,
913 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100914 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800915 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100916 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000917 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000918 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100919 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000920 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000921 uint32_literals_(std::less<uint32_t>(),
922 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100923 uint64_literals_(std::less<uint64_t>(),
924 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
925 method_patches_(MethodReferenceComparator(),
926 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
927 call_patches_(MethodReferenceComparator(),
928 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
929 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000930 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
931 boot_image_string_patches_(StringReferenceValueComparator(),
932 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
933 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100934 boot_image_type_patches_(TypeReferenceValueComparator(),
935 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
936 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000937 boot_image_address_patches_(std::less<uint32_t>(),
938 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000939 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000940 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000941}
Alexandre Rames5319def2014-10-23 10:03:10 +0100942
Alexandre Rames67555f72014-11-18 10:55:16 +0000943#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100944
Zheng Xu3927c8b2015-11-18 17:46:25 +0800945void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100946 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800947 jump_table->EmitTable(this);
948 }
949}
950
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000951void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800952 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000953 // Ensure we emit the literal pool.
954 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000955
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000956 CodeGenerator::Finalize(allocator);
957}
958
Zheng Xuad4450e2015-04-17 18:48:56 +0800959void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
960 // Note: There are 6 kinds of moves:
961 // 1. constant -> GPR/FPR (non-cycle)
962 // 2. constant -> stack (non-cycle)
963 // 3. GPR/FPR -> GPR/FPR
964 // 4. GPR/FPR -> stack
965 // 5. stack -> GPR/FPR
966 // 6. stack -> stack (non-cycle)
967 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
968 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
969 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
970 // dependency.
971 vixl_temps_.Open(GetVIXLAssembler());
972}
973
974void ParallelMoveResolverARM64::FinishEmitNativeCode() {
975 vixl_temps_.Close();
976}
977
978Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
979 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
980 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
981 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
982 Location scratch = GetScratchLocation(kind);
983 if (!scratch.Equals(Location::NoLocation())) {
984 return scratch;
985 }
986 // Allocate from VIXL temp registers.
987 if (kind == Location::kRegister) {
988 scratch = LocationFrom(vixl_temps_.AcquireX());
989 } else {
990 DCHECK(kind == Location::kFpuRegister);
991 scratch = LocationFrom(vixl_temps_.AcquireD());
992 }
993 AddScratchLocation(scratch);
994 return scratch;
995}
996
997void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
998 if (loc.IsRegister()) {
999 vixl_temps_.Release(XRegisterFrom(loc));
1000 } else {
1001 DCHECK(loc.IsFpuRegister());
1002 vixl_temps_.Release(DRegisterFrom(loc));
1003 }
1004 RemoveScratchLocation(loc);
1005}
1006
Alexandre Rames3e69f162014-12-10 10:36:50 +00001007void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001008 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001009 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001010}
1011
Alexandre Rames5319def2014-10-23 10:03:10 +01001012void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001013 MacroAssembler* masm = GetVIXLAssembler();
1014 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001015 __ Bind(&frame_entry_label_);
1016
Serban Constantinescu02164b32014-11-13 14:05:07 +00001017 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1018 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001019 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001020 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001021 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001022 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001023 __ Ldr(wzr, MemOperand(temp, 0));
1024 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001025 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001026
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001027 if (!HasEmptyFrame()) {
1028 int frame_size = GetFrameSize();
1029 // Stack layout:
1030 // sp[frame_size - 8] : lr.
1031 // ... : other preserved core registers.
1032 // ... : other preserved fp registers.
1033 // ... : reserved frame space.
1034 // sp[0] : current method.
1035 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001036 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001037 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1038 frame_size - GetCoreSpillSize());
1039 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1040 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001041 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001042}
1043
1044void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001045 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001046 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001047 if (!HasEmptyFrame()) {
1048 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001049 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1050 frame_size - FrameEntrySpillSize());
1051 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1052 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001053 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001054 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001055 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001056 __ Ret();
1057 GetAssembler()->cfi().RestoreState();
1058 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001059}
1060
Scott Wakeling97c72b72016-06-24 16:19:36 +01001061CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001062 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001063 return CPURegList(CPURegister::kRegister, kXRegSize,
1064 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001065}
1066
Scott Wakeling97c72b72016-06-24 16:19:36 +01001067CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001068 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1069 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001070 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1071 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001072}
1073
Alexandre Rames5319def2014-10-23 10:03:10 +01001074void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1075 __ Bind(GetLabelOf(block));
1076}
1077
Calin Juravle175dc732015-08-25 15:42:32 +01001078void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1079 DCHECK(location.IsRegister());
1080 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1081}
1082
Calin Juravlee460d1d2015-09-29 04:52:17 +01001083void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1084 if (location.IsRegister()) {
1085 locations->AddTemp(location);
1086 } else {
1087 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1088 }
1089}
1090
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001091void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001092 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001093 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001094 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001095 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001096 if (value_can_be_null) {
1097 __ Cbz(value, &done);
1098 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001099 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001100 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001101 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001102 if (value_can_be_null) {
1103 __ Bind(&done);
1104 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001105}
1106
David Brazdil58282f42016-01-14 12:45:10 +00001107void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001108 // Blocked core registers:
1109 // lr : Runtime reserved.
1110 // tr : Runtime reserved.
1111 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1112 // ip1 : VIXL core temp.
1113 // ip0 : VIXL core temp.
1114 //
1115 // Blocked fp registers:
1116 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001117 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1118 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001120 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001122
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001123 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001124 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001125 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001126 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001127
David Brazdil58282f42016-01-14 12:45:10 +00001128 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001129 // Stubs do not save callee-save floating point registers. If the graph
1130 // is debuggable, we need to deal with these registers differently. For
1131 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001132 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1133 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001134 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001135 }
1136 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001137}
1138
Alexandre Rames3e69f162014-12-10 10:36:50 +00001139size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1140 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1141 __ Str(reg, MemOperand(sp, stack_index));
1142 return kArm64WordSize;
1143}
1144
1145size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1146 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1147 __ Ldr(reg, MemOperand(sp, stack_index));
1148 return kArm64WordSize;
1149}
1150
1151size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1152 FPRegister reg = FPRegister(reg_id, kDRegSize);
1153 __ Str(reg, MemOperand(sp, stack_index));
1154 return kArm64WordSize;
1155}
1156
1157size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1158 FPRegister reg = FPRegister(reg_id, kDRegSize);
1159 __ Ldr(reg, MemOperand(sp, stack_index));
1160 return kArm64WordSize;
1161}
1162
Alexandre Rames5319def2014-10-23 10:03:10 +01001163void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001164 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001165}
1166
1167void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001168 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001169}
1170
Alexandre Rames67555f72014-11-18 10:55:16 +00001171void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001172 if (constant->IsIntConstant()) {
1173 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1174 } else if (constant->IsLongConstant()) {
1175 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1176 } else if (constant->IsNullConstant()) {
1177 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001178 } else if (constant->IsFloatConstant()) {
1179 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1180 } else {
1181 DCHECK(constant->IsDoubleConstant());
1182 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1183 }
1184}
1185
Alexandre Rames3e69f162014-12-10 10:36:50 +00001186
1187static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1188 DCHECK(constant.IsConstant());
1189 HConstant* cst = constant.GetConstant();
1190 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001191 // Null is mapped to a core W register, which we associate with kPrimInt.
1192 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001193 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1194 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1195 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1196}
1197
Calin Juravlee460d1d2015-09-29 04:52:17 +01001198void CodeGeneratorARM64::MoveLocation(Location destination,
1199 Location source,
1200 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001201 if (source.Equals(destination)) {
1202 return;
1203 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001204
1205 // A valid move can always be inferred from the destination and source
1206 // locations. When moving from and to a register, the argument type can be
1207 // used to generate 32bit instead of 64bit moves. In debug mode we also
1208 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001209 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001210
1211 if (destination.IsRegister() || destination.IsFpuRegister()) {
1212 if (unspecified_type) {
1213 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1214 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001215 (src_cst != nullptr && (src_cst->IsIntConstant()
1216 || src_cst->IsFloatConstant()
1217 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001218 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001219 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001220 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001221 // If the source is a double stack slot or a 64bit constant, a 64bit
1222 // type is appropriate. Else the source is a register, and since the
1223 // type has not been specified, we chose a 64bit type to force a 64bit
1224 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001225 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001226 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001227 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1229 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1230 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001231 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1232 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1233 __ Ldr(dst, StackOperandFrom(source));
1234 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001235 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001236 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001241 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001242 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1243 ? Primitive::kPrimLong
1244 : Primitive::kPrimInt;
1245 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1246 }
1247 } else {
1248 DCHECK(source.IsFpuRegister());
1249 if (destination.IsRegister()) {
1250 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1251 ? Primitive::kPrimDouble
1252 : Primitive::kPrimFloat;
1253 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1254 } else {
1255 DCHECK(destination.IsFpuRegister());
1256 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001257 }
1258 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001259 } else { // The destination is not a register. It must be a stack slot.
1260 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1261 if (source.IsRegister() || source.IsFpuRegister()) {
1262 if (unspecified_type) {
1263 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001264 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001265 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 }
1268 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001269 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1270 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1271 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001272 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1274 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001275 UseScratchRegisterScope temps(GetVIXLAssembler());
1276 HConstant* src_cst = source.GetConstant();
1277 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001278 if (src_cst->IsZeroBitPattern()) {
1279 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001281 if (src_cst->IsIntConstant()) {
1282 temp = temps.AcquireW();
1283 } else if (src_cst->IsLongConstant()) {
1284 temp = temps.AcquireX();
1285 } else if (src_cst->IsFloatConstant()) {
1286 temp = temps.AcquireS();
1287 } else {
1288 DCHECK(src_cst->IsDoubleConstant());
1289 temp = temps.AcquireD();
1290 }
1291 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001292 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001293 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001294 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001295 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001296 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001297 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001298 // There is generally less pressure on FP registers.
1299 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001300 __ Ldr(temp, StackOperandFrom(source));
1301 __ Str(temp, StackOperandFrom(destination));
1302 }
1303 }
1304}
1305
1306void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001307 CPURegister dst,
1308 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001309 switch (type) {
1310 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001311 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001312 break;
1313 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001314 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001315 break;
1316 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001317 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001318 break;
1319 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001320 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001321 break;
1322 case Primitive::kPrimInt:
1323 case Primitive::kPrimNot:
1324 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001325 case Primitive::kPrimFloat:
1326 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001327 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001328 __ Ldr(dst, src);
1329 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001330 case Primitive::kPrimVoid:
1331 LOG(FATAL) << "Unreachable type " << type;
1332 }
1333}
1334
Calin Juravle77520bc2015-01-12 18:45:46 +00001335void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001336 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001337 const MemOperand& src,
1338 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001339 MacroAssembler* masm = GetVIXLAssembler();
1340 BlockPoolsScope block_pools(masm);
1341 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001342 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001343 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001344
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 DCHECK(!src.IsPreIndex());
1346 DCHECK(!src.IsPostIndex());
1347
1348 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001349 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001350 MemOperand base = MemOperand(temp_base);
1351 switch (type) {
1352 case Primitive::kPrimBoolean:
1353 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001354 if (needs_null_check) {
1355 MaybeRecordImplicitNullCheck(instruction);
1356 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001357 break;
1358 case Primitive::kPrimByte:
1359 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001360 if (needs_null_check) {
1361 MaybeRecordImplicitNullCheck(instruction);
1362 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001363 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1364 break;
1365 case Primitive::kPrimChar:
1366 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001367 if (needs_null_check) {
1368 MaybeRecordImplicitNullCheck(instruction);
1369 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001370 break;
1371 case Primitive::kPrimShort:
1372 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001373 if (needs_null_check) {
1374 MaybeRecordImplicitNullCheck(instruction);
1375 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001376 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1377 break;
1378 case Primitive::kPrimInt:
1379 case Primitive::kPrimNot:
1380 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001381 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001382 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001383 if (needs_null_check) {
1384 MaybeRecordImplicitNullCheck(instruction);
1385 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001386 break;
1387 case Primitive::kPrimFloat:
1388 case Primitive::kPrimDouble: {
1389 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001390 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391
1392 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1393 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001394 if (needs_null_check) {
1395 MaybeRecordImplicitNullCheck(instruction);
1396 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001397 __ Fmov(FPRegister(dst), temp);
1398 break;
1399 }
1400 case Primitive::kPrimVoid:
1401 LOG(FATAL) << "Unreachable type " << type;
1402 }
1403}
1404
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001405void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 CPURegister src,
1407 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001408 switch (type) {
1409 case Primitive::kPrimBoolean:
1410 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001412 break;
1413 case Primitive::kPrimChar:
1414 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001415 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001416 break;
1417 case Primitive::kPrimInt:
1418 case Primitive::kPrimNot:
1419 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001420 case Primitive::kPrimFloat:
1421 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001422 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001423 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001424 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001425 case Primitive::kPrimVoid:
1426 LOG(FATAL) << "Unreachable type " << type;
1427 }
1428}
1429
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001430void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1431 CPURegister src,
1432 const MemOperand& dst) {
1433 UseScratchRegisterScope temps(GetVIXLAssembler());
1434 Register temp_base = temps.AcquireX();
1435
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001436 DCHECK(!dst.IsPreIndex());
1437 DCHECK(!dst.IsPostIndex());
1438
1439 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001440 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001441 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001442 MemOperand base = MemOperand(temp_base);
1443 switch (type) {
1444 case Primitive::kPrimBoolean:
1445 case Primitive::kPrimByte:
1446 __ Stlrb(Register(src), base);
1447 break;
1448 case Primitive::kPrimChar:
1449 case Primitive::kPrimShort:
1450 __ Stlrh(Register(src), base);
1451 break;
1452 case Primitive::kPrimInt:
1453 case Primitive::kPrimNot:
1454 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001455 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001456 __ Stlr(Register(src), base);
1457 break;
1458 case Primitive::kPrimFloat:
1459 case Primitive::kPrimDouble: {
1460 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001461 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001462
1463 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1464 __ Fmov(temp, FPRegister(src));
1465 __ Stlr(temp, base);
1466 break;
1467 }
1468 case Primitive::kPrimVoid:
1469 LOG(FATAL) << "Unreachable type " << type;
1470 }
1471}
1472
Calin Juravle175dc732015-08-25 15:42:32 +01001473void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1474 HInstruction* instruction,
1475 uint32_t dex_pc,
1476 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001478 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Roland Levillain896e32d2015-05-05 18:07:10 +01001479 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001480}
1481
Roland Levillaindec8f632016-07-22 17:10:06 +01001482void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1483 HInstruction* instruction,
1484 SlowPathCode* slow_path) {
1485 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001486 GenerateInvokeRuntime(entry_point_offset);
1487}
1488
1489void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001490 BlockPoolsScope block_pools(GetVIXLAssembler());
1491 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1492 __ Blr(lr);
1493}
1494
Alexandre Rames67555f72014-11-18 10:55:16 +00001495void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001496 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001497 UseScratchRegisterScope temps(GetVIXLAssembler());
1498 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001499 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1500
Serban Constantinescu02164b32014-11-13 14:05:07 +00001501 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001502 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1503 __ Add(temp, class_reg, status_offset);
1504 __ Ldar(temp, HeapOperand(temp));
1505 __ Cmp(temp, mirror::Class::kStatusInitialized);
1506 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001507 __ Bind(slow_path->GetExitLabel());
1508}
Alexandre Rames5319def2014-10-23 10:03:10 +01001509
Roland Levillain44015862016-01-22 11:47:17 +00001510void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001511 BarrierType type = BarrierAll;
1512
1513 switch (kind) {
1514 case MemBarrierKind::kAnyAny:
1515 case MemBarrierKind::kAnyStore: {
1516 type = BarrierAll;
1517 break;
1518 }
1519 case MemBarrierKind::kLoadAny: {
1520 type = BarrierReads;
1521 break;
1522 }
1523 case MemBarrierKind::kStoreStore: {
1524 type = BarrierWrites;
1525 break;
1526 }
1527 default:
1528 LOG(FATAL) << "Unexpected memory barrier " << kind;
1529 }
1530 __ Dmb(InnerShareable, type);
1531}
1532
Serban Constantinescu02164b32014-11-13 14:05:07 +00001533void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1534 HBasicBlock* successor) {
1535 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001536 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1537 if (slow_path == nullptr) {
1538 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1539 instruction->SetSlowPath(slow_path);
1540 codegen_->AddSlowPath(slow_path);
1541 if (successor != nullptr) {
1542 DCHECK(successor->IsLoopHeader());
1543 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1544 }
1545 } else {
1546 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1547 }
1548
Serban Constantinescu02164b32014-11-13 14:05:07 +00001549 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1550 Register temp = temps.AcquireW();
1551
Andreas Gampe542451c2016-07-26 09:02:02 -07001552 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001553 if (successor == nullptr) {
1554 __ Cbnz(temp, slow_path->GetEntryLabel());
1555 __ Bind(slow_path->GetReturnLabel());
1556 } else {
1557 __ Cbz(temp, codegen_->GetLabelOf(successor));
1558 __ B(slow_path->GetEntryLabel());
1559 // slow_path will return to GetLabelOf(successor).
1560 }
1561}
1562
Alexandre Rames5319def2014-10-23 10:03:10 +01001563InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1564 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001565 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001566 assembler_(codegen->GetAssembler()),
1567 codegen_(codegen) {}
1568
1569#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001570 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001571
1572#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1573
1574enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001575 // Using a base helps identify when we hit such breakpoints.
1576 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001577#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1578 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1579#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1580};
1581
1582#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001583 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001584 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1585 } \
1586 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1588 locations->SetOut(Location::Any()); \
1589 }
1590 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1591#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1592
1593#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001594#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001595
Alexandre Rames67555f72014-11-18 10:55:16 +00001596void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001597 DCHECK_EQ(instr->InputCount(), 2U);
1598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1599 Primitive::Type type = instr->GetResultType();
1600 switch (type) {
1601 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001602 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001603 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001604 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001606 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001607
1608 case Primitive::kPrimFloat:
1609 case Primitive::kPrimDouble:
1610 locations->SetInAt(0, Location::RequiresFpuRegister());
1611 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001612 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001613 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001614
Alexandre Rames5319def2014-10-23 10:03:10 +01001615 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001616 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001617 }
1618}
1619
Alexandre Rames09a99962015-04-15 11:47:56 +01001620void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001621 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1622
1623 bool object_field_get_with_read_barrier =
1624 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001625 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001626 new (GetGraph()->GetArena()) LocationSummary(instruction,
1627 object_field_get_with_read_barrier ?
1628 LocationSummary::kCallOnSlowPath :
1629 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001630 locations->SetInAt(0, Location::RequiresRegister());
1631 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1632 locations->SetOut(Location::RequiresFpuRegister());
1633 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001634 // The output overlaps for an object field get when read barriers
1635 // are enabled: we do not want the load to overwrite the object's
1636 // location, as we need it to emit the read barrier.
1637 locations->SetOut(
1638 Location::RequiresRegister(),
1639 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001640 }
1641}
1642
1643void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1644 const FieldInfo& field_info) {
1645 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001646 LocationSummary* locations = instruction->GetLocations();
1647 Location base_loc = locations->InAt(0);
1648 Location out = locations->Out();
1649 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001650 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001651 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001652 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001653
Roland Levillain44015862016-01-22 11:47:17 +00001654 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1655 // Object FieldGet with Baker's read barrier case.
1656 MacroAssembler* masm = GetVIXLAssembler();
1657 UseScratchRegisterScope temps(masm);
1658 // /* HeapReference<Object> */ out = *(base + offset)
1659 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1660 Register temp = temps.AcquireW();
1661 // Note that potential implicit null checks are handled in this
1662 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1663 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1664 instruction,
1665 out,
1666 base,
1667 offset,
1668 temp,
1669 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001670 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001671 } else {
1672 // General case.
1673 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001674 // Note that a potential implicit null check is handled in this
1675 // CodeGeneratorARM64::LoadAcquire call.
1676 // NB: LoadAcquire will record the pc info if needed.
1677 codegen_->LoadAcquire(
1678 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001679 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001680 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001681 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001682 }
Roland Levillain44015862016-01-22 11:47:17 +00001683 if (field_type == Primitive::kPrimNot) {
1684 // If read barriers are enabled, emit read barriers other than
1685 // Baker's using a slow path (and also unpoison the loaded
1686 // reference, if heap poisoning is enabled).
1687 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1688 }
Roland Levillain4d027112015-07-01 15:41:14 +01001689 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001690}
1691
1692void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1693 LocationSummary* locations =
1694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1695 locations->SetInAt(0, Location::RequiresRegister());
1696 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1697 locations->SetInAt(1, Location::RequiresFpuRegister());
1698 } else {
1699 locations->SetInAt(1, Location::RequiresRegister());
1700 }
1701}
1702
1703void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001704 const FieldInfo& field_info,
1705 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001706 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001707 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001708
1709 Register obj = InputRegisterAt(instruction, 0);
1710 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001711 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001712 Offset offset = field_info.GetFieldOffset();
1713 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001714
Roland Levillain4d027112015-07-01 15:41:14 +01001715 {
1716 // We use a block to end the scratch scope before the write barrier, thus
1717 // freeing the temporary registers so they can be used in `MarkGCCard`.
1718 UseScratchRegisterScope temps(GetVIXLAssembler());
1719
1720 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1721 DCHECK(value.IsW());
1722 Register temp = temps.AcquireW();
1723 __ Mov(temp, value.W());
1724 GetAssembler()->PoisonHeapReference(temp.W());
1725 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001726 }
Roland Levillain4d027112015-07-01 15:41:14 +01001727
1728 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001729 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1730 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001731 } else {
1732 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1733 codegen_->MaybeRecordImplicitNullCheck(instruction);
1734 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001735 }
1736
1737 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001738 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001739 }
1740}
1741
Alexandre Rames67555f72014-11-18 10:55:16 +00001742void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001743 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001744
1745 switch (type) {
1746 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001747 case Primitive::kPrimLong: {
1748 Register dst = OutputRegister(instr);
1749 Register lhs = InputRegisterAt(instr, 0);
1750 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001751 if (instr->IsAdd()) {
1752 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001753 } else if (instr->IsAnd()) {
1754 __ And(dst, lhs, rhs);
1755 } else if (instr->IsOr()) {
1756 __ Orr(dst, lhs, rhs);
1757 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001758 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001759 } else if (instr->IsRor()) {
1760 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001761 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001762 __ Ror(dst, lhs, shift);
1763 } else {
1764 // Ensure shift distance is in the same size register as the result. If
1765 // we are rotating a long and the shift comes in a w register originally,
1766 // we don't need to sxtw for use as an x since the shift distances are
1767 // all & reg_bits - 1.
1768 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1769 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001770 } else {
1771 DCHECK(instr->IsXor());
1772 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001773 }
1774 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001775 }
1776 case Primitive::kPrimFloat:
1777 case Primitive::kPrimDouble: {
1778 FPRegister dst = OutputFPRegister(instr);
1779 FPRegister lhs = InputFPRegisterAt(instr, 0);
1780 FPRegister rhs = InputFPRegisterAt(instr, 1);
1781 if (instr->IsAdd()) {
1782 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001784 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001785 } else {
1786 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001787 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001788 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001789 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001790 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001791 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001792 }
1793}
1794
Serban Constantinescu02164b32014-11-13 14:05:07 +00001795void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1796 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1797
1798 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1799 Primitive::Type type = instr->GetResultType();
1800 switch (type) {
1801 case Primitive::kPrimInt:
1802 case Primitive::kPrimLong: {
1803 locations->SetInAt(0, Location::RequiresRegister());
1804 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1805 locations->SetOut(Location::RequiresRegister());
1806 break;
1807 }
1808 default:
1809 LOG(FATAL) << "Unexpected shift type " << type;
1810 }
1811}
1812
1813void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1814 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1815
1816 Primitive::Type type = instr->GetType();
1817 switch (type) {
1818 case Primitive::kPrimInt:
1819 case Primitive::kPrimLong: {
1820 Register dst = OutputRegister(instr);
1821 Register lhs = InputRegisterAt(instr, 0);
1822 Operand rhs = InputOperandAt(instr, 1);
1823 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001824 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001825 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001826 if (instr->IsShl()) {
1827 __ Lsl(dst, lhs, shift_value);
1828 } else if (instr->IsShr()) {
1829 __ Asr(dst, lhs, shift_value);
1830 } else {
1831 __ Lsr(dst, lhs, shift_value);
1832 }
1833 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001834 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001835
1836 if (instr->IsShl()) {
1837 __ Lsl(dst, lhs, rhs_reg);
1838 } else if (instr->IsShr()) {
1839 __ Asr(dst, lhs, rhs_reg);
1840 } else {
1841 __ Lsr(dst, lhs, rhs_reg);
1842 }
1843 }
1844 break;
1845 }
1846 default:
1847 LOG(FATAL) << "Unexpected shift operation type " << type;
1848 }
1849}
1850
Alexandre Rames5319def2014-10-23 10:03:10 +01001851void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001852 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001853}
1854
1855void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001856 HandleBinaryOp(instruction);
1857}
1858
1859void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1860 HandleBinaryOp(instruction);
1861}
1862
1863void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1864 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001865}
1866
Artem Serov7fc63502016-02-09 17:15:29 +00001867void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001868 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1869 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1870 locations->SetInAt(0, Location::RequiresRegister());
1871 // There is no immediate variant of negated bitwise instructions in AArch64.
1872 locations->SetInAt(1, Location::RequiresRegister());
1873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1874}
1875
Artem Serov7fc63502016-02-09 17:15:29 +00001876void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001877 Register dst = OutputRegister(instr);
1878 Register lhs = InputRegisterAt(instr, 0);
1879 Register rhs = InputRegisterAt(instr, 1);
1880
1881 switch (instr->GetOpKind()) {
1882 case HInstruction::kAnd:
1883 __ Bic(dst, lhs, rhs);
1884 break;
1885 case HInstruction::kOr:
1886 __ Orn(dst, lhs, rhs);
1887 break;
1888 case HInstruction::kXor:
1889 __ Eon(dst, lhs, rhs);
1890 break;
1891 default:
1892 LOG(FATAL) << "Unreachable";
1893 }
1894}
1895
Alexandre Rames8626b742015-11-25 16:28:08 +00001896void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1897 HArm64DataProcWithShifterOp* instruction) {
1898 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1899 instruction->GetType() == Primitive::kPrimLong);
1900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1902 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1903 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1904 } else {
1905 locations->SetInAt(0, Location::RequiresRegister());
1906 }
1907 locations->SetInAt(1, Location::RequiresRegister());
1908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1909}
1910
1911void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1912 HArm64DataProcWithShifterOp* instruction) {
1913 Primitive::Type type = instruction->GetType();
1914 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1915 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1916 Register out = OutputRegister(instruction);
1917 Register left;
1918 if (kind != HInstruction::kNeg) {
1919 left = InputRegisterAt(instruction, 0);
1920 }
1921 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1922 // shifter operand operation, the IR generating `right_reg` (input to the type
1923 // conversion) can have a different type from the current instruction's type,
1924 // so we manually indicate the type.
1925 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001926 int64_t shift_amount = instruction->GetShiftAmount() &
1927 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001928
1929 Operand right_operand(0);
1930
1931 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1932 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1933 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1934 } else {
1935 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1936 }
1937
1938 // Logical binary operations do not support extension operations in the
1939 // operand. Note that VIXL would still manage if it was passed by generating
1940 // the extension as a separate instruction.
1941 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1942 DCHECK(!right_operand.IsExtendedRegister() ||
1943 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1944 kind != HInstruction::kNeg));
1945 switch (kind) {
1946 case HInstruction::kAdd:
1947 __ Add(out, left, right_operand);
1948 break;
1949 case HInstruction::kAnd:
1950 __ And(out, left, right_operand);
1951 break;
1952 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001953 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001954 __ Neg(out, right_operand);
1955 break;
1956 case HInstruction::kOr:
1957 __ Orr(out, left, right_operand);
1958 break;
1959 case HInstruction::kSub:
1960 __ Sub(out, left, right_operand);
1961 break;
1962 case HInstruction::kXor:
1963 __ Eor(out, left, right_operand);
1964 break;
1965 default:
1966 LOG(FATAL) << "Unexpected operation kind: " << kind;
1967 UNREACHABLE();
1968 }
1969}
1970
Artem Serov328429f2016-07-06 16:23:04 +01001971void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001972 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1973 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1976 locations->SetInAt(0, Location::RequiresRegister());
1977 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1978 locations->SetOut(Location::RequiresRegister());
1979}
1980
Roland Levillain4a3aa572016-08-15 13:17:06 +00001981void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1982 HIntermediateAddress* instruction) {
1983 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1984 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001985 __ Add(OutputRegister(instruction),
1986 InputRegisterAt(instruction, 0),
1987 Operand(InputOperandAt(instruction, 1)));
1988}
1989
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001990void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001993 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1994 if (instr->GetOpKind() == HInstruction::kSub &&
1995 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00001996 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001997 // Don't allocate register for Mneg instruction.
1998 } else {
1999 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2000 Location::RequiresRegister());
2001 }
2002 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2003 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002004 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2005}
2006
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002007void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002008 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002009 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2010 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002011
2012 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2013 // This fixup should be carried out for all multiply-accumulate instructions:
2014 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2015 if (instr->GetType() == Primitive::kPrimLong &&
2016 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2017 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002018 vixl::aarch64::Instruction* prev =
2019 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002020 if (prev->IsLoadOrStore()) {
2021 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002022 vixl::aarch64::CodeBufferCheckScope scope(masm,
2023 kInstructionSize,
2024 vixl::aarch64::CodeBufferCheckScope::kCheck,
2025 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002026 __ nop();
2027 }
2028 }
2029
2030 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002031 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002032 __ Madd(res, mul_left, mul_right, accumulator);
2033 } else {
2034 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002035 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002036 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002037 __ Mneg(res, mul_left, mul_right);
2038 } else {
2039 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2040 __ Msub(res, mul_left, mul_right, accumulator);
2041 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002042 }
2043}
2044
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002045void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002046 bool object_array_get_with_read_barrier =
2047 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002048 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002049 new (GetGraph()->GetArena()) LocationSummary(instruction,
2050 object_array_get_with_read_barrier ?
2051 LocationSummary::kCallOnSlowPath :
2052 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002053 locations->SetInAt(0, Location::RequiresRegister());
2054 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002055 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2056 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2057 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002058 // The output overlaps in the case of an object array get with
2059 // read barriers enabled: we do not want the move to overwrite the
2060 // array's location, as we need it to emit the read barrier.
2061 locations->SetOut(
2062 Location::RequiresRegister(),
2063 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002064 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002065}
2066
2067void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002068 Primitive::Type type = instruction->GetType();
2069 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002070 LocationSummary* locations = instruction->GetLocations();
2071 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002072 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002073 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002074
Alexandre Ramesd921d642015-04-16 15:07:16 +01002075 MacroAssembler* masm = GetVIXLAssembler();
2076 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002077 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002078 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002079
Roland Levillain44015862016-01-22 11:47:17 +00002080 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2081 // Object ArrayGet with Baker's read barrier case.
2082 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002083 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2084 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002085 // Note that a potential implicit null check is handled in the
2086 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2087 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2088 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002089 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002090 // General case.
2091 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002092 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002093 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2094 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002095 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002096 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002097 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002098 // The read barrier instrumentation does not support the
2099 // HIntermediateAddress instruction yet.
2100 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002101 // We do not need to compute the intermediate address from the array: the
2102 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002103 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002104 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002105 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002106 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2107 }
2108 temp = obj;
2109 } else {
2110 __ Add(temp, obj, offset);
2111 }
2112 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2113 }
2114
2115 codegen_->Load(type, OutputCPURegister(instruction), source);
2116 codegen_->MaybeRecordImplicitNullCheck(instruction);
2117
2118 if (type == Primitive::kPrimNot) {
2119 static_assert(
2120 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2121 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2122 Location obj_loc = locations->InAt(0);
2123 if (index.IsConstant()) {
2124 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2125 } else {
2126 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2127 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002128 }
Roland Levillain4d027112015-07-01 15:41:14 +01002129 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002130}
2131
Alexandre Rames5319def2014-10-23 10:03:10 +01002132void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2133 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2134 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002135 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002136}
2137
2138void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002139 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002140 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002141 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002143}
2144
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002145void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002146 Primitive::Type value_type = instruction->GetComponentType();
2147
2148 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2149 bool object_array_set_with_read_barrier =
2150 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2152 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002153 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2154 LocationSummary::kCallOnSlowPath :
2155 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002156 locations->SetInAt(0, Location::RequiresRegister());
2157 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002158 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002159 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002160 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002161 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002162 }
2163}
2164
2165void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2166 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002167 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002168 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002169 bool needs_write_barrier =
2170 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002171
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002172 Register array = InputRegisterAt(instruction, 0);
2173 CPURegister value = InputCPURegisterAt(instruction, 2);
2174 CPURegister source = value;
2175 Location index = locations->InAt(1);
2176 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2177 MemOperand destination = HeapOperand(array);
2178 MacroAssembler* masm = GetVIXLAssembler();
2179 BlockPoolsScope block_pools(masm);
2180
2181 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002182 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002183 if (index.IsConstant()) {
2184 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2185 destination = HeapOperand(array, offset);
2186 } else {
2187 UseScratchRegisterScope temps(masm);
2188 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002189 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002190 // The read barrier instrumentation does not support the
2191 // HIntermediateAddress instruction yet.
2192 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002193 // We do not need to compute the intermediate address from the array: the
2194 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002195 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002196 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002197 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002198 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2199 }
2200 temp = array;
2201 } else {
2202 __ Add(temp, array, offset);
2203 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002204 destination = HeapOperand(temp,
2205 XRegisterFrom(index),
2206 LSL,
2207 Primitive::ComponentSizeShift(value_type));
2208 }
2209 codegen_->Store(value_type, value, destination);
2210 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002211 } else {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002212 DCHECK(needs_write_barrier);
Artem Serov328429f2016-07-06 16:23:04 +01002213 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002214 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002215 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002216 {
2217 // We use a block to end the scratch scope before the write barrier, thus
2218 // freeing the temporary registers so they can be used in `MarkGCCard`.
2219 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002220 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002221 if (index.IsConstant()) {
2222 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002223 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002224 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002225 destination = HeapOperand(temp,
2226 XRegisterFrom(index),
2227 LSL,
2228 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002229 }
2230
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002231 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2232 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2233 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2234
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002235 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002236 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2237 codegen_->AddSlowPath(slow_path);
2238 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002239 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002240 __ Cbnz(Register(value), &non_zero);
2241 if (!index.IsConstant()) {
2242 __ Add(temp, array, offset);
2243 }
2244 __ Str(wzr, destination);
2245 codegen_->MaybeRecordImplicitNullCheck(instruction);
2246 __ B(&done);
2247 __ Bind(&non_zero);
2248 }
2249
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002250 if (kEmitCompilerReadBarrier) {
2251 // When read barriers are enabled, the type checking
2252 // instrumentation requires two read barriers:
2253 //
2254 // __ Mov(temp2, temp);
2255 // // /* HeapReference<Class> */ temp = temp->component_type_
2256 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002257 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002258 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2259 //
2260 // // /* HeapReference<Class> */ temp2 = value->klass_
2261 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002262 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002263 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2264 //
2265 // __ Cmp(temp, temp2);
2266 //
2267 // However, the second read barrier may trash `temp`, as it
2268 // is a temporary register, and as such would not be saved
2269 // along with live registers before calling the runtime (nor
2270 // restored afterwards). So in this case, we bail out and
2271 // delegate the work to the array set slow path.
2272 //
2273 // TODO: Extend the register allocator to support a new
2274 // "(locally) live temp" location so as to avoid always
2275 // going into the slow path when read barriers are enabled.
2276 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002277 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002278 Register temp2 = temps.AcquireSameSizeAs(array);
2279 // /* HeapReference<Class> */ temp = array->klass_
2280 __ Ldr(temp, HeapOperand(array, class_offset));
2281 codegen_->MaybeRecordImplicitNullCheck(instruction);
2282 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2283
2284 // /* HeapReference<Class> */ temp = temp->component_type_
2285 __ Ldr(temp, HeapOperand(temp, component_offset));
2286 // /* HeapReference<Class> */ temp2 = value->klass_
2287 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2288 // If heap poisoning is enabled, no need to unpoison `temp`
2289 // nor `temp2`, as we are comparing two poisoned references.
2290 __ Cmp(temp, temp2);
2291
2292 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002293 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 __ B(eq, &do_put);
2295 // If heap poisoning is enabled, the `temp` reference has
2296 // not been unpoisoned yet; unpoison it now.
2297 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2298
2299 // /* HeapReference<Class> */ temp = temp->super_class_
2300 __ Ldr(temp, HeapOperand(temp, super_offset));
2301 // If heap poisoning is enabled, no need to unpoison
2302 // `temp`, as we are comparing against null below.
2303 __ Cbnz(temp, slow_path->GetEntryLabel());
2304 __ Bind(&do_put);
2305 } else {
2306 __ B(ne, slow_path->GetEntryLabel());
2307 }
2308 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002309 }
2310 }
2311
2312 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002313 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002314 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002315 __ Mov(temp2, value.W());
2316 GetAssembler()->PoisonHeapReference(temp2);
2317 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002318 }
2319
2320 if (!index.IsConstant()) {
2321 __ Add(temp, array, offset);
2322 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002323 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002324
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002325 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002326 codegen_->MaybeRecordImplicitNullCheck(instruction);
2327 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002328 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002329
2330 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2331
2332 if (done.IsLinked()) {
2333 __ Bind(&done);
2334 }
2335
2336 if (slow_path != nullptr) {
2337 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002338 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002339 }
2340}
2341
Alexandre Rames67555f72014-11-18 10:55:16 +00002342void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002343 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2344 ? LocationSummary::kCallOnSlowPath
2345 : LocationSummary::kNoCall;
2346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002347 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002348 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002349 if (instruction->HasUses()) {
2350 locations->SetOut(Location::SameAsFirstInput());
2351 }
2352}
2353
2354void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002355 BoundsCheckSlowPathARM64* slow_path =
2356 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002357 codegen_->AddSlowPath(slow_path);
2358
2359 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2360 __ B(slow_path->GetEntryLabel(), hs);
2361}
2362
Alexandre Rames67555f72014-11-18 10:55:16 +00002363void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2364 LocationSummary* locations =
2365 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2366 locations->SetInAt(0, Location::RequiresRegister());
2367 if (check->HasUses()) {
2368 locations->SetOut(Location::SameAsFirstInput());
2369 }
2370}
2371
2372void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2373 // We assume the class is not null.
2374 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2375 check->GetLoadClass(), check, check->GetDexPc(), true);
2376 codegen_->AddSlowPath(slow_path);
2377 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2378}
2379
Roland Levillain1a653882016-03-18 18:05:57 +00002380static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2381 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2382 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2383}
2384
2385void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2386 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2387 Location rhs_loc = instruction->GetLocations()->InAt(1);
2388 if (rhs_loc.IsConstant()) {
2389 // 0.0 is the only immediate that can be encoded directly in
2390 // an FCMP instruction.
2391 //
2392 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2393 // specify that in a floating-point comparison, positive zero
2394 // and negative zero are considered equal, so we can use the
2395 // literal 0.0 for both cases here.
2396 //
2397 // Note however that some methods (Float.equal, Float.compare,
2398 // Float.compareTo, Double.equal, Double.compare,
2399 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2400 // StrictMath.min) consider 0.0 to be (strictly) greater than
2401 // -0.0. So if we ever translate calls to these methods into a
2402 // HCompare instruction, we must handle the -0.0 case with
2403 // care here.
2404 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2405 __ Fcmp(lhs_reg, 0.0);
2406 } else {
2407 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2408 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002409}
2410
Serban Constantinescu02164b32014-11-13 14:05:07 +00002411void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002412 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002413 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2414 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002415 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002416 case Primitive::kPrimBoolean:
2417 case Primitive::kPrimByte:
2418 case Primitive::kPrimShort:
2419 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002420 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002421 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002422 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002423 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2425 break;
2426 }
2427 case Primitive::kPrimFloat:
2428 case Primitive::kPrimDouble: {
2429 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002430 locations->SetInAt(1,
2431 IsFloatingPointZeroConstant(compare->InputAt(1))
2432 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2433 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002434 locations->SetOut(Location::RequiresRegister());
2435 break;
2436 }
2437 default:
2438 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2439 }
2440}
2441
2442void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2443 Primitive::Type in_type = compare->InputAt(0)->GetType();
2444
2445 // 0 if: left == right
2446 // 1 if: left > right
2447 // -1 if: left < right
2448 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002449 case Primitive::kPrimBoolean:
2450 case Primitive::kPrimByte:
2451 case Primitive::kPrimShort:
2452 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002453 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002454 case Primitive::kPrimLong: {
2455 Register result = OutputRegister(compare);
2456 Register left = InputRegisterAt(compare, 0);
2457 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002458 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002459 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2460 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002461 break;
2462 }
2463 case Primitive::kPrimFloat:
2464 case Primitive::kPrimDouble: {
2465 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002466 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002467 __ Cset(result, ne);
2468 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002469 break;
2470 }
2471 default:
2472 LOG(FATAL) << "Unimplemented compare type " << in_type;
2473 }
2474}
2475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002476void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002478
2479 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2480 locations->SetInAt(0, Location::RequiresFpuRegister());
2481 locations->SetInAt(1,
2482 IsFloatingPointZeroConstant(instruction->InputAt(1))
2483 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2484 : Location::RequiresFpuRegister());
2485 } else {
2486 // Integer cases.
2487 locations->SetInAt(0, Location::RequiresRegister());
2488 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2489 }
2490
David Brazdilb3e773e2016-01-26 11:28:37 +00002491 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002493 }
2494}
2495
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002496void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002497 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002498 return;
2499 }
2500
2501 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002502 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002503 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002504
Roland Levillain7f63c522015-07-13 15:54:55 +00002505 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002506 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002507 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002508 } else {
2509 // Integer cases.
2510 Register lhs = InputRegisterAt(instruction, 0);
2511 Operand rhs = InputOperandAt(instruction, 1);
2512 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002513 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002514 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002515}
2516
2517#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2518 M(Equal) \
2519 M(NotEqual) \
2520 M(LessThan) \
2521 M(LessThanOrEqual) \
2522 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002523 M(GreaterThanOrEqual) \
2524 M(Below) \
2525 M(BelowOrEqual) \
2526 M(Above) \
2527 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002528#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002529void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2530void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002531FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002532#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002533#undef FOR_EACH_CONDITION_INSTRUCTION
2534
Zheng Xuc6667102015-05-15 16:08:45 +08002535void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2536 DCHECK(instruction->IsDiv() || instruction->IsRem());
2537
2538 LocationSummary* locations = instruction->GetLocations();
2539 Location second = locations->InAt(1);
2540 DCHECK(second.IsConstant());
2541
2542 Register out = OutputRegister(instruction);
2543 Register dividend = InputRegisterAt(instruction, 0);
2544 int64_t imm = Int64FromConstant(second.GetConstant());
2545 DCHECK(imm == 1 || imm == -1);
2546
2547 if (instruction->IsRem()) {
2548 __ Mov(out, 0);
2549 } else {
2550 if (imm == 1) {
2551 __ Mov(out, dividend);
2552 } else {
2553 __ Neg(out, dividend);
2554 }
2555 }
2556}
2557
2558void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2559 DCHECK(instruction->IsDiv() || instruction->IsRem());
2560
2561 LocationSummary* locations = instruction->GetLocations();
2562 Location second = locations->InAt(1);
2563 DCHECK(second.IsConstant());
2564
2565 Register out = OutputRegister(instruction);
2566 Register dividend = InputRegisterAt(instruction, 0);
2567 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002568 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002569 int ctz_imm = CTZ(abs_imm);
2570
2571 UseScratchRegisterScope temps(GetVIXLAssembler());
2572 Register temp = temps.AcquireSameSizeAs(out);
2573
2574 if (instruction->IsDiv()) {
2575 __ Add(temp, dividend, abs_imm - 1);
2576 __ Cmp(dividend, 0);
2577 __ Csel(out, temp, dividend, lt);
2578 if (imm > 0) {
2579 __ Asr(out, out, ctz_imm);
2580 } else {
2581 __ Neg(out, Operand(out, ASR, ctz_imm));
2582 }
2583 } else {
2584 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2585 __ Asr(temp, dividend, bits - 1);
2586 __ Lsr(temp, temp, bits - ctz_imm);
2587 __ Add(out, dividend, temp);
2588 __ And(out, out, abs_imm - 1);
2589 __ Sub(out, out, temp);
2590 }
2591}
2592
2593void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2594 DCHECK(instruction->IsDiv() || instruction->IsRem());
2595
2596 LocationSummary* locations = instruction->GetLocations();
2597 Location second = locations->InAt(1);
2598 DCHECK(second.IsConstant());
2599
2600 Register out = OutputRegister(instruction);
2601 Register dividend = InputRegisterAt(instruction, 0);
2602 int64_t imm = Int64FromConstant(second.GetConstant());
2603
2604 Primitive::Type type = instruction->GetResultType();
2605 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2606
2607 int64_t magic;
2608 int shift;
2609 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2610
2611 UseScratchRegisterScope temps(GetVIXLAssembler());
2612 Register temp = temps.AcquireSameSizeAs(out);
2613
2614 // temp = get_high(dividend * magic)
2615 __ Mov(temp, magic);
2616 if (type == Primitive::kPrimLong) {
2617 __ Smulh(temp, dividend, temp);
2618 } else {
2619 __ Smull(temp.X(), dividend, temp);
2620 __ Lsr(temp.X(), temp.X(), 32);
2621 }
2622
2623 if (imm > 0 && magic < 0) {
2624 __ Add(temp, temp, dividend);
2625 } else if (imm < 0 && magic > 0) {
2626 __ Sub(temp, temp, dividend);
2627 }
2628
2629 if (shift != 0) {
2630 __ Asr(temp, temp, shift);
2631 }
2632
2633 if (instruction->IsDiv()) {
2634 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2635 } else {
2636 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2637 // TODO: Strength reduction for msub.
2638 Register temp_imm = temps.AcquireSameSizeAs(out);
2639 __ Mov(temp_imm, imm);
2640 __ Msub(out, temp, temp_imm, dividend);
2641 }
2642}
2643
2644void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2645 DCHECK(instruction->IsDiv() || instruction->IsRem());
2646 Primitive::Type type = instruction->GetResultType();
2647 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2648
2649 LocationSummary* locations = instruction->GetLocations();
2650 Register out = OutputRegister(instruction);
2651 Location second = locations->InAt(1);
2652
2653 if (second.IsConstant()) {
2654 int64_t imm = Int64FromConstant(second.GetConstant());
2655
2656 if (imm == 0) {
2657 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2658 } else if (imm == 1 || imm == -1) {
2659 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002660 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002661 DivRemByPowerOfTwo(instruction);
2662 } else {
2663 DCHECK(imm <= -2 || imm >= 2);
2664 GenerateDivRemWithAnyConstant(instruction);
2665 }
2666 } else {
2667 Register dividend = InputRegisterAt(instruction, 0);
2668 Register divisor = InputRegisterAt(instruction, 1);
2669 if (instruction->IsDiv()) {
2670 __ Sdiv(out, dividend, divisor);
2671 } else {
2672 UseScratchRegisterScope temps(GetVIXLAssembler());
2673 Register temp = temps.AcquireSameSizeAs(out);
2674 __ Sdiv(temp, dividend, divisor);
2675 __ Msub(out, temp, divisor, dividend);
2676 }
2677 }
2678}
2679
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002680void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2681 LocationSummary* locations =
2682 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2683 switch (div->GetResultType()) {
2684 case Primitive::kPrimInt:
2685 case Primitive::kPrimLong:
2686 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002687 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002688 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2689 break;
2690
2691 case Primitive::kPrimFloat:
2692 case Primitive::kPrimDouble:
2693 locations->SetInAt(0, Location::RequiresFpuRegister());
2694 locations->SetInAt(1, Location::RequiresFpuRegister());
2695 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2696 break;
2697
2698 default:
2699 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2700 }
2701}
2702
2703void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2704 Primitive::Type type = div->GetResultType();
2705 switch (type) {
2706 case Primitive::kPrimInt:
2707 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002708 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002709 break;
2710
2711 case Primitive::kPrimFloat:
2712 case Primitive::kPrimDouble:
2713 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2714 break;
2715
2716 default:
2717 LOG(FATAL) << "Unexpected div type " << type;
2718 }
2719}
2720
Alexandre Rames67555f72014-11-18 10:55:16 +00002721void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002722 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2723 ? LocationSummary::kCallOnSlowPath
2724 : LocationSummary::kNoCall;
2725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002726 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2727 if (instruction->HasUses()) {
2728 locations->SetOut(Location::SameAsFirstInput());
2729 }
2730}
2731
2732void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2733 SlowPathCodeARM64* slow_path =
2734 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2735 codegen_->AddSlowPath(slow_path);
2736 Location value = instruction->GetLocations()->InAt(0);
2737
Alexandre Rames3e69f162014-12-10 10:36:50 +00002738 Primitive::Type type = instruction->GetType();
2739
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002740 if (!Primitive::IsIntegralType(type)) {
2741 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002742 return;
2743 }
2744
Alexandre Rames67555f72014-11-18 10:55:16 +00002745 if (value.IsConstant()) {
2746 int64_t divisor = Int64ConstantFrom(value);
2747 if (divisor == 0) {
2748 __ B(slow_path->GetEntryLabel());
2749 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002750 // A division by a non-null constant is valid. We don't need to perform
2751 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002752 }
2753 } else {
2754 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2755 }
2756}
2757
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002758void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2761 locations->SetOut(Location::ConstantLocation(constant));
2762}
2763
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002764void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2765 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002766 // Will be generated at use site.
2767}
2768
Alexandre Rames5319def2014-10-23 10:03:10 +01002769void LocationsBuilderARM64::VisitExit(HExit* exit) {
2770 exit->SetLocations(nullptr);
2771}
2772
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002773void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002774}
2775
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002776void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2777 LocationSummary* locations =
2778 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2779 locations->SetOut(Location::ConstantLocation(constant));
2780}
2781
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002782void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002783 // Will be generated at use site.
2784}
2785
David Brazdilfc6a86a2015-06-26 10:33:45 +00002786void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002787 DCHECK(!successor->IsExitBlock());
2788 HBasicBlock* block = got->GetBlock();
2789 HInstruction* previous = got->GetPrevious();
2790 HLoopInformation* info = block->GetLoopInformation();
2791
David Brazdil46e2a392015-03-16 17:31:52 +00002792 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002793 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2794 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2795 return;
2796 }
2797 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2798 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2799 }
2800 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002801 __ B(codegen_->GetLabelOf(successor));
2802 }
2803}
2804
David Brazdilfc6a86a2015-06-26 10:33:45 +00002805void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2806 got->SetLocations(nullptr);
2807}
2808
2809void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2810 HandleGoto(got, got->GetSuccessor());
2811}
2812
2813void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2814 try_boundary->SetLocations(nullptr);
2815}
2816
2817void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2818 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2819 if (!successor->IsExitBlock()) {
2820 HandleGoto(try_boundary, successor);
2821 }
2822}
2823
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002824void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002825 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002826 vixl::aarch64::Label* true_target,
2827 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002828 // FP branching requires both targets to be explicit. If either of the targets
2829 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002830 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002831 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002832
David Brazdil0debae72015-11-12 18:37:00 +00002833 if (true_target == nullptr && false_target == nullptr) {
2834 // Nothing to do. The code always falls through.
2835 return;
2836 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002837 // Constant condition, statically compared against "true" (integer value 1).
2838 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002839 if (true_target != nullptr) {
2840 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002841 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002842 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002843 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002844 if (false_target != nullptr) {
2845 __ B(false_target);
2846 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002847 }
David Brazdil0debae72015-11-12 18:37:00 +00002848 return;
2849 }
2850
2851 // The following code generates these patterns:
2852 // (1) true_target == nullptr && false_target != nullptr
2853 // - opposite condition true => branch to false_target
2854 // (2) true_target != nullptr && false_target == nullptr
2855 // - condition true => branch to true_target
2856 // (3) true_target != nullptr && false_target != nullptr
2857 // - condition true => branch to true_target
2858 // - branch to false_target
2859 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002860 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002861 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002862 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002863 if (true_target == nullptr) {
2864 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2865 } else {
2866 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2867 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002868 } else {
2869 // The condition instruction has not been materialized, use its inputs as
2870 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002871 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002872
David Brazdil0debae72015-11-12 18:37:00 +00002873 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002874 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002875 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002876 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002877 IfCondition opposite_condition = condition->GetOppositeCondition();
2878 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002879 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002880 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002881 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002882 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002883 // Integer cases.
2884 Register lhs = InputRegisterAt(condition, 0);
2885 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002886
2887 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002888 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002889 if (true_target == nullptr) {
2890 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2891 non_fallthrough_target = false_target;
2892 } else {
2893 arm64_cond = ARM64Condition(condition->GetCondition());
2894 non_fallthrough_target = true_target;
2895 }
2896
Aart Bik086d27e2016-01-20 17:02:00 -08002897 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002898 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002899 switch (arm64_cond) {
2900 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002901 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002902 break;
2903 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002904 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002905 break;
2906 case lt:
2907 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002908 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002909 break;
2910 case ge:
2911 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002912 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002913 break;
2914 default:
2915 // Without the `static_cast` the compiler throws an error for
2916 // `-Werror=sign-promo`.
2917 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2918 }
2919 } else {
2920 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002921 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002922 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002923 }
2924 }
David Brazdil0debae72015-11-12 18:37:00 +00002925
2926 // If neither branch falls through (case 3), the conditional branch to `true_target`
2927 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2928 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002929 __ B(false_target);
2930 }
David Brazdil0debae72015-11-12 18:37:00 +00002931
2932 if (fallthrough_target.IsLinked()) {
2933 __ Bind(&fallthrough_target);
2934 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002935}
2936
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002937void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002939 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002940 locations->SetInAt(0, Location::RequiresRegister());
2941 }
2942}
2943
2944void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002945 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2946 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002947 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2948 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2949 true_target = nullptr;
2950 }
2951 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2952 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2953 false_target = nullptr;
2954 }
David Brazdil0debae72015-11-12 18:37:00 +00002955 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002956}
2957
2958void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2959 LocationSummary* locations = new (GetGraph()->GetArena())
2960 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002961 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002962 locations->SetInAt(0, Location::RequiresRegister());
2963 }
2964}
2965
2966void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002967 SlowPathCodeARM64* slow_path =
2968 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002969 GenerateTestAndBranch(deoptimize,
2970 /* condition_input_index */ 0,
2971 slow_path->GetEntryLabel(),
2972 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002973}
2974
David Brazdilc0b601b2016-02-08 14:20:45 +00002975static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2976 return condition->IsCondition() &&
2977 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2978}
2979
Alexandre Rames880f1192016-06-13 16:04:50 +01002980static inline Condition GetConditionForSelect(HCondition* condition) {
2981 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002982 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2983 : ARM64Condition(cond);
2984}
2985
David Brazdil74eb1b22015-12-14 11:44:01 +00002986void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002988 if (Primitive::IsFloatingPointType(select->GetType())) {
2989 locations->SetInAt(0, Location::RequiresFpuRegister());
2990 locations->SetInAt(1, Location::RequiresFpuRegister());
2991 locations->SetOut(Location::RequiresFpuRegister());
2992 } else {
2993 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2994 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2995 bool is_true_value_constant = cst_true_value != nullptr;
2996 bool is_false_value_constant = cst_false_value != nullptr;
2997 // Ask VIXL whether we should synthesize constants in registers.
2998 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
2999 Operand true_op = is_true_value_constant ?
3000 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3001 Operand false_op = is_false_value_constant ?
3002 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3003 bool true_value_in_register = false;
3004 bool false_value_in_register = false;
3005 MacroAssembler::GetCselSynthesisInformation(
3006 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3007 true_value_in_register |= !is_true_value_constant;
3008 false_value_in_register |= !is_false_value_constant;
3009
3010 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3011 : Location::ConstantLocation(cst_true_value));
3012 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3013 : Location::ConstantLocation(cst_false_value));
3014 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003015 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003016
David Brazdil74eb1b22015-12-14 11:44:01 +00003017 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3018 locations->SetInAt(2, Location::RequiresRegister());
3019 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003020}
3021
3022void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003023 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003024 Condition csel_cond;
3025
3026 if (IsBooleanValueOrMaterializedCondition(cond)) {
3027 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003028 // Use the condition flags set by the previous instruction.
3029 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003030 } else {
3031 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003032 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003033 }
3034 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003035 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003036 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003037 } else {
3038 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003039 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003040 }
3041
Alexandre Rames880f1192016-06-13 16:04:50 +01003042 if (Primitive::IsFloatingPointType(select->GetType())) {
3043 __ Fcsel(OutputFPRegister(select),
3044 InputFPRegisterAt(select, 1),
3045 InputFPRegisterAt(select, 0),
3046 csel_cond);
3047 } else {
3048 __ Csel(OutputRegister(select),
3049 InputOperandAt(select, 1),
3050 InputOperandAt(select, 0),
3051 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003052 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003053}
3054
David Srbecky0cf44932015-12-09 14:09:59 +00003055void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3056 new (GetGraph()->GetArena()) LocationSummary(info);
3057}
3058
David Srbeckyd28f4a02016-03-14 17:14:24 +00003059void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3060 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003061}
3062
3063void CodeGeneratorARM64::GenerateNop() {
3064 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003065}
3066
Alexandre Rames5319def2014-10-23 10:03:10 +01003067void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003068 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003069}
3070
3071void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003072 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003073}
3074
3075void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003076 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003077}
3078
3079void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003080 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003081}
3082
Roland Levillain44015862016-01-22 11:47:17 +00003083static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3084 return kEmitCompilerReadBarrier &&
3085 (kUseBakerReadBarrier ||
3086 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3087 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3088 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3089}
3090
Alexandre Rames67555f72014-11-18 10:55:16 +00003091void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003092 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003093 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3094 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003095 case TypeCheckKind::kExactCheck:
3096 case TypeCheckKind::kAbstractClassCheck:
3097 case TypeCheckKind::kClassHierarchyCheck:
3098 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003099 call_kind =
3100 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003101 break;
3102 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003103 case TypeCheckKind::kUnresolvedCheck:
3104 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003105 call_kind = LocationSummary::kCallOnSlowPath;
3106 break;
3107 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003108
Alexandre Rames67555f72014-11-18 10:55:16 +00003109 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003110 locations->SetInAt(0, Location::RequiresRegister());
3111 locations->SetInAt(1, Location::RequiresRegister());
3112 // The "out" register is used as a temporary, so it overlaps with the inputs.
3113 // Note that TypeCheckSlowPathARM64 uses this register too.
3114 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3115 // When read barriers are enabled, we need a temporary register for
3116 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003117 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003118 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003119 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003120}
3121
3122void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003123 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003124 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003125 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003126 Register obj = InputRegisterAt(instruction, 0);
3127 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003128 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003129 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003130 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3131 locations->GetTemp(0) :
3132 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003133 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3134 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3135 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3136 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003137
Scott Wakeling97c72b72016-06-24 16:19:36 +01003138 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003140
3141 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003142 // Avoid null check if we know `obj` is not null.
3143 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003144 __ Cbz(obj, &zero);
3145 }
3146
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003147 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003148 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003149
Roland Levillain44015862016-01-22 11:47:17 +00003150 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003151 case TypeCheckKind::kExactCheck: {
3152 __ Cmp(out, cls);
3153 __ Cset(out, eq);
3154 if (zero.IsLinked()) {
3155 __ B(&done);
3156 }
3157 break;
3158 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003159
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003160 case TypeCheckKind::kAbstractClassCheck: {
3161 // If the class is abstract, we eagerly fetch the super class of the
3162 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003163 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003164 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003166 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003167 // If `out` is null, we use it for the result, and jump to `done`.
3168 __ Cbz(out, &done);
3169 __ Cmp(out, cls);
3170 __ B(ne, &loop);
3171 __ Mov(out, 1);
3172 if (zero.IsLinked()) {
3173 __ B(&done);
3174 }
3175 break;
3176 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178 case TypeCheckKind::kClassHierarchyCheck: {
3179 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003180 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003181 __ Bind(&loop);
3182 __ Cmp(out, cls);
3183 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003184 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003185 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003186 __ Cbnz(out, &loop);
3187 // If `out` is null, we use it for the result, and jump to `done`.
3188 __ B(&done);
3189 __ Bind(&success);
3190 __ Mov(out, 1);
3191 if (zero.IsLinked()) {
3192 __ B(&done);
3193 }
3194 break;
3195 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003197 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003198 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003199 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003200 __ Cmp(out, cls);
3201 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003202 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003203 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003204 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003205 // If `out` is null, we use it for the result, and jump to `done`.
3206 __ Cbz(out, &done);
3207 __ Ldrh(out, HeapOperand(out, primitive_offset));
3208 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3209 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003210 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003211 __ Mov(out, 1);
3212 __ B(&done);
3213 break;
3214 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003215
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003216 case TypeCheckKind::kArrayCheck: {
3217 __ Cmp(out, cls);
3218 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003219 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3220 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003221 codegen_->AddSlowPath(slow_path);
3222 __ B(ne, slow_path->GetEntryLabel());
3223 __ Mov(out, 1);
3224 if (zero.IsLinked()) {
3225 __ B(&done);
3226 }
3227 break;
3228 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003229
Calin Juravle98893e12015-10-02 21:05:03 +01003230 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003231 case TypeCheckKind::kInterfaceCheck: {
3232 // Note that we indeed only call on slow path, but we always go
3233 // into the slow path for the unresolved and interface check
3234 // cases.
3235 //
3236 // We cannot directly call the InstanceofNonTrivial runtime
3237 // entry point without resorting to a type checking slow path
3238 // here (i.e. by calling InvokeRuntime directly), as it would
3239 // require to assign fixed registers for the inputs of this
3240 // HInstanceOf instruction (following the runtime calling
3241 // convention), which might be cluttered by the potential first
3242 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003243 //
3244 // TODO: Introduce a new runtime entry point taking the object
3245 // to test (instead of its class) as argument, and let it deal
3246 // with the read barrier issues. This will let us refactor this
3247 // case of the `switch` code as it was previously (with a direct
3248 // call to the runtime not using a type checking slow path).
3249 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003250 DCHECK(locations->OnlyCallsOnSlowPath());
3251 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3252 /* is_fatal */ false);
3253 codegen_->AddSlowPath(slow_path);
3254 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003255 if (zero.IsLinked()) {
3256 __ B(&done);
3257 }
3258 break;
3259 }
3260 }
3261
3262 if (zero.IsLinked()) {
3263 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003264 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003265 }
3266
3267 if (done.IsLinked()) {
3268 __ Bind(&done);
3269 }
3270
3271 if (slow_path != nullptr) {
3272 __ Bind(slow_path->GetExitLabel());
3273 }
3274}
3275
3276void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3277 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3278 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3279
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003280 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3281 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003282 case TypeCheckKind::kExactCheck:
3283 case TypeCheckKind::kAbstractClassCheck:
3284 case TypeCheckKind::kClassHierarchyCheck:
3285 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003286 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3287 LocationSummary::kCallOnSlowPath :
3288 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003289 break;
3290 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003291 case TypeCheckKind::kUnresolvedCheck:
3292 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003293 call_kind = LocationSummary::kCallOnSlowPath;
3294 break;
3295 }
3296
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3298 locations->SetInAt(0, Location::RequiresRegister());
3299 locations->SetInAt(1, Location::RequiresRegister());
3300 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3301 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003302 // When read barriers are enabled, we need an additional temporary
3303 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003304 if (TypeCheckNeedsATemporary(type_check_kind)) {
3305 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003306 }
3307}
3308
3309void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003310 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003311 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003312 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003313 Register obj = InputRegisterAt(instruction, 0);
3314 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003315 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003316 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3317 locations->GetTemp(1) :
3318 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003319 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003320 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3321 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3322 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3323 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003324
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003325 bool is_type_check_slow_path_fatal =
3326 (type_check_kind == TypeCheckKind::kExactCheck ||
3327 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3328 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3329 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3330 !instruction->CanThrowIntoCatchBlock();
3331 SlowPathCodeARM64* type_check_slow_path =
3332 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3333 is_type_check_slow_path_fatal);
3334 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003335
Scott Wakeling97c72b72016-06-24 16:19:36 +01003336 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003337 // Avoid null check if we know obj is not null.
3338 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003339 __ Cbz(obj, &done);
3340 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003341
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003342 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003343 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003344
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003345 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003346 case TypeCheckKind::kExactCheck:
3347 case TypeCheckKind::kArrayCheck: {
3348 __ Cmp(temp, cls);
3349 // Jump to slow path for throwing the exception or doing a
3350 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003351 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003352 break;
3353 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 case TypeCheckKind::kAbstractClassCheck: {
3356 // If the class is abstract, we eagerly fetch the super class of the
3357 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003358 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003359 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003360 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003361 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003362
3363 // If the class reference currently in `temp` is not null, jump
3364 // to the `compare_classes` label to compare it with the checked
3365 // class.
3366 __ Cbnz(temp, &compare_classes);
3367 // Otherwise, jump to the slow path to throw the exception.
3368 //
3369 // But before, move back the object's class into `temp` before
3370 // going into the slow path, as it has been overwritten in the
3371 // meantime.
3372 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003373 GenerateReferenceLoadTwoRegisters(
3374 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003375 __ B(type_check_slow_path->GetEntryLabel());
3376
3377 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003378 __ Cmp(temp, cls);
3379 __ B(ne, &loop);
3380 break;
3381 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003382
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003383 case TypeCheckKind::kClassHierarchyCheck: {
3384 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003385 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003386 __ Bind(&loop);
3387 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003388 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003390 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003391 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392
3393 // If the class reference currently in `temp` is not null, jump
3394 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003395 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396 // Otherwise, jump to the slow path to throw the exception.
3397 //
3398 // But before, move back the object's class into `temp` before
3399 // going into the slow path, as it has been overwritten in the
3400 // meantime.
3401 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003402 GenerateReferenceLoadTwoRegisters(
3403 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003404 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003405 break;
3406 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003407
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003408 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003409 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003410 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003411 __ Cmp(temp, cls);
3412 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413
3414 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003416 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003417
3418 // If the component type is not null (i.e. the object is indeed
3419 // an array), jump to label `check_non_primitive_component_type`
3420 // to further check that this component type is not a primitive
3421 // type.
3422 __ Cbnz(temp, &check_non_primitive_component_type);
3423 // Otherwise, jump to the slow path to throw the exception.
3424 //
3425 // But before, move back the object's class into `temp` before
3426 // going into the slow path, as it has been overwritten in the
3427 // meantime.
3428 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003429 GenerateReferenceLoadTwoRegisters(
3430 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431 __ B(type_check_slow_path->GetEntryLabel());
3432
3433 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003434 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3435 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003436 __ Cbz(temp, &done);
3437 // Same comment as above regarding `temp` and the slow path.
3438 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003439 GenerateReferenceLoadTwoRegisters(
3440 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003442 break;
3443 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003444
Calin Juravle98893e12015-10-02 21:05:03 +01003445 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003446 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447 // We always go into the type check slow path for the unresolved
3448 // and interface check cases.
3449 //
3450 // We cannot directly call the CheckCast runtime entry point
3451 // without resorting to a type checking slow path here (i.e. by
3452 // calling InvokeRuntime directly), as it would require to
3453 // assign fixed registers for the inputs of this HInstanceOf
3454 // instruction (following the runtime calling convention), which
3455 // might be cluttered by the potential first read barrier
3456 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003457 //
3458 // TODO: Introduce a new runtime entry point taking the object
3459 // to test (instead of its class) as argument, and let it deal
3460 // with the read barrier issues. This will let us refactor this
3461 // case of the `switch` code as it was previously (with a direct
3462 // call to the runtime not using a type checking slow path).
3463 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003464 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003465 break;
3466 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003467 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003468
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003469 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003470}
3471
Alexandre Rames5319def2014-10-23 10:03:10 +01003472void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3473 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3474 locations->SetOut(Location::ConstantLocation(constant));
3475}
3476
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003477void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003478 // Will be generated at use site.
3479}
3480
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003481void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3483 locations->SetOut(Location::ConstantLocation(constant));
3484}
3485
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003486void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003487 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003488}
3489
Calin Juravle175dc732015-08-25 15:42:32 +01003490void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3491 // The trampoline uses the same calling convention as dex calling conventions,
3492 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3493 // the method_idx.
3494 HandleInvoke(invoke);
3495}
3496
3497void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3498 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3499}
3500
Alexandre Rames5319def2014-10-23 10:03:10 +01003501void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003502 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003503 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003504}
3505
Alexandre Rames67555f72014-11-18 10:55:16 +00003506void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3507 HandleInvoke(invoke);
3508}
3509
3510void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3511 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003512 LocationSummary* locations = invoke->GetLocations();
3513 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003514 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003515 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003516 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003517
3518 // The register ip1 is required to be used for the hidden argument in
3519 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003520 MacroAssembler* masm = GetVIXLAssembler();
3521 UseScratchRegisterScope scratch_scope(masm);
3522 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003523 scratch_scope.Exclude(ip1);
3524 __ Mov(ip1, invoke->GetDexMethodIndex());
3525
Alexandre Rames67555f72014-11-18 10:55:16 +00003526 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003527 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003528 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003529 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003530 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003531 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003532 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003533 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003534 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003535 // Instead of simply (possibly) unpoisoning `temp` here, we should
3536 // emit a read barrier for the previous class reference load.
3537 // However this is not required in practice, as this is an
3538 // intermediate/temporary reference and because the current
3539 // concurrent copying collector keeps the from-space memory
3540 // intact/accessible until the end of the marking phase (the
3541 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003542 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003543 __ Ldr(temp,
3544 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3545 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003546 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003547 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003548 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003549 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003550 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003551 // lr();
3552 __ Blr(lr);
3553 DCHECK(!codegen_->IsLeafMethod());
3554 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3555}
3556
3557void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003558 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3559 if (intrinsic.TryDispatch(invoke)) {
3560 return;
3561 }
3562
Alexandre Rames67555f72014-11-18 10:55:16 +00003563 HandleInvoke(invoke);
3564}
3565
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003566void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003567 // Explicit clinit checks triggered by static invokes must have been pruned by
3568 // art::PrepareForRegisterAllocation.
3569 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003570
Andreas Gampe878d58c2015-01-15 23:24:00 -08003571 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3572 if (intrinsic.TryDispatch(invoke)) {
3573 return;
3574 }
3575
Alexandre Rames67555f72014-11-18 10:55:16 +00003576 HandleInvoke(invoke);
3577}
3578
Andreas Gampe878d58c2015-01-15 23:24:00 -08003579static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3580 if (invoke->GetLocations()->Intrinsified()) {
3581 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3582 intrinsic.Dispatch(invoke);
3583 return true;
3584 }
3585 return false;
3586}
3587
Vladimir Markodc151b22015-10-15 18:02:30 +01003588HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3589 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3590 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003591 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003592 return desired_dispatch_info;
3593}
3594
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003595void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003596 // For better instruction scheduling we load the direct code pointer before the method pointer.
3597 bool direct_code_loaded = false;
3598 switch (invoke->GetCodePtrLocation()) {
3599 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3600 // LR = code address from literal pool with link-time patch.
3601 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3602 direct_code_loaded = true;
3603 break;
3604 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3605 // LR = invoke->GetDirectCodePtr();
3606 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3607 direct_code_loaded = true;
3608 break;
3609 default:
3610 break;
3611 }
3612
Andreas Gampe878d58c2015-01-15 23:24:00 -08003613 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003614 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3615 switch (invoke->GetMethodLoadKind()) {
3616 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3617 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003618 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003619 break;
3620 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003621 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003622 break;
3623 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3624 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003625 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003626 break;
3627 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3628 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003629 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003630 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3631 break;
3632 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3633 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003634 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3635 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003636 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003637 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003638 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003639 __ Bind(adrp_label);
3640 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003641 }
Vladimir Marko58155012015-08-19 12:49:41 +00003642 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003643 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003644 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003645 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003646 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003647 __ Bind(ldr_label);
3648 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003649 }
Vladimir Marko58155012015-08-19 12:49:41 +00003650 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003651 }
Vladimir Marko58155012015-08-19 12:49:41 +00003652 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003653 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003654 Register reg = XRegisterFrom(temp);
3655 Register method_reg;
3656 if (current_method.IsRegister()) {
3657 method_reg = XRegisterFrom(current_method);
3658 } else {
3659 DCHECK(invoke->GetLocations()->Intrinsified());
3660 DCHECK(!current_method.IsValid());
3661 method_reg = reg;
3662 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3663 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003664
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003665 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003666 __ Ldr(reg.X(),
3667 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003668 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003669 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003670 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3671 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003672 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3673 break;
3674 }
3675 }
3676
3677 switch (invoke->GetCodePtrLocation()) {
3678 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3679 __ Bl(&frame_entry_label_);
3680 break;
3681 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3682 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003683 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3684 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003685 __ Bind(label);
3686 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003687 break;
3688 }
3689 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3690 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3691 // LR prepared above for better instruction scheduling.
3692 DCHECK(direct_code_loaded);
3693 // lr()
3694 __ Blr(lr);
3695 break;
3696 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3697 // LR = callee_method->entry_point_from_quick_compiled_code_;
3698 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003699 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003700 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003701 // lr()
3702 __ Blr(lr);
3703 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003704 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003705
Andreas Gampe878d58c2015-01-15 23:24:00 -08003706 DCHECK(!IsLeafMethod());
3707}
3708
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003709void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003710 // Use the calling convention instead of the location of the receiver, as
3711 // intrinsics may have put the receiver in a different register. In the intrinsics
3712 // slow path, the arguments have been moved to the right place, so here we are
3713 // guaranteed that the receiver is the first register of the calling convention.
3714 InvokeDexCallingConvention calling_convention;
3715 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003716 Register temp = XRegisterFrom(temp_in);
3717 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3718 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3719 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003720 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003721
3722 BlockPoolsScope block_pools(GetVIXLAssembler());
3723
3724 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003725 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003726 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003727 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003728 // Instead of simply (possibly) unpoisoning `temp` here, we should
3729 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003730 // intermediate/temporary reference and because the current
3731 // concurrent copying collector keeps the from-space memory
3732 // intact/accessible until the end of the marking phase (the
3733 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003734 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3735 // temp = temp->GetMethodAt(method_offset);
3736 __ Ldr(temp, MemOperand(temp, method_offset));
3737 // lr = temp->GetEntryPoint();
3738 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3739 // lr();
3740 __ Blr(lr);
3741}
3742
Scott Wakeling97c72b72016-06-24 16:19:36 +01003743vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3744 const DexFile& dex_file,
3745 uint32_t string_index,
3746 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003747 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3748}
3749
Scott Wakeling97c72b72016-06-24 16:19:36 +01003750vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3751 const DexFile& dex_file,
3752 uint32_t type_index,
3753 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003754 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3755}
3756
Scott Wakeling97c72b72016-06-24 16:19:36 +01003757vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3758 const DexFile& dex_file,
3759 uint32_t element_offset,
3760 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003761 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3762}
3763
Scott Wakeling97c72b72016-06-24 16:19:36 +01003764vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3765 const DexFile& dex_file,
3766 uint32_t offset_or_index,
3767 vixl::aarch64::Label* adrp_label,
3768 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003769 // Add a patch entry and return the label.
3770 patches->emplace_back(dex_file, offset_or_index);
3771 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003772 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003773 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3774 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3775 return label;
3776}
3777
Scott Wakeling97c72b72016-06-24 16:19:36 +01003778vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003779 const DexFile& dex_file, uint32_t string_index) {
3780 return boot_image_string_patches_.GetOrCreate(
3781 StringReference(&dex_file, string_index),
3782 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3783}
3784
Scott Wakeling97c72b72016-06-24 16:19:36 +01003785vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003786 const DexFile& dex_file, uint32_t type_index) {
3787 return boot_image_type_patches_.GetOrCreate(
3788 TypeReference(&dex_file, type_index),
3789 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3790}
3791
Scott Wakeling97c72b72016-06-24 16:19:36 +01003792vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3793 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003794 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3795 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3796 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3797}
3798
Scott Wakeling97c72b72016-06-24 16:19:36 +01003799vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3800 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003801 return DeduplicateUint64Literal(address);
3802}
3803
Vladimir Marko58155012015-08-19 12:49:41 +00003804void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3805 DCHECK(linker_patches->empty());
3806 size_t size =
3807 method_patches_.size() +
3808 call_patches_.size() +
3809 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003810 pc_relative_dex_cache_patches_.size() +
3811 boot_image_string_patches_.size() +
3812 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003813 boot_image_type_patches_.size() +
3814 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003815 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003816 linker_patches->reserve(size);
3817 for (const auto& entry : method_patches_) {
3818 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003819 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3820 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003821 target_method.dex_file,
3822 target_method.dex_method_index));
3823 }
3824 for (const auto& entry : call_patches_) {
3825 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003826 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3827 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003828 target_method.dex_file,
3829 target_method.dex_method_index));
3830 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003831 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3832 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003833 info.target_method.dex_file,
3834 info.target_method.dex_method_index));
3835 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003836 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003837 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003838 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003839 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003840 info.offset_or_index));
3841 }
3842 for (const auto& entry : boot_image_string_patches_) {
3843 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003844 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3845 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003846 target_string.dex_file,
3847 target_string.string_index));
3848 }
3849 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003850 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003851 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003852 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003853 info.offset_or_index));
3854 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003855 for (const auto& entry : boot_image_type_patches_) {
3856 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003857 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3858 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003859 target_type.dex_file,
3860 target_type.type_index));
3861 }
3862 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003863 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003864 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003865 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003866 info.offset_or_index));
3867 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003868 for (const auto& entry : boot_image_address_patches_) {
3869 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003870 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3871 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003872 }
3873}
3874
Scott Wakeling97c72b72016-06-24 16:19:36 +01003875vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003876 Uint32ToLiteralMap* map) {
3877 return map->GetOrCreate(
3878 value,
3879 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3880}
3881
Scott Wakeling97c72b72016-06-24 16:19:36 +01003882vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003883 return uint64_literals_.GetOrCreate(
3884 value,
3885 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003886}
3887
Scott Wakeling97c72b72016-06-24 16:19:36 +01003888vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003889 MethodReference target_method,
3890 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003891 return map->GetOrCreate(
3892 target_method,
3893 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003894}
3895
Scott Wakeling97c72b72016-06-24 16:19:36 +01003896vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003897 MethodReference target_method) {
3898 return DeduplicateMethodLiteral(target_method, &method_patches_);
3899}
3900
Scott Wakeling97c72b72016-06-24 16:19:36 +01003901vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003902 MethodReference target_method) {
3903 return DeduplicateMethodLiteral(target_method, &call_patches_);
3904}
3905
3906
Andreas Gampe878d58c2015-01-15 23:24:00 -08003907void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003908 // Explicit clinit checks triggered by static invokes must have been pruned by
3909 // art::PrepareForRegisterAllocation.
3910 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003911
Andreas Gampe878d58c2015-01-15 23:24:00 -08003912 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3913 return;
3914 }
3915
Alexandre Ramesd921d642015-04-16 15:07:16 +01003916 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003917 LocationSummary* locations = invoke->GetLocations();
3918 codegen_->GenerateStaticOrDirectCall(
3919 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003920 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003921}
3922
3923void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003924 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3925 return;
3926 }
3927
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003928 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003929 DCHECK(!codegen_->IsLeafMethod());
3930 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3931}
3932
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003933HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3934 HLoadClass::LoadKind desired_class_load_kind) {
3935 if (kEmitCompilerReadBarrier) {
3936 switch (desired_class_load_kind) {
3937 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3938 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3939 case HLoadClass::LoadKind::kBootImageAddress:
3940 // TODO: Implement for read barrier.
3941 return HLoadClass::LoadKind::kDexCacheViaMethod;
3942 default:
3943 break;
3944 }
3945 }
3946 switch (desired_class_load_kind) {
3947 case HLoadClass::LoadKind::kReferrersClass:
3948 break;
3949 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3950 DCHECK(!GetCompilerOptions().GetCompilePic());
3951 break;
3952 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3953 DCHECK(GetCompilerOptions().GetCompilePic());
3954 break;
3955 case HLoadClass::LoadKind::kBootImageAddress:
3956 break;
3957 case HLoadClass::LoadKind::kDexCacheAddress:
3958 DCHECK(Runtime::Current()->UseJitCompilation());
3959 break;
3960 case HLoadClass::LoadKind::kDexCachePcRelative:
3961 DCHECK(!Runtime::Current()->UseJitCompilation());
3962 break;
3963 case HLoadClass::LoadKind::kDexCacheViaMethod:
3964 break;
3965 }
3966 return desired_class_load_kind;
3967}
3968
Alexandre Rames67555f72014-11-18 10:55:16 +00003969void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003970 if (cls->NeedsAccessCheck()) {
3971 InvokeRuntimeCallingConvention calling_convention;
3972 CodeGenerator::CreateLoadClassLocationSummary(
3973 cls,
3974 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003975 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003976 /* code_generator_supports_read_barrier */ true);
3977 return;
3978 }
3979
3980 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3981 ? LocationSummary::kCallOnSlowPath
3982 : LocationSummary::kNoCall;
3983 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3984 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3985 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3986 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3987 locations->SetInAt(0, Location::RequiresRegister());
3988 }
3989 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003990}
3991
3992void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003993 if (cls->NeedsAccessCheck()) {
3994 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00003995 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003996 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003997 return;
3998 }
3999
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004000 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004001 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004002
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004003 bool generate_null_check = false;
4004 switch (cls->GetLoadKind()) {
4005 case HLoadClass::LoadKind::kReferrersClass: {
4006 DCHECK(!cls->CanCallRuntime());
4007 DCHECK(!cls->MustGenerateClinitCheck());
4008 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4009 Register current_method = InputRegisterAt(cls, 0);
4010 GenerateGcRootFieldLoad(
4011 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4012 break;
4013 }
4014 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4015 DCHECK(!kEmitCompilerReadBarrier);
4016 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4017 cls->GetTypeIndex()));
4018 break;
4019 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4020 DCHECK(!kEmitCompilerReadBarrier);
4021 // Add ADRP with its PC-relative type patch.
4022 const DexFile& dex_file = cls->GetDexFile();
4023 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004024 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004025 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004026 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004027 __ Bind(adrp_label);
4028 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004029 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004030 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004031 vixl::aarch64::Label* add_label =
4032 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004033 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004034 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004035 __ Bind(add_label);
4036 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004037 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004038 break;
4039 }
4040 case HLoadClass::LoadKind::kBootImageAddress: {
4041 DCHECK(!kEmitCompilerReadBarrier);
4042 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4043 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4044 break;
4045 }
4046 case HLoadClass::LoadKind::kDexCacheAddress: {
4047 DCHECK_NE(cls->GetAddress(), 0u);
4048 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4049 // that gives a 16KiB range. To try and reduce the number of literals if we load
4050 // multiple types, simply split the dex cache address to a 16KiB aligned base
4051 // loaded from a literal and the remaining offset embedded in the load.
4052 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4053 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4054 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4055 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4056 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4057 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4058 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4059 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4060 generate_null_check = !cls->IsInDexCache();
4061 break;
4062 }
4063 case HLoadClass::LoadKind::kDexCachePcRelative: {
4064 // Add ADRP with its PC-relative DexCache access patch.
4065 const DexFile& dex_file = cls->GetDexFile();
4066 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004067 vixl::aarch64::Label* adrp_label =
4068 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004069 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004070 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004071 __ Bind(adrp_label);
4072 __ adrp(out.X(), /* offset placeholder */ 0);
4073 }
4074 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004075 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004076 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4077 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4078 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4079 generate_null_check = !cls->IsInDexCache();
4080 break;
4081 }
4082 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4083 MemberOffset resolved_types_offset =
4084 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4085 // /* GcRoot<mirror::Class>[] */ out =
4086 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4087 Register current_method = InputRegisterAt(cls, 0);
4088 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4089 // /* GcRoot<mirror::Class> */ out = out[type_index]
4090 GenerateGcRootFieldLoad(
4091 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4092 generate_null_check = !cls->IsInDexCache();
4093 break;
4094 }
4095 }
4096
4097 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4098 DCHECK(cls->CanCallRuntime());
4099 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4100 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4101 codegen_->AddSlowPath(slow_path);
4102 if (generate_null_check) {
4103 __ Cbz(out, slow_path->GetEntryLabel());
4104 }
4105 if (cls->MustGenerateClinitCheck()) {
4106 GenerateClassInitializationCheck(slow_path, out);
4107 } else {
4108 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004109 }
4110 }
4111}
4112
David Brazdilcb1c0552015-08-04 16:22:25 +01004113static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004114 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004115}
4116
Alexandre Rames67555f72014-11-18 10:55:16 +00004117void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4118 LocationSummary* locations =
4119 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4120 locations->SetOut(Location::RequiresRegister());
4121}
4122
4123void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004124 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4125}
4126
4127void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4128 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4129}
4130
4131void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4132 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004133}
4134
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004135HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4136 HLoadString::LoadKind desired_string_load_kind) {
4137 if (kEmitCompilerReadBarrier) {
4138 switch (desired_string_load_kind) {
4139 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4140 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4141 case HLoadString::LoadKind::kBootImageAddress:
4142 // TODO: Implement for read barrier.
4143 return HLoadString::LoadKind::kDexCacheViaMethod;
4144 default:
4145 break;
4146 }
4147 }
4148 switch (desired_string_load_kind) {
4149 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4150 DCHECK(!GetCompilerOptions().GetCompilePic());
4151 break;
4152 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4153 DCHECK(GetCompilerOptions().GetCompilePic());
4154 break;
4155 case HLoadString::LoadKind::kBootImageAddress:
4156 break;
4157 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004158 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004159 break;
4160 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004161 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004162 break;
4163 case HLoadString::LoadKind::kDexCacheViaMethod:
4164 break;
4165 }
4166 return desired_string_load_kind;
4167}
4168
Alexandre Rames67555f72014-11-18 10:55:16 +00004169void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004170 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004171 ? LocationSummary::kCallOnSlowPath
4172 : LocationSummary::kNoCall;
4173 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004174 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4175 locations->SetInAt(0, Location::RequiresRegister());
4176 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004177 locations->SetOut(Location::RequiresRegister());
4178}
4179
4180void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004181 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004183 switch (load->GetLoadKind()) {
4184 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4185 DCHECK(!kEmitCompilerReadBarrier);
4186 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4187 load->GetStringIndex()));
4188 return; // No dex cache slow path.
4189 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4190 DCHECK(!kEmitCompilerReadBarrier);
4191 // Add ADRP with its PC-relative String patch.
4192 const DexFile& dex_file = load->GetDexFile();
4193 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004194 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004195 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004196 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004197 __ Bind(adrp_label);
4198 __ adrp(out.X(), /* offset placeholder */ 0);
4199 }
4200 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004201 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004202 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4203 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004204 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004205 __ Bind(add_label);
4206 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4207 }
4208 return; // No dex cache slow path.
4209 }
4210 case HLoadString::LoadKind::kBootImageAddress: {
4211 DCHECK(!kEmitCompilerReadBarrier);
4212 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4213 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4214 return; // No dex cache slow path.
4215 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004216 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004217 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004218 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004220 // TODO: Re-add the compiler code to do string dex cache lookup again.
4221 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4222 codegen_->AddSlowPath(slow_path);
4223 __ B(slow_path->GetEntryLabel());
4224 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004225}
4226
Alexandre Rames5319def2014-10-23 10:03:10 +01004227void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4229 locations->SetOut(Location::ConstantLocation(constant));
4230}
4231
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004232void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004233 // Will be generated at use site.
4234}
4235
Alexandre Rames67555f72014-11-18 10:55:16 +00004236void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4237 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004239 InvokeRuntimeCallingConvention calling_convention;
4240 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4241}
4242
4243void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004244 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4245 instruction,
4246 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004247 if (instruction->IsEnter()) {
4248 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4249 } else {
4250 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4251 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004252}
4253
Alexandre Rames42d641b2014-10-27 14:00:51 +00004254void LocationsBuilderARM64::VisitMul(HMul* mul) {
4255 LocationSummary* locations =
4256 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4257 switch (mul->GetResultType()) {
4258 case Primitive::kPrimInt:
4259 case Primitive::kPrimLong:
4260 locations->SetInAt(0, Location::RequiresRegister());
4261 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004263 break;
4264
4265 case Primitive::kPrimFloat:
4266 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004267 locations->SetInAt(0, Location::RequiresFpuRegister());
4268 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004269 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004270 break;
4271
4272 default:
4273 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4274 }
4275}
4276
4277void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4278 switch (mul->GetResultType()) {
4279 case Primitive::kPrimInt:
4280 case Primitive::kPrimLong:
4281 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4282 break;
4283
4284 case Primitive::kPrimFloat:
4285 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004286 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004287 break;
4288
4289 default:
4290 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4291 }
4292}
4293
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004294void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4295 LocationSummary* locations =
4296 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4297 switch (neg->GetResultType()) {
4298 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004299 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004300 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004302 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004303
4304 case Primitive::kPrimFloat:
4305 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004306 locations->SetInAt(0, Location::RequiresFpuRegister());
4307 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004308 break;
4309
4310 default:
4311 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4312 }
4313}
4314
4315void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4316 switch (neg->GetResultType()) {
4317 case Primitive::kPrimInt:
4318 case Primitive::kPrimLong:
4319 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4320 break;
4321
4322 case Primitive::kPrimFloat:
4323 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004324 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004325 break;
4326
4327 default:
4328 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4329 }
4330}
4331
4332void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4333 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004335 InvokeRuntimeCallingConvention calling_convention;
4336 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004337 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004338 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004339 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004340}
4341
4342void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4343 LocationSummary* locations = instruction->GetLocations();
4344 InvokeRuntimeCallingConvention calling_convention;
4345 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4346 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004347 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004348 // Note: if heap poisoning is enabled, the entry point takes cares
4349 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004350 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004351 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004352}
4353
Alexandre Rames5319def2014-10-23 10:03:10 +01004354void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4355 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004356 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004357 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004358 if (instruction->IsStringAlloc()) {
4359 locations->AddTemp(LocationFrom(kArtMethodRegister));
4360 } else {
4361 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4362 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4363 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004364 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4365}
4366
4367void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004368 // Note: if heap poisoning is enabled, the entry point takes cares
4369 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004370 if (instruction->IsStringAlloc()) {
4371 // String is allocated through StringFactory. Call NewEmptyString entry point.
4372 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004373 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004374 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4375 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4376 __ Blr(lr);
4377 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4378 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004379 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004380 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4381 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004382}
4383
4384void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4385 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004386 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004388}
4389
4390void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004391 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004392 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004393 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004394 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004395 break;
4396
4397 default:
4398 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4399 }
4400}
4401
David Brazdil66d126e2015-04-03 16:02:44 +01004402void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4404 locations->SetInAt(0, Location::RequiresRegister());
4405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4406}
4407
4408void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004409 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004410}
4411
Alexandre Rames5319def2014-10-23 10:03:10 +01004412void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004413 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4414 ? LocationSummary::kCallOnSlowPath
4415 : LocationSummary::kNoCall;
4416 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004417 locations->SetInAt(0, Location::RequiresRegister());
4418 if (instruction->HasUses()) {
4419 locations->SetOut(Location::SameAsFirstInput());
4420 }
4421}
4422
Calin Juravle2ae48182016-03-16 14:05:09 +00004423void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4424 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004425 return;
4426 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004427
Alexandre Ramesd921d642015-04-16 15:07:16 +01004428 BlockPoolsScope block_pools(GetVIXLAssembler());
4429 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004430 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004431 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004432}
4433
Calin Juravle2ae48182016-03-16 14:05:09 +00004434void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004435 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004436 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004437
4438 LocationSummary* locations = instruction->GetLocations();
4439 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004440
4441 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004442}
4443
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004444void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004445 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004446}
4447
Alexandre Rames67555f72014-11-18 10:55:16 +00004448void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4449 HandleBinaryOp(instruction);
4450}
4451
4452void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4453 HandleBinaryOp(instruction);
4454}
4455
Alexandre Rames3e69f162014-12-10 10:36:50 +00004456void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4457 LOG(FATAL) << "Unreachable";
4458}
4459
4460void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4461 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4462}
4463
Alexandre Rames5319def2014-10-23 10:03:10 +01004464void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4465 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4466 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4467 if (location.IsStackSlot()) {
4468 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4469 } else if (location.IsDoubleStackSlot()) {
4470 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4471 }
4472 locations->SetOut(location);
4473}
4474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004475void InstructionCodeGeneratorARM64::VisitParameterValue(
4476 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004477 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004478}
4479
4480void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4481 LocationSummary* locations =
4482 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004483 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004484}
4485
4486void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4487 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4488 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004489}
4490
4491void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004493 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004494 locations->SetInAt(i, Location::Any());
4495 }
4496 locations->SetOut(Location::Any());
4497}
4498
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004499void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004500 LOG(FATAL) << "Unreachable";
4501}
4502
Serban Constantinescu02164b32014-11-13 14:05:07 +00004503void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004504 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004505 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004506 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4507 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4509
4510 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004511 case Primitive::kPrimInt:
4512 case Primitive::kPrimLong:
4513 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004514 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004515 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4516 break;
4517
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004518 case Primitive::kPrimFloat:
4519 case Primitive::kPrimDouble: {
4520 InvokeRuntimeCallingConvention calling_convention;
4521 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4522 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4523 locations->SetOut(calling_convention.GetReturnLocation(type));
4524
4525 break;
4526 }
4527
Serban Constantinescu02164b32014-11-13 14:05:07 +00004528 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004529 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004530 }
4531}
4532
4533void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4534 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004535
Serban Constantinescu02164b32014-11-13 14:05:07 +00004536 switch (type) {
4537 case Primitive::kPrimInt:
4538 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004539 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004540 break;
4541 }
4542
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004543 case Primitive::kPrimFloat:
4544 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004545 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4546 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004547 if (type == Primitive::kPrimFloat) {
4548 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4549 } else {
4550 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4551 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004552 break;
4553 }
4554
Serban Constantinescu02164b32014-11-13 14:05:07 +00004555 default:
4556 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004557 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004558 }
4559}
4560
Calin Juravle27df7582015-04-17 19:12:31 +01004561void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4562 memory_barrier->SetLocations(nullptr);
4563}
4564
4565void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004566 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004567}
4568
Alexandre Rames5319def2014-10-23 10:03:10 +01004569void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4570 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4571 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004572 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004573}
4574
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004575void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004576 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004577}
4578
4579void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4580 instruction->SetLocations(nullptr);
4581}
4582
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004583void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004584 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004585}
4586
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004587void LocationsBuilderARM64::VisitRor(HRor* ror) {
4588 HandleBinaryOp(ror);
4589}
4590
4591void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4592 HandleBinaryOp(ror);
4593}
4594
Serban Constantinescu02164b32014-11-13 14:05:07 +00004595void LocationsBuilderARM64::VisitShl(HShl* shl) {
4596 HandleShift(shl);
4597}
4598
4599void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4600 HandleShift(shl);
4601}
4602
4603void LocationsBuilderARM64::VisitShr(HShr* shr) {
4604 HandleShift(shr);
4605}
4606
4607void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4608 HandleShift(shr);
4609}
4610
Alexandre Rames5319def2014-10-23 10:03:10 +01004611void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004612 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004613}
4614
4615void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004616 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004617}
4618
Alexandre Rames67555f72014-11-18 10:55:16 +00004619void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004620 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004621}
4622
4623void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004624 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004625}
4626
4627void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004628 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004629}
4630
Alexandre Rames67555f72014-11-18 10:55:16 +00004631void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004632 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004633}
4634
Calin Juravlee460d1d2015-09-29 04:52:17 +01004635void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4636 HUnresolvedInstanceFieldGet* instruction) {
4637 FieldAccessCallingConventionARM64 calling_convention;
4638 codegen_->CreateUnresolvedFieldLocationSummary(
4639 instruction, instruction->GetFieldType(), calling_convention);
4640}
4641
4642void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4643 HUnresolvedInstanceFieldGet* instruction) {
4644 FieldAccessCallingConventionARM64 calling_convention;
4645 codegen_->GenerateUnresolvedFieldAccess(instruction,
4646 instruction->GetFieldType(),
4647 instruction->GetFieldIndex(),
4648 instruction->GetDexPc(),
4649 calling_convention);
4650}
4651
4652void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4653 HUnresolvedInstanceFieldSet* instruction) {
4654 FieldAccessCallingConventionARM64 calling_convention;
4655 codegen_->CreateUnresolvedFieldLocationSummary(
4656 instruction, instruction->GetFieldType(), calling_convention);
4657}
4658
4659void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4660 HUnresolvedInstanceFieldSet* instruction) {
4661 FieldAccessCallingConventionARM64 calling_convention;
4662 codegen_->GenerateUnresolvedFieldAccess(instruction,
4663 instruction->GetFieldType(),
4664 instruction->GetFieldIndex(),
4665 instruction->GetDexPc(),
4666 calling_convention);
4667}
4668
4669void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4670 HUnresolvedStaticFieldGet* instruction) {
4671 FieldAccessCallingConventionARM64 calling_convention;
4672 codegen_->CreateUnresolvedFieldLocationSummary(
4673 instruction, instruction->GetFieldType(), calling_convention);
4674}
4675
4676void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4677 HUnresolvedStaticFieldGet* instruction) {
4678 FieldAccessCallingConventionARM64 calling_convention;
4679 codegen_->GenerateUnresolvedFieldAccess(instruction,
4680 instruction->GetFieldType(),
4681 instruction->GetFieldIndex(),
4682 instruction->GetDexPc(),
4683 calling_convention);
4684}
4685
4686void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4687 HUnresolvedStaticFieldSet* instruction) {
4688 FieldAccessCallingConventionARM64 calling_convention;
4689 codegen_->CreateUnresolvedFieldLocationSummary(
4690 instruction, instruction->GetFieldType(), calling_convention);
4691}
4692
4693void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4694 HUnresolvedStaticFieldSet* instruction) {
4695 FieldAccessCallingConventionARM64 calling_convention;
4696 codegen_->GenerateUnresolvedFieldAccess(instruction,
4697 instruction->GetFieldType(),
4698 instruction->GetFieldIndex(),
4699 instruction->GetDexPc(),
4700 calling_convention);
4701}
4702
Alexandre Rames5319def2014-10-23 10:03:10 +01004703void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4704 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4705}
4706
4707void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004708 HBasicBlock* block = instruction->GetBlock();
4709 if (block->GetLoopInformation() != nullptr) {
4710 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4711 // The back edge will generate the suspend check.
4712 return;
4713 }
4714 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4715 // The goto will generate the suspend check.
4716 return;
4717 }
4718 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004719}
4720
Alexandre Rames67555f72014-11-18 10:55:16 +00004721void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4722 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004724 InvokeRuntimeCallingConvention calling_convention;
4725 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4726}
4727
4728void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004729 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004730 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004731}
4732
4733void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4734 LocationSummary* locations =
4735 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4736 Primitive::Type input_type = conversion->GetInputType();
4737 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004738 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004739 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4740 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4741 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4742 }
4743
Alexandre Rames542361f2015-01-29 16:57:31 +00004744 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004745 locations->SetInAt(0, Location::RequiresFpuRegister());
4746 } else {
4747 locations->SetInAt(0, Location::RequiresRegister());
4748 }
4749
Alexandre Rames542361f2015-01-29 16:57:31 +00004750 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004751 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4752 } else {
4753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4754 }
4755}
4756
4757void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4758 Primitive::Type result_type = conversion->GetResultType();
4759 Primitive::Type input_type = conversion->GetInputType();
4760
4761 DCHECK_NE(input_type, result_type);
4762
Alexandre Rames542361f2015-01-29 16:57:31 +00004763 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004764 int result_size = Primitive::ComponentSize(result_type);
4765 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004766 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004767 Register output = OutputRegister(conversion);
4768 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004769 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004770 // 'int' values are used directly as W registers, discarding the top
4771 // bits, so we don't need to sign-extend and can just perform a move.
4772 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4773 // top 32 bits of the target register. We theoretically could leave those
4774 // bits unchanged, but we would have to make sure that no code uses a
4775 // 32bit input value as a 64bit value assuming that the top 32 bits are
4776 // zero.
4777 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004778 } else if (result_type == Primitive::kPrimChar ||
4779 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4780 __ Ubfx(output,
4781 output.IsX() ? source.X() : source.W(),
4782 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004783 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004784 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004785 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004786 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004787 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004788 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004789 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4790 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004791 } else if (Primitive::IsFloatingPointType(result_type) &&
4792 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004793 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4794 } else {
4795 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4796 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004797 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004798}
Alexandre Rames67555f72014-11-18 10:55:16 +00004799
Serban Constantinescu02164b32014-11-13 14:05:07 +00004800void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4801 HandleShift(ushr);
4802}
4803
4804void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4805 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004806}
4807
4808void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4809 HandleBinaryOp(instruction);
4810}
4811
4812void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4813 HandleBinaryOp(instruction);
4814}
4815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004816void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004817 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004818 LOG(FATAL) << "Unreachable";
4819}
4820
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004821void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004822 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004823 LOG(FATAL) << "Unreachable";
4824}
4825
Mark Mendellfe57faa2015-09-18 09:26:15 -04004826// Simple implementation of packed switch - generate cascaded compare/jumps.
4827void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4828 LocationSummary* locations =
4829 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4830 locations->SetInAt(0, Location::RequiresRegister());
4831}
4832
4833void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4834 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004835 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004836 Register value_reg = InputRegisterAt(switch_instr, 0);
4837 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4838
Zheng Xu3927c8b2015-11-18 17:46:25 +08004839 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004840 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004841 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4842 // make sure we don't emit it if the target may run out of range.
4843 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4844 // ranges and emit the tables only as required.
4845 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004846
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004847 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004848 // Current instruction id is an upper bound of the number of HIRs in the graph.
4849 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4850 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004851 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4852 Register temp = temps.AcquireW();
4853 __ Subs(temp, value_reg, Operand(lower_bound));
4854
Zheng Xu3927c8b2015-11-18 17:46:25 +08004855 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004856 // Jump to successors[0] if value == lower_bound.
4857 __ B(eq, codegen_->GetLabelOf(successors[0]));
4858 int32_t last_index = 0;
4859 for (; num_entries - last_index > 2; last_index += 2) {
4860 __ Subs(temp, temp, Operand(2));
4861 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4862 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4863 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4864 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4865 }
4866 if (num_entries - last_index == 2) {
4867 // The last missing case_value.
4868 __ Cmp(temp, Operand(1));
4869 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004870 }
4871
4872 // And the default for any other value.
4873 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4874 __ B(codegen_->GetLabelOf(default_block));
4875 }
4876 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004877 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004878
4879 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4880
4881 // Below instructions should use at most one blocked register. Since there are two blocked
4882 // registers, we are free to block one.
4883 Register temp_w = temps.AcquireW();
4884 Register index;
4885 // Remove the bias.
4886 if (lower_bound != 0) {
4887 index = temp_w;
4888 __ Sub(index, value_reg, Operand(lower_bound));
4889 } else {
4890 index = value_reg;
4891 }
4892
4893 // Jump to default block if index is out of the range.
4894 __ Cmp(index, Operand(num_entries));
4895 __ B(hs, codegen_->GetLabelOf(default_block));
4896
4897 // In current VIXL implementation, it won't require any blocked registers to encode the
4898 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4899 // register pressure.
4900 Register table_base = temps.AcquireX();
4901 // Load jump offset from the table.
4902 __ Adr(table_base, jump_table->GetTableStartLabel());
4903 Register jump_offset = temp_w;
4904 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4905
4906 // Jump to target block by branching to table_base(pc related) + offset.
4907 Register target_address = table_base;
4908 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4909 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004910 }
4911}
4912
Roland Levillain44015862016-01-22 11:47:17 +00004913void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4914 Location out,
4915 uint32_t offset,
4916 Location maybe_temp) {
4917 Primitive::Type type = Primitive::kPrimNot;
4918 Register out_reg = RegisterFrom(out, type);
4919 if (kEmitCompilerReadBarrier) {
4920 Register temp_reg = RegisterFrom(maybe_temp, type);
4921 if (kUseBakerReadBarrier) {
4922 // Load with fast path based Baker's read barrier.
4923 // /* HeapReference<Object> */ out = *(out + offset)
4924 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4925 out,
4926 out_reg,
4927 offset,
4928 temp_reg,
4929 /* needs_null_check */ false,
4930 /* use_load_acquire */ false);
4931 } else {
4932 // Load with slow path based read barrier.
4933 // Save the value of `out` into `maybe_temp` before overwriting it
4934 // in the following move operation, as we will need it for the
4935 // read barrier below.
4936 __ Mov(temp_reg, out_reg);
4937 // /* HeapReference<Object> */ out = *(out + offset)
4938 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4939 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4940 }
4941 } else {
4942 // Plain load with no read barrier.
4943 // /* HeapReference<Object> */ out = *(out + offset)
4944 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4945 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4946 }
4947}
4948
4949void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4950 Location out,
4951 Location obj,
4952 uint32_t offset,
4953 Location maybe_temp) {
4954 Primitive::Type type = Primitive::kPrimNot;
4955 Register out_reg = RegisterFrom(out, type);
4956 Register obj_reg = RegisterFrom(obj, type);
4957 if (kEmitCompilerReadBarrier) {
4958 if (kUseBakerReadBarrier) {
4959 // Load with fast path based Baker's read barrier.
4960 Register temp_reg = RegisterFrom(maybe_temp, type);
4961 // /* HeapReference<Object> */ out = *(obj + offset)
4962 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4963 out,
4964 obj_reg,
4965 offset,
4966 temp_reg,
4967 /* needs_null_check */ false,
4968 /* use_load_acquire */ false);
4969 } else {
4970 // Load with slow path based read barrier.
4971 // /* HeapReference<Object> */ out = *(obj + offset)
4972 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4973 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4974 }
4975 } else {
4976 // Plain load with no read barrier.
4977 // /* HeapReference<Object> */ out = *(obj + offset)
4978 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4979 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4980 }
4981}
4982
4983void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4984 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004985 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004986 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004987 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00004988 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
4989 if (kEmitCompilerReadBarrier) {
4990 if (kUseBakerReadBarrier) {
4991 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4992 // Baker's read barrier are used:
4993 //
4994 // root = obj.field;
4995 // if (Thread::Current()->GetIsGcMarking()) {
4996 // root = ReadBarrier::Mark(root)
4997 // }
4998
4999 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005000 if (fixup_label == nullptr) {
5001 __ Ldr(root_reg, MemOperand(obj, offset));
5002 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005003 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005004 __ Bind(fixup_label);
5005 __ ldr(root_reg, MemOperand(obj, offset));
5006 }
Roland Levillain44015862016-01-22 11:47:17 +00005007 static_assert(
5008 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5009 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5010 "have different sizes.");
5011 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5012 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5013 "have different sizes.");
5014
5015 // Slow path used to mark the GC root `root`.
5016 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005017 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005018 codegen_->AddSlowPath(slow_path);
5019
5020 MacroAssembler* masm = GetVIXLAssembler();
5021 UseScratchRegisterScope temps(masm);
5022 Register temp = temps.AcquireW();
5023 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005024 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005025 __ Cbnz(temp, slow_path->GetEntryLabel());
5026 __ Bind(slow_path->GetExitLabel());
5027 } else {
5028 // GC root loaded through a slow path for read barriers other
5029 // than Baker's.
5030 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005031 if (fixup_label == nullptr) {
5032 __ Add(root_reg.X(), obj.X(), offset);
5033 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005034 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005035 __ Bind(fixup_label);
5036 __ add(root_reg.X(), obj.X(), offset);
5037 }
Roland Levillain44015862016-01-22 11:47:17 +00005038 // /* mirror::Object* */ root = root->Read()
5039 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5040 }
5041 } else {
5042 // Plain GC root load with no read barrier.
5043 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005044 if (fixup_label == nullptr) {
5045 __ Ldr(root_reg, MemOperand(obj, offset));
5046 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005047 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005048 __ Bind(fixup_label);
5049 __ ldr(root_reg, MemOperand(obj, offset));
5050 }
Roland Levillain44015862016-01-22 11:47:17 +00005051 // Note that GC roots are not affected by heap poisoning, thus we
5052 // do not have to unpoison `root_reg` here.
5053 }
5054}
5055
5056void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5057 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005058 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005059 uint32_t offset,
5060 Register temp,
5061 bool needs_null_check,
5062 bool use_load_acquire) {
5063 DCHECK(kEmitCompilerReadBarrier);
5064 DCHECK(kUseBakerReadBarrier);
5065
5066 // /* HeapReference<Object> */ ref = *(obj + offset)
5067 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005068 size_t no_scale_factor = 0U;
5069 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5070 ref,
5071 obj,
5072 offset,
5073 no_index,
5074 no_scale_factor,
5075 temp,
5076 needs_null_check,
5077 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005078}
5079
5080void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5081 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005082 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005083 uint32_t data_offset,
5084 Location index,
5085 Register temp,
5086 bool needs_null_check) {
5087 DCHECK(kEmitCompilerReadBarrier);
5088 DCHECK(kUseBakerReadBarrier);
5089
5090 // Array cells are never volatile variables, therefore array loads
5091 // never use Load-Acquire instructions on ARM64.
5092 const bool use_load_acquire = false;
5093
Roland Levillainbfea3352016-06-23 13:48:47 +01005094 static_assert(
5095 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5096 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005097 // /* HeapReference<Object> */ ref =
5098 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005099 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5100 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5101 ref,
5102 obj,
5103 data_offset,
5104 index,
5105 scale_factor,
5106 temp,
5107 needs_null_check,
5108 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005109}
5110
5111void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5112 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005113 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005114 uint32_t offset,
5115 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005116 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005117 Register temp,
5118 bool needs_null_check,
5119 bool use_load_acquire) {
5120 DCHECK(kEmitCompilerReadBarrier);
5121 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005122 // If we are emitting an array load, we should not be using a
5123 // Load Acquire instruction. In other words:
5124 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5125 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005126
5127 MacroAssembler* masm = GetVIXLAssembler();
5128 UseScratchRegisterScope temps(masm);
5129
5130 // In slow path based read barriers, the read barrier call is
5131 // inserted after the original load. However, in fast path based
5132 // Baker's read barriers, we need to perform the load of
5133 // mirror::Object::monitor_ *before* the original reference load.
5134 // This load-load ordering is required by the read barrier.
5135 // The fast path/slow path (for Baker's algorithm) should look like:
5136 //
5137 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5138 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5139 // HeapReference<Object> ref = *src; // Original reference load.
5140 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5141 // if (is_gray) {
5142 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5143 // }
5144 //
5145 // Note: the original implementation in ReadBarrier::Barrier is
5146 // slightly more complex as it performs additional checks that we do
5147 // not do here for performance reasons.
5148
5149 Primitive::Type type = Primitive::kPrimNot;
5150 Register ref_reg = RegisterFrom(ref, type);
5151 DCHECK(obj.IsW());
5152 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5153
5154 // /* int32_t */ monitor = obj->monitor_
5155 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5156 if (needs_null_check) {
5157 MaybeRecordImplicitNullCheck(instruction);
5158 }
5159 // /* LockWord */ lock_word = LockWord(monitor)
5160 static_assert(sizeof(LockWord) == sizeof(int32_t),
5161 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005162
Vladimir Marko877a0332016-07-11 19:30:56 +01005163 // Introduce a dependency on the lock_word including rb_state,
5164 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005165 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005166 // `obj` is unchanged by this operation, but its value now depends
5167 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005168 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005169
5170 // The actual reference load.
5171 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005172 // Load types involving an "index".
5173 if (use_load_acquire) {
5174 // UnsafeGetObjectVolatile intrinsic case.
5175 // Register `index` is not an index in an object array, but an
5176 // offset to an object reference field within object `obj`.
5177 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5178 DCHECK(instruction->GetLocations()->Intrinsified());
5179 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5180 << instruction->AsInvoke()->GetIntrinsic();
5181 DCHECK_EQ(offset, 0U);
5182 DCHECK_EQ(scale_factor, 0U);
5183 DCHECK_EQ(needs_null_check, 0U);
5184 // /* HeapReference<Object> */ ref = *(obj + index)
5185 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5186 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005187 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005188 // ArrayGet and UnsafeGetObject intrinsics cases.
5189 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5190 if (index.IsConstant()) {
5191 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5192 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5193 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005194 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005195 __ Add(temp2, obj, offset);
5196 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5197 temps.Release(temp2);
5198 }
Roland Levillain44015862016-01-22 11:47:17 +00005199 }
Roland Levillain44015862016-01-22 11:47:17 +00005200 } else {
5201 // /* HeapReference<Object> */ ref = *(obj + offset)
5202 MemOperand field = HeapOperand(obj, offset);
5203 if (use_load_acquire) {
5204 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5205 } else {
5206 Load(type, ref_reg, field);
5207 }
5208 }
5209
5210 // Object* ref = ref_addr->AsMirrorPtr()
5211 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5212
5213 // Slow path used to mark the object `ref` when it is gray.
5214 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005215 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005216 AddSlowPath(slow_path);
5217
5218 // if (rb_state == ReadBarrier::gray_ptr_)
5219 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005220 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5221 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5222 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5223 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5224 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005225 __ Bind(slow_path->GetExitLabel());
5226}
5227
5228void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5229 Location out,
5230 Location ref,
5231 Location obj,
5232 uint32_t offset,
5233 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005234 DCHECK(kEmitCompilerReadBarrier);
5235
Roland Levillain44015862016-01-22 11:47:17 +00005236 // Insert a slow path based read barrier *after* the reference load.
5237 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005238 // If heap poisoning is enabled, the unpoisoning of the loaded
5239 // reference will be carried out by the runtime within the slow
5240 // path.
5241 //
5242 // Note that `ref` currently does not get unpoisoned (when heap
5243 // poisoning is enabled), which is alright as the `ref` argument is
5244 // not used by the artReadBarrierSlow entry point.
5245 //
5246 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5247 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5248 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5249 AddSlowPath(slow_path);
5250
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005251 __ B(slow_path->GetEntryLabel());
5252 __ Bind(slow_path->GetExitLabel());
5253}
5254
Roland Levillain44015862016-01-22 11:47:17 +00005255void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5256 Location out,
5257 Location ref,
5258 Location obj,
5259 uint32_t offset,
5260 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005261 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005262 // Baker's read barriers shall be handled by the fast path
5263 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5264 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005265 // If heap poisoning is enabled, unpoisoning will be taken care of
5266 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005267 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005268 } else if (kPoisonHeapReferences) {
5269 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5270 }
5271}
5272
Roland Levillain44015862016-01-22 11:47:17 +00005273void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5274 Location out,
5275 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005276 DCHECK(kEmitCompilerReadBarrier);
5277
Roland Levillain44015862016-01-22 11:47:17 +00005278 // Insert a slow path based read barrier *after* the GC root load.
5279 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005280 // Note that GC roots are not affected by heap poisoning, so we do
5281 // not need to do anything special for this here.
5282 SlowPathCodeARM64* slow_path =
5283 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5284 AddSlowPath(slow_path);
5285
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005286 __ B(slow_path->GetEntryLabel());
5287 __ Bind(slow_path->GetExitLabel());
5288}
5289
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005290void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5291 LocationSummary* locations =
5292 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5293 locations->SetInAt(0, Location::RequiresRegister());
5294 locations->SetOut(Location::RequiresRegister());
5295}
5296
5297void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5298 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005299 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005300 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005301 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005302 __ Ldr(XRegisterFrom(locations->Out()),
5303 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005304 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005305 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005306 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005307 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5308 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005309 __ Ldr(XRegisterFrom(locations->Out()),
5310 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005311 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005312}
5313
5314
5315
Alexandre Rames67555f72014-11-18 10:55:16 +00005316#undef __
5317#undef QUICK_ENTRY_POINT
5318
Alexandre Rames5319def2014-10-23 10:03:10 +01005319} // namespace arm64
5320} // namespace art