blob: 4fcad510a3dc8a32ab860b762ce45999e13f92ea [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
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
142 RegisterSet* register_set,
143 int64_t spill_offset,
144 bool is_save) {
145 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
146 codegen->GetNumberOfCoreRegisters(),
147 register_set->GetFloatingPointRegisters(),
148 codegen->GetNumberOfFloatingPointRegisters()));
149
150 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100151 register_set->GetCoreRegisters() & (~callee_saved_core_registers.GetList()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000152 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100153 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.GetList()));
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
189 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
190 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
191 // If the register holds an object, update the stack mask.
192 if (locations->RegisterContainsObject(i)) {
193 locations->SetStackBit(stack_offset / kVRegSize);
194 }
195 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
196 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
197 saved_core_stack_offsets_[i] = stack_offset;
198 stack_offset += kXRegSizeInBytes;
199 }
200 }
201
202 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
203 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
204 register_set->ContainsFloatingPointRegister(i)) {
205 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
206 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
207 saved_fpu_stack_offsets_[i] = stack_offset;
208 stack_offset += kDRegSizeInBytes;
209 }
210 }
211
212 SaveRestoreLiveRegistersHelper(codegen, register_set,
213 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
214}
215
216void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
217 RegisterSet* register_set = locations->GetLiveRegisters();
218 SaveRestoreLiveRegistersHelper(codegen, register_set,
219 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
220}
221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
223 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000224 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100225
Alexandre Rames67555f72014-11-18 10:55:16 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000228 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000231 if (instruction_->CanThrowIntoCatchBlock()) {
232 // Live registers will be restored in the catch block if caught.
233 SaveLiveRegisters(codegen, instruction_->GetLocations());
234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000235 // We're moving two locations to locations that could overlap, so we need a parallel
236 // move resolver.
237 InvokeRuntimeCallingConvention calling_convention;
238 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100239 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
240 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000241 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
242 ? kQuickThrowStringBounds
243 : kQuickThrowArrayBounds;
244 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100245 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100247 }
248
Alexandre Rames8158f282015-08-07 10:26:17 +0100249 bool IsFatal() const OVERRIDE { return true; }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
255};
256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
258 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000259 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000260
261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000264 if (instruction_->CanThrowIntoCatchBlock()) {
265 // Live registers will be restored in the catch block if caught.
266 SaveLiveRegisters(codegen, instruction_->GetLocations());
267 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000268 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 }
271
Alexandre Rames8158f282015-08-07 10:26:17 +0100272 bool IsFatal() const OVERRIDE { return true; }
273
Alexandre Rames9931f312015-06-19 14:47:01 +0100274 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
278};
279
280class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
281 public:
282 LoadClassSlowPathARM64(HLoadClass* cls,
283 HInstruction* at,
284 uint32_t dex_pc,
285 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
288 }
289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 LocationSummary* locations = at_->GetLocations();
292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
293
294 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000295 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000296
297 InvokeRuntimeCallingConvention calling_convention;
298 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000299 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
300 : kQuickInitializeType;
301 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800302 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100303 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800304 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100305 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800306 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000307
308 // Move the class to the desired location.
309 Location out = locations->Out();
310 if (out.IsValid()) {
311 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
312 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000313 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 }
315
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 __ B(GetExitLabel());
318 }
319
Alexandre Rames9931f312015-06-19 14:47:01 +0100320 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 private:
323 // The class this slow path will load.
324 HLoadClass* const cls_;
325
326 // The instruction where this slow path is happening.
327 // (Might be the load class or an initialization check).
328 HInstruction* const at_;
329
330 // The dex PC of `at_`.
331 const uint32_t dex_pc_;
332
333 // Whether to initialize the class.
334 const bool do_clinit_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
337};
338
339class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000341 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
345 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
347
348 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000349 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 __ B(GetExitLabel());
361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
364
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
367};
368
Alexandre Rames5319def2014-10-23 10:03:10 +0100369class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
370 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000376 if (instruction_->CanThrowIntoCatchBlock()) {
377 // Live registers will be restored in the catch block if caught.
378 SaveLiveRegisters(codegen, instruction_->GetLocations());
379 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000380 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
381 instruction_,
382 instruction_->GetDexPc(),
383 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800384 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 }
386
Alexandre Rames8158f282015-08-07 10:26:17 +0100387 bool IsFatal() const OVERRIDE { return true; }
388
Alexandre Rames9931f312015-06-19 14:47:01 +0100389 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100392 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
393};
394
395class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
396 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100397 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000398 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100399
Alexandre Rames67555f72014-11-18 10:55:16 +0000400 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
401 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100402 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000403 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800404 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 if (successor_ == nullptr) {
406 __ B(GetReturnLabel());
407 } else {
408 __ B(arm64_codegen->GetLabelOf(successor_));
409 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100410 }
411
Scott Wakeling97c72b72016-06-24 16:19:36 +0100412 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 DCHECK(successor_ == nullptr);
414 return &return_label_;
415 }
416
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100417 HBasicBlock* GetSuccessor() const {
418 return successor_;
419 }
420
Alexandre Rames9931f312015-06-19 14:47:01 +0100421 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
422
Alexandre Rames5319def2014-10-23 10:03:10 +0100423 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 // If not null, the block to branch to after the suspend check.
425 HBasicBlock* const successor_;
426
427 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100428 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100429
430 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
431};
432
Alexandre Rames67555f72014-11-18 10:55:16 +0000433class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
434 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000435 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000436 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000437
438 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000439 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100440 Location class_to_check = locations->InAt(1);
441 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
442 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443 DCHECK(instruction_->IsCheckCast()
444 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
445 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100446 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000447
Alexandre Rames67555f72014-11-18 10:55:16 +0000448 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000450 if (!is_fatal_) {
451 SaveLiveRegisters(codegen, locations);
452 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453
454 // We're moving two locations to locations that could overlap, so we need a parallel
455 // move resolver.
456 InvokeRuntimeCallingConvention calling_convention;
457 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100458 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
459 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000460
461 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000462 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700463 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000464 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 Primitive::Type ret_type = instruction_->GetType();
466 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
467 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
468 } else {
469 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000470 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800471 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 }
473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000474 if (!is_fatal_) {
475 RestoreLiveRegisters(codegen, locations);
476 __ B(GetExitLabel());
477 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000478 }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100482
Alexandre Rames67555f72014-11-18 10:55:16 +0000483 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
487};
488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700489class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
490 public:
Aart Bik42249c32016-01-07 15:33:50 -0800491 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000492 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493
494 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 __ Bind(GetEntryLabel());
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000498 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000499 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700500 }
501
Alexandre Rames9931f312015-06-19 14:47:01 +0100502 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
503
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700504 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700505 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
506};
507
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100508class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
509 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000510 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100511
512 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
513 LocationSummary* locations = instruction_->GetLocations();
514 __ Bind(GetEntryLabel());
515 SaveLiveRegisters(codegen, locations);
516
517 InvokeRuntimeCallingConvention calling_convention;
518 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
519 parallel_move.AddMove(
520 locations->InAt(0),
521 LocationFrom(calling_convention.GetRegisterAt(0)),
522 Primitive::kPrimNot,
523 nullptr);
524 parallel_move.AddMove(
525 locations->InAt(1),
526 LocationFrom(calling_convention.GetRegisterAt(1)),
527 Primitive::kPrimInt,
528 nullptr);
529 parallel_move.AddMove(
530 locations->InAt(2),
531 LocationFrom(calling_convention.GetRegisterAt(2)),
532 Primitive::kPrimNot,
533 nullptr);
534 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
535
536 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000537 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100538 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
539 RestoreLiveRegisters(codegen, locations);
540 __ B(GetExitLabel());
541 }
542
543 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
544
545 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100546 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
547};
548
Zheng Xu3927c8b2015-11-18 17:46:25 +0800549void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
550 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000551 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800552
553 // We are about to use the assembler to place literals directly. Make sure we have enough
554 // underlying code buffer and we have generated the jump table with right size.
555 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
556 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
557
558 __ Bind(&table_start_);
559 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
560 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100561 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800562 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100563 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800564 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
565 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
566 Literal<int32_t> literal(jump_offset);
567 __ place(&literal);
568 }
569}
570
Roland Levillain44015862016-01-22 11:47:17 +0000571// Slow path marking an object during a read barrier.
572class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
573 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100574 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
575 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000576 DCHECK(kEmitCompilerReadBarrier);
577 }
578
579 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
580
581 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
582 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000583 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100584 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000585 DCHECK(instruction_->IsInstanceFieldGet() ||
586 instruction_->IsStaticFieldGet() ||
587 instruction_->IsArrayGet() ||
588 instruction_->IsLoadClass() ||
589 instruction_->IsLoadString() ||
590 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100591 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100592 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
593 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000594 << "Unexpected instruction in read barrier marking slow path: "
595 << instruction_->DebugName();
596
597 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100598 // No need to save live registers; it's taken care of by the
599 // entrypoint. Also, there is no need to update the stack mask,
600 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000601 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100602 DCHECK_NE(obj_.reg(), LR);
603 DCHECK_NE(obj_.reg(), WSP);
604 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100605 // IP0 is used internally by the ReadBarrierMarkRegX entry point
606 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700607 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100608 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
609 // "Compact" slow path, saving two moves.
610 //
611 // Instead of using the standard runtime calling convention (input
612 // and output in W0):
613 //
614 // W0 <- obj
615 // W0 <- ReadBarrierMark(W0)
616 // obj <- W0
617 //
618 // we just use rX (the register holding `obj`) as input and output
619 // of a dedicated entrypoint:
620 //
621 // rX <- ReadBarrierMarkRegX(rX)
622 //
623 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700624 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100625 // This runtime call does not require a stack map.
626 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000627 __ B(GetExitLabel());
628 }
629
630 private:
Roland Levillain44015862016-01-22 11:47:17 +0000631 const Location obj_;
632
633 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
634};
635
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000636// Slow path generating a read barrier for a heap reference.
637class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
638 public:
639 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
640 Location out,
641 Location ref,
642 Location obj,
643 uint32_t offset,
644 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000645 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000646 out_(out),
647 ref_(ref),
648 obj_(obj),
649 offset_(offset),
650 index_(index) {
651 DCHECK(kEmitCompilerReadBarrier);
652 // If `obj` is equal to `out` or `ref`, it means the initial object
653 // has been overwritten by (or after) the heap object reference load
654 // to be instrumented, e.g.:
655 //
656 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000657 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000658 //
659 // In that case, we have lost the information about the original
660 // object, and the emitted read barrier cannot work properly.
661 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
662 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
663 }
664
665 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
666 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
667 LocationSummary* locations = instruction_->GetLocations();
668 Primitive::Type type = Primitive::kPrimNot;
669 DCHECK(locations->CanCall());
670 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100671 DCHECK(instruction_->IsInstanceFieldGet() ||
672 instruction_->IsStaticFieldGet() ||
673 instruction_->IsArrayGet() ||
674 instruction_->IsInstanceOf() ||
675 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100676 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000677 << "Unexpected instruction in read barrier for heap reference slow path: "
678 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000679 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000680 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100681 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000682
683 __ Bind(GetEntryLabel());
684
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000685 SaveLiveRegisters(codegen, locations);
686
687 // We may have to change the index's value, but as `index_` is a
688 // constant member (like other "inputs" of this slow path),
689 // introduce a copy of it, `index`.
690 Location index = index_;
691 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100692 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000693 if (instruction_->IsArrayGet()) {
694 // Compute the actual memory offset and store it in `index`.
695 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
696 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
697 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
698 // We are about to change the value of `index_reg` (see the
699 // calls to vixl::MacroAssembler::Lsl and
700 // vixl::MacroAssembler::Mov below), but it has
701 // not been saved by the previous call to
702 // art::SlowPathCode::SaveLiveRegisters, as it is a
703 // callee-save register --
704 // art::SlowPathCode::SaveLiveRegisters does not consider
705 // callee-save registers, as it has been designed with the
706 // assumption that callee-save registers are supposed to be
707 // handled by the called function. So, as a callee-save
708 // register, `index_reg` _would_ eventually be saved onto
709 // the stack, but it would be too late: we would have
710 // changed its value earlier. Therefore, we manually save
711 // it here into another freely available register,
712 // `free_reg`, chosen of course among the caller-save
713 // registers (as a callee-save `free_reg` register would
714 // exhibit the same problem).
715 //
716 // Note we could have requested a temporary register from
717 // the register allocator instead; but we prefer not to, as
718 // this is a slow path, and we know we can find a
719 // caller-save register that is available.
720 Register free_reg = FindAvailableCallerSaveRegister(codegen);
721 __ Mov(free_reg.W(), index_reg);
722 index_reg = free_reg;
723 index = LocationFrom(index_reg);
724 } else {
725 // The initial register stored in `index_` has already been
726 // saved in the call to art::SlowPathCode::SaveLiveRegisters
727 // (as it is not a callee-save register), so we can freely
728 // use it.
729 }
730 // Shifting the index value contained in `index_reg` by the scale
731 // factor (2) cannot overflow in practice, as the runtime is
732 // unable to allocate object arrays with a size larger than
733 // 2^26 - 1 (that is, 2^28 - 4 bytes).
734 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
735 static_assert(
736 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
737 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
738 __ Add(index_reg, index_reg, Operand(offset_));
739 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100740 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
741 // intrinsics, `index_` is not shifted by a scale factor of 2
742 // (as in the case of ArrayGet), as it is actually an offset
743 // to an object field within an object.
744 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000745 DCHECK(instruction_->GetLocations()->Intrinsified());
746 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
747 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
748 << instruction_->AsInvoke()->GetIntrinsic();
749 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100750 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000751 }
752 }
753
754 // We're moving two or three locations to locations that could
755 // overlap, so we need a parallel move resolver.
756 InvokeRuntimeCallingConvention calling_convention;
757 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
758 parallel_move.AddMove(ref_,
759 LocationFrom(calling_convention.GetRegisterAt(0)),
760 type,
761 nullptr);
762 parallel_move.AddMove(obj_,
763 LocationFrom(calling_convention.GetRegisterAt(1)),
764 type,
765 nullptr);
766 if (index.IsValid()) {
767 parallel_move.AddMove(index,
768 LocationFrom(calling_convention.GetRegisterAt(2)),
769 Primitive::kPrimInt,
770 nullptr);
771 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
772 } else {
773 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
774 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
775 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000776 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000777 instruction_,
778 instruction_->GetDexPc(),
779 this);
780 CheckEntrypointTypes<
781 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
782 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
783
784 RestoreLiveRegisters(codegen, locations);
785
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000786 __ B(GetExitLabel());
787 }
788
789 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
790
791 private:
792 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100793 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
794 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000795 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
796 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
797 return Register(VIXLRegCodeFromART(i), kXRegSize);
798 }
799 }
800 // We shall never fail to find a free caller-save register, as
801 // there are more than two core caller-save registers on ARM64
802 // (meaning it is possible to find one which is different from
803 // `ref` and `obj`).
804 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
805 LOG(FATAL) << "Could not find a free register";
806 UNREACHABLE();
807 }
808
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000809 const Location out_;
810 const Location ref_;
811 const Location obj_;
812 const uint32_t offset_;
813 // An additional location containing an index to an array.
814 // Only used for HArrayGet and the UnsafeGetObject &
815 // UnsafeGetObjectVolatile intrinsics.
816 const Location index_;
817
818 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
819};
820
821// Slow path generating a read barrier for a GC root.
822class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
823 public:
824 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000825 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000826 DCHECK(kEmitCompilerReadBarrier);
827 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000828
829 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
830 LocationSummary* locations = instruction_->GetLocations();
831 Primitive::Type type = Primitive::kPrimNot;
832 DCHECK(locations->CanCall());
833 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000834 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
835 << "Unexpected instruction in read barrier for GC root slow path: "
836 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000837
838 __ Bind(GetEntryLabel());
839 SaveLiveRegisters(codegen, locations);
840
841 InvokeRuntimeCallingConvention calling_convention;
842 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
843 // The argument of the ReadBarrierForRootSlow is not a managed
844 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
845 // thus we need a 64-bit move here, and we cannot use
846 //
847 // arm64_codegen->MoveLocation(
848 // LocationFrom(calling_convention.GetRegisterAt(0)),
849 // root_,
850 // type);
851 //
852 // which would emit a 32-bit move, as `type` is a (32-bit wide)
853 // reference type (`Primitive::kPrimNot`).
854 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000855 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000856 instruction_,
857 instruction_->GetDexPc(),
858 this);
859 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
860 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
861
862 RestoreLiveRegisters(codegen, locations);
863 __ B(GetExitLabel());
864 }
865
866 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
867
868 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000869 const Location out_;
870 const Location root_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
873};
874
Alexandre Rames5319def2014-10-23 10:03:10 +0100875#undef __
876
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100877Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100878 Location next_location;
879 if (type == Primitive::kPrimVoid) {
880 LOG(FATAL) << "Unreachable type " << type;
881 }
882
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100884 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
885 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000886 } else if (!Primitive::IsFloatingPointType(type) &&
887 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000888 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
889 } else {
890 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000891 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
892 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100893 }
894
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000896 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100897 return next_location;
898}
899
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100900Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100901 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100902}
903
Serban Constantinescu579885a2015-02-22 20:51:33 +0000904CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
905 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100906 const CompilerOptions& compiler_options,
907 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100908 : CodeGenerator(graph,
909 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000910 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000911 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100912 callee_saved_core_registers.GetList(),
913 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100914 compiler_options,
915 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100916 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800917 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100918 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000919 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000920 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100921 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000922 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000923 uint32_literals_(std::less<uint32_t>(),
924 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100925 uint64_literals_(std::less<uint64_t>(),
926 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
927 method_patches_(MethodReferenceComparator(),
928 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
929 call_patches_(MethodReferenceComparator(),
930 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
931 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000932 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
933 boot_image_string_patches_(StringReferenceValueComparator(),
934 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
935 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100936 boot_image_type_patches_(TypeReferenceValueComparator(),
937 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
938 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939 boot_image_address_patches_(std::less<uint32_t>(),
940 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000941 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000942 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000943}
Alexandre Rames5319def2014-10-23 10:03:10 +0100944
Alexandre Rames67555f72014-11-18 10:55:16 +0000945#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100946
Zheng Xu3927c8b2015-11-18 17:46:25 +0800947void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100948 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800949 jump_table->EmitTable(this);
950 }
951}
952
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000953void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800954 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000955 // Ensure we emit the literal pool.
956 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000957
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000958 CodeGenerator::Finalize(allocator);
959}
960
Zheng Xuad4450e2015-04-17 18:48:56 +0800961void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
962 // Note: There are 6 kinds of moves:
963 // 1. constant -> GPR/FPR (non-cycle)
964 // 2. constant -> stack (non-cycle)
965 // 3. GPR/FPR -> GPR/FPR
966 // 4. GPR/FPR -> stack
967 // 5. stack -> GPR/FPR
968 // 6. stack -> stack (non-cycle)
969 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
970 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
971 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
972 // dependency.
973 vixl_temps_.Open(GetVIXLAssembler());
974}
975
976void ParallelMoveResolverARM64::FinishEmitNativeCode() {
977 vixl_temps_.Close();
978}
979
980Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
981 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
982 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
983 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
984 Location scratch = GetScratchLocation(kind);
985 if (!scratch.Equals(Location::NoLocation())) {
986 return scratch;
987 }
988 // Allocate from VIXL temp registers.
989 if (kind == Location::kRegister) {
990 scratch = LocationFrom(vixl_temps_.AcquireX());
991 } else {
992 DCHECK(kind == Location::kFpuRegister);
993 scratch = LocationFrom(vixl_temps_.AcquireD());
994 }
995 AddScratchLocation(scratch);
996 return scratch;
997}
998
999void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1000 if (loc.IsRegister()) {
1001 vixl_temps_.Release(XRegisterFrom(loc));
1002 } else {
1003 DCHECK(loc.IsFpuRegister());
1004 vixl_temps_.Release(DRegisterFrom(loc));
1005 }
1006 RemoveScratchLocation(loc);
1007}
1008
Alexandre Rames3e69f162014-12-10 10:36:50 +00001009void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001010 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001011 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001012}
1013
Alexandre Rames5319def2014-10-23 10:03:10 +01001014void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001015 MacroAssembler* masm = GetVIXLAssembler();
1016 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001017 __ Bind(&frame_entry_label_);
1018
Serban Constantinescu02164b32014-11-13 14:05:07 +00001019 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1020 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001021 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001022 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001023 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001024 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001025 __ Ldr(wzr, MemOperand(temp, 0));
1026 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001027 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001028
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001029 if (!HasEmptyFrame()) {
1030 int frame_size = GetFrameSize();
1031 // Stack layout:
1032 // sp[frame_size - 8] : lr.
1033 // ... : other preserved core registers.
1034 // ... : other preserved fp registers.
1035 // ... : reserved frame space.
1036 // sp[0] : current method.
1037 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001039 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1040 frame_size - GetCoreSpillSize());
1041 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1042 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001043 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001044}
1045
1046void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001047 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001048 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001049 if (!HasEmptyFrame()) {
1050 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001051 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1052 frame_size - FrameEntrySpillSize());
1053 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1054 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001055 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001057 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001058 __ Ret();
1059 GetAssembler()->cfi().RestoreState();
1060 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001061}
1062
Scott Wakeling97c72b72016-06-24 16:19:36 +01001063CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001064 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001065 return CPURegList(CPURegister::kRegister, kXRegSize,
1066 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001067}
1068
Scott Wakeling97c72b72016-06-24 16:19:36 +01001069CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001070 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1071 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001072 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1073 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001074}
1075
Alexandre Rames5319def2014-10-23 10:03:10 +01001076void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1077 __ Bind(GetLabelOf(block));
1078}
1079
Calin Juravle175dc732015-08-25 15:42:32 +01001080void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1081 DCHECK(location.IsRegister());
1082 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1083}
1084
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1086 if (location.IsRegister()) {
1087 locations->AddTemp(location);
1088 } else {
1089 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1090 }
1091}
1092
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001093void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001094 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001096 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001097 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001098 if (value_can_be_null) {
1099 __ Cbz(value, &done);
1100 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001101 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001104 if (value_can_be_null) {
1105 __ Bind(&done);
1106 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001107}
1108
David Brazdil58282f42016-01-14 12:45:10 +00001109void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001110 // Blocked core registers:
1111 // lr : Runtime reserved.
1112 // tr : Runtime reserved.
1113 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1114 // ip1 : VIXL core temp.
1115 // ip0 : VIXL core temp.
1116 //
1117 // Blocked fp registers:
1118 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1120 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001122 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001123 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001124
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001125 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001126 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001127 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001128 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001129
David Brazdil58282f42016-01-14 12:45:10 +00001130 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001131 // Stubs do not save callee-save floating point registers. If the graph
1132 // is debuggable, we need to deal with these registers differently. For
1133 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001134 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1135 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001136 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001137 }
1138 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001139}
1140
Alexandre Rames3e69f162014-12-10 10:36:50 +00001141size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1142 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1143 __ Str(reg, MemOperand(sp, stack_index));
1144 return kArm64WordSize;
1145}
1146
1147size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1148 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1149 __ Ldr(reg, MemOperand(sp, stack_index));
1150 return kArm64WordSize;
1151}
1152
1153size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1154 FPRegister reg = FPRegister(reg_id, kDRegSize);
1155 __ Str(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 FPRegister reg = FPRegister(reg_id, kDRegSize);
1161 __ Ldr(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
Alexandre Rames5319def2014-10-23 10:03:10 +01001165void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001166 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001167}
1168
1169void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001170 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001171}
1172
Alexandre Rames67555f72014-11-18 10:55:16 +00001173void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001174 if (constant->IsIntConstant()) {
1175 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1176 } else if (constant->IsLongConstant()) {
1177 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1178 } else if (constant->IsNullConstant()) {
1179 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001180 } else if (constant->IsFloatConstant()) {
1181 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1182 } else {
1183 DCHECK(constant->IsDoubleConstant());
1184 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1185 }
1186}
1187
Alexandre Rames3e69f162014-12-10 10:36:50 +00001188
1189static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1190 DCHECK(constant.IsConstant());
1191 HConstant* cst = constant.GetConstant();
1192 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001193 // Null is mapped to a core W register, which we associate with kPrimInt.
1194 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1196 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1197 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1198}
1199
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200void CodeGeneratorARM64::MoveLocation(Location destination,
1201 Location source,
1202 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001203 if (source.Equals(destination)) {
1204 return;
1205 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206
1207 // A valid move can always be inferred from the destination and source
1208 // locations. When moving from and to a register, the argument type can be
1209 // used to generate 32bit instead of 64bit moves. In debug mode we also
1210 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212
1213 if (destination.IsRegister() || destination.IsFpuRegister()) {
1214 if (unspecified_type) {
1215 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1216 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001217 (src_cst != nullptr && (src_cst->IsIntConstant()
1218 || src_cst->IsFloatConstant()
1219 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001220 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001222 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001223 // If the source is a double stack slot or a 64bit constant, a 64bit
1224 // type is appropriate. Else the source is a register, and since the
1225 // type has not been specified, we chose a 64bit type to force a 64bit
1226 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001230 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1231 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1232 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001233 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1234 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1235 __ Ldr(dst, StackOperandFrom(source));
1236 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001242 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001243 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1245 ? Primitive::kPrimLong
1246 : Primitive::kPrimInt;
1247 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1248 }
1249 } else {
1250 DCHECK(source.IsFpuRegister());
1251 if (destination.IsRegister()) {
1252 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1253 ? Primitive::kPrimDouble
1254 : Primitive::kPrimFloat;
1255 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1256 } else {
1257 DCHECK(destination.IsFpuRegister());
1258 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001259 }
1260 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 } else { // The destination is not a register. It must be a stack slot.
1262 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1263 if (source.IsRegister() || source.IsFpuRegister()) {
1264 if (unspecified_type) {
1265 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001269 }
1270 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1272 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1273 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1276 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277 UseScratchRegisterScope temps(GetVIXLAssembler());
1278 HConstant* src_cst = source.GetConstant();
1279 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001280 if (src_cst->IsZeroBitPattern()) {
1281 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001282 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001283 if (src_cst->IsIntConstant()) {
1284 temp = temps.AcquireW();
1285 } else if (src_cst->IsLongConstant()) {
1286 temp = temps.AcquireX();
1287 } else if (src_cst->IsFloatConstant()) {
1288 temp = temps.AcquireS();
1289 } else {
1290 DCHECK(src_cst->IsDoubleConstant());
1291 temp = temps.AcquireD();
1292 }
1293 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001294 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001295 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001296 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001297 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001298 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001300 // There is generally less pressure on FP registers.
1301 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001302 __ Ldr(temp, StackOperandFrom(source));
1303 __ Str(temp, StackOperandFrom(destination));
1304 }
1305 }
1306}
1307
1308void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001309 CPURegister dst,
1310 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001311 switch (type) {
1312 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001313 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001314 break;
1315 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001316 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001317 break;
1318 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001320 break;
1321 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 break;
1324 case Primitive::kPrimInt:
1325 case Primitive::kPrimNot:
1326 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 case Primitive::kPrimFloat:
1328 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001329 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001330 __ Ldr(dst, src);
1331 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001332 case Primitive::kPrimVoid:
1333 LOG(FATAL) << "Unreachable type " << type;
1334 }
1335}
1336
Calin Juravle77520bc2015-01-12 18:45:46 +00001337void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001338 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001339 const MemOperand& src,
1340 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001341 MacroAssembler* masm = GetVIXLAssembler();
1342 BlockPoolsScope block_pools(masm);
1343 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001344 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001345 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001346
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001347 DCHECK(!src.IsPreIndex());
1348 DCHECK(!src.IsPostIndex());
1349
1350 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001352 MemOperand base = MemOperand(temp_base);
1353 switch (type) {
1354 case Primitive::kPrimBoolean:
1355 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001356 if (needs_null_check) {
1357 MaybeRecordImplicitNullCheck(instruction);
1358 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001359 break;
1360 case Primitive::kPrimByte:
1361 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001362 if (needs_null_check) {
1363 MaybeRecordImplicitNullCheck(instruction);
1364 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001365 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1366 break;
1367 case Primitive::kPrimChar:
1368 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001369 if (needs_null_check) {
1370 MaybeRecordImplicitNullCheck(instruction);
1371 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001372 break;
1373 case Primitive::kPrimShort:
1374 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001375 if (needs_null_check) {
1376 MaybeRecordImplicitNullCheck(instruction);
1377 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001378 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1379 break;
1380 case Primitive::kPrimInt:
1381 case Primitive::kPrimNot:
1382 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001383 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001385 if (needs_null_check) {
1386 MaybeRecordImplicitNullCheck(instruction);
1387 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001388 break;
1389 case Primitive::kPrimFloat:
1390 case Primitive::kPrimDouble: {
1391 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001392 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001393
1394 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1395 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001396 if (needs_null_check) {
1397 MaybeRecordImplicitNullCheck(instruction);
1398 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001399 __ Fmov(FPRegister(dst), temp);
1400 break;
1401 }
1402 case Primitive::kPrimVoid:
1403 LOG(FATAL) << "Unreachable type " << type;
1404 }
1405}
1406
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001407void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001408 CPURegister src,
1409 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410 switch (type) {
1411 case Primitive::kPrimBoolean:
1412 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001413 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414 break;
1415 case Primitive::kPrimChar:
1416 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001417 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001418 break;
1419 case Primitive::kPrimInt:
1420 case Primitive::kPrimNot:
1421 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001422 case Primitive::kPrimFloat:
1423 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001424 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001425 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001426 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001427 case Primitive::kPrimVoid:
1428 LOG(FATAL) << "Unreachable type " << type;
1429 }
1430}
1431
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001432void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1433 CPURegister src,
1434 const MemOperand& dst) {
1435 UseScratchRegisterScope temps(GetVIXLAssembler());
1436 Register temp_base = temps.AcquireX();
1437
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001438 DCHECK(!dst.IsPreIndex());
1439 DCHECK(!dst.IsPostIndex());
1440
1441 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001442 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001443 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444 MemOperand base = MemOperand(temp_base);
1445 switch (type) {
1446 case Primitive::kPrimBoolean:
1447 case Primitive::kPrimByte:
1448 __ Stlrb(Register(src), base);
1449 break;
1450 case Primitive::kPrimChar:
1451 case Primitive::kPrimShort:
1452 __ Stlrh(Register(src), base);
1453 break;
1454 case Primitive::kPrimInt:
1455 case Primitive::kPrimNot:
1456 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001457 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001458 __ Stlr(Register(src), base);
1459 break;
1460 case Primitive::kPrimFloat:
1461 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001462 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001463 Register temp_src;
1464 if (src.IsZero()) {
1465 // The zero register is used to avoid synthesizing zero constants.
1466 temp_src = Register(src);
1467 } else {
1468 DCHECK(src.IsFPRegister());
1469 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1470 __ Fmov(temp_src, FPRegister(src));
1471 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001472
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001473 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001474 break;
1475 }
1476 case Primitive::kPrimVoid:
1477 LOG(FATAL) << "Unreachable type " << type;
1478 }
1479}
1480
Calin Juravle175dc732015-08-25 15:42:32 +01001481void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1482 HInstruction* instruction,
1483 uint32_t dex_pc,
1484 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001485 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001486 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Roland Levillain896e32d2015-05-05 18:07:10 +01001487 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001488}
1489
Roland Levillaindec8f632016-07-22 17:10:06 +01001490void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1491 HInstruction* instruction,
1492 SlowPathCode* slow_path) {
1493 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001494 GenerateInvokeRuntime(entry_point_offset);
1495}
1496
1497void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001498 BlockPoolsScope block_pools(GetVIXLAssembler());
1499 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1500 __ Blr(lr);
1501}
1502
Alexandre Rames67555f72014-11-18 10:55:16 +00001503void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001504 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001505 UseScratchRegisterScope temps(GetVIXLAssembler());
1506 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001507 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1508
Serban Constantinescu02164b32014-11-13 14:05:07 +00001509 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001510 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1511 __ Add(temp, class_reg, status_offset);
1512 __ Ldar(temp, HeapOperand(temp));
1513 __ Cmp(temp, mirror::Class::kStatusInitialized);
1514 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001515 __ Bind(slow_path->GetExitLabel());
1516}
Alexandre Rames5319def2014-10-23 10:03:10 +01001517
Roland Levillain44015862016-01-22 11:47:17 +00001518void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001519 BarrierType type = BarrierAll;
1520
1521 switch (kind) {
1522 case MemBarrierKind::kAnyAny:
1523 case MemBarrierKind::kAnyStore: {
1524 type = BarrierAll;
1525 break;
1526 }
1527 case MemBarrierKind::kLoadAny: {
1528 type = BarrierReads;
1529 break;
1530 }
1531 case MemBarrierKind::kStoreStore: {
1532 type = BarrierWrites;
1533 break;
1534 }
1535 default:
1536 LOG(FATAL) << "Unexpected memory barrier " << kind;
1537 }
1538 __ Dmb(InnerShareable, type);
1539}
1540
Serban Constantinescu02164b32014-11-13 14:05:07 +00001541void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1542 HBasicBlock* successor) {
1543 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001544 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1545 if (slow_path == nullptr) {
1546 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1547 instruction->SetSlowPath(slow_path);
1548 codegen_->AddSlowPath(slow_path);
1549 if (successor != nullptr) {
1550 DCHECK(successor->IsLoopHeader());
1551 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1552 }
1553 } else {
1554 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1555 }
1556
Serban Constantinescu02164b32014-11-13 14:05:07 +00001557 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1558 Register temp = temps.AcquireW();
1559
Andreas Gampe542451c2016-07-26 09:02:02 -07001560 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001561 if (successor == nullptr) {
1562 __ Cbnz(temp, slow_path->GetEntryLabel());
1563 __ Bind(slow_path->GetReturnLabel());
1564 } else {
1565 __ Cbz(temp, codegen_->GetLabelOf(successor));
1566 __ B(slow_path->GetEntryLabel());
1567 // slow_path will return to GetLabelOf(successor).
1568 }
1569}
1570
Alexandre Rames5319def2014-10-23 10:03:10 +01001571InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1572 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001573 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001574 assembler_(codegen->GetAssembler()),
1575 codegen_(codegen) {}
1576
1577#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001578 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001579
1580#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1581
1582enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001583 // Using a base helps identify when we hit such breakpoints.
1584 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001585#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1586 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1587#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1588};
1589
1590#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001591 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001592 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1593 } \
1594 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1596 locations->SetOut(Location::Any()); \
1597 }
1598 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1599#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1600
1601#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001602#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001603
Alexandre Rames67555f72014-11-18 10:55:16 +00001604void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001605 DCHECK_EQ(instr->InputCount(), 2U);
1606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1607 Primitive::Type type = instr->GetResultType();
1608 switch (type) {
1609 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001610 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001611 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001612 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001613 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001614 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001615
1616 case Primitive::kPrimFloat:
1617 case Primitive::kPrimDouble:
1618 locations->SetInAt(0, Location::RequiresFpuRegister());
1619 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001620 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001621 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001622
Alexandre Rames5319def2014-10-23 10:03:10 +01001623 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001624 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001625 }
1626}
1627
Alexandre Rames09a99962015-04-15 11:47:56 +01001628void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001629 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1630
1631 bool object_field_get_with_read_barrier =
1632 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001633 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001634 new (GetGraph()->GetArena()) LocationSummary(instruction,
1635 object_field_get_with_read_barrier ?
1636 LocationSummary::kCallOnSlowPath :
1637 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001638 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
1639 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
1640 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001641 locations->SetInAt(0, Location::RequiresRegister());
1642 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1643 locations->SetOut(Location::RequiresFpuRegister());
1644 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001645 // The output overlaps for an object field get when read barriers
1646 // are enabled: we do not want the load to overwrite the object's
1647 // location, as we need it to emit the read barrier.
1648 locations->SetOut(
1649 Location::RequiresRegister(),
1650 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001651 }
1652}
1653
1654void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1655 const FieldInfo& field_info) {
1656 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001657 LocationSummary* locations = instruction->GetLocations();
1658 Location base_loc = locations->InAt(0);
1659 Location out = locations->Out();
1660 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001661 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001662 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001663 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001664
Roland Levillain44015862016-01-22 11:47:17 +00001665 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1666 // Object FieldGet with Baker's read barrier case.
1667 MacroAssembler* masm = GetVIXLAssembler();
1668 UseScratchRegisterScope temps(masm);
1669 // /* HeapReference<Object> */ out = *(base + offset)
1670 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1671 Register temp = temps.AcquireW();
1672 // Note that potential implicit null checks are handled in this
1673 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1674 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1675 instruction,
1676 out,
1677 base,
1678 offset,
1679 temp,
1680 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001681 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001682 } else {
1683 // General case.
1684 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001685 // Note that a potential implicit null check is handled in this
1686 // CodeGeneratorARM64::LoadAcquire call.
1687 // NB: LoadAcquire will record the pc info if needed.
1688 codegen_->LoadAcquire(
1689 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001690 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001691 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001692 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001693 }
Roland Levillain44015862016-01-22 11:47:17 +00001694 if (field_type == Primitive::kPrimNot) {
1695 // If read barriers are enabled, emit read barriers other than
1696 // Baker's using a slow path (and also unpoison the loaded
1697 // reference, if heap poisoning is enabled).
1698 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1699 }
Roland Levillain4d027112015-07-01 15:41:14 +01001700 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001701}
1702
1703void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1704 LocationSummary* locations =
1705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1706 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001707 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1708 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1709 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001710 locations->SetInAt(1, Location::RequiresFpuRegister());
1711 } else {
1712 locations->SetInAt(1, Location::RequiresRegister());
1713 }
1714}
1715
1716void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001717 const FieldInfo& field_info,
1718 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001719 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001720 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001721
1722 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001723 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001724 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001725 Offset offset = field_info.GetFieldOffset();
1726 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001727
Roland Levillain4d027112015-07-01 15:41:14 +01001728 {
1729 // We use a block to end the scratch scope before the write barrier, thus
1730 // freeing the temporary registers so they can be used in `MarkGCCard`.
1731 UseScratchRegisterScope temps(GetVIXLAssembler());
1732
1733 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1734 DCHECK(value.IsW());
1735 Register temp = temps.AcquireW();
1736 __ Mov(temp, value.W());
1737 GetAssembler()->PoisonHeapReference(temp.W());
1738 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001739 }
Roland Levillain4d027112015-07-01 15:41:14 +01001740
1741 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001742 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1743 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001744 } else {
1745 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1746 codegen_->MaybeRecordImplicitNullCheck(instruction);
1747 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001748 }
1749
1750 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001751 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001752 }
1753}
1754
Alexandre Rames67555f72014-11-18 10:55:16 +00001755void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001756 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001757
1758 switch (type) {
1759 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001760 case Primitive::kPrimLong: {
1761 Register dst = OutputRegister(instr);
1762 Register lhs = InputRegisterAt(instr, 0);
1763 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001764 if (instr->IsAdd()) {
1765 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001766 } else if (instr->IsAnd()) {
1767 __ And(dst, lhs, rhs);
1768 } else if (instr->IsOr()) {
1769 __ Orr(dst, lhs, rhs);
1770 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001771 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001772 } else if (instr->IsRor()) {
1773 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001774 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001775 __ Ror(dst, lhs, shift);
1776 } else {
1777 // Ensure shift distance is in the same size register as the result. If
1778 // we are rotating a long and the shift comes in a w register originally,
1779 // we don't need to sxtw for use as an x since the shift distances are
1780 // all & reg_bits - 1.
1781 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1782 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 } else {
1784 DCHECK(instr->IsXor());
1785 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001786 }
1787 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001788 }
1789 case Primitive::kPrimFloat:
1790 case Primitive::kPrimDouble: {
1791 FPRegister dst = OutputFPRegister(instr);
1792 FPRegister lhs = InputFPRegisterAt(instr, 0);
1793 FPRegister rhs = InputFPRegisterAt(instr, 1);
1794 if (instr->IsAdd()) {
1795 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001796 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001797 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001798 } else {
1799 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001800 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001801 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001802 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001803 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001804 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 }
1806}
1807
Serban Constantinescu02164b32014-11-13 14:05:07 +00001808void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1809 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1810
1811 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1812 Primitive::Type type = instr->GetResultType();
1813 switch (type) {
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimLong: {
1816 locations->SetInAt(0, Location::RequiresRegister());
1817 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1818 locations->SetOut(Location::RequiresRegister());
1819 break;
1820 }
1821 default:
1822 LOG(FATAL) << "Unexpected shift type " << type;
1823 }
1824}
1825
1826void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1827 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1828
1829 Primitive::Type type = instr->GetType();
1830 switch (type) {
1831 case Primitive::kPrimInt:
1832 case Primitive::kPrimLong: {
1833 Register dst = OutputRegister(instr);
1834 Register lhs = InputRegisterAt(instr, 0);
1835 Operand rhs = InputOperandAt(instr, 1);
1836 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001837 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001838 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001839 if (instr->IsShl()) {
1840 __ Lsl(dst, lhs, shift_value);
1841 } else if (instr->IsShr()) {
1842 __ Asr(dst, lhs, shift_value);
1843 } else {
1844 __ Lsr(dst, lhs, shift_value);
1845 }
1846 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001847 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001848
1849 if (instr->IsShl()) {
1850 __ Lsl(dst, lhs, rhs_reg);
1851 } else if (instr->IsShr()) {
1852 __ Asr(dst, lhs, rhs_reg);
1853 } else {
1854 __ Lsr(dst, lhs, rhs_reg);
1855 }
1856 }
1857 break;
1858 }
1859 default:
1860 LOG(FATAL) << "Unexpected shift operation type " << type;
1861 }
1862}
1863
Alexandre Rames5319def2014-10-23 10:03:10 +01001864void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001865 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001866}
1867
1868void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001869 HandleBinaryOp(instruction);
1870}
1871
1872void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1873 HandleBinaryOp(instruction);
1874}
1875
1876void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1877 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001878}
1879
Artem Serov7fc63502016-02-09 17:15:29 +00001880void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001881 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 // There is no immediate variant of negated bitwise instructions in AArch64.
1885 locations->SetInAt(1, Location::RequiresRegister());
1886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1887}
1888
Artem Serov7fc63502016-02-09 17:15:29 +00001889void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001890 Register dst = OutputRegister(instr);
1891 Register lhs = InputRegisterAt(instr, 0);
1892 Register rhs = InputRegisterAt(instr, 1);
1893
1894 switch (instr->GetOpKind()) {
1895 case HInstruction::kAnd:
1896 __ Bic(dst, lhs, rhs);
1897 break;
1898 case HInstruction::kOr:
1899 __ Orn(dst, lhs, rhs);
1900 break;
1901 case HInstruction::kXor:
1902 __ Eon(dst, lhs, rhs);
1903 break;
1904 default:
1905 LOG(FATAL) << "Unreachable";
1906 }
1907}
1908
Alexandre Rames8626b742015-11-25 16:28:08 +00001909void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1910 HArm64DataProcWithShifterOp* instruction) {
1911 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1912 instruction->GetType() == Primitive::kPrimLong);
1913 LocationSummary* locations =
1914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1915 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1916 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1917 } else {
1918 locations->SetInAt(0, Location::RequiresRegister());
1919 }
1920 locations->SetInAt(1, Location::RequiresRegister());
1921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1922}
1923
1924void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1925 HArm64DataProcWithShifterOp* instruction) {
1926 Primitive::Type type = instruction->GetType();
1927 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1928 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1929 Register out = OutputRegister(instruction);
1930 Register left;
1931 if (kind != HInstruction::kNeg) {
1932 left = InputRegisterAt(instruction, 0);
1933 }
1934 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1935 // shifter operand operation, the IR generating `right_reg` (input to the type
1936 // conversion) can have a different type from the current instruction's type,
1937 // so we manually indicate the type.
1938 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001939 int64_t shift_amount = instruction->GetShiftAmount() &
1940 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001941
1942 Operand right_operand(0);
1943
1944 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1945 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1946 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1947 } else {
1948 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1949 }
1950
1951 // Logical binary operations do not support extension operations in the
1952 // operand. Note that VIXL would still manage if it was passed by generating
1953 // the extension as a separate instruction.
1954 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1955 DCHECK(!right_operand.IsExtendedRegister() ||
1956 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1957 kind != HInstruction::kNeg));
1958 switch (kind) {
1959 case HInstruction::kAdd:
1960 __ Add(out, left, right_operand);
1961 break;
1962 case HInstruction::kAnd:
1963 __ And(out, left, right_operand);
1964 break;
1965 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001966 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001967 __ Neg(out, right_operand);
1968 break;
1969 case HInstruction::kOr:
1970 __ Orr(out, left, right_operand);
1971 break;
1972 case HInstruction::kSub:
1973 __ Sub(out, left, right_operand);
1974 break;
1975 case HInstruction::kXor:
1976 __ Eor(out, left, right_operand);
1977 break;
1978 default:
1979 LOG(FATAL) << "Unexpected operation kind: " << kind;
1980 UNREACHABLE();
1981 }
1982}
1983
Artem Serov328429f2016-07-06 16:23:04 +01001984void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001985 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1986 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001987 LocationSummary* locations =
1988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1989 locations->SetInAt(0, Location::RequiresRegister());
1990 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1991 locations->SetOut(Location::RequiresRegister());
1992}
1993
Roland Levillain4a3aa572016-08-15 13:17:06 +00001994void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1995 HIntermediateAddress* instruction) {
1996 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1997 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001998 __ Add(OutputRegister(instruction),
1999 InputRegisterAt(instruction, 0),
2000 Operand(InputOperandAt(instruction, 1)));
2001}
2002
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002003void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002004 LocationSummary* locations =
2005 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002006 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2007 if (instr->GetOpKind() == HInstruction::kSub &&
2008 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002009 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002010 // Don't allocate register for Mneg instruction.
2011 } else {
2012 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2013 Location::RequiresRegister());
2014 }
2015 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2016 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2018}
2019
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002020void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002021 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002022 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2023 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002024
2025 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2026 // This fixup should be carried out for all multiply-accumulate instructions:
2027 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2028 if (instr->GetType() == Primitive::kPrimLong &&
2029 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2030 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002031 vixl::aarch64::Instruction* prev =
2032 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002033 if (prev->IsLoadOrStore()) {
2034 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002035 vixl::aarch64::CodeBufferCheckScope scope(masm,
2036 kInstructionSize,
2037 vixl::aarch64::CodeBufferCheckScope::kCheck,
2038 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002039 __ nop();
2040 }
2041 }
2042
2043 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002044 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002045 __ Madd(res, mul_left, mul_right, accumulator);
2046 } else {
2047 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002048 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002049 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002050 __ Mneg(res, mul_left, mul_right);
2051 } else {
2052 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2053 __ Msub(res, mul_left, mul_right, accumulator);
2054 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002055 }
2056}
2057
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002058void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002059 bool object_array_get_with_read_barrier =
2060 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002062 new (GetGraph()->GetArena()) LocationSummary(instruction,
2063 object_array_get_with_read_barrier ?
2064 LocationSummary::kCallOnSlowPath :
2065 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002066 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2067 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
2068 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002069 locations->SetInAt(0, Location::RequiresRegister());
2070 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002071 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2072 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2073 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002074 // The output overlaps in the case of an object array get with
2075 // read barriers enabled: we do not want the move to overwrite the
2076 // array's location, as we need it to emit the read barrier.
2077 locations->SetOut(
2078 Location::RequiresRegister(),
2079 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002080 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002081}
2082
2083void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002084 Primitive::Type type = instruction->GetType();
2085 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002086 LocationSummary* locations = instruction->GetLocations();
2087 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002088 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002089 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002090
Alexandre Ramesd921d642015-04-16 15:07:16 +01002091 MacroAssembler* masm = GetVIXLAssembler();
2092 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002093 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002094 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002095
Roland Levillain44015862016-01-22 11:47:17 +00002096 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2097 // Object ArrayGet with Baker's read barrier case.
2098 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002099 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2100 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002101 // Note that a potential implicit null check is handled in the
2102 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2103 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2104 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002105 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002106 // General case.
2107 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002108 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002109 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2110 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002111 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002112 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002113 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002114 // The read barrier instrumentation does not support the
2115 // HIntermediateAddress instruction yet.
2116 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002117 // We do not need to compute the intermediate address from the array: the
2118 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002119 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002120 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002121 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002122 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2123 }
2124 temp = obj;
2125 } else {
2126 __ Add(temp, obj, offset);
2127 }
2128 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2129 }
2130
2131 codegen_->Load(type, OutputCPURegister(instruction), source);
2132 codegen_->MaybeRecordImplicitNullCheck(instruction);
2133
2134 if (type == Primitive::kPrimNot) {
2135 static_assert(
2136 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2137 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2138 Location obj_loc = locations->InAt(0);
2139 if (index.IsConstant()) {
2140 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2141 } else {
2142 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2143 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002144 }
Roland Levillain4d027112015-07-01 15:41:14 +01002145 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002146}
2147
Alexandre Rames5319def2014-10-23 10:03:10 +01002148void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2150 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002151 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002152}
2153
2154void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002155 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002156 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002157 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002158 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002159}
2160
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002161void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002162 Primitive::Type value_type = instruction->GetComponentType();
2163
2164 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2166 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002167 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002168 LocationSummary::kCallOnSlowPath :
2169 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002170 locations->SetInAt(0, Location::RequiresRegister());
2171 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002172 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2173 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2174 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002175 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002176 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002177 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002178 }
2179}
2180
2181void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2182 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002183 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002184 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002185 bool needs_write_barrier =
2186 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002187
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002188 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002189 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002190 CPURegister source = value;
2191 Location index = locations->InAt(1);
2192 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2193 MemOperand destination = HeapOperand(array);
2194 MacroAssembler* masm = GetVIXLAssembler();
2195 BlockPoolsScope block_pools(masm);
2196
2197 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002198 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002199 if (index.IsConstant()) {
2200 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2201 destination = HeapOperand(array, offset);
2202 } else {
2203 UseScratchRegisterScope temps(masm);
2204 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002205 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002206 // The read barrier instrumentation does not support the
2207 // HIntermediateAddress instruction yet.
2208 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002209 // We do not need to compute the intermediate address from the array: the
2210 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002211 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002212 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002213 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002214 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2215 }
2216 temp = array;
2217 } else {
2218 __ Add(temp, array, offset);
2219 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002220 destination = HeapOperand(temp,
2221 XRegisterFrom(index),
2222 LSL,
2223 Primitive::ComponentSizeShift(value_type));
2224 }
2225 codegen_->Store(value_type, value, destination);
2226 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002227 } else {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002228 DCHECK(needs_write_barrier);
Artem Serov328429f2016-07-06 16:23:04 +01002229 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002230 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002231 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002232 {
2233 // We use a block to end the scratch scope before the write barrier, thus
2234 // freeing the temporary registers so they can be used in `MarkGCCard`.
2235 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002236 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002237 if (index.IsConstant()) {
2238 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002239 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002240 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002241 destination = HeapOperand(temp,
2242 XRegisterFrom(index),
2243 LSL,
2244 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002245 }
2246
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002247 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2248 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2249 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2250
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002251 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002252 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2253 codegen_->AddSlowPath(slow_path);
2254 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002255 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002256 __ Cbnz(Register(value), &non_zero);
2257 if (!index.IsConstant()) {
2258 __ Add(temp, array, offset);
2259 }
2260 __ Str(wzr, destination);
2261 codegen_->MaybeRecordImplicitNullCheck(instruction);
2262 __ B(&done);
2263 __ Bind(&non_zero);
2264 }
2265
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002266 if (kEmitCompilerReadBarrier) {
2267 // When read barriers are enabled, the type checking
2268 // instrumentation requires two read barriers:
2269 //
2270 // __ Mov(temp2, temp);
2271 // // /* HeapReference<Class> */ temp = temp->component_type_
2272 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002273 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002274 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2275 //
2276 // // /* HeapReference<Class> */ temp2 = value->klass_
2277 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002278 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002279 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2280 //
2281 // __ Cmp(temp, temp2);
2282 //
2283 // However, the second read barrier may trash `temp`, as it
2284 // is a temporary register, and as such would not be saved
2285 // along with live registers before calling the runtime (nor
2286 // restored afterwards). So in this case, we bail out and
2287 // delegate the work to the array set slow path.
2288 //
2289 // TODO: Extend the register allocator to support a new
2290 // "(locally) live temp" location so as to avoid always
2291 // going into the slow path when read barriers are enabled.
2292 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002293 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 Register temp2 = temps.AcquireSameSizeAs(array);
2295 // /* HeapReference<Class> */ temp = array->klass_
2296 __ Ldr(temp, HeapOperand(array, class_offset));
2297 codegen_->MaybeRecordImplicitNullCheck(instruction);
2298 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2299
2300 // /* HeapReference<Class> */ temp = temp->component_type_
2301 __ Ldr(temp, HeapOperand(temp, component_offset));
2302 // /* HeapReference<Class> */ temp2 = value->klass_
2303 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2304 // If heap poisoning is enabled, no need to unpoison `temp`
2305 // nor `temp2`, as we are comparing two poisoned references.
2306 __ Cmp(temp, temp2);
2307
2308 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002309 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002310 __ B(eq, &do_put);
2311 // If heap poisoning is enabled, the `temp` reference has
2312 // not been unpoisoned yet; unpoison it now.
2313 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2314
2315 // /* HeapReference<Class> */ temp = temp->super_class_
2316 __ Ldr(temp, HeapOperand(temp, super_offset));
2317 // If heap poisoning is enabled, no need to unpoison
2318 // `temp`, as we are comparing against null below.
2319 __ Cbnz(temp, slow_path->GetEntryLabel());
2320 __ Bind(&do_put);
2321 } else {
2322 __ B(ne, slow_path->GetEntryLabel());
2323 }
2324 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002325 }
2326 }
2327
2328 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002329 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002330 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002331 __ Mov(temp2, value.W());
2332 GetAssembler()->PoisonHeapReference(temp2);
2333 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002334 }
2335
2336 if (!index.IsConstant()) {
2337 __ Add(temp, array, offset);
2338 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002339 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002340
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002341 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002342 codegen_->MaybeRecordImplicitNullCheck(instruction);
2343 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002344 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002345
2346 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2347
2348 if (done.IsLinked()) {
2349 __ Bind(&done);
2350 }
2351
2352 if (slow_path != nullptr) {
2353 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002354 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002355 }
2356}
2357
Alexandre Rames67555f72014-11-18 10:55:16 +00002358void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002359 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2360 ? LocationSummary::kCallOnSlowPath
2361 : LocationSummary::kNoCall;
2362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002363 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002364 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002365 if (instruction->HasUses()) {
2366 locations->SetOut(Location::SameAsFirstInput());
2367 }
2368}
2369
2370void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002371 BoundsCheckSlowPathARM64* slow_path =
2372 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002373 codegen_->AddSlowPath(slow_path);
2374
2375 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2376 __ B(slow_path->GetEntryLabel(), hs);
2377}
2378
Alexandre Rames67555f72014-11-18 10:55:16 +00002379void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2380 LocationSummary* locations =
2381 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2382 locations->SetInAt(0, Location::RequiresRegister());
2383 if (check->HasUses()) {
2384 locations->SetOut(Location::SameAsFirstInput());
2385 }
2386}
2387
2388void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2389 // We assume the class is not null.
2390 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2391 check->GetLoadClass(), check, check->GetDexPc(), true);
2392 codegen_->AddSlowPath(slow_path);
2393 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2394}
2395
Roland Levillain1a653882016-03-18 18:05:57 +00002396static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2397 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2398 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2399}
2400
2401void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2402 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2403 Location rhs_loc = instruction->GetLocations()->InAt(1);
2404 if (rhs_loc.IsConstant()) {
2405 // 0.0 is the only immediate that can be encoded directly in
2406 // an FCMP instruction.
2407 //
2408 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2409 // specify that in a floating-point comparison, positive zero
2410 // and negative zero are considered equal, so we can use the
2411 // literal 0.0 for both cases here.
2412 //
2413 // Note however that some methods (Float.equal, Float.compare,
2414 // Float.compareTo, Double.equal, Double.compare,
2415 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2416 // StrictMath.min) consider 0.0 to be (strictly) greater than
2417 // -0.0. So if we ever translate calls to these methods into a
2418 // HCompare instruction, we must handle the -0.0 case with
2419 // care here.
2420 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2421 __ Fcmp(lhs_reg, 0.0);
2422 } else {
2423 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2424 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002425}
2426
Serban Constantinescu02164b32014-11-13 14:05:07 +00002427void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002428 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002429 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2430 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002431 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002432 case Primitive::kPrimBoolean:
2433 case Primitive::kPrimByte:
2434 case Primitive::kPrimShort:
2435 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002436 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002437 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002438 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002439 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2441 break;
2442 }
2443 case Primitive::kPrimFloat:
2444 case Primitive::kPrimDouble: {
2445 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002446 locations->SetInAt(1,
2447 IsFloatingPointZeroConstant(compare->InputAt(1))
2448 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2449 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002450 locations->SetOut(Location::RequiresRegister());
2451 break;
2452 }
2453 default:
2454 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2455 }
2456}
2457
2458void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2459 Primitive::Type in_type = compare->InputAt(0)->GetType();
2460
2461 // 0 if: left == right
2462 // 1 if: left > right
2463 // -1 if: left < right
2464 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002465 case Primitive::kPrimBoolean:
2466 case Primitive::kPrimByte:
2467 case Primitive::kPrimShort:
2468 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002469 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002470 case Primitive::kPrimLong: {
2471 Register result = OutputRegister(compare);
2472 Register left = InputRegisterAt(compare, 0);
2473 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002474 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002475 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2476 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002477 break;
2478 }
2479 case Primitive::kPrimFloat:
2480 case Primitive::kPrimDouble: {
2481 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002482 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002483 __ Cset(result, ne);
2484 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002485 break;
2486 }
2487 default:
2488 LOG(FATAL) << "Unimplemented compare type " << in_type;
2489 }
2490}
2491
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002492void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002494
2495 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2496 locations->SetInAt(0, Location::RequiresFpuRegister());
2497 locations->SetInAt(1,
2498 IsFloatingPointZeroConstant(instruction->InputAt(1))
2499 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2500 : Location::RequiresFpuRegister());
2501 } else {
2502 // Integer cases.
2503 locations->SetInAt(0, Location::RequiresRegister());
2504 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2505 }
2506
David Brazdilb3e773e2016-01-26 11:28:37 +00002507 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002509 }
2510}
2511
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002512void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002513 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002514 return;
2515 }
2516
2517 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002518 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002519 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002520
Roland Levillain7f63c522015-07-13 15:54:55 +00002521 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002522 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002523 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002524 } else {
2525 // Integer cases.
2526 Register lhs = InputRegisterAt(instruction, 0);
2527 Operand rhs = InputOperandAt(instruction, 1);
2528 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002529 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002530 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002531}
2532
2533#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2534 M(Equal) \
2535 M(NotEqual) \
2536 M(LessThan) \
2537 M(LessThanOrEqual) \
2538 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002539 M(GreaterThanOrEqual) \
2540 M(Below) \
2541 M(BelowOrEqual) \
2542 M(Above) \
2543 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002544#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002545void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2546void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002547FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002548#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002549#undef FOR_EACH_CONDITION_INSTRUCTION
2550
Zheng Xuc6667102015-05-15 16:08:45 +08002551void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2552 DCHECK(instruction->IsDiv() || instruction->IsRem());
2553
2554 LocationSummary* locations = instruction->GetLocations();
2555 Location second = locations->InAt(1);
2556 DCHECK(second.IsConstant());
2557
2558 Register out = OutputRegister(instruction);
2559 Register dividend = InputRegisterAt(instruction, 0);
2560 int64_t imm = Int64FromConstant(second.GetConstant());
2561 DCHECK(imm == 1 || imm == -1);
2562
2563 if (instruction->IsRem()) {
2564 __ Mov(out, 0);
2565 } else {
2566 if (imm == 1) {
2567 __ Mov(out, dividend);
2568 } else {
2569 __ Neg(out, dividend);
2570 }
2571 }
2572}
2573
2574void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2575 DCHECK(instruction->IsDiv() || instruction->IsRem());
2576
2577 LocationSummary* locations = instruction->GetLocations();
2578 Location second = locations->InAt(1);
2579 DCHECK(second.IsConstant());
2580
2581 Register out = OutputRegister(instruction);
2582 Register dividend = InputRegisterAt(instruction, 0);
2583 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002584 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002585 int ctz_imm = CTZ(abs_imm);
2586
2587 UseScratchRegisterScope temps(GetVIXLAssembler());
2588 Register temp = temps.AcquireSameSizeAs(out);
2589
2590 if (instruction->IsDiv()) {
2591 __ Add(temp, dividend, abs_imm - 1);
2592 __ Cmp(dividend, 0);
2593 __ Csel(out, temp, dividend, lt);
2594 if (imm > 0) {
2595 __ Asr(out, out, ctz_imm);
2596 } else {
2597 __ Neg(out, Operand(out, ASR, ctz_imm));
2598 }
2599 } else {
2600 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2601 __ Asr(temp, dividend, bits - 1);
2602 __ Lsr(temp, temp, bits - ctz_imm);
2603 __ Add(out, dividend, temp);
2604 __ And(out, out, abs_imm - 1);
2605 __ Sub(out, out, temp);
2606 }
2607}
2608
2609void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2610 DCHECK(instruction->IsDiv() || instruction->IsRem());
2611
2612 LocationSummary* locations = instruction->GetLocations();
2613 Location second = locations->InAt(1);
2614 DCHECK(second.IsConstant());
2615
2616 Register out = OutputRegister(instruction);
2617 Register dividend = InputRegisterAt(instruction, 0);
2618 int64_t imm = Int64FromConstant(second.GetConstant());
2619
2620 Primitive::Type type = instruction->GetResultType();
2621 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2622
2623 int64_t magic;
2624 int shift;
2625 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2626
2627 UseScratchRegisterScope temps(GetVIXLAssembler());
2628 Register temp = temps.AcquireSameSizeAs(out);
2629
2630 // temp = get_high(dividend * magic)
2631 __ Mov(temp, magic);
2632 if (type == Primitive::kPrimLong) {
2633 __ Smulh(temp, dividend, temp);
2634 } else {
2635 __ Smull(temp.X(), dividend, temp);
2636 __ Lsr(temp.X(), temp.X(), 32);
2637 }
2638
2639 if (imm > 0 && magic < 0) {
2640 __ Add(temp, temp, dividend);
2641 } else if (imm < 0 && magic > 0) {
2642 __ Sub(temp, temp, dividend);
2643 }
2644
2645 if (shift != 0) {
2646 __ Asr(temp, temp, shift);
2647 }
2648
2649 if (instruction->IsDiv()) {
2650 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2651 } else {
2652 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2653 // TODO: Strength reduction for msub.
2654 Register temp_imm = temps.AcquireSameSizeAs(out);
2655 __ Mov(temp_imm, imm);
2656 __ Msub(out, temp, temp_imm, dividend);
2657 }
2658}
2659
2660void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2661 DCHECK(instruction->IsDiv() || instruction->IsRem());
2662 Primitive::Type type = instruction->GetResultType();
2663 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2664
2665 LocationSummary* locations = instruction->GetLocations();
2666 Register out = OutputRegister(instruction);
2667 Location second = locations->InAt(1);
2668
2669 if (second.IsConstant()) {
2670 int64_t imm = Int64FromConstant(second.GetConstant());
2671
2672 if (imm == 0) {
2673 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2674 } else if (imm == 1 || imm == -1) {
2675 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002676 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002677 DivRemByPowerOfTwo(instruction);
2678 } else {
2679 DCHECK(imm <= -2 || imm >= 2);
2680 GenerateDivRemWithAnyConstant(instruction);
2681 }
2682 } else {
2683 Register dividend = InputRegisterAt(instruction, 0);
2684 Register divisor = InputRegisterAt(instruction, 1);
2685 if (instruction->IsDiv()) {
2686 __ Sdiv(out, dividend, divisor);
2687 } else {
2688 UseScratchRegisterScope temps(GetVIXLAssembler());
2689 Register temp = temps.AcquireSameSizeAs(out);
2690 __ Sdiv(temp, dividend, divisor);
2691 __ Msub(out, temp, divisor, dividend);
2692 }
2693 }
2694}
2695
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002696void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2699 switch (div->GetResultType()) {
2700 case Primitive::kPrimInt:
2701 case Primitive::kPrimLong:
2702 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002703 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2705 break;
2706
2707 case Primitive::kPrimFloat:
2708 case Primitive::kPrimDouble:
2709 locations->SetInAt(0, Location::RequiresFpuRegister());
2710 locations->SetInAt(1, Location::RequiresFpuRegister());
2711 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2712 break;
2713
2714 default:
2715 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2716 }
2717}
2718
2719void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2720 Primitive::Type type = div->GetResultType();
2721 switch (type) {
2722 case Primitive::kPrimInt:
2723 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002724 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002725 break;
2726
2727 case Primitive::kPrimFloat:
2728 case Primitive::kPrimDouble:
2729 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2730 break;
2731
2732 default:
2733 LOG(FATAL) << "Unexpected div type " << type;
2734 }
2735}
2736
Alexandre Rames67555f72014-11-18 10:55:16 +00002737void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002738 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2739 ? LocationSummary::kCallOnSlowPath
2740 : LocationSummary::kNoCall;
2741 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002742 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2743 if (instruction->HasUses()) {
2744 locations->SetOut(Location::SameAsFirstInput());
2745 }
2746}
2747
2748void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2749 SlowPathCodeARM64* slow_path =
2750 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2751 codegen_->AddSlowPath(slow_path);
2752 Location value = instruction->GetLocations()->InAt(0);
2753
Alexandre Rames3e69f162014-12-10 10:36:50 +00002754 Primitive::Type type = instruction->GetType();
2755
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002756 if (!Primitive::IsIntegralType(type)) {
2757 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002758 return;
2759 }
2760
Alexandre Rames67555f72014-11-18 10:55:16 +00002761 if (value.IsConstant()) {
2762 int64_t divisor = Int64ConstantFrom(value);
2763 if (divisor == 0) {
2764 __ B(slow_path->GetEntryLabel());
2765 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002766 // A division by a non-null constant is valid. We don't need to perform
2767 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002768 }
2769 } else {
2770 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2771 }
2772}
2773
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002774void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2775 LocationSummary* locations =
2776 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2777 locations->SetOut(Location::ConstantLocation(constant));
2778}
2779
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002780void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2781 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002782 // Will be generated at use site.
2783}
2784
Alexandre Rames5319def2014-10-23 10:03:10 +01002785void LocationsBuilderARM64::VisitExit(HExit* exit) {
2786 exit->SetLocations(nullptr);
2787}
2788
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002789void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002790}
2791
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002792void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2793 LocationSummary* locations =
2794 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2795 locations->SetOut(Location::ConstantLocation(constant));
2796}
2797
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002798void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002799 // Will be generated at use site.
2800}
2801
David Brazdilfc6a86a2015-06-26 10:33:45 +00002802void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002803 DCHECK(!successor->IsExitBlock());
2804 HBasicBlock* block = got->GetBlock();
2805 HInstruction* previous = got->GetPrevious();
2806 HLoopInformation* info = block->GetLoopInformation();
2807
David Brazdil46e2a392015-03-16 17:31:52 +00002808 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002809 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2810 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2811 return;
2812 }
2813 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2814 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2815 }
2816 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002817 __ B(codegen_->GetLabelOf(successor));
2818 }
2819}
2820
David Brazdilfc6a86a2015-06-26 10:33:45 +00002821void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2822 got->SetLocations(nullptr);
2823}
2824
2825void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2826 HandleGoto(got, got->GetSuccessor());
2827}
2828
2829void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2830 try_boundary->SetLocations(nullptr);
2831}
2832
2833void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2834 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2835 if (!successor->IsExitBlock()) {
2836 HandleGoto(try_boundary, successor);
2837 }
2838}
2839
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002840void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002841 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002842 vixl::aarch64::Label* true_target,
2843 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002844 // FP branching requires both targets to be explicit. If either of the targets
2845 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002846 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002847 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002848
David Brazdil0debae72015-11-12 18:37:00 +00002849 if (true_target == nullptr && false_target == nullptr) {
2850 // Nothing to do. The code always falls through.
2851 return;
2852 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002853 // Constant condition, statically compared against "true" (integer value 1).
2854 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002855 if (true_target != nullptr) {
2856 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002857 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002858 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002859 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002860 if (false_target != nullptr) {
2861 __ B(false_target);
2862 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002863 }
David Brazdil0debae72015-11-12 18:37:00 +00002864 return;
2865 }
2866
2867 // The following code generates these patterns:
2868 // (1) true_target == nullptr && false_target != nullptr
2869 // - opposite condition true => branch to false_target
2870 // (2) true_target != nullptr && false_target == nullptr
2871 // - condition true => branch to true_target
2872 // (3) true_target != nullptr && false_target != nullptr
2873 // - condition true => branch to true_target
2874 // - branch to false_target
2875 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002876 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002877 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002878 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002879 if (true_target == nullptr) {
2880 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2881 } else {
2882 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2883 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002884 } else {
2885 // The condition instruction has not been materialized, use its inputs as
2886 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002887 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002888
David Brazdil0debae72015-11-12 18:37:00 +00002889 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002890 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002891 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002892 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002893 IfCondition opposite_condition = condition->GetOppositeCondition();
2894 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002895 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002896 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002897 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002898 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002899 // Integer cases.
2900 Register lhs = InputRegisterAt(condition, 0);
2901 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002902
2903 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002904 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002905 if (true_target == nullptr) {
2906 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2907 non_fallthrough_target = false_target;
2908 } else {
2909 arm64_cond = ARM64Condition(condition->GetCondition());
2910 non_fallthrough_target = true_target;
2911 }
2912
Aart Bik086d27e2016-01-20 17:02:00 -08002913 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002914 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002915 switch (arm64_cond) {
2916 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002917 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002918 break;
2919 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002920 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002921 break;
2922 case lt:
2923 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002924 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002925 break;
2926 case ge:
2927 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002928 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002929 break;
2930 default:
2931 // Without the `static_cast` the compiler throws an error for
2932 // `-Werror=sign-promo`.
2933 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2934 }
2935 } else {
2936 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002937 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002938 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002939 }
2940 }
David Brazdil0debae72015-11-12 18:37:00 +00002941
2942 // If neither branch falls through (case 3), the conditional branch to `true_target`
2943 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2944 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002945 __ B(false_target);
2946 }
David Brazdil0debae72015-11-12 18:37:00 +00002947
2948 if (fallthrough_target.IsLinked()) {
2949 __ Bind(&fallthrough_target);
2950 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002951}
2952
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002953void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002955 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002956 locations->SetInAt(0, Location::RequiresRegister());
2957 }
2958}
2959
2960void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002961 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2962 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002963 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2964 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2965 true_target = nullptr;
2966 }
2967 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2968 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2969 false_target = nullptr;
2970 }
David Brazdil0debae72015-11-12 18:37:00 +00002971 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002972}
2973
2974void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2975 LocationSummary* locations = new (GetGraph()->GetArena())
2976 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002977 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002978 locations->SetInAt(0, Location::RequiresRegister());
2979 }
2980}
2981
2982void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002983 SlowPathCodeARM64* slow_path =
2984 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002985 GenerateTestAndBranch(deoptimize,
2986 /* condition_input_index */ 0,
2987 slow_path->GetEntryLabel(),
2988 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002989}
2990
David Brazdilc0b601b2016-02-08 14:20:45 +00002991static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2992 return condition->IsCondition() &&
2993 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2994}
2995
Alexandre Rames880f1192016-06-13 16:04:50 +01002996static inline Condition GetConditionForSelect(HCondition* condition) {
2997 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002998 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2999 : ARM64Condition(cond);
3000}
3001
David Brazdil74eb1b22015-12-14 11:44:01 +00003002void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003004 if (Primitive::IsFloatingPointType(select->GetType())) {
3005 locations->SetInAt(0, Location::RequiresFpuRegister());
3006 locations->SetInAt(1, Location::RequiresFpuRegister());
3007 locations->SetOut(Location::RequiresFpuRegister());
3008 } else {
3009 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3010 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3011 bool is_true_value_constant = cst_true_value != nullptr;
3012 bool is_false_value_constant = cst_false_value != nullptr;
3013 // Ask VIXL whether we should synthesize constants in registers.
3014 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3015 Operand true_op = is_true_value_constant ?
3016 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3017 Operand false_op = is_false_value_constant ?
3018 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3019 bool true_value_in_register = false;
3020 bool false_value_in_register = false;
3021 MacroAssembler::GetCselSynthesisInformation(
3022 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3023 true_value_in_register |= !is_true_value_constant;
3024 false_value_in_register |= !is_false_value_constant;
3025
3026 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3027 : Location::ConstantLocation(cst_true_value));
3028 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3029 : Location::ConstantLocation(cst_false_value));
3030 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003031 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003032
David Brazdil74eb1b22015-12-14 11:44:01 +00003033 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3034 locations->SetInAt(2, Location::RequiresRegister());
3035 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003036}
3037
3038void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003039 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003040 Condition csel_cond;
3041
3042 if (IsBooleanValueOrMaterializedCondition(cond)) {
3043 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003044 // Use the condition flags set by the previous instruction.
3045 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003046 } else {
3047 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003048 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003049 }
3050 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003051 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003052 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003053 } else {
3054 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003055 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003056 }
3057
Alexandre Rames880f1192016-06-13 16:04:50 +01003058 if (Primitive::IsFloatingPointType(select->GetType())) {
3059 __ Fcsel(OutputFPRegister(select),
3060 InputFPRegisterAt(select, 1),
3061 InputFPRegisterAt(select, 0),
3062 csel_cond);
3063 } else {
3064 __ Csel(OutputRegister(select),
3065 InputOperandAt(select, 1),
3066 InputOperandAt(select, 0),
3067 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003068 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003069}
3070
David Srbecky0cf44932015-12-09 14:09:59 +00003071void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3072 new (GetGraph()->GetArena()) LocationSummary(info);
3073}
3074
David Srbeckyd28f4a02016-03-14 17:14:24 +00003075void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3076 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003077}
3078
3079void CodeGeneratorARM64::GenerateNop() {
3080 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003081}
3082
Alexandre Rames5319def2014-10-23 10:03:10 +01003083void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003084 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003085}
3086
3087void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003088 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003089}
3090
3091void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003092 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003093}
3094
3095void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003096 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003097}
3098
Roland Levillain44015862016-01-22 11:47:17 +00003099static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3100 return kEmitCompilerReadBarrier &&
3101 (kUseBakerReadBarrier ||
3102 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3103 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3104 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3105}
3106
Alexandre Rames67555f72014-11-18 10:55:16 +00003107void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003108 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003109 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003110 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003111 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003112 case TypeCheckKind::kExactCheck:
3113 case TypeCheckKind::kAbstractClassCheck:
3114 case TypeCheckKind::kClassHierarchyCheck:
3115 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003116 call_kind =
3117 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003118 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003119 break;
3120 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003121 case TypeCheckKind::kUnresolvedCheck:
3122 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003123 call_kind = LocationSummary::kCallOnSlowPath;
3124 break;
3125 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003126
Alexandre Rames67555f72014-11-18 10:55:16 +00003127 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003128 if (baker_read_barrier_slow_path) {
3129 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3130 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003131 locations->SetInAt(0, Location::RequiresRegister());
3132 locations->SetInAt(1, Location::RequiresRegister());
3133 // The "out" register is used as a temporary, so it overlaps with the inputs.
3134 // Note that TypeCheckSlowPathARM64 uses this register too.
3135 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3136 // When read barriers are enabled, we need a temporary register for
3137 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003138 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003139 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003140 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003141}
3142
3143void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003144 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003145 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003146 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003147 Register obj = InputRegisterAt(instruction, 0);
3148 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003149 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003150 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003151 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3152 locations->GetTemp(0) :
3153 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003154 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3155 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3156 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3157 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003158
Scott Wakeling97c72b72016-06-24 16:19:36 +01003159 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003160 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003161
3162 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003163 // Avoid null check if we know `obj` is not null.
3164 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003165 __ Cbz(obj, &zero);
3166 }
3167
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003168 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003169 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003170
Roland Levillain44015862016-01-22 11:47:17 +00003171 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003172 case TypeCheckKind::kExactCheck: {
3173 __ Cmp(out, cls);
3174 __ Cset(out, eq);
3175 if (zero.IsLinked()) {
3176 __ B(&done);
3177 }
3178 break;
3179 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003180
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003181 case TypeCheckKind::kAbstractClassCheck: {
3182 // If the class is abstract, we eagerly fetch the super class of the
3183 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003184 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003185 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003186 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003187 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003188 // If `out` is null, we use it for the result, and jump to `done`.
3189 __ Cbz(out, &done);
3190 __ Cmp(out, cls);
3191 __ B(ne, &loop);
3192 __ Mov(out, 1);
3193 if (zero.IsLinked()) {
3194 __ B(&done);
3195 }
3196 break;
3197 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003198
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003199 case TypeCheckKind::kClassHierarchyCheck: {
3200 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003201 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003202 __ Bind(&loop);
3203 __ Cmp(out, cls);
3204 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003205 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003206 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003207 __ Cbnz(out, &loop);
3208 // If `out` is null, we use it for the result, and jump to `done`.
3209 __ B(&done);
3210 __ Bind(&success);
3211 __ Mov(out, 1);
3212 if (zero.IsLinked()) {
3213 __ B(&done);
3214 }
3215 break;
3216 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003217
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003218 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003219 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003220 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003221 __ Cmp(out, cls);
3222 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003223 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003224 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003225 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003226 // If `out` is null, we use it for the result, and jump to `done`.
3227 __ Cbz(out, &done);
3228 __ Ldrh(out, HeapOperand(out, primitive_offset));
3229 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3230 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003231 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003232 __ Mov(out, 1);
3233 __ B(&done);
3234 break;
3235 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003236
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 case TypeCheckKind::kArrayCheck: {
3238 __ Cmp(out, cls);
3239 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003240 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3241 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003242 codegen_->AddSlowPath(slow_path);
3243 __ B(ne, slow_path->GetEntryLabel());
3244 __ Mov(out, 1);
3245 if (zero.IsLinked()) {
3246 __ B(&done);
3247 }
3248 break;
3249 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003250
Calin Juravle98893e12015-10-02 21:05:03 +01003251 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003252 case TypeCheckKind::kInterfaceCheck: {
3253 // Note that we indeed only call on slow path, but we always go
3254 // into the slow path for the unresolved and interface check
3255 // cases.
3256 //
3257 // We cannot directly call the InstanceofNonTrivial runtime
3258 // entry point without resorting to a type checking slow path
3259 // here (i.e. by calling InvokeRuntime directly), as it would
3260 // require to assign fixed registers for the inputs of this
3261 // HInstanceOf instruction (following the runtime calling
3262 // convention), which might be cluttered by the potential first
3263 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003264 //
3265 // TODO: Introduce a new runtime entry point taking the object
3266 // to test (instead of its class) as argument, and let it deal
3267 // with the read barrier issues. This will let us refactor this
3268 // case of the `switch` code as it was previously (with a direct
3269 // call to the runtime not using a type checking slow path).
3270 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003271 DCHECK(locations->OnlyCallsOnSlowPath());
3272 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3273 /* is_fatal */ false);
3274 codegen_->AddSlowPath(slow_path);
3275 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003276 if (zero.IsLinked()) {
3277 __ B(&done);
3278 }
3279 break;
3280 }
3281 }
3282
3283 if (zero.IsLinked()) {
3284 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003285 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003286 }
3287
3288 if (done.IsLinked()) {
3289 __ Bind(&done);
3290 }
3291
3292 if (slow_path != nullptr) {
3293 __ Bind(slow_path->GetExitLabel());
3294 }
3295}
3296
3297void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3298 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3299 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3300
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003301 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003302 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003304 case TypeCheckKind::kExactCheck:
3305 case TypeCheckKind::kAbstractClassCheck:
3306 case TypeCheckKind::kClassHierarchyCheck:
3307 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003308 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3309 LocationSummary::kCallOnSlowPath :
3310 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01003311 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003312 break;
3313 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003314 case TypeCheckKind::kUnresolvedCheck:
3315 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003316 call_kind = LocationSummary::kCallOnSlowPath;
3317 break;
3318 }
3319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003321 if (baker_read_barrier_slow_path) {
3322 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3323 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003324 locations->SetInAt(0, Location::RequiresRegister());
3325 locations->SetInAt(1, Location::RequiresRegister());
3326 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3327 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003328 // When read barriers are enabled, we need an additional temporary
3329 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003330 if (TypeCheckNeedsATemporary(type_check_kind)) {
3331 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003332 }
3333}
3334
3335void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003336 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003337 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003338 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003339 Register obj = InputRegisterAt(instruction, 0);
3340 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003341 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003342 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3343 locations->GetTemp(1) :
3344 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003345 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003346 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3347 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3348 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3349 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003350
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003351 bool is_type_check_slow_path_fatal =
3352 (type_check_kind == TypeCheckKind::kExactCheck ||
3353 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3354 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3355 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3356 !instruction->CanThrowIntoCatchBlock();
3357 SlowPathCodeARM64* type_check_slow_path =
3358 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3359 is_type_check_slow_path_fatal);
3360 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003361
Scott Wakeling97c72b72016-06-24 16:19:36 +01003362 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003363 // Avoid null check if we know obj is not null.
3364 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003365 __ Cbz(obj, &done);
3366 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003367
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003369 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003370
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003371 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003372 case TypeCheckKind::kExactCheck:
3373 case TypeCheckKind::kArrayCheck: {
3374 __ Cmp(temp, cls);
3375 // Jump to slow path for throwing the exception or doing a
3376 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003377 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003378 break;
3379 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003380
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003381 case TypeCheckKind::kAbstractClassCheck: {
3382 // If the class is abstract, we eagerly fetch the super class of the
3383 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003384 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003385 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003387 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003388
3389 // If the class reference currently in `temp` is not null, jump
3390 // to the `compare_classes` label to compare it with the checked
3391 // class.
3392 __ Cbnz(temp, &compare_classes);
3393 // Otherwise, jump to the slow path to throw the exception.
3394 //
3395 // But before, move back the object's class into `temp` before
3396 // going into the slow path, as it has been overwritten in the
3397 // meantime.
3398 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003399 GenerateReferenceLoadTwoRegisters(
3400 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003401 __ B(type_check_slow_path->GetEntryLabel());
3402
3403 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003404 __ Cmp(temp, cls);
3405 __ B(ne, &loop);
3406 break;
3407 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003408
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003409 case TypeCheckKind::kClassHierarchyCheck: {
3410 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003411 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003412 __ Bind(&loop);
3413 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003414 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003416 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003417 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003418
3419 // If the class reference currently in `temp` is not null, jump
3420 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003421 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003422 // Otherwise, jump to the slow path to throw the exception.
3423 //
3424 // But before, move back the object's class into `temp` before
3425 // going into the slow path, as it has been overwritten in the
3426 // meantime.
3427 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003428 GenerateReferenceLoadTwoRegisters(
3429 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 break;
3432 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003433
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003434 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003435 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003436 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003437 __ Cmp(temp, cls);
3438 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003439
3440 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003442 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003443
3444 // If the component type is not null (i.e. the object is indeed
3445 // an array), jump to label `check_non_primitive_component_type`
3446 // to further check that this component type is not a primitive
3447 // type.
3448 __ Cbnz(temp, &check_non_primitive_component_type);
3449 // Otherwise, jump to the slow path to throw the exception.
3450 //
3451 // But before, move back the object's class into `temp` before
3452 // going into the slow path, as it has been overwritten in the
3453 // meantime.
3454 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003455 GenerateReferenceLoadTwoRegisters(
3456 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003457 __ B(type_check_slow_path->GetEntryLabel());
3458
3459 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003460 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3461 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003462 __ Cbz(temp, &done);
3463 // Same comment as above regarding `temp` and the slow path.
3464 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003465 GenerateReferenceLoadTwoRegisters(
3466 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003467 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003468 break;
3469 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003470
Calin Juravle98893e12015-10-02 21:05:03 +01003471 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003472 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003473 // We always go into the type check slow path for the unresolved
3474 // and interface check cases.
3475 //
3476 // We cannot directly call the CheckCast runtime entry point
3477 // without resorting to a type checking slow path here (i.e. by
3478 // calling InvokeRuntime directly), as it would require to
3479 // assign fixed registers for the inputs of this HInstanceOf
3480 // instruction (following the runtime calling convention), which
3481 // might be cluttered by the potential first read barrier
3482 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003483 //
3484 // TODO: Introduce a new runtime entry point taking the object
3485 // to test (instead of its class) as argument, and let it deal
3486 // with the read barrier issues. This will let us refactor this
3487 // case of the `switch` code as it was previously (with a direct
3488 // call to the runtime not using a type checking slow path).
3489 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003490 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003491 break;
3492 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003493 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003494
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003495 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003496}
3497
Alexandre Rames5319def2014-10-23 10:03:10 +01003498void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3500 locations->SetOut(Location::ConstantLocation(constant));
3501}
3502
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003503void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003504 // Will be generated at use site.
3505}
3506
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003507void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3509 locations->SetOut(Location::ConstantLocation(constant));
3510}
3511
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003512void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003513 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003514}
3515
Calin Juravle175dc732015-08-25 15:42:32 +01003516void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3517 // The trampoline uses the same calling convention as dex calling conventions,
3518 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3519 // the method_idx.
3520 HandleInvoke(invoke);
3521}
3522
3523void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3524 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3525}
3526
Alexandre Rames5319def2014-10-23 10:03:10 +01003527void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003528 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003529 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003530}
3531
Alexandre Rames67555f72014-11-18 10:55:16 +00003532void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3533 HandleInvoke(invoke);
3534}
3535
3536void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3537 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003538 LocationSummary* locations = invoke->GetLocations();
3539 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003540 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003541 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003542 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003543
3544 // The register ip1 is required to be used for the hidden argument in
3545 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003546 MacroAssembler* masm = GetVIXLAssembler();
3547 UseScratchRegisterScope scratch_scope(masm);
3548 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003549 scratch_scope.Exclude(ip1);
3550 __ Mov(ip1, invoke->GetDexMethodIndex());
3551
Alexandre Rames67555f72014-11-18 10:55:16 +00003552 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003553 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003554 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003555 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003556 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003557 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003558 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003559 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003560 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003561 // Instead of simply (possibly) unpoisoning `temp` here, we should
3562 // emit a read barrier for the previous class reference load.
3563 // However this is not required in practice, as this is an
3564 // intermediate/temporary reference and because the current
3565 // concurrent copying collector keeps the from-space memory
3566 // intact/accessible until the end of the marking phase (the
3567 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003568 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003569 __ Ldr(temp,
3570 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3571 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003572 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003573 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003574 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003575 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003576 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003577 // lr();
3578 __ Blr(lr);
3579 DCHECK(!codegen_->IsLeafMethod());
3580 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3581}
3582
3583void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003584 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3585 if (intrinsic.TryDispatch(invoke)) {
3586 return;
3587 }
3588
Alexandre Rames67555f72014-11-18 10:55:16 +00003589 HandleInvoke(invoke);
3590}
3591
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003592void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003593 // Explicit clinit checks triggered by static invokes must have been pruned by
3594 // art::PrepareForRegisterAllocation.
3595 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003596
Andreas Gampe878d58c2015-01-15 23:24:00 -08003597 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3598 if (intrinsic.TryDispatch(invoke)) {
3599 return;
3600 }
3601
Alexandre Rames67555f72014-11-18 10:55:16 +00003602 HandleInvoke(invoke);
3603}
3604
Andreas Gampe878d58c2015-01-15 23:24:00 -08003605static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3606 if (invoke->GetLocations()->Intrinsified()) {
3607 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3608 intrinsic.Dispatch(invoke);
3609 return true;
3610 }
3611 return false;
3612}
3613
Vladimir Markodc151b22015-10-15 18:02:30 +01003614HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3615 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3616 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003617 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003618 return desired_dispatch_info;
3619}
3620
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003621void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003622 // For better instruction scheduling we load the direct code pointer before the method pointer.
3623 bool direct_code_loaded = false;
3624 switch (invoke->GetCodePtrLocation()) {
3625 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3626 // LR = code address from literal pool with link-time patch.
3627 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3628 direct_code_loaded = true;
3629 break;
3630 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3631 // LR = invoke->GetDirectCodePtr();
3632 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3633 direct_code_loaded = true;
3634 break;
3635 default:
3636 break;
3637 }
3638
Andreas Gampe878d58c2015-01-15 23:24:00 -08003639 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003640 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3641 switch (invoke->GetMethodLoadKind()) {
3642 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3643 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003644 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003645 break;
3646 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003647 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003648 break;
3649 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3650 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003651 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003652 break;
3653 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3654 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003655 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003656 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3657 break;
3658 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3659 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003660 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3661 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003662 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003663 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003664 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003665 __ Bind(adrp_label);
3666 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003667 }
Vladimir Marko58155012015-08-19 12:49:41 +00003668 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003669 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003670 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003671 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003672 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003673 __ Bind(ldr_label);
3674 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003675 }
Vladimir Marko58155012015-08-19 12:49:41 +00003676 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003677 }
Vladimir Marko58155012015-08-19 12:49:41 +00003678 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003679 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003680 Register reg = XRegisterFrom(temp);
3681 Register method_reg;
3682 if (current_method.IsRegister()) {
3683 method_reg = XRegisterFrom(current_method);
3684 } else {
3685 DCHECK(invoke->GetLocations()->Intrinsified());
3686 DCHECK(!current_method.IsValid());
3687 method_reg = reg;
3688 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3689 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003690
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003691 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003692 __ Ldr(reg.X(),
3693 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003694 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003695 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003696 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3697 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003698 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3699 break;
3700 }
3701 }
3702
3703 switch (invoke->GetCodePtrLocation()) {
3704 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3705 __ Bl(&frame_entry_label_);
3706 break;
3707 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3708 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003709 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3710 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003711 __ Bind(label);
3712 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003713 break;
3714 }
3715 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3716 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3717 // LR prepared above for better instruction scheduling.
3718 DCHECK(direct_code_loaded);
3719 // lr()
3720 __ Blr(lr);
3721 break;
3722 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3723 // LR = callee_method->entry_point_from_quick_compiled_code_;
3724 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003725 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003726 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003727 // lr()
3728 __ Blr(lr);
3729 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003730 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003731
Andreas Gampe878d58c2015-01-15 23:24:00 -08003732 DCHECK(!IsLeafMethod());
3733}
3734
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003735void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003736 // Use the calling convention instead of the location of the receiver, as
3737 // intrinsics may have put the receiver in a different register. In the intrinsics
3738 // slow path, the arguments have been moved to the right place, so here we are
3739 // guaranteed that the receiver is the first register of the calling convention.
3740 InvokeDexCallingConvention calling_convention;
3741 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003742 Register temp = XRegisterFrom(temp_in);
3743 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3744 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3745 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003746 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003747
3748 BlockPoolsScope block_pools(GetVIXLAssembler());
3749
3750 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003751 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003752 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003753 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003754 // Instead of simply (possibly) unpoisoning `temp` here, we should
3755 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003756 // intermediate/temporary reference and because the current
3757 // concurrent copying collector keeps the from-space memory
3758 // intact/accessible until the end of the marking phase (the
3759 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003760 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3761 // temp = temp->GetMethodAt(method_offset);
3762 __ Ldr(temp, MemOperand(temp, method_offset));
3763 // lr = temp->GetEntryPoint();
3764 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3765 // lr();
3766 __ Blr(lr);
3767}
3768
Scott Wakeling97c72b72016-06-24 16:19:36 +01003769vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3770 const DexFile& dex_file,
3771 uint32_t string_index,
3772 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003773 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3774}
3775
Scott Wakeling97c72b72016-06-24 16:19:36 +01003776vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3777 const DexFile& dex_file,
3778 uint32_t type_index,
3779 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003780 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3781}
3782
Scott Wakeling97c72b72016-06-24 16:19:36 +01003783vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3784 const DexFile& dex_file,
3785 uint32_t element_offset,
3786 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003787 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3788}
3789
Scott Wakeling97c72b72016-06-24 16:19:36 +01003790vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3791 const DexFile& dex_file,
3792 uint32_t offset_or_index,
3793 vixl::aarch64::Label* adrp_label,
3794 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003795 // Add a patch entry and return the label.
3796 patches->emplace_back(dex_file, offset_or_index);
3797 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003798 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003799 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3800 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3801 return label;
3802}
3803
Scott Wakeling97c72b72016-06-24 16:19:36 +01003804vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003805 const DexFile& dex_file, uint32_t string_index) {
3806 return boot_image_string_patches_.GetOrCreate(
3807 StringReference(&dex_file, string_index),
3808 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3809}
3810
Scott Wakeling97c72b72016-06-24 16:19:36 +01003811vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003812 const DexFile& dex_file, uint32_t type_index) {
3813 return boot_image_type_patches_.GetOrCreate(
3814 TypeReference(&dex_file, type_index),
3815 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3816}
3817
Scott Wakeling97c72b72016-06-24 16:19:36 +01003818vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3819 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003820 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3821 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3822 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3823}
3824
Scott Wakeling97c72b72016-06-24 16:19:36 +01003825vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3826 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003827 return DeduplicateUint64Literal(address);
3828}
3829
Vladimir Marko58155012015-08-19 12:49:41 +00003830void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3831 DCHECK(linker_patches->empty());
3832 size_t size =
3833 method_patches_.size() +
3834 call_patches_.size() +
3835 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003836 pc_relative_dex_cache_patches_.size() +
3837 boot_image_string_patches_.size() +
3838 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003839 boot_image_type_patches_.size() +
3840 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003841 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003842 linker_patches->reserve(size);
3843 for (const auto& entry : method_patches_) {
3844 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003845 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3846 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003847 target_method.dex_file,
3848 target_method.dex_method_index));
3849 }
3850 for (const auto& entry : call_patches_) {
3851 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003852 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3853 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003854 target_method.dex_file,
3855 target_method.dex_method_index));
3856 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003857 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3858 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003859 info.target_method.dex_file,
3860 info.target_method.dex_method_index));
3861 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003862 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003863 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003864 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003865 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003866 info.offset_or_index));
3867 }
3868 for (const auto& entry : boot_image_string_patches_) {
3869 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003870 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3871 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003872 target_string.dex_file,
3873 target_string.string_index));
3874 }
3875 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003876 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003877 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003878 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003879 info.offset_or_index));
3880 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003881 for (const auto& entry : boot_image_type_patches_) {
3882 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003883 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3884 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003885 target_type.dex_file,
3886 target_type.type_index));
3887 }
3888 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003889 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003890 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003891 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003892 info.offset_or_index));
3893 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003894 for (const auto& entry : boot_image_address_patches_) {
3895 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003896 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3897 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003898 }
3899}
3900
Scott Wakeling97c72b72016-06-24 16:19:36 +01003901vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003902 Uint32ToLiteralMap* map) {
3903 return map->GetOrCreate(
3904 value,
3905 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3906}
3907
Scott Wakeling97c72b72016-06-24 16:19:36 +01003908vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003909 return uint64_literals_.GetOrCreate(
3910 value,
3911 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003912}
3913
Scott Wakeling97c72b72016-06-24 16:19:36 +01003914vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003915 MethodReference target_method,
3916 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003917 return map->GetOrCreate(
3918 target_method,
3919 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003920}
3921
Scott Wakeling97c72b72016-06-24 16:19:36 +01003922vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003923 MethodReference target_method) {
3924 return DeduplicateMethodLiteral(target_method, &method_patches_);
3925}
3926
Scott Wakeling97c72b72016-06-24 16:19:36 +01003927vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003928 MethodReference target_method) {
3929 return DeduplicateMethodLiteral(target_method, &call_patches_);
3930}
3931
3932
Andreas Gampe878d58c2015-01-15 23:24:00 -08003933void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003934 // Explicit clinit checks triggered by static invokes must have been pruned by
3935 // art::PrepareForRegisterAllocation.
3936 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003937
Andreas Gampe878d58c2015-01-15 23:24:00 -08003938 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3939 return;
3940 }
3941
Alexandre Ramesd921d642015-04-16 15:07:16 +01003942 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003943 LocationSummary* locations = invoke->GetLocations();
3944 codegen_->GenerateStaticOrDirectCall(
3945 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003946 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003947}
3948
3949void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003950 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3951 return;
3952 }
3953
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003954 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003955 DCHECK(!codegen_->IsLeafMethod());
3956 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3957}
3958
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003959HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3960 HLoadClass::LoadKind desired_class_load_kind) {
3961 if (kEmitCompilerReadBarrier) {
3962 switch (desired_class_load_kind) {
3963 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3964 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3965 case HLoadClass::LoadKind::kBootImageAddress:
3966 // TODO: Implement for read barrier.
3967 return HLoadClass::LoadKind::kDexCacheViaMethod;
3968 default:
3969 break;
3970 }
3971 }
3972 switch (desired_class_load_kind) {
3973 case HLoadClass::LoadKind::kReferrersClass:
3974 break;
3975 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3976 DCHECK(!GetCompilerOptions().GetCompilePic());
3977 break;
3978 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3979 DCHECK(GetCompilerOptions().GetCompilePic());
3980 break;
3981 case HLoadClass::LoadKind::kBootImageAddress:
3982 break;
3983 case HLoadClass::LoadKind::kDexCacheAddress:
3984 DCHECK(Runtime::Current()->UseJitCompilation());
3985 break;
3986 case HLoadClass::LoadKind::kDexCachePcRelative:
3987 DCHECK(!Runtime::Current()->UseJitCompilation());
3988 break;
3989 case HLoadClass::LoadKind::kDexCacheViaMethod:
3990 break;
3991 }
3992 return desired_class_load_kind;
3993}
3994
Alexandre Rames67555f72014-11-18 10:55:16 +00003995void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003996 if (cls->NeedsAccessCheck()) {
3997 InvokeRuntimeCallingConvention calling_convention;
3998 CodeGenerator::CreateLoadClassLocationSummary(
3999 cls,
4000 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004001 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004002 /* code_generator_supports_read_barrier */ true);
4003 return;
4004 }
4005
4006 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4007 ? LocationSummary::kCallOnSlowPath
4008 : LocationSummary::kNoCall;
4009 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01004010 if (kUseBakerReadBarrier && !cls->NeedsEnvironment()) {
4011 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4012 }
4013
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004014 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4015 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4016 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4017 locations->SetInAt(0, Location::RequiresRegister());
4018 }
4019 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004020}
4021
4022void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004023 if (cls->NeedsAccessCheck()) {
4024 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004025 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004026 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004027 return;
4028 }
4029
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004030 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004031 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004032
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004033 bool generate_null_check = false;
4034 switch (cls->GetLoadKind()) {
4035 case HLoadClass::LoadKind::kReferrersClass: {
4036 DCHECK(!cls->CanCallRuntime());
4037 DCHECK(!cls->MustGenerateClinitCheck());
4038 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4039 Register current_method = InputRegisterAt(cls, 0);
4040 GenerateGcRootFieldLoad(
4041 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4042 break;
4043 }
4044 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4045 DCHECK(!kEmitCompilerReadBarrier);
4046 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4047 cls->GetTypeIndex()));
4048 break;
4049 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4050 DCHECK(!kEmitCompilerReadBarrier);
4051 // Add ADRP with its PC-relative type patch.
4052 const DexFile& dex_file = cls->GetDexFile();
4053 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004054 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004055 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004056 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004057 __ Bind(adrp_label);
4058 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004059 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004060 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004061 vixl::aarch64::Label* add_label =
4062 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004063 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004064 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004065 __ Bind(add_label);
4066 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004067 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004068 break;
4069 }
4070 case HLoadClass::LoadKind::kBootImageAddress: {
4071 DCHECK(!kEmitCompilerReadBarrier);
4072 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4073 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4074 break;
4075 }
4076 case HLoadClass::LoadKind::kDexCacheAddress: {
4077 DCHECK_NE(cls->GetAddress(), 0u);
4078 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4079 // that gives a 16KiB range. To try and reduce the number of literals if we load
4080 // multiple types, simply split the dex cache address to a 16KiB aligned base
4081 // loaded from a literal and the remaining offset embedded in the load.
4082 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4083 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4084 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4085 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4086 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4087 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4088 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4089 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4090 generate_null_check = !cls->IsInDexCache();
4091 break;
4092 }
4093 case HLoadClass::LoadKind::kDexCachePcRelative: {
4094 // Add ADRP with its PC-relative DexCache access patch.
4095 const DexFile& dex_file = cls->GetDexFile();
4096 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004097 vixl::aarch64::Label* adrp_label =
4098 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004099 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004100 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004101 __ Bind(adrp_label);
4102 __ adrp(out.X(), /* offset placeholder */ 0);
4103 }
4104 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004105 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004106 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4107 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4108 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4109 generate_null_check = !cls->IsInDexCache();
4110 break;
4111 }
4112 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4113 MemberOffset resolved_types_offset =
4114 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4115 // /* GcRoot<mirror::Class>[] */ out =
4116 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4117 Register current_method = InputRegisterAt(cls, 0);
4118 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4119 // /* GcRoot<mirror::Class> */ out = out[type_index]
4120 GenerateGcRootFieldLoad(
4121 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4122 generate_null_check = !cls->IsInDexCache();
4123 break;
4124 }
4125 }
4126
4127 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4128 DCHECK(cls->CanCallRuntime());
4129 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4130 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4131 codegen_->AddSlowPath(slow_path);
4132 if (generate_null_check) {
4133 __ Cbz(out, slow_path->GetEntryLabel());
4134 }
4135 if (cls->MustGenerateClinitCheck()) {
4136 GenerateClassInitializationCheck(slow_path, out);
4137 } else {
4138 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004139 }
4140 }
4141}
4142
David Brazdilcb1c0552015-08-04 16:22:25 +01004143static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004144 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004145}
4146
Alexandre Rames67555f72014-11-18 10:55:16 +00004147void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4148 LocationSummary* locations =
4149 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4150 locations->SetOut(Location::RequiresRegister());
4151}
4152
4153void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004154 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4155}
4156
4157void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4158 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4159}
4160
4161void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4162 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004163}
4164
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004165HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4166 HLoadString::LoadKind desired_string_load_kind) {
4167 if (kEmitCompilerReadBarrier) {
4168 switch (desired_string_load_kind) {
4169 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4170 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4171 case HLoadString::LoadKind::kBootImageAddress:
4172 // TODO: Implement for read barrier.
4173 return HLoadString::LoadKind::kDexCacheViaMethod;
4174 default:
4175 break;
4176 }
4177 }
4178 switch (desired_string_load_kind) {
4179 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4180 DCHECK(!GetCompilerOptions().GetCompilePic());
4181 break;
4182 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4183 DCHECK(GetCompilerOptions().GetCompilePic());
4184 break;
4185 case HLoadString::LoadKind::kBootImageAddress:
4186 break;
4187 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004188 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004189 break;
4190 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004191 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004192 break;
4193 case HLoadString::LoadKind::kDexCacheViaMethod:
4194 break;
4195 }
4196 return desired_string_load_kind;
4197}
4198
Alexandre Rames67555f72014-11-18 10:55:16 +00004199void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004200 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004201 ? LocationSummary::kCallOnSlowPath
4202 : LocationSummary::kNoCall;
4203 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01004204 if (kUseBakerReadBarrier && !load->NeedsEnvironment()) {
4205 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4206 }
4207
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004208 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4209 locations->SetInAt(0, Location::RequiresRegister());
4210 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004211 locations->SetOut(Location::RequiresRegister());
4212}
4213
4214void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004215 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004216
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004217 switch (load->GetLoadKind()) {
4218 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4219 DCHECK(!kEmitCompilerReadBarrier);
4220 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4221 load->GetStringIndex()));
4222 return; // No dex cache slow path.
4223 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4224 DCHECK(!kEmitCompilerReadBarrier);
4225 // Add ADRP with its PC-relative String patch.
4226 const DexFile& dex_file = load->GetDexFile();
4227 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004228 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004229 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004230 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004231 __ Bind(adrp_label);
4232 __ adrp(out.X(), /* offset placeholder */ 0);
4233 }
4234 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004235 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004236 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4237 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004238 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004239 __ Bind(add_label);
4240 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4241 }
4242 return; // No dex cache slow path.
4243 }
4244 case HLoadString::LoadKind::kBootImageAddress: {
4245 DCHECK(!kEmitCompilerReadBarrier);
4246 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4247 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4248 return; // No dex cache slow path.
4249 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004250 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004251 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004252 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004253
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004254 // TODO: Re-add the compiler code to do string dex cache lookup again.
4255 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4256 codegen_->AddSlowPath(slow_path);
4257 __ B(slow_path->GetEntryLabel());
4258 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004259}
4260
Alexandre Rames5319def2014-10-23 10:03:10 +01004261void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4262 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4263 locations->SetOut(Location::ConstantLocation(constant));
4264}
4265
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004266void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004267 // Will be generated at use site.
4268}
4269
Alexandre Rames67555f72014-11-18 10:55:16 +00004270void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4271 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004273 InvokeRuntimeCallingConvention calling_convention;
4274 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4275}
4276
4277void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004278 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4279 instruction,
4280 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004281 if (instruction->IsEnter()) {
4282 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4283 } else {
4284 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4285 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004286}
4287
Alexandre Rames42d641b2014-10-27 14:00:51 +00004288void LocationsBuilderARM64::VisitMul(HMul* mul) {
4289 LocationSummary* locations =
4290 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4291 switch (mul->GetResultType()) {
4292 case Primitive::kPrimInt:
4293 case Primitive::kPrimLong:
4294 locations->SetInAt(0, Location::RequiresRegister());
4295 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004297 break;
4298
4299 case Primitive::kPrimFloat:
4300 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004301 locations->SetInAt(0, Location::RequiresFpuRegister());
4302 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004303 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004304 break;
4305
4306 default:
4307 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4308 }
4309}
4310
4311void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4312 switch (mul->GetResultType()) {
4313 case Primitive::kPrimInt:
4314 case Primitive::kPrimLong:
4315 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4316 break;
4317
4318 case Primitive::kPrimFloat:
4319 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004320 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004321 break;
4322
4323 default:
4324 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4325 }
4326}
4327
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004328void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4329 LocationSummary* locations =
4330 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4331 switch (neg->GetResultType()) {
4332 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004333 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004334 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004335 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004336 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004337
4338 case Primitive::kPrimFloat:
4339 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004340 locations->SetInAt(0, Location::RequiresFpuRegister());
4341 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004342 break;
4343
4344 default:
4345 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4346 }
4347}
4348
4349void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4350 switch (neg->GetResultType()) {
4351 case Primitive::kPrimInt:
4352 case Primitive::kPrimLong:
4353 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4354 break;
4355
4356 case Primitive::kPrimFloat:
4357 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004358 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004359 break;
4360
4361 default:
4362 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4363 }
4364}
4365
4366void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4367 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004368 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004369 InvokeRuntimeCallingConvention calling_convention;
4370 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004371 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004372 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004373 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004374}
4375
4376void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4377 LocationSummary* locations = instruction->GetLocations();
4378 InvokeRuntimeCallingConvention calling_convention;
4379 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4380 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004381 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004382 // Note: if heap poisoning is enabled, the entry point takes cares
4383 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004384 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004385 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004386}
4387
Alexandre Rames5319def2014-10-23 10:03:10 +01004388void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4389 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004390 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004391 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004392 if (instruction->IsStringAlloc()) {
4393 locations->AddTemp(LocationFrom(kArtMethodRegister));
4394 } else {
4395 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4396 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4397 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004398 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4399}
4400
4401void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004402 // Note: if heap poisoning is enabled, the entry point takes cares
4403 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004404 if (instruction->IsStringAlloc()) {
4405 // String is allocated through StringFactory. Call NewEmptyString entry point.
4406 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004407 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004408 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4409 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4410 __ Blr(lr);
4411 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4412 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004413 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004414 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4415 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004416}
4417
4418void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004420 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004422}
4423
4424void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004425 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004426 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004427 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004428 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004429 break;
4430
4431 default:
4432 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4433 }
4434}
4435
David Brazdil66d126e2015-04-03 16:02:44 +01004436void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4438 locations->SetInAt(0, Location::RequiresRegister());
4439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4440}
4441
4442void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004443 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004444}
4445
Alexandre Rames5319def2014-10-23 10:03:10 +01004446void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004447 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4448 ? LocationSummary::kCallOnSlowPath
4449 : LocationSummary::kNoCall;
4450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004451 locations->SetInAt(0, Location::RequiresRegister());
4452 if (instruction->HasUses()) {
4453 locations->SetOut(Location::SameAsFirstInput());
4454 }
4455}
4456
Calin Juravle2ae48182016-03-16 14:05:09 +00004457void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4458 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004459 return;
4460 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004461
Alexandre Ramesd921d642015-04-16 15:07:16 +01004462 BlockPoolsScope block_pools(GetVIXLAssembler());
4463 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004464 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004465 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004466}
4467
Calin Juravle2ae48182016-03-16 14:05:09 +00004468void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004469 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004470 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004471
4472 LocationSummary* locations = instruction->GetLocations();
4473 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004474
4475 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004476}
4477
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004478void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004479 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004480}
4481
Alexandre Rames67555f72014-11-18 10:55:16 +00004482void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4483 HandleBinaryOp(instruction);
4484}
4485
4486void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4487 HandleBinaryOp(instruction);
4488}
4489
Alexandre Rames3e69f162014-12-10 10:36:50 +00004490void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4491 LOG(FATAL) << "Unreachable";
4492}
4493
4494void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4495 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4496}
4497
Alexandre Rames5319def2014-10-23 10:03:10 +01004498void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4500 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4501 if (location.IsStackSlot()) {
4502 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4503 } else if (location.IsDoubleStackSlot()) {
4504 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4505 }
4506 locations->SetOut(location);
4507}
4508
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004509void InstructionCodeGeneratorARM64::VisitParameterValue(
4510 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004511 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004512}
4513
4514void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4515 LocationSummary* locations =
4516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004517 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004518}
4519
4520void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4521 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4522 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004523}
4524
4525void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004527 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004528 locations->SetInAt(i, Location::Any());
4529 }
4530 locations->SetOut(Location::Any());
4531}
4532
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004533void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004534 LOG(FATAL) << "Unreachable";
4535}
4536
Serban Constantinescu02164b32014-11-13 14:05:07 +00004537void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004538 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004539 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004540 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4541 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4543
4544 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004545 case Primitive::kPrimInt:
4546 case Primitive::kPrimLong:
4547 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004548 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4550 break;
4551
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004552 case Primitive::kPrimFloat:
4553 case Primitive::kPrimDouble: {
4554 InvokeRuntimeCallingConvention calling_convention;
4555 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4556 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4557 locations->SetOut(calling_convention.GetReturnLocation(type));
4558
4559 break;
4560 }
4561
Serban Constantinescu02164b32014-11-13 14:05:07 +00004562 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004563 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004564 }
4565}
4566
4567void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4568 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004569
Serban Constantinescu02164b32014-11-13 14:05:07 +00004570 switch (type) {
4571 case Primitive::kPrimInt:
4572 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004573 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004574 break;
4575 }
4576
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004577 case Primitive::kPrimFloat:
4578 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004579 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4580 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004581 if (type == Primitive::kPrimFloat) {
4582 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4583 } else {
4584 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4585 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004586 break;
4587 }
4588
Serban Constantinescu02164b32014-11-13 14:05:07 +00004589 default:
4590 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004591 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004592 }
4593}
4594
Calin Juravle27df7582015-04-17 19:12:31 +01004595void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4596 memory_barrier->SetLocations(nullptr);
4597}
4598
4599void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004600 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004601}
4602
Alexandre Rames5319def2014-10-23 10:03:10 +01004603void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4605 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004606 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004607}
4608
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004609void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004610 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004611}
4612
4613void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4614 instruction->SetLocations(nullptr);
4615}
4616
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004617void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004618 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004619}
4620
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004621void LocationsBuilderARM64::VisitRor(HRor* ror) {
4622 HandleBinaryOp(ror);
4623}
4624
4625void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4626 HandleBinaryOp(ror);
4627}
4628
Serban Constantinescu02164b32014-11-13 14:05:07 +00004629void LocationsBuilderARM64::VisitShl(HShl* shl) {
4630 HandleShift(shl);
4631}
4632
4633void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4634 HandleShift(shl);
4635}
4636
4637void LocationsBuilderARM64::VisitShr(HShr* shr) {
4638 HandleShift(shr);
4639}
4640
4641void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4642 HandleShift(shr);
4643}
4644
Alexandre Rames5319def2014-10-23 10:03:10 +01004645void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004646 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004647}
4648
4649void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004650 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004651}
4652
Alexandre Rames67555f72014-11-18 10:55:16 +00004653void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004654 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004655}
4656
4657void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004658 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004659}
4660
4661void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004662 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004663}
4664
Alexandre Rames67555f72014-11-18 10:55:16 +00004665void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004666 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004667}
4668
Calin Juravlee460d1d2015-09-29 04:52:17 +01004669void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4670 HUnresolvedInstanceFieldGet* instruction) {
4671 FieldAccessCallingConventionARM64 calling_convention;
4672 codegen_->CreateUnresolvedFieldLocationSummary(
4673 instruction, instruction->GetFieldType(), calling_convention);
4674}
4675
4676void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4677 HUnresolvedInstanceFieldGet* 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::VisitUnresolvedInstanceFieldSet(
4687 HUnresolvedInstanceFieldSet* instruction) {
4688 FieldAccessCallingConventionARM64 calling_convention;
4689 codegen_->CreateUnresolvedFieldLocationSummary(
4690 instruction, instruction->GetFieldType(), calling_convention);
4691}
4692
4693void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4694 HUnresolvedInstanceFieldSet* instruction) {
4695 FieldAccessCallingConventionARM64 calling_convention;
4696 codegen_->GenerateUnresolvedFieldAccess(instruction,
4697 instruction->GetFieldType(),
4698 instruction->GetFieldIndex(),
4699 instruction->GetDexPc(),
4700 calling_convention);
4701}
4702
4703void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4704 HUnresolvedStaticFieldGet* instruction) {
4705 FieldAccessCallingConventionARM64 calling_convention;
4706 codegen_->CreateUnresolvedFieldLocationSummary(
4707 instruction, instruction->GetFieldType(), calling_convention);
4708}
4709
4710void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4711 HUnresolvedStaticFieldGet* instruction) {
4712 FieldAccessCallingConventionARM64 calling_convention;
4713 codegen_->GenerateUnresolvedFieldAccess(instruction,
4714 instruction->GetFieldType(),
4715 instruction->GetFieldIndex(),
4716 instruction->GetDexPc(),
4717 calling_convention);
4718}
4719
4720void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4721 HUnresolvedStaticFieldSet* instruction) {
4722 FieldAccessCallingConventionARM64 calling_convention;
4723 codegen_->CreateUnresolvedFieldLocationSummary(
4724 instruction, instruction->GetFieldType(), calling_convention);
4725}
4726
4727void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4728 HUnresolvedStaticFieldSet* instruction) {
4729 FieldAccessCallingConventionARM64 calling_convention;
4730 codegen_->GenerateUnresolvedFieldAccess(instruction,
4731 instruction->GetFieldType(),
4732 instruction->GetFieldIndex(),
4733 instruction->GetDexPc(),
4734 calling_convention);
4735}
4736
Alexandre Rames5319def2014-10-23 10:03:10 +01004737void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004738 LocationSummary* locations =
4739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4740 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004741}
4742
4743void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004744 HBasicBlock* block = instruction->GetBlock();
4745 if (block->GetLoopInformation() != nullptr) {
4746 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4747 // The back edge will generate the suspend check.
4748 return;
4749 }
4750 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4751 // The goto will generate the suspend check.
4752 return;
4753 }
4754 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004755}
4756
Alexandre Rames67555f72014-11-18 10:55:16 +00004757void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4758 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004760 InvokeRuntimeCallingConvention calling_convention;
4761 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4762}
4763
4764void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004765 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004766 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004767}
4768
4769void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4770 LocationSummary* locations =
4771 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4772 Primitive::Type input_type = conversion->GetInputType();
4773 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004774 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004775 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4776 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4777 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4778 }
4779
Alexandre Rames542361f2015-01-29 16:57:31 +00004780 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004781 locations->SetInAt(0, Location::RequiresFpuRegister());
4782 } else {
4783 locations->SetInAt(0, Location::RequiresRegister());
4784 }
4785
Alexandre Rames542361f2015-01-29 16:57:31 +00004786 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004787 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4788 } else {
4789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4790 }
4791}
4792
4793void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4794 Primitive::Type result_type = conversion->GetResultType();
4795 Primitive::Type input_type = conversion->GetInputType();
4796
4797 DCHECK_NE(input_type, result_type);
4798
Alexandre Rames542361f2015-01-29 16:57:31 +00004799 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004800 int result_size = Primitive::ComponentSize(result_type);
4801 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004802 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004803 Register output = OutputRegister(conversion);
4804 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004805 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004806 // 'int' values are used directly as W registers, discarding the top
4807 // bits, so we don't need to sign-extend and can just perform a move.
4808 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4809 // top 32 bits of the target register. We theoretically could leave those
4810 // bits unchanged, but we would have to make sure that no code uses a
4811 // 32bit input value as a 64bit value assuming that the top 32 bits are
4812 // zero.
4813 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004814 } else if (result_type == Primitive::kPrimChar ||
4815 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4816 __ Ubfx(output,
4817 output.IsX() ? source.X() : source.W(),
4818 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004819 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004820 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004821 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004822 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004823 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004824 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004825 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4826 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004827 } else if (Primitive::IsFloatingPointType(result_type) &&
4828 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004829 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4830 } else {
4831 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4832 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004833 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004834}
Alexandre Rames67555f72014-11-18 10:55:16 +00004835
Serban Constantinescu02164b32014-11-13 14:05:07 +00004836void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4837 HandleShift(ushr);
4838}
4839
4840void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4841 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004842}
4843
4844void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4845 HandleBinaryOp(instruction);
4846}
4847
4848void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4849 HandleBinaryOp(instruction);
4850}
4851
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004852void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004853 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004854 LOG(FATAL) << "Unreachable";
4855}
4856
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004857void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004858 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004859 LOG(FATAL) << "Unreachable";
4860}
4861
Mark Mendellfe57faa2015-09-18 09:26:15 -04004862// Simple implementation of packed switch - generate cascaded compare/jumps.
4863void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4864 LocationSummary* locations =
4865 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4866 locations->SetInAt(0, Location::RequiresRegister());
4867}
4868
4869void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4870 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004871 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004872 Register value_reg = InputRegisterAt(switch_instr, 0);
4873 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4874
Zheng Xu3927c8b2015-11-18 17:46:25 +08004875 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004876 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004877 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4878 // make sure we don't emit it if the target may run out of range.
4879 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4880 // ranges and emit the tables only as required.
4881 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004882
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004883 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004884 // Current instruction id is an upper bound of the number of HIRs in the graph.
4885 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4886 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004887 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4888 Register temp = temps.AcquireW();
4889 __ Subs(temp, value_reg, Operand(lower_bound));
4890
Zheng Xu3927c8b2015-11-18 17:46:25 +08004891 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004892 // Jump to successors[0] if value == lower_bound.
4893 __ B(eq, codegen_->GetLabelOf(successors[0]));
4894 int32_t last_index = 0;
4895 for (; num_entries - last_index > 2; last_index += 2) {
4896 __ Subs(temp, temp, Operand(2));
4897 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4898 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4899 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4900 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4901 }
4902 if (num_entries - last_index == 2) {
4903 // The last missing case_value.
4904 __ Cmp(temp, Operand(1));
4905 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004906 }
4907
4908 // And the default for any other value.
4909 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4910 __ B(codegen_->GetLabelOf(default_block));
4911 }
4912 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004913 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004914
4915 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4916
4917 // Below instructions should use at most one blocked register. Since there are two blocked
4918 // registers, we are free to block one.
4919 Register temp_w = temps.AcquireW();
4920 Register index;
4921 // Remove the bias.
4922 if (lower_bound != 0) {
4923 index = temp_w;
4924 __ Sub(index, value_reg, Operand(lower_bound));
4925 } else {
4926 index = value_reg;
4927 }
4928
4929 // Jump to default block if index is out of the range.
4930 __ Cmp(index, Operand(num_entries));
4931 __ B(hs, codegen_->GetLabelOf(default_block));
4932
4933 // In current VIXL implementation, it won't require any blocked registers to encode the
4934 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4935 // register pressure.
4936 Register table_base = temps.AcquireX();
4937 // Load jump offset from the table.
4938 __ Adr(table_base, jump_table->GetTableStartLabel());
4939 Register jump_offset = temp_w;
4940 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4941
4942 // Jump to target block by branching to table_base(pc related) + offset.
4943 Register target_address = table_base;
4944 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4945 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004946 }
4947}
4948
Roland Levillain44015862016-01-22 11:47:17 +00004949void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4950 Location out,
4951 uint32_t offset,
4952 Location maybe_temp) {
4953 Primitive::Type type = Primitive::kPrimNot;
4954 Register out_reg = RegisterFrom(out, type);
4955 if (kEmitCompilerReadBarrier) {
4956 Register temp_reg = RegisterFrom(maybe_temp, type);
4957 if (kUseBakerReadBarrier) {
4958 // Load with fast path based Baker's read barrier.
4959 // /* HeapReference<Object> */ out = *(out + offset)
4960 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4961 out,
4962 out_reg,
4963 offset,
4964 temp_reg,
4965 /* needs_null_check */ false,
4966 /* use_load_acquire */ false);
4967 } else {
4968 // Load with slow path based read barrier.
4969 // Save the value of `out` into `maybe_temp` before overwriting it
4970 // in the following move operation, as we will need it for the
4971 // read barrier below.
4972 __ Mov(temp_reg, out_reg);
4973 // /* HeapReference<Object> */ out = *(out + offset)
4974 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4975 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4976 }
4977 } else {
4978 // Plain load with no read barrier.
4979 // /* HeapReference<Object> */ out = *(out + offset)
4980 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4981 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4982 }
4983}
4984
4985void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4986 Location out,
4987 Location obj,
4988 uint32_t offset,
4989 Location maybe_temp) {
4990 Primitive::Type type = Primitive::kPrimNot;
4991 Register out_reg = RegisterFrom(out, type);
4992 Register obj_reg = RegisterFrom(obj, type);
4993 if (kEmitCompilerReadBarrier) {
4994 if (kUseBakerReadBarrier) {
4995 // Load with fast path based Baker's read barrier.
4996 Register temp_reg = RegisterFrom(maybe_temp, type);
4997 // /* HeapReference<Object> */ out = *(obj + offset)
4998 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4999 out,
5000 obj_reg,
5001 offset,
5002 temp_reg,
5003 /* needs_null_check */ false,
5004 /* use_load_acquire */ false);
5005 } else {
5006 // Load with slow path based read barrier.
5007 // /* HeapReference<Object> */ out = *(obj + offset)
5008 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5009 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5010 }
5011 } else {
5012 // Plain load with no read barrier.
5013 // /* HeapReference<Object> */ out = *(obj + offset)
5014 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5015 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5016 }
5017}
5018
5019void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5020 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005021 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005022 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005023 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005024 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5025 if (kEmitCompilerReadBarrier) {
5026 if (kUseBakerReadBarrier) {
5027 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5028 // Baker's read barrier are used:
5029 //
5030 // root = obj.field;
5031 // if (Thread::Current()->GetIsGcMarking()) {
5032 // root = ReadBarrier::Mark(root)
5033 // }
5034
5035 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005036 if (fixup_label == nullptr) {
5037 __ Ldr(root_reg, MemOperand(obj, offset));
5038 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005039 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005040 __ Bind(fixup_label);
5041 __ ldr(root_reg, MemOperand(obj, offset));
5042 }
Roland Levillain44015862016-01-22 11:47:17 +00005043 static_assert(
5044 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5045 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5046 "have different sizes.");
5047 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5048 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5049 "have different sizes.");
5050
Vladimir Marko953437b2016-08-24 08:30:46 +00005051 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005052 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005053 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005054 codegen_->AddSlowPath(slow_path);
5055
5056 MacroAssembler* masm = GetVIXLAssembler();
5057 UseScratchRegisterScope temps(masm);
5058 Register temp = temps.AcquireW();
5059 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005060 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005061 __ Cbnz(temp, slow_path->GetEntryLabel());
5062 __ Bind(slow_path->GetExitLabel());
5063 } else {
5064 // GC root loaded through a slow path for read barriers other
5065 // than Baker's.
5066 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005067 if (fixup_label == nullptr) {
5068 __ Add(root_reg.X(), obj.X(), offset);
5069 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005070 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005071 __ Bind(fixup_label);
5072 __ add(root_reg.X(), obj.X(), offset);
5073 }
Roland Levillain44015862016-01-22 11:47:17 +00005074 // /* mirror::Object* */ root = root->Read()
5075 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5076 }
5077 } else {
5078 // Plain GC root load with no read barrier.
5079 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005080 if (fixup_label == nullptr) {
5081 __ Ldr(root_reg, MemOperand(obj, offset));
5082 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005083 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005084 __ Bind(fixup_label);
5085 __ ldr(root_reg, MemOperand(obj, offset));
5086 }
Roland Levillain44015862016-01-22 11:47:17 +00005087 // Note that GC roots are not affected by heap poisoning, thus we
5088 // do not have to unpoison `root_reg` here.
5089 }
5090}
5091
5092void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5093 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005094 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005095 uint32_t offset,
5096 Register temp,
5097 bool needs_null_check,
5098 bool use_load_acquire) {
5099 DCHECK(kEmitCompilerReadBarrier);
5100 DCHECK(kUseBakerReadBarrier);
5101
5102 // /* HeapReference<Object> */ ref = *(obj + offset)
5103 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005104 size_t no_scale_factor = 0U;
5105 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5106 ref,
5107 obj,
5108 offset,
5109 no_index,
5110 no_scale_factor,
5111 temp,
5112 needs_null_check,
5113 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005114}
5115
5116void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5117 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005118 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005119 uint32_t data_offset,
5120 Location index,
5121 Register temp,
5122 bool needs_null_check) {
5123 DCHECK(kEmitCompilerReadBarrier);
5124 DCHECK(kUseBakerReadBarrier);
5125
5126 // Array cells are never volatile variables, therefore array loads
5127 // never use Load-Acquire instructions on ARM64.
5128 const bool use_load_acquire = false;
5129
Roland Levillainbfea3352016-06-23 13:48:47 +01005130 static_assert(
5131 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5132 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005133 // /* HeapReference<Object> */ ref =
5134 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005135 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5136 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5137 ref,
5138 obj,
5139 data_offset,
5140 index,
5141 scale_factor,
5142 temp,
5143 needs_null_check,
5144 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005145}
5146
5147void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5148 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005149 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005150 uint32_t offset,
5151 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005152 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005153 Register temp,
5154 bool needs_null_check,
5155 bool use_load_acquire) {
5156 DCHECK(kEmitCompilerReadBarrier);
5157 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005158 // If we are emitting an array load, we should not be using a
5159 // Load Acquire instruction. In other words:
5160 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5161 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005162
5163 MacroAssembler* masm = GetVIXLAssembler();
5164 UseScratchRegisterScope temps(masm);
5165
5166 // In slow path based read barriers, the read barrier call is
5167 // inserted after the original load. However, in fast path based
5168 // Baker's read barriers, we need to perform the load of
5169 // mirror::Object::monitor_ *before* the original reference load.
5170 // This load-load ordering is required by the read barrier.
5171 // The fast path/slow path (for Baker's algorithm) should look like:
5172 //
5173 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5174 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5175 // HeapReference<Object> ref = *src; // Original reference load.
5176 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5177 // if (is_gray) {
5178 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5179 // }
5180 //
5181 // Note: the original implementation in ReadBarrier::Barrier is
5182 // slightly more complex as it performs additional checks that we do
5183 // not do here for performance reasons.
5184
5185 Primitive::Type type = Primitive::kPrimNot;
5186 Register ref_reg = RegisterFrom(ref, type);
5187 DCHECK(obj.IsW());
5188 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5189
5190 // /* int32_t */ monitor = obj->monitor_
5191 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5192 if (needs_null_check) {
5193 MaybeRecordImplicitNullCheck(instruction);
5194 }
5195 // /* LockWord */ lock_word = LockWord(monitor)
5196 static_assert(sizeof(LockWord) == sizeof(int32_t),
5197 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005198
Vladimir Marko877a0332016-07-11 19:30:56 +01005199 // Introduce a dependency on the lock_word including rb_state,
5200 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005201 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005202 // `obj` is unchanged by this operation, but its value now depends
5203 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005204 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005205
5206 // The actual reference load.
5207 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005208 // Load types involving an "index".
5209 if (use_load_acquire) {
5210 // UnsafeGetObjectVolatile intrinsic case.
5211 // Register `index` is not an index in an object array, but an
5212 // offset to an object reference field within object `obj`.
5213 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5214 DCHECK(instruction->GetLocations()->Intrinsified());
5215 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5216 << instruction->AsInvoke()->GetIntrinsic();
5217 DCHECK_EQ(offset, 0U);
5218 DCHECK_EQ(scale_factor, 0U);
5219 DCHECK_EQ(needs_null_check, 0U);
5220 // /* HeapReference<Object> */ ref = *(obj + index)
5221 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5222 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005223 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005224 // ArrayGet and UnsafeGetObject intrinsics cases.
5225 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5226 if (index.IsConstant()) {
5227 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5228 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5229 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005230 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005231 __ Add(temp2, obj, offset);
5232 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5233 temps.Release(temp2);
5234 }
Roland Levillain44015862016-01-22 11:47:17 +00005235 }
Roland Levillain44015862016-01-22 11:47:17 +00005236 } else {
5237 // /* HeapReference<Object> */ ref = *(obj + offset)
5238 MemOperand field = HeapOperand(obj, offset);
5239 if (use_load_acquire) {
5240 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5241 } else {
5242 Load(type, ref_reg, field);
5243 }
5244 }
5245
5246 // Object* ref = ref_addr->AsMirrorPtr()
5247 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5248
Vladimir Marko953437b2016-08-24 08:30:46 +00005249 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005250 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005251 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005252 AddSlowPath(slow_path);
5253
5254 // if (rb_state == ReadBarrier::gray_ptr_)
5255 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005256 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5257 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5258 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5259 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5260 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005261 __ Bind(slow_path->GetExitLabel());
5262}
5263
5264void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5265 Location out,
5266 Location ref,
5267 Location obj,
5268 uint32_t offset,
5269 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005270 DCHECK(kEmitCompilerReadBarrier);
5271
Roland Levillain44015862016-01-22 11:47:17 +00005272 // Insert a slow path based read barrier *after* the reference load.
5273 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005274 // If heap poisoning is enabled, the unpoisoning of the loaded
5275 // reference will be carried out by the runtime within the slow
5276 // path.
5277 //
5278 // Note that `ref` currently does not get unpoisoned (when heap
5279 // poisoning is enabled), which is alright as the `ref` argument is
5280 // not used by the artReadBarrierSlow entry point.
5281 //
5282 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5283 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5284 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5285 AddSlowPath(slow_path);
5286
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005287 __ B(slow_path->GetEntryLabel());
5288 __ Bind(slow_path->GetExitLabel());
5289}
5290
Roland Levillain44015862016-01-22 11:47:17 +00005291void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5292 Location out,
5293 Location ref,
5294 Location obj,
5295 uint32_t offset,
5296 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005297 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005298 // Baker's read barriers shall be handled by the fast path
5299 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5300 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005301 // If heap poisoning is enabled, unpoisoning will be taken care of
5302 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005303 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005304 } else if (kPoisonHeapReferences) {
5305 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5306 }
5307}
5308
Roland Levillain44015862016-01-22 11:47:17 +00005309void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5310 Location out,
5311 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005312 DCHECK(kEmitCompilerReadBarrier);
5313
Roland Levillain44015862016-01-22 11:47:17 +00005314 // Insert a slow path based read barrier *after* the GC root load.
5315 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005316 // Note that GC roots are not affected by heap poisoning, so we do
5317 // not need to do anything special for this here.
5318 SlowPathCodeARM64* slow_path =
5319 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5320 AddSlowPath(slow_path);
5321
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005322 __ B(slow_path->GetEntryLabel());
5323 __ Bind(slow_path->GetExitLabel());
5324}
5325
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005326void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5327 LocationSummary* locations =
5328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5329 locations->SetInAt(0, Location::RequiresRegister());
5330 locations->SetOut(Location::RequiresRegister());
5331}
5332
5333void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5334 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005335 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005336 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005337 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005338 __ Ldr(XRegisterFrom(locations->Out()),
5339 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005340 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005341 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005342 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005343 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5344 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005345 __ Ldr(XRegisterFrom(locations->Out()),
5346 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005347 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005348}
5349
5350
5351
Alexandre Rames67555f72014-11-18 10:55:16 +00005352#undef __
5353#undef QUICK_ENTRY_POINT
5354
Alexandre Rames5319def2014-10-23 10:03:10 +01005355} // namespace arm64
5356} // namespace art