blob: db2defb5963e281c849b3ecb7b95b6d7c662538b [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
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
Roland Levillain22ccc3a2015-11-24 13:10:05 +000045template<class MirrorType>
46class GcRoot;
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace arm64 {
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050using helpers::CPURegisterFrom;
51using helpers::DRegisterFrom;
52using helpers::FPRegisterFrom;
53using helpers::HeapOperand;
54using helpers::HeapOperandFrom;
55using helpers::InputCPURegisterAt;
56using helpers::InputFPRegisterAt;
57using helpers::InputRegisterAt;
58using helpers::InputOperandAt;
59using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080060using helpers::LocationFrom;
61using helpers::OperandFromMemOperand;
62using helpers::OutputCPURegister;
63using helpers::OutputFPRegister;
64using helpers::OutputRegister;
65using helpers::RegisterFrom;
66using helpers::StackOperandFrom;
67using helpers::VIXLRegCodeFromART;
68using helpers::WRegisterFrom;
69using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000070using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080071using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
Alexandre Rames5319def2014-10-23 10:03:10 +010073static constexpr int kCurrentMethodStackOffset = 0;
Zheng Xu3927c8b2015-11-18 17:46:25 +080074// The compare/jump sequence will generate about (2 * num_entries + 1) instructions. While jump
75// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
77static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Alexandre Ramesa89086e2014-11-07 17:13:25 +000096Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
98 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
99 // but we use the exact registers for clarity.
100 if (return_type == Primitive::kPrimFloat) {
101 return LocationFrom(s0);
102 } else if (return_type == Primitive::kPrimDouble) {
103 return LocationFrom(d0);
104 } else if (return_type == Primitive::kPrimLong) {
105 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100106 } else if (return_type == Primitive::kPrimVoid) {
107 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000108 } else {
109 return LocationFrom(w0);
110 }
111}
112
Alexandre Rames5319def2014-10-23 10:03:10 +0100113Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100115}
116
Alexandre Rames67555f72014-11-18 10:55:16 +0000117#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
118#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100119
Zheng Xuda403092015-04-24 17:35:39 +0800120// Calculate memory accessing operand for save/restore live registers.
121static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
122 RegisterSet* register_set,
123 int64_t spill_offset,
124 bool is_save) {
125 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
126 codegen->GetNumberOfCoreRegisters(),
127 register_set->GetFloatingPointRegisters(),
128 codegen->GetNumberOfFloatingPointRegisters()));
129
130 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
131 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000132 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
133 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800134
135 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
136 UseScratchRegisterScope temps(masm);
137
138 Register base = masm->StackPointer();
139 int64_t core_spill_size = core_list.TotalSizeInBytes();
140 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
141 int64_t reg_size = kXRegSizeInBytes;
142 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
143 uint32_t ls_access_size = WhichPowerOf2(reg_size);
144 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
145 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
146 // If the offset does not fit in the instruction's immediate field, use an alternate register
147 // to compute the base address(float point registers spill base address).
148 Register new_base = temps.AcquireSameSizeAs(base);
149 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
150 base = new_base;
151 spill_offset = -core_spill_size;
152 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
153 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
154 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
155 }
156
157 if (is_save) {
158 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
159 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
160 } else {
161 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
162 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
163 }
164}
165
166void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
167 RegisterSet* register_set = locations->GetLiveRegisters();
168 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
169 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
170 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
171 // If the register holds an object, update the stack mask.
172 if (locations->RegisterContainsObject(i)) {
173 locations->SetStackBit(stack_offset / kVRegSize);
174 }
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_core_stack_offsets_[i] = stack_offset;
178 stack_offset += kXRegSizeInBytes;
179 }
180 }
181
182 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
183 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
184 register_set->ContainsFloatingPointRegister(i)) {
185 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
186 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
187 saved_fpu_stack_offsets_[i] = stack_offset;
188 stack_offset += kDRegSizeInBytes;
189 }
190 }
191
192 SaveRestoreLiveRegistersHelper(codegen, register_set,
193 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
194}
195
196void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
197 RegisterSet* register_set = locations->GetLiveRegisters();
198 SaveRestoreLiveRegistersHelper(codegen, register_set,
199 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
200}
201
Alexandre Rames5319def2014-10-23 10:03:10 +0100202class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
203 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100205
Alexandre Rames67555f72014-11-18 10:55:16 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100207 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000208 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100209
Alexandre Rames5319def2014-10-23 10:03:10 +0100210 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000211 if (instruction_->CanThrowIntoCatchBlock()) {
212 // Live registers will be restored in the catch block if caught.
213 SaveLiveRegisters(codegen, instruction_->GetLocations());
214 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000215 // We're moving two locations to locations that could overlap, so we need a parallel
216 // move resolver.
217 InvokeRuntimeCallingConvention calling_convention;
218 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100219 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
220 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000222 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800223 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100224 }
225
Alexandre Rames8158f282015-08-07 10:26:17 +0100226 bool IsFatal() const OVERRIDE { return true; }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000231 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232
Alexandre Rames5319def2014-10-23 10:03:10 +0100233 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
234};
235
Alexandre Rames67555f72014-11-18 10:55:16 +0000236class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
237 public:
238 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
239
240 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
241 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
242 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000243 if (instruction_->CanThrowIntoCatchBlock()) {
244 // Live registers will be restored in the catch block if caught.
245 SaveLiveRegisters(codegen, instruction_->GetLocations());
246 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000247 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000248 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800249 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 }
251
Alexandre Rames8158f282015-08-07 10:26:17 +0100252 bool IsFatal() const OVERRIDE { return true; }
253
Alexandre Rames9931f312015-06-19 14:47:01 +0100254 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 private:
257 HDivZeroCheck* const instruction_;
258 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
259};
260
261class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
262 public:
263 LoadClassSlowPathARM64(HLoadClass* cls,
264 HInstruction* at,
265 uint32_t dex_pc,
266 bool do_clinit)
267 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
268 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
269 }
270
271 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
272 LocationSummary* locations = at_->GetLocations();
273 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
274
275 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
278 InvokeRuntimeCallingConvention calling_convention;
279 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
281 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000282 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800283 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100284 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800285 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100286 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800287 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000288
289 // Move the class to the desired location.
290 Location out = locations->Out();
291 if (out.IsValid()) {
292 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
293 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000294 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 }
296
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 __ B(GetExitLabel());
299 }
300
Alexandre Rames9931f312015-06-19 14:47:01 +0100301 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
302
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 private:
304 // The class this slow path will load.
305 HLoadClass* const cls_;
306
307 // The instruction where this slow path is happening.
308 // (Might be the load class or an initialization check).
309 HInstruction* const at_;
310
311 // The dex PC of `at_`.
312 const uint32_t dex_pc_;
313
314 // Whether to initialize the class.
315 const bool do_clinit_;
316
317 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
318};
319
320class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
321 public:
322 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
323
324 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
325 LocationSummary* locations = instruction_->GetLocations();
326 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
327 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
328
329 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000330 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000331
332 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800333 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000335 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100336 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000338 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000340 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 __ B(GetExitLabel());
342 }
343
Alexandre Rames9931f312015-06-19 14:47:01 +0100344 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
345
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 private:
347 HLoadString* const instruction_;
348
349 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
350};
351
Alexandre Rames5319def2014-10-23 10:03:10 +0100352class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
353 public:
354 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
355
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
357 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100358 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000359 if (instruction_->CanThrowIntoCatchBlock()) {
360 // Live registers will be restored in the catch block if caught.
361 SaveLiveRegisters(codegen, instruction_->GetLocations());
362 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000364 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800365 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100366 }
367
Alexandre Rames8158f282015-08-07 10:26:17 +0100368 bool IsFatal() const OVERRIDE { return true; }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
371
Alexandre Rames5319def2014-10-23 10:03:10 +0100372 private:
373 HNullCheck* const instruction_;
374
375 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
376};
377
378class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
379 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100380 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100381 : instruction_(instruction), successor_(successor) {}
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
384 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000386 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000388 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800389 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000390 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000391 if (successor_ == nullptr) {
392 __ B(GetReturnLabel());
393 } else {
394 __ B(arm64_codegen->GetLabelOf(successor_));
395 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100396 }
397
398 vixl::Label* GetReturnLabel() {
399 DCHECK(successor_ == nullptr);
400 return &return_label_;
401 }
402
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100403 HBasicBlock* GetSuccessor() const {
404 return successor_;
405 }
406
Alexandre Rames9931f312015-06-19 14:47:01 +0100407 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
408
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 private:
410 HSuspendCheck* const instruction_;
411 // If not null, the block to branch to after the suspend check.
412 HBasicBlock* const successor_;
413
414 // If `successor_` is null, the label to branch to after the suspend check.
415 vixl::Label return_label_;
416
417 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
418};
419
Alexandre Rames67555f72014-11-18 10:55:16 +0000420class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
421 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000422 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
423 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000424
425 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000426 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100427 Location class_to_check = locations->InAt(1);
428 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
429 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430 DCHECK(instruction_->IsCheckCast()
431 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
432 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100433 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434
Alexandre Rames67555f72014-11-18 10:55:16 +0000435 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000437 if (!is_fatal_) {
438 SaveLiveRegisters(codegen, locations);
439 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000440
441 // We're moving two locations to locations that could overlap, so we need a parallel
442 // move resolver.
443 InvokeRuntimeCallingConvention calling_convention;
444 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
446 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000447
448 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000449 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100450 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000451 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
452 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453 Primitive::Type ret_type = instruction_->GetType();
454 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
455 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
456 } else {
457 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100458 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800459 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000460 }
461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000462 if (!is_fatal_) {
463 RestoreLiveRegisters(codegen, locations);
464 __ B(GetExitLabel());
465 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 }
467
Alexandre Rames9931f312015-06-19 14:47:01 +0100468 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100470
Alexandre Rames67555f72014-11-18 10:55:16 +0000471 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000473 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474
Alexandre Rames67555f72014-11-18 10:55:16 +0000475 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
476};
477
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700478class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
479 public:
480 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100481 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700482
483 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
484 __ Bind(GetEntryLabel());
485 SaveLiveRegisters(codegen, instruction_->GetLocations());
486 DCHECK(instruction_->IsDeoptimize());
487 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
488 uint32_t dex_pc = deoptimize->GetDexPc();
489 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
490 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000491 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700492 }
493
Alexandre Rames9931f312015-06-19 14:47:01 +0100494 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 private:
497 HInstruction* const instruction_;
498 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
499};
500
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100501class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
502 public:
503 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
504
505 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
506 LocationSummary* locations = instruction_->GetLocations();
507 __ Bind(GetEntryLabel());
508 SaveLiveRegisters(codegen, locations);
509
510 InvokeRuntimeCallingConvention calling_convention;
511 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
512 parallel_move.AddMove(
513 locations->InAt(0),
514 LocationFrom(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 nullptr);
517 parallel_move.AddMove(
518 locations->InAt(1),
519 LocationFrom(calling_convention.GetRegisterAt(1)),
520 Primitive::kPrimInt,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(2),
524 LocationFrom(calling_convention.GetRegisterAt(2)),
525 Primitive::kPrimNot,
526 nullptr);
527 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
528
529 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
530 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
531 instruction_,
532 instruction_->GetDexPc(),
533 this);
534 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
535 RestoreLiveRegisters(codegen, locations);
536 __ B(GetExitLabel());
537 }
538
539 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
540
541 private:
542 HInstruction* const instruction_;
543
544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
545};
546
Zheng Xu3927c8b2015-11-18 17:46:25 +0800547void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
548 uint32_t num_entries = switch_instr_->GetNumEntries();
549 DCHECK_GE(num_entries, kPackedSwitchJumpTableThreshold);
550
551 // We are about to use the assembler to place literals directly. Make sure we have enough
552 // underlying code buffer and we have generated the jump table with right size.
553 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
554 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
555
556 __ Bind(&table_start_);
557 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
558 for (uint32_t i = 0; i < num_entries; i++) {
559 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
560 DCHECK(target_label->IsBound());
561 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
562 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
563 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
564 Literal<int32_t> literal(jump_offset);
565 __ place(&literal);
566 }
567}
568
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000569// Slow path generating a read barrier for a heap reference.
570class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
571 public:
572 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
573 Location out,
574 Location ref,
575 Location obj,
576 uint32_t offset,
577 Location index)
578 : instruction_(instruction),
579 out_(out),
580 ref_(ref),
581 obj_(obj),
582 offset_(offset),
583 index_(index) {
584 DCHECK(kEmitCompilerReadBarrier);
585 // If `obj` is equal to `out` or `ref`, it means the initial object
586 // has been overwritten by (or after) the heap object reference load
587 // to be instrumented, e.g.:
588 //
589 // __ Ldr(out, HeapOperand(out, class_offset);
590 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
591 //
592 // In that case, we have lost the information about the original
593 // object, and the emitted read barrier cannot work properly.
594 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
595 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
596 }
597
598 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
599 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
600 LocationSummary* locations = instruction_->GetLocations();
601 Primitive::Type type = Primitive::kPrimNot;
602 DCHECK(locations->CanCall());
603 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
604 DCHECK(!instruction_->IsInvoke() ||
605 (instruction_->IsInvokeStaticOrDirect() &&
606 instruction_->GetLocations()->Intrinsified()));
607
608 __ Bind(GetEntryLabel());
609
610 // Note: In the case of a HArrayGet instruction, when the base
611 // address is a HArm64IntermediateAddress instruction, it does not
612 // point to the array object itself, but to an offset within this
613 // object. However, the read barrier entry point needs the array
614 // object address to be passed as first argument. So we
615 // temporarily set back `obj_` to that address, and restore its
616 // initial value later.
617 if (instruction_->IsArrayGet() &&
618 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()) {
619 if (kIsDebugBuild) {
620 HArm64IntermediateAddress* intermediate_address =
621 instruction_->AsArrayGet()->GetArray()->AsArm64IntermediateAddress();
622 uint32_t intermediate_address_offset =
623 intermediate_address->GetOffset()->AsIntConstant()->GetValueAsUint64();
624 DCHECK_EQ(intermediate_address_offset, offset_);
625 DCHECK_EQ(mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value(), offset_);
626 }
627 Register obj_reg = RegisterFrom(obj_, Primitive::kPrimInt);
628 __ Sub(obj_reg, obj_reg, offset_);
629 }
630
631 SaveLiveRegisters(codegen, locations);
632
633 // We may have to change the index's value, but as `index_` is a
634 // constant member (like other "inputs" of this slow path),
635 // introduce a copy of it, `index`.
636 Location index = index_;
637 if (index_.IsValid()) {
638 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
639 if (instruction_->IsArrayGet()) {
640 // Compute the actual memory offset and store it in `index`.
641 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
642 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
643 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
644 // We are about to change the value of `index_reg` (see the
645 // calls to vixl::MacroAssembler::Lsl and
646 // vixl::MacroAssembler::Mov below), but it has
647 // not been saved by the previous call to
648 // art::SlowPathCode::SaveLiveRegisters, as it is a
649 // callee-save register --
650 // art::SlowPathCode::SaveLiveRegisters does not consider
651 // callee-save registers, as it has been designed with the
652 // assumption that callee-save registers are supposed to be
653 // handled by the called function. So, as a callee-save
654 // register, `index_reg` _would_ eventually be saved onto
655 // the stack, but it would be too late: we would have
656 // changed its value earlier. Therefore, we manually save
657 // it here into another freely available register,
658 // `free_reg`, chosen of course among the caller-save
659 // registers (as a callee-save `free_reg` register would
660 // exhibit the same problem).
661 //
662 // Note we could have requested a temporary register from
663 // the register allocator instead; but we prefer not to, as
664 // this is a slow path, and we know we can find a
665 // caller-save register that is available.
666 Register free_reg = FindAvailableCallerSaveRegister(codegen);
667 __ Mov(free_reg.W(), index_reg);
668 index_reg = free_reg;
669 index = LocationFrom(index_reg);
670 } else {
671 // The initial register stored in `index_` has already been
672 // saved in the call to art::SlowPathCode::SaveLiveRegisters
673 // (as it is not a callee-save register), so we can freely
674 // use it.
675 }
676 // Shifting the index value contained in `index_reg` by the scale
677 // factor (2) cannot overflow in practice, as the runtime is
678 // unable to allocate object arrays with a size larger than
679 // 2^26 - 1 (that is, 2^28 - 4 bytes).
680 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
681 static_assert(
682 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
683 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
684 __ Add(index_reg, index_reg, Operand(offset_));
685 } else {
686 DCHECK(instruction_->IsInvoke());
687 DCHECK(instruction_->GetLocations()->Intrinsified());
688 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
689 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
690 << instruction_->AsInvoke()->GetIntrinsic();
691 DCHECK_EQ(offset_, 0U);
692 DCHECK(index_.IsRegisterPair());
693 // UnsafeGet's offset location is a register pair, the low
694 // part contains the correct offset.
695 index = index_.ToLow();
696 }
697 }
698
699 // We're moving two or three locations to locations that could
700 // overlap, so we need a parallel move resolver.
701 InvokeRuntimeCallingConvention calling_convention;
702 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
703 parallel_move.AddMove(ref_,
704 LocationFrom(calling_convention.GetRegisterAt(0)),
705 type,
706 nullptr);
707 parallel_move.AddMove(obj_,
708 LocationFrom(calling_convention.GetRegisterAt(1)),
709 type,
710 nullptr);
711 if (index.IsValid()) {
712 parallel_move.AddMove(index,
713 LocationFrom(calling_convention.GetRegisterAt(2)),
714 Primitive::kPrimInt,
715 nullptr);
716 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
717 } else {
718 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
719 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
720 }
721 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
722 instruction_,
723 instruction_->GetDexPc(),
724 this);
725 CheckEntrypointTypes<
726 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
727 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
728
729 RestoreLiveRegisters(codegen, locations);
730
731 // Restore the value of `obj_` when it corresponds to a
732 // HArm64IntermediateAddress instruction.
733 if (instruction_->IsArrayGet() &&
734 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()) {
735 if (kIsDebugBuild) {
736 HArm64IntermediateAddress* intermediate_address =
737 instruction_->AsArrayGet()->GetArray()->AsArm64IntermediateAddress();
738 uint32_t intermediate_address_offset =
739 intermediate_address->GetOffset()->AsIntConstant()->GetValueAsUint64();
740 DCHECK_EQ(intermediate_address_offset, offset_);
741 DCHECK_EQ(mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value(), offset_);
742 }
743 Register obj_reg = RegisterFrom(obj_, Primitive::kPrimInt);
744 __ Add(obj_reg, obj_reg, offset_);
745 }
746
747 __ B(GetExitLabel());
748 }
749
750 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
751
752 private:
753 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
754 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
755 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
756 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
757 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
758 return Register(VIXLRegCodeFromART(i), kXRegSize);
759 }
760 }
761 // We shall never fail to find a free caller-save register, as
762 // there are more than two core caller-save registers on ARM64
763 // (meaning it is possible to find one which is different from
764 // `ref` and `obj`).
765 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
766 LOG(FATAL) << "Could not find a free register";
767 UNREACHABLE();
768 }
769
770 HInstruction* const instruction_;
771 const Location out_;
772 const Location ref_;
773 const Location obj_;
774 const uint32_t offset_;
775 // An additional location containing an index to an array.
776 // Only used for HArrayGet and the UnsafeGetObject &
777 // UnsafeGetObjectVolatile intrinsics.
778 const Location index_;
779
780 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
781};
782
783// Slow path generating a read barrier for a GC root.
784class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
785 public:
786 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
787 : instruction_(instruction), out_(out), root_(root) {}
788
789 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
790 LocationSummary* locations = instruction_->GetLocations();
791 Primitive::Type type = Primitive::kPrimNot;
792 DCHECK(locations->CanCall());
793 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
794 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
795
796 __ Bind(GetEntryLabel());
797 SaveLiveRegisters(codegen, locations);
798
799 InvokeRuntimeCallingConvention calling_convention;
800 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
801 // The argument of the ReadBarrierForRootSlow is not a managed
802 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
803 // thus we need a 64-bit move here, and we cannot use
804 //
805 // arm64_codegen->MoveLocation(
806 // LocationFrom(calling_convention.GetRegisterAt(0)),
807 // root_,
808 // type);
809 //
810 // which would emit a 32-bit move, as `type` is a (32-bit wide)
811 // reference type (`Primitive::kPrimNot`).
812 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
813 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
814 instruction_,
815 instruction_->GetDexPc(),
816 this);
817 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
818 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
819
820 RestoreLiveRegisters(codegen, locations);
821 __ B(GetExitLabel());
822 }
823
824 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
825
826 private:
827 HInstruction* const instruction_;
828 const Location out_;
829 const Location root_;
830
831 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
832};
833
Alexandre Rames5319def2014-10-23 10:03:10 +0100834#undef __
835
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100836Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100837 Location next_location;
838 if (type == Primitive::kPrimVoid) {
839 LOG(FATAL) << "Unreachable type " << type;
840 }
841
Alexandre Rames542361f2015-01-29 16:57:31 +0000842 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100843 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
844 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000845 } else if (!Primitive::IsFloatingPointType(type) &&
846 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000847 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
848 } else {
849 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000850 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
851 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 }
853
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000854 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000855 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100856 return next_location;
857}
858
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100859Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100860 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100861}
862
Serban Constantinescu579885a2015-02-22 20:51:33 +0000863CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
864 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100865 const CompilerOptions& compiler_options,
866 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100867 : CodeGenerator(graph,
868 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000869 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000870 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000871 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000872 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100873 compiler_options,
874 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100875 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800876 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100877 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000878 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000879 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000880 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100881 uint64_literals_(std::less<uint64_t>(),
882 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
883 method_patches_(MethodReferenceComparator(),
884 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
885 call_patches_(MethodReferenceComparator(),
886 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
887 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000888 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000889 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000890 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000891}
Alexandre Rames5319def2014-10-23 10:03:10 +0100892
Alexandre Rames67555f72014-11-18 10:55:16 +0000893#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100894
Zheng Xu3927c8b2015-11-18 17:46:25 +0800895void CodeGeneratorARM64::EmitJumpTables() {
896 for (auto jump_table : jump_tables_) {
897 jump_table->EmitTable(this);
898 }
899}
900
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000901void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800902 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000903 // Ensure we emit the literal pool.
904 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000905
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000906 CodeGenerator::Finalize(allocator);
907}
908
Zheng Xuad4450e2015-04-17 18:48:56 +0800909void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
910 // Note: There are 6 kinds of moves:
911 // 1. constant -> GPR/FPR (non-cycle)
912 // 2. constant -> stack (non-cycle)
913 // 3. GPR/FPR -> GPR/FPR
914 // 4. GPR/FPR -> stack
915 // 5. stack -> GPR/FPR
916 // 6. stack -> stack (non-cycle)
917 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
918 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
919 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
920 // dependency.
921 vixl_temps_.Open(GetVIXLAssembler());
922}
923
924void ParallelMoveResolverARM64::FinishEmitNativeCode() {
925 vixl_temps_.Close();
926}
927
928Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
929 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
930 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
931 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
932 Location scratch = GetScratchLocation(kind);
933 if (!scratch.Equals(Location::NoLocation())) {
934 return scratch;
935 }
936 // Allocate from VIXL temp registers.
937 if (kind == Location::kRegister) {
938 scratch = LocationFrom(vixl_temps_.AcquireX());
939 } else {
940 DCHECK(kind == Location::kFpuRegister);
941 scratch = LocationFrom(vixl_temps_.AcquireD());
942 }
943 AddScratchLocation(scratch);
944 return scratch;
945}
946
947void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
948 if (loc.IsRegister()) {
949 vixl_temps_.Release(XRegisterFrom(loc));
950 } else {
951 DCHECK(loc.IsFpuRegister());
952 vixl_temps_.Release(DRegisterFrom(loc));
953 }
954 RemoveScratchLocation(loc);
955}
956
Alexandre Rames3e69f162014-12-10 10:36:50 +0000957void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100958 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100959 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000960}
961
Alexandre Rames5319def2014-10-23 10:03:10 +0100962void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100963 MacroAssembler* masm = GetVIXLAssembler();
964 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000965 __ Bind(&frame_entry_label_);
966
Serban Constantinescu02164b32014-11-13 14:05:07 +0000967 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
968 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100969 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000970 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000971 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000972 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000973 __ Ldr(wzr, MemOperand(temp, 0));
974 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000975 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100976
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000977 if (!HasEmptyFrame()) {
978 int frame_size = GetFrameSize();
979 // Stack layout:
980 // sp[frame_size - 8] : lr.
981 // ... : other preserved core registers.
982 // ... : other preserved fp registers.
983 // ... : reserved frame space.
984 // sp[0] : current method.
985 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100986 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800987 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
988 frame_size - GetCoreSpillSize());
989 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
990 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000991 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100992}
993
994void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100995 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100996 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000997 if (!HasEmptyFrame()) {
998 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800999 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1000 frame_size - FrameEntrySpillSize());
1001 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1002 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001003 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001004 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001005 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001006 __ Ret();
1007 GetAssembler()->cfi().RestoreState();
1008 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001009}
1010
Zheng Xuda403092015-04-24 17:35:39 +08001011vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1012 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1013 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1014 core_spill_mask_);
1015}
1016
1017vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1018 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1019 GetNumberOfFloatingPointRegisters()));
1020 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1021 fpu_spill_mask_);
1022}
1023
Alexandre Rames5319def2014-10-23 10:03:10 +01001024void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1025 __ Bind(GetLabelOf(block));
1026}
1027
Alexandre Rames5319def2014-10-23 10:03:10 +01001028void CodeGeneratorARM64::Move(HInstruction* instruction,
1029 Location location,
1030 HInstruction* move_for) {
1031 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001032 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001033 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +01001034
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +01001035 if (instruction->IsFakeString()) {
1036 // The fake string is an alias for null.
1037 DCHECK(IsBaseline());
1038 instruction = locations->Out().GetConstant();
1039 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
1040 }
1041
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001042 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001043 MoveLocation(location,
1044 Location::DoubleStackSlot(kCurrentMethodStackOffset),
1045 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001046 } else if (locations != nullptr && locations->Out().Equals(location)) {
1047 return;
1048 } else if (instruction->IsIntConstant()
1049 || instruction->IsLongConstant()
1050 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001051 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +01001052 if (location.IsRegister()) {
1053 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001054 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +01001055 (instruction->IsLongConstant() && dst.Is64Bits()));
1056 __ Mov(dst, value);
1057 } else {
1058 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001059 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001060 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
1061 ? temps.AcquireW()
1062 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +01001063 __ Mov(temp, value);
1064 __ Str(temp, StackOperandFrom(location));
1065 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001066 } else if (instruction->IsTemporary()) {
1067 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001068 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001069 } else if (instruction->IsLoadLocal()) {
1070 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +00001071 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001072 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001073 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001074 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001075 }
1076
1077 } else {
1078 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001079 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001080 }
1081}
1082
Calin Juravle175dc732015-08-25 15:42:32 +01001083void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1084 DCHECK(location.IsRegister());
1085 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1086}
1087
Calin Juravlee460d1d2015-09-29 04:52:17 +01001088void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1089 if (location.IsRegister()) {
1090 locations->AddTemp(location);
1091 } else {
1092 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1093 }
1094}
1095
Alexandre Rames5319def2014-10-23 10:03:10 +01001096Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1097 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001098
Alexandre Rames5319def2014-10-23 10:03:10 +01001099 switch (type) {
1100 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001101 case Primitive::kPrimInt:
1102 case Primitive::kPrimFloat:
1103 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1104
1105 case Primitive::kPrimLong:
1106 case Primitive::kPrimDouble:
1107 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1108
Alexandre Rames5319def2014-10-23 10:03:10 +01001109 case Primitive::kPrimBoolean:
1110 case Primitive::kPrimByte:
1111 case Primitive::kPrimChar:
1112 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001113 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001114 LOG(FATAL) << "Unexpected type " << type;
1115 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001116
Alexandre Rames5319def2014-10-23 10:03:10 +01001117 LOG(FATAL) << "Unreachable";
1118 return Location::NoLocation();
1119}
1120
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001121void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001122 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001123 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001124 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001125 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001126 if (value_can_be_null) {
1127 __ Cbz(value, &done);
1128 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001129 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1130 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001131 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001132 if (value_can_be_null) {
1133 __ Bind(&done);
1134 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001135}
1136
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001137void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
1138 // Blocked core registers:
1139 // lr : Runtime reserved.
1140 // tr : Runtime reserved.
1141 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1142 // ip1 : VIXL core temp.
1143 // ip0 : VIXL core temp.
1144 //
1145 // Blocked fp registers:
1146 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001147 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1148 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001149 while (!reserved_core_registers.IsEmpty()) {
1150 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1151 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001152
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001153 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001154 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001155 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1156 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001157
1158 if (is_baseline) {
1159 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
1160 while (!reserved_core_baseline_registers.IsEmpty()) {
1161 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
1162 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001163 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001164
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001165 if (is_baseline || GetGraph()->IsDebuggable()) {
1166 // Stubs do not save callee-save floating point registers. If the graph
1167 // is debuggable, we need to deal with these registers differently. For
1168 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001169 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
1170 while (!reserved_fp_baseline_registers.IsEmpty()) {
1171 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
1172 }
1173 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001174}
1175
1176Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
1177 if (type == Primitive::kPrimVoid) {
1178 LOG(FATAL) << "Unreachable type " << type;
1179 }
1180
Alexandre Rames542361f2015-01-29 16:57:31 +00001181 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001182 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
1183 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001184 return Location::FpuRegisterLocation(reg);
1185 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001186 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
1187 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001188 return Location::RegisterLocation(reg);
1189 }
1190}
1191
Alexandre Rames3e69f162014-12-10 10:36:50 +00001192size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1193 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1194 __ Str(reg, MemOperand(sp, stack_index));
1195 return kArm64WordSize;
1196}
1197
1198size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1199 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1200 __ Ldr(reg, MemOperand(sp, stack_index));
1201 return kArm64WordSize;
1202}
1203
1204size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1205 FPRegister reg = FPRegister(reg_id, kDRegSize);
1206 __ Str(reg, MemOperand(sp, stack_index));
1207 return kArm64WordSize;
1208}
1209
1210size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1211 FPRegister reg = FPRegister(reg_id, kDRegSize);
1212 __ Ldr(reg, MemOperand(sp, stack_index));
1213 return kArm64WordSize;
1214}
1215
Alexandre Rames5319def2014-10-23 10:03:10 +01001216void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001217 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001218}
1219
1220void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001221 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001222}
1223
Alexandre Rames67555f72014-11-18 10:55:16 +00001224void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001225 if (constant->IsIntConstant()) {
1226 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1227 } else if (constant->IsLongConstant()) {
1228 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1229 } else if (constant->IsNullConstant()) {
1230 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001231 } else if (constant->IsFloatConstant()) {
1232 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1233 } else {
1234 DCHECK(constant->IsDoubleConstant());
1235 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1236 }
1237}
1238
Alexandre Rames3e69f162014-12-10 10:36:50 +00001239
1240static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1241 DCHECK(constant.IsConstant());
1242 HConstant* cst = constant.GetConstant();
1243 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001244 // Null is mapped to a core W register, which we associate with kPrimInt.
1245 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001246 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1247 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1248 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1249}
1250
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251void CodeGeneratorARM64::MoveLocation(Location destination,
1252 Location source,
1253 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001254 if (source.Equals(destination)) {
1255 return;
1256 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001257
1258 // A valid move can always be inferred from the destination and source
1259 // locations. When moving from and to a register, the argument type can be
1260 // used to generate 32bit instead of 64bit moves. In debug mode we also
1261 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001263
1264 if (destination.IsRegister() || destination.IsFpuRegister()) {
1265 if (unspecified_type) {
1266 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1267 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001268 (src_cst != nullptr && (src_cst->IsIntConstant()
1269 || src_cst->IsFloatConstant()
1270 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001271 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001273 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 // If the source is a double stack slot or a 64bit constant, a 64bit
1275 // type is appropriate. Else the source is a register, and since the
1276 // type has not been specified, we chose a 64bit type to force a 64bit
1277 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001279 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1282 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1283 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001284 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1285 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1286 __ Ldr(dst, StackOperandFrom(source));
1287 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001288 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001289 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001291 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001292 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001293 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001294 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001295 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1296 ? Primitive::kPrimLong
1297 : Primitive::kPrimInt;
1298 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1299 }
1300 } else {
1301 DCHECK(source.IsFpuRegister());
1302 if (destination.IsRegister()) {
1303 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1304 ? Primitive::kPrimDouble
1305 : Primitive::kPrimFloat;
1306 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1307 } else {
1308 DCHECK(destination.IsFpuRegister());
1309 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001310 }
1311 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001312 } else { // The destination is not a register. It must be a stack slot.
1313 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1314 if (source.IsRegister() || source.IsFpuRegister()) {
1315 if (unspecified_type) {
1316 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001317 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001318 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001319 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001320 }
1321 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001322 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1323 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1324 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001325 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001326 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1327 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001328 UseScratchRegisterScope temps(GetVIXLAssembler());
1329 HConstant* src_cst = source.GetConstant();
1330 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001331 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001332 temp = temps.AcquireW();
1333 } else if (src_cst->IsLongConstant()) {
1334 temp = temps.AcquireX();
1335 } else if (src_cst->IsFloatConstant()) {
1336 temp = temps.AcquireS();
1337 } else {
1338 DCHECK(src_cst->IsDoubleConstant());
1339 temp = temps.AcquireD();
1340 }
1341 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001342 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001343 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001344 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001345 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001346 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001347 // There is generally less pressure on FP registers.
1348 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001349 __ Ldr(temp, StackOperandFrom(source));
1350 __ Str(temp, StackOperandFrom(destination));
1351 }
1352 }
1353}
1354
1355void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001356 CPURegister dst,
1357 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001358 switch (type) {
1359 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001360 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001361 break;
1362 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001363 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001364 break;
1365 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001366 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001367 break;
1368 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001369 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001370 break;
1371 case Primitive::kPrimInt:
1372 case Primitive::kPrimNot:
1373 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001374 case Primitive::kPrimFloat:
1375 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001376 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001377 __ Ldr(dst, src);
1378 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001379 case Primitive::kPrimVoid:
1380 LOG(FATAL) << "Unreachable type " << type;
1381 }
1382}
1383
Calin Juravle77520bc2015-01-12 18:45:46 +00001384void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001385 CPURegister dst,
1386 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001387 MacroAssembler* masm = GetVIXLAssembler();
1388 BlockPoolsScope block_pools(masm);
1389 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001390 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001391 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001392
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001393 DCHECK(!src.IsPreIndex());
1394 DCHECK(!src.IsPostIndex());
1395
1396 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001397 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001398 MemOperand base = MemOperand(temp_base);
1399 switch (type) {
1400 case Primitive::kPrimBoolean:
1401 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001402 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001403 break;
1404 case Primitive::kPrimByte:
1405 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001406 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001407 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1408 break;
1409 case Primitive::kPrimChar:
1410 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001411 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001412 break;
1413 case Primitive::kPrimShort:
1414 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001415 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001416 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1417 break;
1418 case Primitive::kPrimInt:
1419 case Primitive::kPrimNot:
1420 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001421 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001422 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001423 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001424 break;
1425 case Primitive::kPrimFloat:
1426 case Primitive::kPrimDouble: {
1427 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001428 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001429
1430 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1431 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001432 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001433 __ Fmov(FPRegister(dst), temp);
1434 break;
1435 }
1436 case Primitive::kPrimVoid:
1437 LOG(FATAL) << "Unreachable type " << type;
1438 }
1439}
1440
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001441void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001442 CPURegister src,
1443 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001444 switch (type) {
1445 case Primitive::kPrimBoolean:
1446 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001447 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001448 break;
1449 case Primitive::kPrimChar:
1450 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001451 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001452 break;
1453 case Primitive::kPrimInt:
1454 case Primitive::kPrimNot:
1455 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001456 case Primitive::kPrimFloat:
1457 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001458 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001459 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001460 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001461 case Primitive::kPrimVoid:
1462 LOG(FATAL) << "Unreachable type " << type;
1463 }
1464}
1465
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001466void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1467 CPURegister src,
1468 const MemOperand& dst) {
1469 UseScratchRegisterScope temps(GetVIXLAssembler());
1470 Register temp_base = temps.AcquireX();
1471
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001472 DCHECK(!dst.IsPreIndex());
1473 DCHECK(!dst.IsPostIndex());
1474
1475 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001476 Operand op = OperandFromMemOperand(dst);
1477 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001478 MemOperand base = MemOperand(temp_base);
1479 switch (type) {
1480 case Primitive::kPrimBoolean:
1481 case Primitive::kPrimByte:
1482 __ Stlrb(Register(src), base);
1483 break;
1484 case Primitive::kPrimChar:
1485 case Primitive::kPrimShort:
1486 __ Stlrh(Register(src), base);
1487 break;
1488 case Primitive::kPrimInt:
1489 case Primitive::kPrimNot:
1490 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001491 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001492 __ Stlr(Register(src), base);
1493 break;
1494 case Primitive::kPrimFloat:
1495 case Primitive::kPrimDouble: {
1496 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001497 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001498
1499 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1500 __ Fmov(temp, FPRegister(src));
1501 __ Stlr(temp, base);
1502 break;
1503 }
1504 case Primitive::kPrimVoid:
1505 LOG(FATAL) << "Unreachable type " << type;
1506 }
1507}
1508
Calin Juravle175dc732015-08-25 15:42:32 +01001509void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1510 HInstruction* instruction,
1511 uint32_t dex_pc,
1512 SlowPathCode* slow_path) {
1513 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1514 instruction,
1515 dex_pc,
1516 slow_path);
1517}
1518
Alexandre Rames67555f72014-11-18 10:55:16 +00001519void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1520 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001521 uint32_t dex_pc,
1522 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001524 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001525 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1526 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001527 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001528}
1529
1530void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1531 vixl::Register class_reg) {
1532 UseScratchRegisterScope temps(GetVIXLAssembler());
1533 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001534 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001535 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001536
Serban Constantinescu02164b32014-11-13 14:05:07 +00001537 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001538 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001539 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1540 __ Add(temp, class_reg, status_offset);
1541 __ Ldar(temp, HeapOperand(temp));
1542 __ Cmp(temp, mirror::Class::kStatusInitialized);
1543 __ B(lt, slow_path->GetEntryLabel());
1544 } else {
1545 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1546 __ Cmp(temp, mirror::Class::kStatusInitialized);
1547 __ B(lt, slow_path->GetEntryLabel());
1548 __ Dmb(InnerShareable, BarrierReads);
1549 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001550 __ Bind(slow_path->GetExitLabel());
1551}
Alexandre Rames5319def2014-10-23 10:03:10 +01001552
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001553void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1554 BarrierType type = BarrierAll;
1555
1556 switch (kind) {
1557 case MemBarrierKind::kAnyAny:
1558 case MemBarrierKind::kAnyStore: {
1559 type = BarrierAll;
1560 break;
1561 }
1562 case MemBarrierKind::kLoadAny: {
1563 type = BarrierReads;
1564 break;
1565 }
1566 case MemBarrierKind::kStoreStore: {
1567 type = BarrierWrites;
1568 break;
1569 }
1570 default:
1571 LOG(FATAL) << "Unexpected memory barrier " << kind;
1572 }
1573 __ Dmb(InnerShareable, type);
1574}
1575
Serban Constantinescu02164b32014-11-13 14:05:07 +00001576void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1577 HBasicBlock* successor) {
1578 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001579 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1580 if (slow_path == nullptr) {
1581 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1582 instruction->SetSlowPath(slow_path);
1583 codegen_->AddSlowPath(slow_path);
1584 if (successor != nullptr) {
1585 DCHECK(successor->IsLoopHeader());
1586 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1587 }
1588 } else {
1589 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1590 }
1591
Serban Constantinescu02164b32014-11-13 14:05:07 +00001592 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1593 Register temp = temps.AcquireW();
1594
1595 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1596 if (successor == nullptr) {
1597 __ Cbnz(temp, slow_path->GetEntryLabel());
1598 __ Bind(slow_path->GetReturnLabel());
1599 } else {
1600 __ Cbz(temp, codegen_->GetLabelOf(successor));
1601 __ B(slow_path->GetEntryLabel());
1602 // slow_path will return to GetLabelOf(successor).
1603 }
1604}
1605
Alexandre Rames5319def2014-10-23 10:03:10 +01001606InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1607 CodeGeneratorARM64* codegen)
1608 : HGraphVisitor(graph),
1609 assembler_(codegen->GetAssembler()),
1610 codegen_(codegen) {}
1611
1612#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001613 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001614
1615#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1616
1617enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001618 // Using a base helps identify when we hit such breakpoints.
1619 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001620#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1621 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1622#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1623};
1624
1625#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001626 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001627 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1628 } \
1629 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1631 locations->SetOut(Location::Any()); \
1632 }
1633 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1634#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1635
1636#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001637#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001638
Alexandre Rames67555f72014-11-18 10:55:16 +00001639void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001640 DCHECK_EQ(instr->InputCount(), 2U);
1641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1642 Primitive::Type type = instr->GetResultType();
1643 switch (type) {
1644 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001645 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001646 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001647 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001649 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001650
1651 case Primitive::kPrimFloat:
1652 case Primitive::kPrimDouble:
1653 locations->SetInAt(0, Location::RequiresFpuRegister());
1654 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001655 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001656 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001657
Alexandre Rames5319def2014-10-23 10:03:10 +01001658 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001659 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001660 }
1661}
1662
Alexandre Rames09a99962015-04-15 11:47:56 +01001663void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001664 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1665
1666 bool object_field_get_with_read_barrier =
1667 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001668 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001669 new (GetGraph()->GetArena()) LocationSummary(instruction,
1670 object_field_get_with_read_barrier ?
1671 LocationSummary::kCallOnSlowPath :
1672 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001673 locations->SetInAt(0, Location::RequiresRegister());
1674 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1675 locations->SetOut(Location::RequiresFpuRegister());
1676 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001677 // The output overlaps for an object field get when read barriers
1678 // are enabled: we do not want the load to overwrite the object's
1679 // location, as we need it to emit the read barrier.
1680 locations->SetOut(
1681 Location::RequiresRegister(),
1682 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001683 }
1684}
1685
1686void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1687 const FieldInfo& field_info) {
1688 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001689 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001690 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001691
1692 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1693 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1694
1695 if (field_info.IsVolatile()) {
1696 if (use_acquire_release) {
1697 // NB: LoadAcquire will record the pc info if needed.
1698 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1699 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001700 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001701 codegen_->MaybeRecordImplicitNullCheck(instruction);
1702 // For IRIW sequential consistency kLoadAny is not sufficient.
1703 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1704 }
1705 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001706 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001707 codegen_->MaybeRecordImplicitNullCheck(instruction);
1708 }
Roland Levillain4d027112015-07-01 15:41:14 +01001709
1710 if (field_type == Primitive::kPrimNot) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001711 LocationSummary* locations = instruction->GetLocations();
1712 Location base = locations->InAt(0);
1713 Location out = locations->Out();
1714 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
1715 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01001716 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001717}
1718
1719void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1720 LocationSummary* locations =
1721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1722 locations->SetInAt(0, Location::RequiresRegister());
1723 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1724 locations->SetInAt(1, Location::RequiresFpuRegister());
1725 } else {
1726 locations->SetInAt(1, Location::RequiresRegister());
1727 }
1728}
1729
1730void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001731 const FieldInfo& field_info,
1732 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001733 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001734 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001735
1736 Register obj = InputRegisterAt(instruction, 0);
1737 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001738 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001739 Offset offset = field_info.GetFieldOffset();
1740 Primitive::Type field_type = field_info.GetFieldType();
1741 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1742
Roland Levillain4d027112015-07-01 15:41:14 +01001743 {
1744 // We use a block to end the scratch scope before the write barrier, thus
1745 // freeing the temporary registers so they can be used in `MarkGCCard`.
1746 UseScratchRegisterScope temps(GetVIXLAssembler());
1747
1748 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1749 DCHECK(value.IsW());
1750 Register temp = temps.AcquireW();
1751 __ Mov(temp, value.W());
1752 GetAssembler()->PoisonHeapReference(temp.W());
1753 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001754 }
Roland Levillain4d027112015-07-01 15:41:14 +01001755
1756 if (field_info.IsVolatile()) {
1757 if (use_acquire_release) {
1758 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1759 codegen_->MaybeRecordImplicitNullCheck(instruction);
1760 } else {
1761 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1762 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1763 codegen_->MaybeRecordImplicitNullCheck(instruction);
1764 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1765 }
1766 } else {
1767 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1768 codegen_->MaybeRecordImplicitNullCheck(instruction);
1769 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001770 }
1771
1772 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001773 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001774 }
1775}
1776
Alexandre Rames67555f72014-11-18 10:55:16 +00001777void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001778 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001779
1780 switch (type) {
1781 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001782 case Primitive::kPrimLong: {
1783 Register dst = OutputRegister(instr);
1784 Register lhs = InputRegisterAt(instr, 0);
1785 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001786 if (instr->IsAdd()) {
1787 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001788 } else if (instr->IsAnd()) {
1789 __ And(dst, lhs, rhs);
1790 } else if (instr->IsOr()) {
1791 __ Orr(dst, lhs, rhs);
1792 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001793 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001794 } else {
1795 DCHECK(instr->IsXor());
1796 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001797 }
1798 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001799 }
1800 case Primitive::kPrimFloat:
1801 case Primitive::kPrimDouble: {
1802 FPRegister dst = OutputFPRegister(instr);
1803 FPRegister lhs = InputFPRegisterAt(instr, 0);
1804 FPRegister rhs = InputFPRegisterAt(instr, 1);
1805 if (instr->IsAdd()) {
1806 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001807 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001808 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001809 } else {
1810 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001811 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001812 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001813 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001814 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001815 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001816 }
1817}
1818
Serban Constantinescu02164b32014-11-13 14:05:07 +00001819void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1820 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1821
1822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1823 Primitive::Type type = instr->GetResultType();
1824 switch (type) {
1825 case Primitive::kPrimInt:
1826 case Primitive::kPrimLong: {
1827 locations->SetInAt(0, Location::RequiresRegister());
1828 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1829 locations->SetOut(Location::RequiresRegister());
1830 break;
1831 }
1832 default:
1833 LOG(FATAL) << "Unexpected shift type " << type;
1834 }
1835}
1836
1837void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1838 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1839
1840 Primitive::Type type = instr->GetType();
1841 switch (type) {
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimLong: {
1844 Register dst = OutputRegister(instr);
1845 Register lhs = InputRegisterAt(instr, 0);
1846 Operand rhs = InputOperandAt(instr, 1);
1847 if (rhs.IsImmediate()) {
1848 uint32_t shift_value = (type == Primitive::kPrimInt)
1849 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1850 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1851 if (instr->IsShl()) {
1852 __ Lsl(dst, lhs, shift_value);
1853 } else if (instr->IsShr()) {
1854 __ Asr(dst, lhs, shift_value);
1855 } else {
1856 __ Lsr(dst, lhs, shift_value);
1857 }
1858 } else {
1859 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1860
1861 if (instr->IsShl()) {
1862 __ Lsl(dst, lhs, rhs_reg);
1863 } else if (instr->IsShr()) {
1864 __ Asr(dst, lhs, rhs_reg);
1865 } else {
1866 __ Lsr(dst, lhs, rhs_reg);
1867 }
1868 }
1869 break;
1870 }
1871 default:
1872 LOG(FATAL) << "Unexpected shift operation type " << type;
1873 }
1874}
1875
Alexandre Rames5319def2014-10-23 10:03:10 +01001876void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001877 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001878}
1879
1880void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001881 HandleBinaryOp(instruction);
1882}
1883
1884void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1885 HandleBinaryOp(instruction);
1886}
1887
1888void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1889 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001890}
1891
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001892void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
1893 LocationSummary* locations =
1894 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1895 locations->SetInAt(0, Location::RequiresRegister());
1896 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1897 locations->SetOut(Location::RequiresRegister());
1898}
1899
1900void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1901 HArm64IntermediateAddress* instruction) {
1902 __ Add(OutputRegister(instruction),
1903 InputRegisterAt(instruction, 0),
1904 Operand(InputOperandAt(instruction, 1)));
1905}
1906
Alexandre Rames418318f2015-11-20 15:55:47 +00001907void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1908 LocationSummary* locations =
1909 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1910 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1911 Location::RequiresRegister());
1912 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1913 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1915}
1916
1917void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1918 Register res = OutputRegister(instr);
1919 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
1920 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
1921 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
1922
1923 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1924 // This fixup should be carried out for all multiply-accumulate instructions:
1925 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1926 if (instr->GetType() == Primitive::kPrimLong &&
1927 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1928 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
1929 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
1930 if (prev->IsLoadOrStore()) {
1931 // Make sure we emit only exactly one nop.
1932 vixl::CodeBufferCheckScope scope(masm,
1933 vixl::kInstructionSize,
1934 vixl::CodeBufferCheckScope::kCheck,
1935 vixl::CodeBufferCheckScope::kExactSize);
1936 __ nop();
1937 }
1938 }
1939
1940 if (instr->GetOpKind() == HInstruction::kAdd) {
1941 __ Madd(res, mul_left, mul_right, accumulator);
1942 } else {
1943 DCHECK(instr->GetOpKind() == HInstruction::kSub);
1944 __ Msub(res, mul_left, mul_right, accumulator);
1945 }
1946}
1947
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001948void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001949 bool object_array_get_with_read_barrier =
1950 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001951 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001952 new (GetGraph()->GetArena()) LocationSummary(instruction,
1953 object_array_get_with_read_barrier ?
1954 LocationSummary::kCallOnSlowPath :
1955 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001956 locations->SetInAt(0, Location::RequiresRegister());
1957 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001958 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1959 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1960 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001961 // The output overlaps in the case of an object array get with
1962 // read barriers enabled: we do not want the move to overwrite the
1963 // array's location, as we need it to emit the read barrier.
1964 locations->SetOut(
1965 Location::RequiresRegister(),
1966 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001967 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001968}
1969
1970void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001971 Primitive::Type type = instruction->GetType();
1972 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001973 LocationSummary* locations = instruction->GetLocations();
1974 Location index = locations->InAt(1);
1975 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001976 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001977 CPURegister dest = OutputCPURegister(instruction);
1978
Alexandre Ramesd921d642015-04-16 15:07:16 +01001979 MacroAssembler* masm = GetVIXLAssembler();
1980 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001981 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01001982 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001983
1984 if (index.IsConstant()) {
1985 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001986 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001987 } else {
1988 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001989 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1990 // We do not need to compute the intermediate address from the array: the
1991 // input instruction has done it already. See the comment in
1992 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1993 if (kIsDebugBuild) {
1994 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1995 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1996 }
1997 temp = obj;
1998 } else {
1999 __ Add(temp, obj, offset);
2000 }
Alexandre Rames82000b02015-07-07 11:34:16 +01002001 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002002 }
2003
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002004 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00002005 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002006
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002007 if (type == Primitive::kPrimNot) {
2008 static_assert(
2009 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2010 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2011 Location obj_loc = locations->InAt(0);
2012 Location out = locations->Out();
2013 if (index.IsConstant()) {
2014 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
2015 } else {
2016 // Note: when `obj_loc` is a HArm64IntermediateAddress, it does
2017 // not contain the base address of the array object, which is
2018 // needed by the read barrier entry point. So the read barrier
2019 // slow path will temporarily set back `obj_loc` to the right
2020 // address (see ReadBarrierForHeapReferenceSlowPathARM64::EmitNativeCode).
2021 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset, index);
2022 }
Roland Levillain4d027112015-07-01 15:41:14 +01002023 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002024}
2025
Alexandre Rames5319def2014-10-23 10:03:10 +01002026void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2028 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002029 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002030}
2031
2032void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002033 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002034 __ Ldr(OutputRegister(instruction),
2035 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002036 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002037}
2038
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002039void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002040 Primitive::Type value_type = instruction->GetComponentType();
2041
2042 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2043 bool object_array_set_with_read_barrier =
2044 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002045 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2046 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002047 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2048 LocationSummary::kCallOnSlowPath :
2049 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002050 locations->SetInAt(0, Location::RequiresRegister());
2051 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002052 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002053 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002054 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002055 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002056 }
2057}
2058
2059void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2060 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002061 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002062 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002063 bool needs_write_barrier =
2064 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002065
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002066 Register array = InputRegisterAt(instruction, 0);
2067 CPURegister value = InputCPURegisterAt(instruction, 2);
2068 CPURegister source = value;
2069 Location index = locations->InAt(1);
2070 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2071 MemOperand destination = HeapOperand(array);
2072 MacroAssembler* masm = GetVIXLAssembler();
2073 BlockPoolsScope block_pools(masm);
2074
2075 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002076 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002077 if (index.IsConstant()) {
2078 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2079 destination = HeapOperand(array, offset);
2080 } else {
2081 UseScratchRegisterScope temps(masm);
2082 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002083 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2084 // We do not need to compute the intermediate address from the array: the
2085 // input instruction has done it already. See the comment in
2086 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2087 if (kIsDebugBuild) {
2088 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2089 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2090 }
2091 temp = array;
2092 } else {
2093 __ Add(temp, array, offset);
2094 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002095 destination = HeapOperand(temp,
2096 XRegisterFrom(index),
2097 LSL,
2098 Primitive::ComponentSizeShift(value_type));
2099 }
2100 codegen_->Store(value_type, value, destination);
2101 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002102 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002103 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002104 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002105 vixl::Label done;
2106 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002107 {
2108 // We use a block to end the scratch scope before the write barrier, thus
2109 // freeing the temporary registers so they can be used in `MarkGCCard`.
2110 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002111 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002112 if (index.IsConstant()) {
2113 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002114 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002115 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002116 destination = HeapOperand(temp,
2117 XRegisterFrom(index),
2118 LSL,
2119 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002120 }
2121
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002122 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2123 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2124 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2125
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002126 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002127 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2128 codegen_->AddSlowPath(slow_path);
2129 if (instruction->GetValueCanBeNull()) {
2130 vixl::Label non_zero;
2131 __ Cbnz(Register(value), &non_zero);
2132 if (!index.IsConstant()) {
2133 __ Add(temp, array, offset);
2134 }
2135 __ Str(wzr, destination);
2136 codegen_->MaybeRecordImplicitNullCheck(instruction);
2137 __ B(&done);
2138 __ Bind(&non_zero);
2139 }
2140
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002141 if (kEmitCompilerReadBarrier) {
2142 // When read barriers are enabled, the type checking
2143 // instrumentation requires two read barriers:
2144 //
2145 // __ Mov(temp2, temp);
2146 // // /* HeapReference<Class> */ temp = temp->component_type_
2147 // __ Ldr(temp, HeapOperand(temp, component_offset));
2148 // codegen_->GenerateReadBarrier(
2149 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2150 //
2151 // // /* HeapReference<Class> */ temp2 = value->klass_
2152 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2153 // codegen_->GenerateReadBarrier(
2154 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2155 //
2156 // __ Cmp(temp, temp2);
2157 //
2158 // However, the second read barrier may trash `temp`, as it
2159 // is a temporary register, and as such would not be saved
2160 // along with live registers before calling the runtime (nor
2161 // restored afterwards). So in this case, we bail out and
2162 // delegate the work to the array set slow path.
2163 //
2164 // TODO: Extend the register allocator to support a new
2165 // "(locally) live temp" location so as to avoid always
2166 // going into the slow path when read barriers are enabled.
2167 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002169 Register temp2 = temps.AcquireSameSizeAs(array);
2170 // /* HeapReference<Class> */ temp = array->klass_
2171 __ Ldr(temp, HeapOperand(array, class_offset));
2172 codegen_->MaybeRecordImplicitNullCheck(instruction);
2173 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2174
2175 // /* HeapReference<Class> */ temp = temp->component_type_
2176 __ Ldr(temp, HeapOperand(temp, component_offset));
2177 // /* HeapReference<Class> */ temp2 = value->klass_
2178 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2179 // If heap poisoning is enabled, no need to unpoison `temp`
2180 // nor `temp2`, as we are comparing two poisoned references.
2181 __ Cmp(temp, temp2);
2182
2183 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2184 vixl::Label do_put;
2185 __ B(eq, &do_put);
2186 // If heap poisoning is enabled, the `temp` reference has
2187 // not been unpoisoned yet; unpoison it now.
2188 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2189
2190 // /* HeapReference<Class> */ temp = temp->super_class_
2191 __ Ldr(temp, HeapOperand(temp, super_offset));
2192 // If heap poisoning is enabled, no need to unpoison
2193 // `temp`, as we are comparing against null below.
2194 __ Cbnz(temp, slow_path->GetEntryLabel());
2195 __ Bind(&do_put);
2196 } else {
2197 __ B(ne, slow_path->GetEntryLabel());
2198 }
2199 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002200 }
2201 }
2202
2203 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002204 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002205 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002206 __ Mov(temp2, value.W());
2207 GetAssembler()->PoisonHeapReference(temp2);
2208 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002209 }
2210
2211 if (!index.IsConstant()) {
2212 __ Add(temp, array, offset);
2213 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002214 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002215
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002216 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002217 codegen_->MaybeRecordImplicitNullCheck(instruction);
2218 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002219 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002220
2221 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2222
2223 if (done.IsLinked()) {
2224 __ Bind(&done);
2225 }
2226
2227 if (slow_path != nullptr) {
2228 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002229 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002230 }
2231}
2232
Alexandre Rames67555f72014-11-18 10:55:16 +00002233void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002234 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2235 ? LocationSummary::kCallOnSlowPath
2236 : LocationSummary::kNoCall;
2237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002238 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002239 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002240 if (instruction->HasUses()) {
2241 locations->SetOut(Location::SameAsFirstInput());
2242 }
2243}
2244
2245void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002246 BoundsCheckSlowPathARM64* slow_path =
2247 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002248 codegen_->AddSlowPath(slow_path);
2249
2250 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2251 __ B(slow_path->GetEntryLabel(), hs);
2252}
2253
Alexandre Rames67555f72014-11-18 10:55:16 +00002254void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2255 LocationSummary* locations =
2256 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2257 locations->SetInAt(0, Location::RequiresRegister());
2258 if (check->HasUses()) {
2259 locations->SetOut(Location::SameAsFirstInput());
2260 }
2261}
2262
2263void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2264 // We assume the class is not null.
2265 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2266 check->GetLoadClass(), check, check->GetDexPc(), true);
2267 codegen_->AddSlowPath(slow_path);
2268 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2269}
2270
Roland Levillain7f63c522015-07-13 15:54:55 +00002271static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2272 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2273 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2274}
2275
Serban Constantinescu02164b32014-11-13 14:05:07 +00002276void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002277 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002278 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2279 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002280 switch (in_type) {
2281 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002282 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002283 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2285 break;
2286 }
2287 case Primitive::kPrimFloat:
2288 case Primitive::kPrimDouble: {
2289 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002290 locations->SetInAt(1,
2291 IsFloatingPointZeroConstant(compare->InputAt(1))
2292 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2293 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002294 locations->SetOut(Location::RequiresRegister());
2295 break;
2296 }
2297 default:
2298 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2299 }
2300}
2301
2302void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2303 Primitive::Type in_type = compare->InputAt(0)->GetType();
2304
2305 // 0 if: left == right
2306 // 1 if: left > right
2307 // -1 if: left < right
2308 switch (in_type) {
2309 case Primitive::kPrimLong: {
2310 Register result = OutputRegister(compare);
2311 Register left = InputRegisterAt(compare, 0);
2312 Operand right = InputOperandAt(compare, 1);
2313
2314 __ Cmp(left, right);
2315 __ Cset(result, ne);
2316 __ Cneg(result, result, lt);
2317 break;
2318 }
2319 case Primitive::kPrimFloat:
2320 case Primitive::kPrimDouble: {
2321 Register result = OutputRegister(compare);
2322 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002323 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002324 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2325 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002326 __ Fcmp(left, 0.0);
2327 } else {
2328 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2329 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002330 if (compare->IsGtBias()) {
2331 __ Cset(result, ne);
2332 } else {
2333 __ Csetm(result, ne);
2334 }
2335 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01002336 break;
2337 }
2338 default:
2339 LOG(FATAL) << "Unimplemented compare type " << in_type;
2340 }
2341}
2342
2343void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
2344 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002345
2346 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2347 locations->SetInAt(0, Location::RequiresFpuRegister());
2348 locations->SetInAt(1,
2349 IsFloatingPointZeroConstant(instruction->InputAt(1))
2350 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2351 : Location::RequiresFpuRegister());
2352 } else {
2353 // Integer cases.
2354 locations->SetInAt(0, Location::RequiresRegister());
2355 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2356 }
2357
Alexandre Rames5319def2014-10-23 10:03:10 +01002358 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002360 }
2361}
2362
2363void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
2364 if (!instruction->NeedsMaterialization()) {
2365 return;
2366 }
2367
2368 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002369 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002370 IfCondition if_cond = instruction->GetCondition();
2371 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002372
Roland Levillain7f63c522015-07-13 15:54:55 +00002373 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2374 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2375 if (locations->InAt(1).IsConstant()) {
2376 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2377 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2378 __ Fcmp(lhs, 0.0);
2379 } else {
2380 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2381 }
2382 __ Cset(res, arm64_cond);
2383 if (instruction->IsFPConditionTrueIfNaN()) {
2384 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
2385 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
2386 } else if (instruction->IsFPConditionFalseIfNaN()) {
2387 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
2388 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
2389 }
2390 } else {
2391 // Integer cases.
2392 Register lhs = InputRegisterAt(instruction, 0);
2393 Operand rhs = InputOperandAt(instruction, 1);
2394 __ Cmp(lhs, rhs);
2395 __ Cset(res, arm64_cond);
2396 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002397}
2398
2399#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2400 M(Equal) \
2401 M(NotEqual) \
2402 M(LessThan) \
2403 M(LessThanOrEqual) \
2404 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002405 M(GreaterThanOrEqual) \
2406 M(Below) \
2407 M(BelowOrEqual) \
2408 M(Above) \
2409 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002410#define DEFINE_CONDITION_VISITORS(Name) \
2411void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
2412void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
2413FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002414#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002415#undef FOR_EACH_CONDITION_INSTRUCTION
2416
Zheng Xuc6667102015-05-15 16:08:45 +08002417void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2418 DCHECK(instruction->IsDiv() || instruction->IsRem());
2419
2420 LocationSummary* locations = instruction->GetLocations();
2421 Location second = locations->InAt(1);
2422 DCHECK(second.IsConstant());
2423
2424 Register out = OutputRegister(instruction);
2425 Register dividend = InputRegisterAt(instruction, 0);
2426 int64_t imm = Int64FromConstant(second.GetConstant());
2427 DCHECK(imm == 1 || imm == -1);
2428
2429 if (instruction->IsRem()) {
2430 __ Mov(out, 0);
2431 } else {
2432 if (imm == 1) {
2433 __ Mov(out, dividend);
2434 } else {
2435 __ Neg(out, dividend);
2436 }
2437 }
2438}
2439
2440void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2441 DCHECK(instruction->IsDiv() || instruction->IsRem());
2442
2443 LocationSummary* locations = instruction->GetLocations();
2444 Location second = locations->InAt(1);
2445 DCHECK(second.IsConstant());
2446
2447 Register out = OutputRegister(instruction);
2448 Register dividend = InputRegisterAt(instruction, 0);
2449 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01002450 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002451 DCHECK(IsPowerOfTwo(abs_imm));
2452 int ctz_imm = CTZ(abs_imm);
2453
2454 UseScratchRegisterScope temps(GetVIXLAssembler());
2455 Register temp = temps.AcquireSameSizeAs(out);
2456
2457 if (instruction->IsDiv()) {
2458 __ Add(temp, dividend, abs_imm - 1);
2459 __ Cmp(dividend, 0);
2460 __ Csel(out, temp, dividend, lt);
2461 if (imm > 0) {
2462 __ Asr(out, out, ctz_imm);
2463 } else {
2464 __ Neg(out, Operand(out, ASR, ctz_imm));
2465 }
2466 } else {
2467 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2468 __ Asr(temp, dividend, bits - 1);
2469 __ Lsr(temp, temp, bits - ctz_imm);
2470 __ Add(out, dividend, temp);
2471 __ And(out, out, abs_imm - 1);
2472 __ Sub(out, out, temp);
2473 }
2474}
2475
2476void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2477 DCHECK(instruction->IsDiv() || instruction->IsRem());
2478
2479 LocationSummary* locations = instruction->GetLocations();
2480 Location second = locations->InAt(1);
2481 DCHECK(second.IsConstant());
2482
2483 Register out = OutputRegister(instruction);
2484 Register dividend = InputRegisterAt(instruction, 0);
2485 int64_t imm = Int64FromConstant(second.GetConstant());
2486
2487 Primitive::Type type = instruction->GetResultType();
2488 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2489
2490 int64_t magic;
2491 int shift;
2492 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2493
2494 UseScratchRegisterScope temps(GetVIXLAssembler());
2495 Register temp = temps.AcquireSameSizeAs(out);
2496
2497 // temp = get_high(dividend * magic)
2498 __ Mov(temp, magic);
2499 if (type == Primitive::kPrimLong) {
2500 __ Smulh(temp, dividend, temp);
2501 } else {
2502 __ Smull(temp.X(), dividend, temp);
2503 __ Lsr(temp.X(), temp.X(), 32);
2504 }
2505
2506 if (imm > 0 && magic < 0) {
2507 __ Add(temp, temp, dividend);
2508 } else if (imm < 0 && magic > 0) {
2509 __ Sub(temp, temp, dividend);
2510 }
2511
2512 if (shift != 0) {
2513 __ Asr(temp, temp, shift);
2514 }
2515
2516 if (instruction->IsDiv()) {
2517 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2518 } else {
2519 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2520 // TODO: Strength reduction for msub.
2521 Register temp_imm = temps.AcquireSameSizeAs(out);
2522 __ Mov(temp_imm, imm);
2523 __ Msub(out, temp, temp_imm, dividend);
2524 }
2525}
2526
2527void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2528 DCHECK(instruction->IsDiv() || instruction->IsRem());
2529 Primitive::Type type = instruction->GetResultType();
2530 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2531
2532 LocationSummary* locations = instruction->GetLocations();
2533 Register out = OutputRegister(instruction);
2534 Location second = locations->InAt(1);
2535
2536 if (second.IsConstant()) {
2537 int64_t imm = Int64FromConstant(second.GetConstant());
2538
2539 if (imm == 0) {
2540 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2541 } else if (imm == 1 || imm == -1) {
2542 DivRemOneOrMinusOne(instruction);
2543 } else if (IsPowerOfTwo(std::abs(imm))) {
2544 DivRemByPowerOfTwo(instruction);
2545 } else {
2546 DCHECK(imm <= -2 || imm >= 2);
2547 GenerateDivRemWithAnyConstant(instruction);
2548 }
2549 } else {
2550 Register dividend = InputRegisterAt(instruction, 0);
2551 Register divisor = InputRegisterAt(instruction, 1);
2552 if (instruction->IsDiv()) {
2553 __ Sdiv(out, dividend, divisor);
2554 } else {
2555 UseScratchRegisterScope temps(GetVIXLAssembler());
2556 Register temp = temps.AcquireSameSizeAs(out);
2557 __ Sdiv(temp, dividend, divisor);
2558 __ Msub(out, temp, divisor, dividend);
2559 }
2560 }
2561}
2562
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002563void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2564 LocationSummary* locations =
2565 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2566 switch (div->GetResultType()) {
2567 case Primitive::kPrimInt:
2568 case Primitive::kPrimLong:
2569 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002570 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2572 break;
2573
2574 case Primitive::kPrimFloat:
2575 case Primitive::kPrimDouble:
2576 locations->SetInAt(0, Location::RequiresFpuRegister());
2577 locations->SetInAt(1, Location::RequiresFpuRegister());
2578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2583 }
2584}
2585
2586void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2587 Primitive::Type type = div->GetResultType();
2588 switch (type) {
2589 case Primitive::kPrimInt:
2590 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002591 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002592 break;
2593
2594 case Primitive::kPrimFloat:
2595 case Primitive::kPrimDouble:
2596 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2597 break;
2598
2599 default:
2600 LOG(FATAL) << "Unexpected div type " << type;
2601 }
2602}
2603
Alexandre Rames67555f72014-11-18 10:55:16 +00002604void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002605 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2606 ? LocationSummary::kCallOnSlowPath
2607 : LocationSummary::kNoCall;
2608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002609 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2610 if (instruction->HasUses()) {
2611 locations->SetOut(Location::SameAsFirstInput());
2612 }
2613}
2614
2615void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2616 SlowPathCodeARM64* slow_path =
2617 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2618 codegen_->AddSlowPath(slow_path);
2619 Location value = instruction->GetLocations()->InAt(0);
2620
Alexandre Rames3e69f162014-12-10 10:36:50 +00002621 Primitive::Type type = instruction->GetType();
2622
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002623 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2624 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002625 return;
2626 }
2627
Alexandre Rames67555f72014-11-18 10:55:16 +00002628 if (value.IsConstant()) {
2629 int64_t divisor = Int64ConstantFrom(value);
2630 if (divisor == 0) {
2631 __ B(slow_path->GetEntryLabel());
2632 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002633 // A division by a non-null constant is valid. We don't need to perform
2634 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002635 }
2636 } else {
2637 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2638 }
2639}
2640
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002641void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2642 LocationSummary* locations =
2643 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2644 locations->SetOut(Location::ConstantLocation(constant));
2645}
2646
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002647void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2648 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002649 // Will be generated at use site.
2650}
2651
Alexandre Rames5319def2014-10-23 10:03:10 +01002652void LocationsBuilderARM64::VisitExit(HExit* exit) {
2653 exit->SetLocations(nullptr);
2654}
2655
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002656void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002657}
2658
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002659void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2660 LocationSummary* locations =
2661 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2662 locations->SetOut(Location::ConstantLocation(constant));
2663}
2664
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002665void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002666 // Will be generated at use site.
2667}
2668
David Brazdilfc6a86a2015-06-26 10:33:45 +00002669void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002670 DCHECK(!successor->IsExitBlock());
2671 HBasicBlock* block = got->GetBlock();
2672 HInstruction* previous = got->GetPrevious();
2673 HLoopInformation* info = block->GetLoopInformation();
2674
David Brazdil46e2a392015-03-16 17:31:52 +00002675 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002676 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2677 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2678 return;
2679 }
2680 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2681 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2682 }
2683 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002684 __ B(codegen_->GetLabelOf(successor));
2685 }
2686}
2687
David Brazdilfc6a86a2015-06-26 10:33:45 +00002688void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2689 got->SetLocations(nullptr);
2690}
2691
2692void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2693 HandleGoto(got, got->GetSuccessor());
2694}
2695
2696void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2697 try_boundary->SetLocations(nullptr);
2698}
2699
2700void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2701 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2702 if (!successor->IsExitBlock()) {
2703 HandleGoto(try_boundary, successor);
2704 }
2705}
2706
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002707void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002708 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002709 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002710 vixl::Label* false_target) {
2711 // FP branching requires both targets to be explicit. If either of the targets
2712 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2713 vixl::Label fallthrough_target;
2714 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002715
David Brazdil0debae72015-11-12 18:37:00 +00002716 if (true_target == nullptr && false_target == nullptr) {
2717 // Nothing to do. The code always falls through.
2718 return;
2719 } else if (cond->IsIntConstant()) {
2720 // Constant condition, statically compared against 1.
2721 if (cond->AsIntConstant()->IsOne()) {
2722 if (true_target != nullptr) {
2723 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002724 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002725 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002726 DCHECK(cond->AsIntConstant()->IsZero());
2727 if (false_target != nullptr) {
2728 __ B(false_target);
2729 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002730 }
David Brazdil0debae72015-11-12 18:37:00 +00002731 return;
2732 }
2733
2734 // The following code generates these patterns:
2735 // (1) true_target == nullptr && false_target != nullptr
2736 // - opposite condition true => branch to false_target
2737 // (2) true_target != nullptr && false_target == nullptr
2738 // - condition true => branch to true_target
2739 // (3) true_target != nullptr && false_target != nullptr
2740 // - condition true => branch to true_target
2741 // - branch to false_target
2742 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002743 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002744 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002745 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002746 if (true_target == nullptr) {
2747 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2748 } else {
2749 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2750 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002751 } else {
2752 // The condition instruction has not been materialized, use its inputs as
2753 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002754 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002755
David Brazdil0debae72015-11-12 18:37:00 +00002756 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002757 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002758 FPRegister lhs = InputFPRegisterAt(condition, 0);
2759 if (condition->GetLocations()->InAt(1).IsConstant()) {
2760 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2761 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2762 __ Fcmp(lhs, 0.0);
2763 } else {
2764 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2765 }
2766 if (condition->IsFPConditionTrueIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002767 __ B(vs, true_target == nullptr ? &fallthrough_target : true_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002768 } else if (condition->IsFPConditionFalseIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002769 __ B(vs, false_target == nullptr ? &fallthrough_target : false_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002770 }
David Brazdil0debae72015-11-12 18:37:00 +00002771 if (true_target == nullptr) {
2772 __ B(ARM64Condition(condition->GetOppositeCondition()), false_target);
2773 } else {
2774 __ B(ARM64Condition(condition->GetCondition()), true_target);
2775 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002776 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002777 // Integer cases.
2778 Register lhs = InputRegisterAt(condition, 0);
2779 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002780
2781 Condition arm64_cond;
2782 vixl::Label* non_fallthrough_target;
2783 if (true_target == nullptr) {
2784 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2785 non_fallthrough_target = false_target;
2786 } else {
2787 arm64_cond = ARM64Condition(condition->GetCondition());
2788 non_fallthrough_target = true_target;
2789 }
2790
Roland Levillain7f63c522015-07-13 15:54:55 +00002791 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2792 switch (arm64_cond) {
2793 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002794 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002795 break;
2796 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002797 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002798 break;
2799 case lt:
2800 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002801 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002802 break;
2803 case ge:
2804 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002805 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002806 break;
2807 default:
2808 // Without the `static_cast` the compiler throws an error for
2809 // `-Werror=sign-promo`.
2810 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2811 }
2812 } else {
2813 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002814 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002815 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002816 }
2817 }
David Brazdil0debae72015-11-12 18:37:00 +00002818
2819 // If neither branch falls through (case 3), the conditional branch to `true_target`
2820 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2821 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002822 __ B(false_target);
2823 }
David Brazdil0debae72015-11-12 18:37:00 +00002824
2825 if (fallthrough_target.IsLinked()) {
2826 __ Bind(&fallthrough_target);
2827 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002828}
2829
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002830void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002832 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002833 locations->SetInAt(0, Location::RequiresRegister());
2834 }
2835}
2836
2837void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002838 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2839 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2840 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2841 nullptr : codegen_->GetLabelOf(true_successor);
2842 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2843 nullptr : codegen_->GetLabelOf(false_successor);
2844 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002845}
2846
2847void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2848 LocationSummary* locations = new (GetGraph()->GetArena())
2849 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002850 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002851 locations->SetInAt(0, Location::RequiresRegister());
2852 }
2853}
2854
2855void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2856 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2857 DeoptimizationSlowPathARM64(deoptimize);
2858 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002859 GenerateTestAndBranch(deoptimize,
2860 /* condition_input_index */ 0,
2861 slow_path->GetEntryLabel(),
2862 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002863}
2864
Alexandre Rames5319def2014-10-23 10:03:10 +01002865void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002866 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002867}
2868
2869void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002870 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002871}
2872
2873void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002874 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002875}
2876
2877void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002878 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002879}
2880
Alexandre Rames67555f72014-11-18 10:55:16 +00002881void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002882 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002883 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2884 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002885 case TypeCheckKind::kExactCheck:
2886 case TypeCheckKind::kAbstractClassCheck:
2887 case TypeCheckKind::kClassHierarchyCheck:
2888 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002889 call_kind =
2890 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002891 break;
2892 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002893 case TypeCheckKind::kUnresolvedCheck:
2894 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002895 call_kind = LocationSummary::kCallOnSlowPath;
2896 break;
2897 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002898
Alexandre Rames67555f72014-11-18 10:55:16 +00002899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetInAt(1, Location::RequiresRegister());
2902 // The "out" register is used as a temporary, so it overlaps with the inputs.
2903 // Note that TypeCheckSlowPathARM64 uses this register too.
2904 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2905 // When read barriers are enabled, we need a temporary register for
2906 // some cases.
2907 if (kEmitCompilerReadBarrier &&
2908 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2909 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2910 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2911 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002912 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002913}
2914
2915void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2916 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002917 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002918 Register obj = InputRegisterAt(instruction, 0);
2919 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002920 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00002921 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002922 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2923 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2924 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2925 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002926
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002927 vixl::Label done, zero;
2928 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002929
2930 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002931 // Avoid null check if we know `obj` is not null.
2932 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002933 __ Cbz(obj, &zero);
2934 }
2935
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002936 // /* HeapReference<Class> */ out = obj->klass_
2937 __ Ldr(out, HeapOperand(obj.W(), class_offset));
2938 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002939
2940 switch (instruction->GetTypeCheckKind()) {
2941 case TypeCheckKind::kExactCheck: {
2942 __ Cmp(out, cls);
2943 __ Cset(out, eq);
2944 if (zero.IsLinked()) {
2945 __ B(&done);
2946 }
2947 break;
2948 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002949
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002950 case TypeCheckKind::kAbstractClassCheck: {
2951 // If the class is abstract, we eagerly fetch the super class of the
2952 // object to avoid doing a comparison we know will fail.
2953 vixl::Label loop, success;
2954 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002955 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
2956 if (kEmitCompilerReadBarrier) {
2957 // Save the value of `out` into `temp` before overwriting it
2958 // in the following move operation, as we will need it for the
2959 // read barrier below.
2960 Register temp = WRegisterFrom(temp_loc);
2961 __ Mov(temp, out);
2962 }
2963 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002964 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002965 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002966 // If `out` is null, we use it for the result, and jump to `done`.
2967 __ Cbz(out, &done);
2968 __ Cmp(out, cls);
2969 __ B(ne, &loop);
2970 __ Mov(out, 1);
2971 if (zero.IsLinked()) {
2972 __ B(&done);
2973 }
2974 break;
2975 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002976
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002977 case TypeCheckKind::kClassHierarchyCheck: {
2978 // Walk over the class hierarchy to find a match.
2979 vixl::Label loop, success;
2980 __ Bind(&loop);
2981 __ Cmp(out, cls);
2982 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002983 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
2984 if (kEmitCompilerReadBarrier) {
2985 // Save the value of `out` into `temp` before overwriting it
2986 // in the following move operation, as we will need it for the
2987 // read barrier below.
2988 Register temp = WRegisterFrom(temp_loc);
2989 __ Mov(temp, out);
2990 }
2991 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002992 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002993 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002994 __ Cbnz(out, &loop);
2995 // If `out` is null, we use it for the result, and jump to `done`.
2996 __ B(&done);
2997 __ Bind(&success);
2998 __ Mov(out, 1);
2999 if (zero.IsLinked()) {
3000 __ B(&done);
3001 }
3002 break;
3003 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003004
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003005 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003006 // Do an exact check.
3007 vixl::Label exact_check;
3008 __ Cmp(out, cls);
3009 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003010 // Otherwise, we need to check that the object's class is a non-primitive array.
3011 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3012 if (kEmitCompilerReadBarrier) {
3013 // Save the value of `out` into `temp` before overwriting it
3014 // in the following move operation, as we will need it for the
3015 // read barrier below.
3016 Register temp = WRegisterFrom(temp_loc);
3017 __ Mov(temp, out);
3018 }
3019 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003020 __ Ldr(out, HeapOperand(out, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003021 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003022 // If `out` is null, we use it for the result, and jump to `done`.
3023 __ Cbz(out, &done);
3024 __ Ldrh(out, HeapOperand(out, primitive_offset));
3025 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3026 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003027 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003028 __ Mov(out, 1);
3029 __ B(&done);
3030 break;
3031 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003032
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003033 case TypeCheckKind::kArrayCheck: {
3034 __ Cmp(out, cls);
3035 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003036 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3037 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003038 codegen_->AddSlowPath(slow_path);
3039 __ B(ne, slow_path->GetEntryLabel());
3040 __ Mov(out, 1);
3041 if (zero.IsLinked()) {
3042 __ B(&done);
3043 }
3044 break;
3045 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003046
Calin Juravle98893e12015-10-02 21:05:03 +01003047 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003048 case TypeCheckKind::kInterfaceCheck: {
3049 // Note that we indeed only call on slow path, but we always go
3050 // into the slow path for the unresolved and interface check
3051 // cases.
3052 //
3053 // We cannot directly call the InstanceofNonTrivial runtime
3054 // entry point without resorting to a type checking slow path
3055 // here (i.e. by calling InvokeRuntime directly), as it would
3056 // require to assign fixed registers for the inputs of this
3057 // HInstanceOf instruction (following the runtime calling
3058 // convention), which might be cluttered by the potential first
3059 // read barrier emission at the beginning of this method.
3060 DCHECK(locations->OnlyCallsOnSlowPath());
3061 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3062 /* is_fatal */ false);
3063 codegen_->AddSlowPath(slow_path);
3064 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003065 if (zero.IsLinked()) {
3066 __ B(&done);
3067 }
3068 break;
3069 }
3070 }
3071
3072 if (zero.IsLinked()) {
3073 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003074 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003075 }
3076
3077 if (done.IsLinked()) {
3078 __ Bind(&done);
3079 }
3080
3081 if (slow_path != nullptr) {
3082 __ Bind(slow_path->GetExitLabel());
3083 }
3084}
3085
3086void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3087 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3088 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3089
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003090 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3091 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003092 case TypeCheckKind::kExactCheck:
3093 case TypeCheckKind::kAbstractClassCheck:
3094 case TypeCheckKind::kClassHierarchyCheck:
3095 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003096 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3097 LocationSummary::kCallOnSlowPath :
3098 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003099 break;
3100 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003101 case TypeCheckKind::kUnresolvedCheck:
3102 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003103 call_kind = LocationSummary::kCallOnSlowPath;
3104 break;
3105 }
3106
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3108 locations->SetInAt(0, Location::RequiresRegister());
3109 locations->SetInAt(1, Location::RequiresRegister());
3110 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3111 locations->AddTemp(Location::RequiresRegister());
3112 locations->AddTemp(Location::RequiresRegister());
3113 // When read barriers are enabled, we need an additional temporary
3114 // register for some cases.
3115 if (kEmitCompilerReadBarrier &&
3116 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3117 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3118 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3119 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003120 }
3121}
3122
3123void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
3124 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003125 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003126 Register obj = InputRegisterAt(instruction, 0);
3127 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003128 Location temp_loc = locations->GetTemp(0);
3129 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003130 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3131 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3132 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3133 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003134
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003135 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3136 bool is_type_check_slow_path_fatal =
3137 (type_check_kind == TypeCheckKind::kExactCheck ||
3138 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3139 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3140 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3141 !instruction->CanThrowIntoCatchBlock();
3142 SlowPathCodeARM64* type_check_slow_path =
3143 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3144 is_type_check_slow_path_fatal);
3145 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003146
3147 vixl::Label done;
3148 // Avoid null check if we know obj is not null.
3149 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003150 __ Cbz(obj, &done);
3151 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003152
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003153 // /* HeapReference<Class> */ temp = obj->klass_
3154 __ Ldr(temp, HeapOperand(obj, class_offset));
3155 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003156
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003157 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003158 case TypeCheckKind::kExactCheck:
3159 case TypeCheckKind::kArrayCheck: {
3160 __ Cmp(temp, cls);
3161 // Jump to slow path for throwing the exception or doing a
3162 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003163 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003164 break;
3165 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003166
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003167 case TypeCheckKind::kAbstractClassCheck: {
3168 // If the class is abstract, we eagerly fetch the super class of the
3169 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003170 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003171 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003172 Location temp2_loc =
3173 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3174 if (kEmitCompilerReadBarrier) {
3175 // Save the value of `temp` into `temp2` before overwriting it
3176 // in the following move operation, as we will need it for the
3177 // read barrier below.
3178 Register temp2 = WRegisterFrom(temp2_loc);
3179 __ Mov(temp2, temp);
3180 }
3181 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003182 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003183 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3184
3185 // If the class reference currently in `temp` is not null, jump
3186 // to the `compare_classes` label to compare it with the checked
3187 // class.
3188 __ Cbnz(temp, &compare_classes);
3189 // Otherwise, jump to the slow path to throw the exception.
3190 //
3191 // But before, move back the object's class into `temp` before
3192 // going into the slow path, as it has been overwritten in the
3193 // meantime.
3194 // /* HeapReference<Class> */ temp = obj->klass_
3195 __ Ldr(temp, HeapOperand(obj, class_offset));
3196 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3197 __ B(type_check_slow_path->GetEntryLabel());
3198
3199 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200 __ Cmp(temp, cls);
3201 __ B(ne, &loop);
3202 break;
3203 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003205 case TypeCheckKind::kClassHierarchyCheck: {
3206 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003207 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003208 __ Bind(&loop);
3209 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003210 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003211
3212 Location temp2_loc =
3213 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3214 if (kEmitCompilerReadBarrier) {
3215 // Save the value of `temp` into `temp2` before overwriting it
3216 // in the following move operation, as we will need it for the
3217 // read barrier below.
3218 Register temp2 = WRegisterFrom(temp2_loc);
3219 __ Mov(temp2, temp);
3220 }
3221 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003222 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003223 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3224
3225 // If the class reference currently in `temp` is not null, jump
3226 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003227 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003228 // Otherwise, jump to the slow path to throw the exception.
3229 //
3230 // But before, move back the object's class into `temp` before
3231 // going into the slow path, as it has been overwritten in the
3232 // meantime.
3233 // /* HeapReference<Class> */ temp = obj->klass_
3234 __ Ldr(temp, HeapOperand(obj, class_offset));
3235 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3236 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 break;
3238 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003239
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003240 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003241 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003242 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003243 __ Cmp(temp, cls);
3244 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003245
3246 // Otherwise, we need to check that the object's class is a non-primitive array.
3247 Location temp2_loc =
3248 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3249 if (kEmitCompilerReadBarrier) {
3250 // Save the value of `temp` into `temp2` before overwriting it
3251 // in the following move operation, as we will need it for the
3252 // read barrier below.
3253 Register temp2 = WRegisterFrom(temp2_loc);
3254 __ Mov(temp2, temp);
3255 }
3256 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003257 __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003258 codegen_->MaybeGenerateReadBarrier(
3259 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
3260
3261 // If the component type is not null (i.e. the object is indeed
3262 // an array), jump to label `check_non_primitive_component_type`
3263 // to further check that this component type is not a primitive
3264 // type.
3265 __ Cbnz(temp, &check_non_primitive_component_type);
3266 // Otherwise, jump to the slow path to throw the exception.
3267 //
3268 // But before, move back the object's class into `temp` before
3269 // going into the slow path, as it has been overwritten in the
3270 // meantime.
3271 // /* HeapReference<Class> */ temp = obj->klass_
3272 __ Ldr(temp, HeapOperand(obj, class_offset));
3273 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3274 __ B(type_check_slow_path->GetEntryLabel());
3275
3276 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003277 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3278 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003279 __ Cbz(temp, &done);
3280 // Same comment as above regarding `temp` and the slow path.
3281 // /* HeapReference<Class> */ temp = obj->klass_
3282 __ Ldr(temp, HeapOperand(obj, class_offset));
3283 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3284 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003285 break;
3286 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003287
Calin Juravle98893e12015-10-02 21:05:03 +01003288 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003289 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003290 // We always go into the type check slow path for the unresolved
3291 // and interface check cases.
3292 //
3293 // We cannot directly call the CheckCast runtime entry point
3294 // without resorting to a type checking slow path here (i.e. by
3295 // calling InvokeRuntime directly), as it would require to
3296 // assign fixed registers for the inputs of this HInstanceOf
3297 // instruction (following the runtime calling convention), which
3298 // might be cluttered by the potential first read barrier
3299 // emission at the beginning of this method.
3300 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003301 break;
3302 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003303 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003304
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003305 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003306}
3307
Alexandre Rames5319def2014-10-23 10:03:10 +01003308void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3309 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3310 locations->SetOut(Location::ConstantLocation(constant));
3311}
3312
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003313void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003314 // Will be generated at use site.
3315}
3316
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003317void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3319 locations->SetOut(Location::ConstantLocation(constant));
3320}
3321
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003322void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003323 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003324}
3325
Calin Juravle175dc732015-08-25 15:42:32 +01003326void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3327 // The trampoline uses the same calling convention as dex calling conventions,
3328 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3329 // the method_idx.
3330 HandleInvoke(invoke);
3331}
3332
3333void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3334 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3335}
3336
Alexandre Rames5319def2014-10-23 10:03:10 +01003337void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003338 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003339 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003340}
3341
Alexandre Rames67555f72014-11-18 10:55:16 +00003342void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3343 HandleInvoke(invoke);
3344}
3345
3346void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3347 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003348 LocationSummary* locations = invoke->GetLocations();
3349 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003350 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3351 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003352 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003353 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003354 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003355
3356 // The register ip1 is required to be used for the hidden argument in
3357 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003358 MacroAssembler* masm = GetVIXLAssembler();
3359 UseScratchRegisterScope scratch_scope(masm);
3360 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003361 scratch_scope.Exclude(ip1);
3362 __ Mov(ip1, invoke->GetDexMethodIndex());
3363
Alexandre Rames67555f72014-11-18 10:55:16 +00003364 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003365 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003366 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003367 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003368 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003370 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003371 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003372 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003373 // Instead of simply (possibly) unpoisoning `temp` here, we should
3374 // emit a read barrier for the previous class reference load.
3375 // However this is not required in practice, as this is an
3376 // intermediate/temporary reference and because the current
3377 // concurrent copying collector keeps the from-space memory
3378 // intact/accessible until the end of the marking phase (the
3379 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003380 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003381 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003382 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003383 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003384 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003385 // lr();
3386 __ Blr(lr);
3387 DCHECK(!codegen_->IsLeafMethod());
3388 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3389}
3390
3391void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003392 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3393 if (intrinsic.TryDispatch(invoke)) {
3394 return;
3395 }
3396
Alexandre Rames67555f72014-11-18 10:55:16 +00003397 HandleInvoke(invoke);
3398}
3399
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003400void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003401 // When we do not run baseline, explicit clinit checks triggered by static
3402 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3403 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003404
Andreas Gampe878d58c2015-01-15 23:24:00 -08003405 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3406 if (intrinsic.TryDispatch(invoke)) {
3407 return;
3408 }
3409
Alexandre Rames67555f72014-11-18 10:55:16 +00003410 HandleInvoke(invoke);
3411}
3412
Andreas Gampe878d58c2015-01-15 23:24:00 -08003413static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3414 if (invoke->GetLocations()->Intrinsified()) {
3415 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3416 intrinsic.Dispatch(invoke);
3417 return true;
3418 }
3419 return false;
3420}
3421
Vladimir Markodc151b22015-10-15 18:02:30 +01003422HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3423 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3424 MethodReference target_method ATTRIBUTE_UNUSED) {
3425 // On arm64 we support all dispatch types.
3426 return desired_dispatch_info;
3427}
3428
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003429void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003430 // For better instruction scheduling we load the direct code pointer before the method pointer.
3431 bool direct_code_loaded = false;
3432 switch (invoke->GetCodePtrLocation()) {
3433 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3434 // LR = code address from literal pool with link-time patch.
3435 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3436 direct_code_loaded = true;
3437 break;
3438 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3439 // LR = invoke->GetDirectCodePtr();
3440 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3441 direct_code_loaded = true;
3442 break;
3443 default:
3444 break;
3445 }
3446
Andreas Gampe878d58c2015-01-15 23:24:00 -08003447 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003448 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3449 switch (invoke->GetMethodLoadKind()) {
3450 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3451 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003452 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003453 break;
3454 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003455 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003456 break;
3457 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3458 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003459 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003460 break;
3461 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3462 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003463 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003464 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3465 break;
3466 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3467 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003468 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3469 invoke->GetDexCacheArrayOffset());
3470 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003471 {
3472 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003473 __ Bind(pc_insn_label);
3474 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003475 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003476 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003477 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003478 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3479 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003480 {
3481 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3482 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3483 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3484 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3485 }
Vladimir Marko58155012015-08-19 12:49:41 +00003486 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003487 }
Vladimir Marko58155012015-08-19 12:49:41 +00003488 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003489 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003490 Register reg = XRegisterFrom(temp);
3491 Register method_reg;
3492 if (current_method.IsRegister()) {
3493 method_reg = XRegisterFrom(current_method);
3494 } else {
3495 DCHECK(invoke->GetLocations()->Intrinsified());
3496 DCHECK(!current_method.IsValid());
3497 method_reg = reg;
3498 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3499 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003500
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003501 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003502 __ Ldr(reg.X(),
3503 MemOperand(method_reg.X(),
3504 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003505 // temp = temp[index_in_cache];
3506 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3507 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3508 break;
3509 }
3510 }
3511
3512 switch (invoke->GetCodePtrLocation()) {
3513 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3514 __ Bl(&frame_entry_label_);
3515 break;
3516 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3517 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3518 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003519 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3520 __ Bind(label);
3521 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003522 break;
3523 }
3524 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3525 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3526 // LR prepared above for better instruction scheduling.
3527 DCHECK(direct_code_loaded);
3528 // lr()
3529 __ Blr(lr);
3530 break;
3531 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3532 // LR = callee_method->entry_point_from_quick_compiled_code_;
3533 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003534 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003535 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3536 // lr()
3537 __ Blr(lr);
3538 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003539 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003540
Andreas Gampe878d58c2015-01-15 23:24:00 -08003541 DCHECK(!IsLeafMethod());
3542}
3543
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003544void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3545 LocationSummary* locations = invoke->GetLocations();
3546 Location receiver = locations->InAt(0);
3547 Register temp = XRegisterFrom(temp_in);
3548 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3549 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3550 Offset class_offset = mirror::Object::ClassOffset();
3551 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3552
3553 BlockPoolsScope block_pools(GetVIXLAssembler());
3554
3555 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003556 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003557 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
3558 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003559 // Instead of simply (possibly) unpoisoning `temp` here, we should
3560 // emit a read barrier for the previous class reference load.
3561 // However this is not required in practice, as this is an
3562 // intermediate/temporary reference and because the current
3563 // concurrent copying collector keeps the from-space memory
3564 // intact/accessible until the end of the marking phase (the
3565 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003566 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3567 // temp = temp->GetMethodAt(method_offset);
3568 __ Ldr(temp, MemOperand(temp, method_offset));
3569 // lr = temp->GetEntryPoint();
3570 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3571 // lr();
3572 __ Blr(lr);
3573}
3574
Vladimir Marko58155012015-08-19 12:49:41 +00003575void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3576 DCHECK(linker_patches->empty());
3577 size_t size =
3578 method_patches_.size() +
3579 call_patches_.size() +
3580 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003581 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003582 linker_patches->reserve(size);
3583 for (const auto& entry : method_patches_) {
3584 const MethodReference& target_method = entry.first;
3585 vixl::Literal<uint64_t>* literal = entry.second;
3586 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3587 target_method.dex_file,
3588 target_method.dex_method_index));
3589 }
3590 for (const auto& entry : call_patches_) {
3591 const MethodReference& target_method = entry.first;
3592 vixl::Literal<uint64_t>* literal = entry.second;
3593 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3594 target_method.dex_file,
3595 target_method.dex_method_index));
3596 }
3597 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003598 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003599 info.target_method.dex_file,
3600 info.target_method.dex_method_index));
3601 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003602 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003603 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003604 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003605 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003606 info.element_offset));
3607 }
3608}
3609
3610vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3611 // Look up the literal for value.
3612 auto lb = uint64_literals_.lower_bound(value);
3613 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3614 return lb->second;
3615 }
3616 // We don't have a literal for this value, insert a new one.
3617 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3618 uint64_literals_.PutBefore(lb, value, literal);
3619 return literal;
3620}
3621
3622vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3623 MethodReference target_method,
3624 MethodToLiteralMap* map) {
3625 // Look up the literal for target_method.
3626 auto lb = map->lower_bound(target_method);
3627 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3628 return lb->second;
3629 }
3630 // We don't have a literal for this method yet, insert a new one.
3631 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3632 map->PutBefore(lb, target_method, literal);
3633 return literal;
3634}
3635
3636vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3637 MethodReference target_method) {
3638 return DeduplicateMethodLiteral(target_method, &method_patches_);
3639}
3640
3641vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3642 MethodReference target_method) {
3643 return DeduplicateMethodLiteral(target_method, &call_patches_);
3644}
3645
3646
Andreas Gampe878d58c2015-01-15 23:24:00 -08003647void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003648 // When we do not run baseline, explicit clinit checks triggered by static
3649 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3650 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003651
Andreas Gampe878d58c2015-01-15 23:24:00 -08003652 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3653 return;
3654 }
3655
Alexandre Ramesd921d642015-04-16 15:07:16 +01003656 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003657 LocationSummary* locations = invoke->GetLocations();
3658 codegen_->GenerateStaticOrDirectCall(
3659 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003660 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003661}
3662
3663void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003664 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3665 return;
3666 }
3667
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003668 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003669 DCHECK(!codegen_->IsLeafMethod());
3670 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3671}
3672
Alexandre Rames67555f72014-11-18 10:55:16 +00003673void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003674 InvokeRuntimeCallingConvention calling_convention;
3675 CodeGenerator::CreateLoadClassLocationSummary(
3676 cls,
3677 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003678 LocationFrom(vixl::x0),
3679 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003680}
3681
3682void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003683 if (cls->NeedsAccessCheck()) {
3684 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3685 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3686 cls,
3687 cls->GetDexPc(),
3688 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003689 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003690 return;
3691 }
3692
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003693 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003694 Register out = OutputRegister(cls);
3695 Register current_method = InputRegisterAt(cls, 0);
3696 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003697 DCHECK(!cls->CanCallRuntime());
3698 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003699 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3700 if (kEmitCompilerReadBarrier) {
3701 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3702 __ Add(out.X(), current_method.X(), declaring_class_offset);
3703 // /* mirror::Class* */ out = out->Read()
3704 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3705 } else {
3706 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3707 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3708 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003709 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003710 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003711 // /* GcRoot<mirror::Class>[] */ out =
3712 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003713 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003714
3715 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
3716 if (kEmitCompilerReadBarrier) {
3717 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
3718 __ Add(out.X(), out.X(), cache_offset);
3719 // /* mirror::Class* */ out = out->Read()
3720 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3721 } else {
3722 // /* GcRoot<mirror::Class> */ out = out[type_index]
3723 __ Ldr(out, MemOperand(out.X(), cache_offset));
3724 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003725
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003726 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3727 DCHECK(cls->CanCallRuntime());
3728 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3729 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3730 codegen_->AddSlowPath(slow_path);
3731 if (!cls->IsInDexCache()) {
3732 __ Cbz(out, slow_path->GetEntryLabel());
3733 }
3734 if (cls->MustGenerateClinitCheck()) {
3735 GenerateClassInitializationCheck(slow_path, out);
3736 } else {
3737 __ Bind(slow_path->GetExitLabel());
3738 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003739 }
3740 }
3741}
3742
David Brazdilcb1c0552015-08-04 16:22:25 +01003743static MemOperand GetExceptionTlsAddress() {
3744 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3745}
3746
Alexandre Rames67555f72014-11-18 10:55:16 +00003747void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3748 LocationSummary* locations =
3749 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3750 locations->SetOut(Location::RequiresRegister());
3751}
3752
3753void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003754 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3755}
3756
3757void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3758 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3759}
3760
3761void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3762 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003763}
3764
Alexandre Rames5319def2014-10-23 10:03:10 +01003765void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3766 load->SetLocations(nullptr);
3767}
3768
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003769void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003770 // Nothing to do, this is driven by the code generator.
3771}
3772
Alexandre Rames67555f72014-11-18 10:55:16 +00003773void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3774 LocationSummary* locations =
3775 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003776 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003777 locations->SetOut(Location::RequiresRegister());
3778}
3779
3780void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3781 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3782 codegen_->AddSlowPath(slow_path);
3783
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003784 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003785 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003786 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003787
3788 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3789 if (kEmitCompilerReadBarrier) {
3790 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3791 __ Add(out.X(), current_method.X(), declaring_class_offset);
3792 // /* mirror::Class* */ out = out->Read()
3793 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3794 } else {
3795 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3796 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3797 }
3798
3799 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3800 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
3801
3802 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
3803 if (kEmitCompilerReadBarrier) {
3804 // /* GcRoot<mirror::String>* */ out = &out[string_index]
3805 __ Add(out.X(), out.X(), cache_offset);
3806 // /* mirror::String* */ out = out->Read()
3807 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3808 } else {
3809 // /* GcRoot<mirror::String> */ out = out[string_index]
3810 __ Ldr(out, MemOperand(out.X(), cache_offset));
3811 }
3812
Alexandre Rames67555f72014-11-18 10:55:16 +00003813 __ Cbz(out, slow_path->GetEntryLabel());
3814 __ Bind(slow_path->GetExitLabel());
3815}
3816
Alexandre Rames5319def2014-10-23 10:03:10 +01003817void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3818 local->SetLocations(nullptr);
3819}
3820
3821void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3822 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3823}
3824
3825void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3827 locations->SetOut(Location::ConstantLocation(constant));
3828}
3829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003830void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003831 // Will be generated at use site.
3832}
3833
Alexandre Rames67555f72014-11-18 10:55:16 +00003834void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3835 LocationSummary* locations =
3836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3837 InvokeRuntimeCallingConvention calling_convention;
3838 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3839}
3840
3841void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3842 codegen_->InvokeRuntime(instruction->IsEnter()
3843 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3844 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003845 instruction->GetDexPc(),
3846 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003847 if (instruction->IsEnter()) {
3848 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3849 } else {
3850 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3851 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003852}
3853
Alexandre Rames42d641b2014-10-27 14:00:51 +00003854void LocationsBuilderARM64::VisitMul(HMul* mul) {
3855 LocationSummary* locations =
3856 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3857 switch (mul->GetResultType()) {
3858 case Primitive::kPrimInt:
3859 case Primitive::kPrimLong:
3860 locations->SetInAt(0, Location::RequiresRegister());
3861 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003862 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003863 break;
3864
3865 case Primitive::kPrimFloat:
3866 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003867 locations->SetInAt(0, Location::RequiresFpuRegister());
3868 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003869 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003870 break;
3871
3872 default:
3873 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3874 }
3875}
3876
3877void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3878 switch (mul->GetResultType()) {
3879 case Primitive::kPrimInt:
3880 case Primitive::kPrimLong:
3881 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3882 break;
3883
3884 case Primitive::kPrimFloat:
3885 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003886 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003887 break;
3888
3889 default:
3890 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3891 }
3892}
3893
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003894void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3895 LocationSummary* locations =
3896 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3897 switch (neg->GetResultType()) {
3898 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003899 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003900 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003901 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003902 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003903
3904 case Primitive::kPrimFloat:
3905 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003906 locations->SetInAt(0, Location::RequiresFpuRegister());
3907 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003908 break;
3909
3910 default:
3911 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3912 }
3913}
3914
3915void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3916 switch (neg->GetResultType()) {
3917 case Primitive::kPrimInt:
3918 case Primitive::kPrimLong:
3919 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3920 break;
3921
3922 case Primitive::kPrimFloat:
3923 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003924 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003925 break;
3926
3927 default:
3928 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3929 }
3930}
3931
3932void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3933 LocationSummary* locations =
3934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3935 InvokeRuntimeCallingConvention calling_convention;
3936 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003937 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003938 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003939 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003940}
3941
3942void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3943 LocationSummary* locations = instruction->GetLocations();
3944 InvokeRuntimeCallingConvention calling_convention;
3945 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3946 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003947 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003948 // Note: if heap poisoning is enabled, the entry point takes cares
3949 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003950 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3951 instruction,
3952 instruction->GetDexPc(),
3953 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003954 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003955}
3956
Alexandre Rames5319def2014-10-23 10:03:10 +01003957void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3958 LocationSummary* locations =
3959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3960 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003961 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3962 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003963 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3964}
3965
3966void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003967 // Note: if heap poisoning is enabled, the entry point takes cares
3968 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003969 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3970 instruction,
3971 instruction->GetDexPc(),
3972 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003973 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003974}
3975
3976void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003978 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003980}
3981
3982void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003983 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003984 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003985 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003986 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003987 break;
3988
3989 default:
3990 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3991 }
3992}
3993
David Brazdil66d126e2015-04-03 16:02:44 +01003994void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3996 locations->SetInAt(0, Location::RequiresRegister());
3997 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3998}
3999
4000void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004001 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4002}
4003
Alexandre Rames5319def2014-10-23 10:03:10 +01004004void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004005 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4006 ? LocationSummary::kCallOnSlowPath
4007 : LocationSummary::kNoCall;
4008 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004009 locations->SetInAt(0, Location::RequiresRegister());
4010 if (instruction->HasUses()) {
4011 locations->SetOut(Location::SameAsFirstInput());
4012 }
4013}
4014
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004015void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004016 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4017 return;
4018 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004019
Alexandre Ramesd921d642015-04-16 15:07:16 +01004020 BlockPoolsScope block_pools(GetVIXLAssembler());
4021 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004022 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4023 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4024}
4025
4026void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004027 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4028 codegen_->AddSlowPath(slow_path);
4029
4030 LocationSummary* locations = instruction->GetLocations();
4031 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004032
4033 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004034}
4035
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004036void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004037 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004038 GenerateImplicitNullCheck(instruction);
4039 } else {
4040 GenerateExplicitNullCheck(instruction);
4041 }
4042}
4043
Alexandre Rames67555f72014-11-18 10:55:16 +00004044void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4045 HandleBinaryOp(instruction);
4046}
4047
4048void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4049 HandleBinaryOp(instruction);
4050}
4051
Alexandre Rames3e69f162014-12-10 10:36:50 +00004052void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4053 LOG(FATAL) << "Unreachable";
4054}
4055
4056void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4057 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4058}
4059
Alexandre Rames5319def2014-10-23 10:03:10 +01004060void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4062 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4063 if (location.IsStackSlot()) {
4064 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4065 } else if (location.IsDoubleStackSlot()) {
4066 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4067 }
4068 locations->SetOut(location);
4069}
4070
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004071void InstructionCodeGeneratorARM64::VisitParameterValue(
4072 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004073 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004074}
4075
4076void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4077 LocationSummary* locations =
4078 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004079 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004080}
4081
4082void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4083 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4084 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004085}
4086
4087void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4089 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4090 locations->SetInAt(i, Location::Any());
4091 }
4092 locations->SetOut(Location::Any());
4093}
4094
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004095void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004096 LOG(FATAL) << "Unreachable";
4097}
4098
Serban Constantinescu02164b32014-11-13 14:05:07 +00004099void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004100 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004101 LocationSummary::CallKind call_kind =
4102 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4104
4105 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004106 case Primitive::kPrimInt:
4107 case Primitive::kPrimLong:
4108 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004109 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4111 break;
4112
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004113 case Primitive::kPrimFloat:
4114 case Primitive::kPrimDouble: {
4115 InvokeRuntimeCallingConvention calling_convention;
4116 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4117 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4118 locations->SetOut(calling_convention.GetReturnLocation(type));
4119
4120 break;
4121 }
4122
Serban Constantinescu02164b32014-11-13 14:05:07 +00004123 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004124 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004125 }
4126}
4127
4128void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4129 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004130
Serban Constantinescu02164b32014-11-13 14:05:07 +00004131 switch (type) {
4132 case Primitive::kPrimInt:
4133 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004134 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004135 break;
4136 }
4137
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004138 case Primitive::kPrimFloat:
4139 case Primitive::kPrimDouble: {
4140 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4141 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004142 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004143 if (type == Primitive::kPrimFloat) {
4144 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4145 } else {
4146 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4147 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004148 break;
4149 }
4150
Serban Constantinescu02164b32014-11-13 14:05:07 +00004151 default:
4152 LOG(FATAL) << "Unexpected rem type " << type;
4153 }
4154}
4155
Calin Juravle27df7582015-04-17 19:12:31 +01004156void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4157 memory_barrier->SetLocations(nullptr);
4158}
4159
4160void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4161 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4162}
4163
Alexandre Rames5319def2014-10-23 10:03:10 +01004164void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4166 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004167 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004168}
4169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004170void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004171 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004172}
4173
4174void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4175 instruction->SetLocations(nullptr);
4176}
4177
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004178void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004179 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004180}
4181
Serban Constantinescu02164b32014-11-13 14:05:07 +00004182void LocationsBuilderARM64::VisitShl(HShl* shl) {
4183 HandleShift(shl);
4184}
4185
4186void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4187 HandleShift(shl);
4188}
4189
4190void LocationsBuilderARM64::VisitShr(HShr* shr) {
4191 HandleShift(shr);
4192}
4193
4194void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4195 HandleShift(shr);
4196}
4197
Alexandre Rames5319def2014-10-23 10:03:10 +01004198void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4199 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4200 Primitive::Type field_type = store->InputAt(1)->GetType();
4201 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004202 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004203 case Primitive::kPrimBoolean:
4204 case Primitive::kPrimByte:
4205 case Primitive::kPrimChar:
4206 case Primitive::kPrimShort:
4207 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004208 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004209 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4210 break;
4211
4212 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004213 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004214 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4215 break;
4216
4217 default:
4218 LOG(FATAL) << "Unimplemented local type " << field_type;
4219 }
4220}
4221
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004222void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004223}
4224
4225void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004226 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004227}
4228
4229void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004230 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004231}
4232
Alexandre Rames67555f72014-11-18 10:55:16 +00004233void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004234 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004235}
4236
4237void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004238 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004239}
4240
4241void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004242 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004243}
4244
Alexandre Rames67555f72014-11-18 10:55:16 +00004245void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004246 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004247}
4248
Calin Juravlee460d1d2015-09-29 04:52:17 +01004249void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4250 HUnresolvedInstanceFieldGet* instruction) {
4251 FieldAccessCallingConventionARM64 calling_convention;
4252 codegen_->CreateUnresolvedFieldLocationSummary(
4253 instruction, instruction->GetFieldType(), calling_convention);
4254}
4255
4256void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4257 HUnresolvedInstanceFieldGet* instruction) {
4258 FieldAccessCallingConventionARM64 calling_convention;
4259 codegen_->GenerateUnresolvedFieldAccess(instruction,
4260 instruction->GetFieldType(),
4261 instruction->GetFieldIndex(),
4262 instruction->GetDexPc(),
4263 calling_convention);
4264}
4265
4266void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4267 HUnresolvedInstanceFieldSet* instruction) {
4268 FieldAccessCallingConventionARM64 calling_convention;
4269 codegen_->CreateUnresolvedFieldLocationSummary(
4270 instruction, instruction->GetFieldType(), calling_convention);
4271}
4272
4273void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4274 HUnresolvedInstanceFieldSet* instruction) {
4275 FieldAccessCallingConventionARM64 calling_convention;
4276 codegen_->GenerateUnresolvedFieldAccess(instruction,
4277 instruction->GetFieldType(),
4278 instruction->GetFieldIndex(),
4279 instruction->GetDexPc(),
4280 calling_convention);
4281}
4282
4283void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4284 HUnresolvedStaticFieldGet* instruction) {
4285 FieldAccessCallingConventionARM64 calling_convention;
4286 codegen_->CreateUnresolvedFieldLocationSummary(
4287 instruction, instruction->GetFieldType(), calling_convention);
4288}
4289
4290void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4291 HUnresolvedStaticFieldGet* instruction) {
4292 FieldAccessCallingConventionARM64 calling_convention;
4293 codegen_->GenerateUnresolvedFieldAccess(instruction,
4294 instruction->GetFieldType(),
4295 instruction->GetFieldIndex(),
4296 instruction->GetDexPc(),
4297 calling_convention);
4298}
4299
4300void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4301 HUnresolvedStaticFieldSet* instruction) {
4302 FieldAccessCallingConventionARM64 calling_convention;
4303 codegen_->CreateUnresolvedFieldLocationSummary(
4304 instruction, instruction->GetFieldType(), calling_convention);
4305}
4306
4307void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4308 HUnresolvedStaticFieldSet* instruction) {
4309 FieldAccessCallingConventionARM64 calling_convention;
4310 codegen_->GenerateUnresolvedFieldAccess(instruction,
4311 instruction->GetFieldType(),
4312 instruction->GetFieldIndex(),
4313 instruction->GetDexPc(),
4314 calling_convention);
4315}
4316
Alexandre Rames5319def2014-10-23 10:03:10 +01004317void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4318 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4319}
4320
4321void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004322 HBasicBlock* block = instruction->GetBlock();
4323 if (block->GetLoopInformation() != nullptr) {
4324 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4325 // The back edge will generate the suspend check.
4326 return;
4327 }
4328 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4329 // The goto will generate the suspend check.
4330 return;
4331 }
4332 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004333}
4334
4335void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4336 temp->SetLocations(nullptr);
4337}
4338
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004339void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004340 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004341}
4342
Alexandre Rames67555f72014-11-18 10:55:16 +00004343void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4344 LocationSummary* locations =
4345 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4346 InvokeRuntimeCallingConvention calling_convention;
4347 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4348}
4349
4350void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4351 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004352 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004353 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004354}
4355
4356void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4357 LocationSummary* locations =
4358 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4359 Primitive::Type input_type = conversion->GetInputType();
4360 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004361 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004362 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4363 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4364 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4365 }
4366
Alexandre Rames542361f2015-01-29 16:57:31 +00004367 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004368 locations->SetInAt(0, Location::RequiresFpuRegister());
4369 } else {
4370 locations->SetInAt(0, Location::RequiresRegister());
4371 }
4372
Alexandre Rames542361f2015-01-29 16:57:31 +00004373 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004374 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4375 } else {
4376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4377 }
4378}
4379
4380void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4381 Primitive::Type result_type = conversion->GetResultType();
4382 Primitive::Type input_type = conversion->GetInputType();
4383
4384 DCHECK_NE(input_type, result_type);
4385
Alexandre Rames542361f2015-01-29 16:57:31 +00004386 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004387 int result_size = Primitive::ComponentSize(result_type);
4388 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004389 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004390 Register output = OutputRegister(conversion);
4391 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004392 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
4393 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004394 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
4395 // 'int' values are used directly as W registers, discarding the top
4396 // bits, so we don't need to sign-extend and can just perform a move.
4397 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4398 // top 32 bits of the target register. We theoretically could leave those
4399 // bits unchanged, but we would have to make sure that no code uses a
4400 // 32bit input value as a 64bit value assuming that the top 32 bits are
4401 // zero.
4402 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00004403 } else if ((result_type == Primitive::kPrimChar) ||
4404 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
4405 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004406 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004407 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004408 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004409 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004410 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004411 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004412 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4413 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004414 } else if (Primitive::IsFloatingPointType(result_type) &&
4415 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004416 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4417 } else {
4418 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4419 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004420 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004421}
Alexandre Rames67555f72014-11-18 10:55:16 +00004422
Serban Constantinescu02164b32014-11-13 14:05:07 +00004423void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4424 HandleShift(ushr);
4425}
4426
4427void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4428 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004429}
4430
4431void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4432 HandleBinaryOp(instruction);
4433}
4434
4435void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4436 HandleBinaryOp(instruction);
4437}
4438
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004439void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004440 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004441 LOG(FATAL) << "Unreachable";
4442}
4443
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004444void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004445 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004446 LOG(FATAL) << "Unreachable";
4447}
4448
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004449void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
4450 DCHECK(codegen_->IsBaseline());
4451 LocationSummary* locations =
4452 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4453 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4454}
4455
4456void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4457 DCHECK(codegen_->IsBaseline());
4458 // Will be generated at use site.
4459}
4460
Mark Mendellfe57faa2015-09-18 09:26:15 -04004461// Simple implementation of packed switch - generate cascaded compare/jumps.
4462void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4463 LocationSummary* locations =
4464 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4465 locations->SetInAt(0, Location::RequiresRegister());
4466}
4467
4468void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4469 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004470 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004471 Register value_reg = InputRegisterAt(switch_instr, 0);
4472 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4473
Zheng Xu3927c8b2015-11-18 17:46:25 +08004474 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4475 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4476 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4477 // make sure we don't emit it if the target may run out of range.
4478 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4479 // ranges and emit the tables only as required.
4480 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004481
Zheng Xu3927c8b2015-11-18 17:46:25 +08004482 if (num_entries < kPackedSwitchJumpTableThreshold ||
4483 // Current instruction id is an upper bound of the number of HIRs in the graph.
4484 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4485 // Create a series of compare/jumps.
4486 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
4487 for (uint32_t i = 0; i < num_entries; i++) {
4488 int32_t case_value = lower_bound + i;
4489 vixl::Label* succ = codegen_->GetLabelOf(successors[i]);
4490 if (case_value == 0) {
4491 __ Cbz(value_reg, succ);
4492 } else {
4493 __ Cmp(value_reg, Operand(case_value));
4494 __ B(eq, succ);
4495 }
4496 }
4497
4498 // And the default for any other value.
4499 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4500 __ B(codegen_->GetLabelOf(default_block));
4501 }
4502 } else {
4503 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4504 codegen_->AddJumpTable(jump_table);
4505
4506 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4507
4508 // Below instructions should use at most one blocked register. Since there are two blocked
4509 // registers, we are free to block one.
4510 Register temp_w = temps.AcquireW();
4511 Register index;
4512 // Remove the bias.
4513 if (lower_bound != 0) {
4514 index = temp_w;
4515 __ Sub(index, value_reg, Operand(lower_bound));
4516 } else {
4517 index = value_reg;
4518 }
4519
4520 // Jump to default block if index is out of the range.
4521 __ Cmp(index, Operand(num_entries));
4522 __ B(hs, codegen_->GetLabelOf(default_block));
4523
4524 // In current VIXL implementation, it won't require any blocked registers to encode the
4525 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4526 // register pressure.
4527 Register table_base = temps.AcquireX();
4528 // Load jump offset from the table.
4529 __ Adr(table_base, jump_table->GetTableStartLabel());
4530 Register jump_offset = temp_w;
4531 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4532
4533 // Jump to target block by branching to table_base(pc related) + offset.
4534 Register target_address = table_base;
4535 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4536 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004537 }
4538}
4539
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004540void CodeGeneratorARM64::GenerateReadBarrier(HInstruction* instruction,
4541 Location out,
4542 Location ref,
4543 Location obj,
4544 uint32_t offset,
4545 Location index) {
4546 DCHECK(kEmitCompilerReadBarrier);
4547
4548 // If heap poisoning is enabled, the unpoisoning of the loaded
4549 // reference will be carried out by the runtime within the slow
4550 // path.
4551 //
4552 // Note that `ref` currently does not get unpoisoned (when heap
4553 // poisoning is enabled), which is alright as the `ref` argument is
4554 // not used by the artReadBarrierSlow entry point.
4555 //
4556 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4557 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4558 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4559 AddSlowPath(slow_path);
4560
4561 // TODO: When read barrier has a fast path, add it here.
4562 /* Currently the read barrier call is inserted after the original load.
4563 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
4564 * original load. This load-load ordering is required by the read barrier.
4565 * The fast path/slow path (for Baker's algorithm) should look like:
4566 *
4567 * bool isGray = obj.LockWord & kReadBarrierMask;
4568 * lfence; // load fence or artificial data dependence to prevent load-load reordering
4569 * ref = obj.field; // this is the original load
4570 * if (isGray) {
4571 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
4572 * }
4573 */
4574
4575 __ B(slow_path->GetEntryLabel());
4576 __ Bind(slow_path->GetExitLabel());
4577}
4578
4579void CodeGeneratorARM64::MaybeGenerateReadBarrier(HInstruction* instruction,
4580 Location out,
4581 Location ref,
4582 Location obj,
4583 uint32_t offset,
4584 Location index) {
4585 if (kEmitCompilerReadBarrier) {
4586 // If heap poisoning is enabled, unpoisoning will be taken care of
4587 // by the runtime within the slow path.
4588 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
4589 } else if (kPoisonHeapReferences) {
4590 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4591 }
4592}
4593
4594void CodeGeneratorARM64::GenerateReadBarrierForRoot(HInstruction* instruction,
4595 Location out,
4596 Location root) {
4597 DCHECK(kEmitCompilerReadBarrier);
4598
4599 // Note that GC roots are not affected by heap poisoning, so we do
4600 // not need to do anything special for this here.
4601 SlowPathCodeARM64* slow_path =
4602 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4603 AddSlowPath(slow_path);
4604
4605 // TODO: Implement a fast path for ReadBarrierForRoot, performing
4606 // the following operation (for Baker's algorithm):
4607 //
4608 // if (thread.tls32_.is_gc_marking) {
4609 // root = Mark(root);
4610 // }
4611
4612 __ B(slow_path->GetEntryLabel());
4613 __ Bind(slow_path->GetExitLabel());
4614}
4615
Alexandre Rames67555f72014-11-18 10:55:16 +00004616#undef __
4617#undef QUICK_ENTRY_POINT
4618
Alexandre Rames5319def2014-10-23 10:03:10 +01004619} // namespace arm64
4620} // namespace art