blob: 397b3be6ec46e1a94b2fee1d5f69ec208e19676b [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);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451 Primitive::Type ret_type = instruction_->GetType();
452 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
453 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800454 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
455 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000456 } 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);
491 }
492
Alexandre Rames9931f312015-06-19 14:47:01 +0100493 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
494
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700495 private:
496 HInstruction* const instruction_;
497 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
498};
499
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100500class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
501 public:
502 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
503
504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
505 LocationSummary* locations = instruction_->GetLocations();
506 __ Bind(GetEntryLabel());
507 SaveLiveRegisters(codegen, locations);
508
509 InvokeRuntimeCallingConvention calling_convention;
510 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
511 parallel_move.AddMove(
512 locations->InAt(0),
513 LocationFrom(calling_convention.GetRegisterAt(0)),
514 Primitive::kPrimNot,
515 nullptr);
516 parallel_move.AddMove(
517 locations->InAt(1),
518 LocationFrom(calling_convention.GetRegisterAt(1)),
519 Primitive::kPrimInt,
520 nullptr);
521 parallel_move.AddMove(
522 locations->InAt(2),
523 LocationFrom(calling_convention.GetRegisterAt(2)),
524 Primitive::kPrimNot,
525 nullptr);
526 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
527
528 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
529 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
530 instruction_,
531 instruction_->GetDexPc(),
532 this);
533 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
534 RestoreLiveRegisters(codegen, locations);
535 __ B(GetExitLabel());
536 }
537
538 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
539
540 private:
541 HInstruction* const instruction_;
542
543 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
544};
545
Zheng Xu3927c8b2015-11-18 17:46:25 +0800546void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
547 uint32_t num_entries = switch_instr_->GetNumEntries();
548 DCHECK_GE(num_entries, kPackedSwitchJumpTableThreshold);
549
550 // We are about to use the assembler to place literals directly. Make sure we have enough
551 // underlying code buffer and we have generated the jump table with right size.
552 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
553 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
554
555 __ Bind(&table_start_);
556 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
557 for (uint32_t i = 0; i < num_entries; i++) {
558 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
559 DCHECK(target_label->IsBound());
560 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
561 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
562 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
563 Literal<int32_t> literal(jump_offset);
564 __ place(&literal);
565 }
566}
567
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000568// Slow path generating a read barrier for a heap reference.
569class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
570 public:
571 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
572 Location out,
573 Location ref,
574 Location obj,
575 uint32_t offset,
576 Location index)
577 : instruction_(instruction),
578 out_(out),
579 ref_(ref),
580 obj_(obj),
581 offset_(offset),
582 index_(index) {
583 DCHECK(kEmitCompilerReadBarrier);
584 // If `obj` is equal to `out` or `ref`, it means the initial object
585 // has been overwritten by (or after) the heap object reference load
586 // to be instrumented, e.g.:
587 //
588 // __ Ldr(out, HeapOperand(out, class_offset);
589 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
590 //
591 // In that case, we have lost the information about the original
592 // object, and the emitted read barrier cannot work properly.
593 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
594 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
595 }
596
597 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
598 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
599 LocationSummary* locations = instruction_->GetLocations();
600 Primitive::Type type = Primitive::kPrimNot;
601 DCHECK(locations->CanCall());
602 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
603 DCHECK(!instruction_->IsInvoke() ||
604 (instruction_->IsInvokeStaticOrDirect() &&
605 instruction_->GetLocations()->Intrinsified()));
606
607 __ Bind(GetEntryLabel());
608
609 // Note: In the case of a HArrayGet instruction, when the base
610 // address is a HArm64IntermediateAddress instruction, it does not
611 // point to the array object itself, but to an offset within this
612 // object. However, the read barrier entry point needs the array
613 // object address to be passed as first argument. So we
614 // temporarily set back `obj_` to that address, and restore its
615 // initial value later.
616 if (instruction_->IsArrayGet() &&
617 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()) {
618 if (kIsDebugBuild) {
619 HArm64IntermediateAddress* intermediate_address =
620 instruction_->AsArrayGet()->GetArray()->AsArm64IntermediateAddress();
621 uint32_t intermediate_address_offset =
622 intermediate_address->GetOffset()->AsIntConstant()->GetValueAsUint64();
623 DCHECK_EQ(intermediate_address_offset, offset_);
624 DCHECK_EQ(mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value(), offset_);
625 }
626 Register obj_reg = RegisterFrom(obj_, Primitive::kPrimInt);
627 __ Sub(obj_reg, obj_reg, offset_);
628 }
629
630 SaveLiveRegisters(codegen, locations);
631
632 // We may have to change the index's value, but as `index_` is a
633 // constant member (like other "inputs" of this slow path),
634 // introduce a copy of it, `index`.
635 Location index = index_;
636 if (index_.IsValid()) {
637 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
638 if (instruction_->IsArrayGet()) {
639 // Compute the actual memory offset and store it in `index`.
640 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
641 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
642 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
643 // We are about to change the value of `index_reg` (see the
644 // calls to vixl::MacroAssembler::Lsl and
645 // vixl::MacroAssembler::Mov below), but it has
646 // not been saved by the previous call to
647 // art::SlowPathCode::SaveLiveRegisters, as it is a
648 // callee-save register --
649 // art::SlowPathCode::SaveLiveRegisters does not consider
650 // callee-save registers, as it has been designed with the
651 // assumption that callee-save registers are supposed to be
652 // handled by the called function. So, as a callee-save
653 // register, `index_reg` _would_ eventually be saved onto
654 // the stack, but it would be too late: we would have
655 // changed its value earlier. Therefore, we manually save
656 // it here into another freely available register,
657 // `free_reg`, chosen of course among the caller-save
658 // registers (as a callee-save `free_reg` register would
659 // exhibit the same problem).
660 //
661 // Note we could have requested a temporary register from
662 // the register allocator instead; but we prefer not to, as
663 // this is a slow path, and we know we can find a
664 // caller-save register that is available.
665 Register free_reg = FindAvailableCallerSaveRegister(codegen);
666 __ Mov(free_reg.W(), index_reg);
667 index_reg = free_reg;
668 index = LocationFrom(index_reg);
669 } else {
670 // The initial register stored in `index_` has already been
671 // saved in the call to art::SlowPathCode::SaveLiveRegisters
672 // (as it is not a callee-save register), so we can freely
673 // use it.
674 }
675 // Shifting the index value contained in `index_reg` by the scale
676 // factor (2) cannot overflow in practice, as the runtime is
677 // unable to allocate object arrays with a size larger than
678 // 2^26 - 1 (that is, 2^28 - 4 bytes).
679 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
680 static_assert(
681 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
682 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
683 __ Add(index_reg, index_reg, Operand(offset_));
684 } else {
685 DCHECK(instruction_->IsInvoke());
686 DCHECK(instruction_->GetLocations()->Intrinsified());
687 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
688 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
689 << instruction_->AsInvoke()->GetIntrinsic();
690 DCHECK_EQ(offset_, 0U);
691 DCHECK(index_.IsRegisterPair());
692 // UnsafeGet's offset location is a register pair, the low
693 // part contains the correct offset.
694 index = index_.ToLow();
695 }
696 }
697
698 // We're moving two or three locations to locations that could
699 // overlap, so we need a parallel move resolver.
700 InvokeRuntimeCallingConvention calling_convention;
701 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
702 parallel_move.AddMove(ref_,
703 LocationFrom(calling_convention.GetRegisterAt(0)),
704 type,
705 nullptr);
706 parallel_move.AddMove(obj_,
707 LocationFrom(calling_convention.GetRegisterAt(1)),
708 type,
709 nullptr);
710 if (index.IsValid()) {
711 parallel_move.AddMove(index,
712 LocationFrom(calling_convention.GetRegisterAt(2)),
713 Primitive::kPrimInt,
714 nullptr);
715 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
716 } else {
717 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
718 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
719 }
720 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
721 instruction_,
722 instruction_->GetDexPc(),
723 this);
724 CheckEntrypointTypes<
725 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
726 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
727
728 RestoreLiveRegisters(codegen, locations);
729
730 // Restore the value of `obj_` when it corresponds to a
731 // HArm64IntermediateAddress instruction.
732 if (instruction_->IsArrayGet() &&
733 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()) {
734 if (kIsDebugBuild) {
735 HArm64IntermediateAddress* intermediate_address =
736 instruction_->AsArrayGet()->GetArray()->AsArm64IntermediateAddress();
737 uint32_t intermediate_address_offset =
738 intermediate_address->GetOffset()->AsIntConstant()->GetValueAsUint64();
739 DCHECK_EQ(intermediate_address_offset, offset_);
740 DCHECK_EQ(mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value(), offset_);
741 }
742 Register obj_reg = RegisterFrom(obj_, Primitive::kPrimInt);
743 __ Add(obj_reg, obj_reg, offset_);
744 }
745
746 __ B(GetExitLabel());
747 }
748
749 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
750
751 private:
752 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
753 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
754 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
755 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
756 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
757 return Register(VIXLRegCodeFromART(i), kXRegSize);
758 }
759 }
760 // We shall never fail to find a free caller-save register, as
761 // there are more than two core caller-save registers on ARM64
762 // (meaning it is possible to find one which is different from
763 // `ref` and `obj`).
764 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
765 LOG(FATAL) << "Could not find a free register";
766 UNREACHABLE();
767 }
768
769 HInstruction* const instruction_;
770 const Location out_;
771 const Location ref_;
772 const Location obj_;
773 const uint32_t offset_;
774 // An additional location containing an index to an array.
775 // Only used for HArrayGet and the UnsafeGetObject &
776 // UnsafeGetObjectVolatile intrinsics.
777 const Location index_;
778
779 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
780};
781
782// Slow path generating a read barrier for a GC root.
783class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
784 public:
785 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
786 : instruction_(instruction), out_(out), root_(root) {}
787
788 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
789 LocationSummary* locations = instruction_->GetLocations();
790 Primitive::Type type = Primitive::kPrimNot;
791 DCHECK(locations->CanCall());
792 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
793 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
794
795 __ Bind(GetEntryLabel());
796 SaveLiveRegisters(codegen, locations);
797
798 InvokeRuntimeCallingConvention calling_convention;
799 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
800 // The argument of the ReadBarrierForRootSlow is not a managed
801 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
802 // thus we need a 64-bit move here, and we cannot use
803 //
804 // arm64_codegen->MoveLocation(
805 // LocationFrom(calling_convention.GetRegisterAt(0)),
806 // root_,
807 // type);
808 //
809 // which would emit a 32-bit move, as `type` is a (32-bit wide)
810 // reference type (`Primitive::kPrimNot`).
811 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
812 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
813 instruction_,
814 instruction_->GetDexPc(),
815 this);
816 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
817 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
818
819 RestoreLiveRegisters(codegen, locations);
820 __ B(GetExitLabel());
821 }
822
823 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
824
825 private:
826 HInstruction* const instruction_;
827 const Location out_;
828 const Location root_;
829
830 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
831};
832
Alexandre Rames5319def2014-10-23 10:03:10 +0100833#undef __
834
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100835Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100836 Location next_location;
837 if (type == Primitive::kPrimVoid) {
838 LOG(FATAL) << "Unreachable type " << type;
839 }
840
Alexandre Rames542361f2015-01-29 16:57:31 +0000841 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100842 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
843 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000844 } else if (!Primitive::IsFloatingPointType(type) &&
845 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000846 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
847 } else {
848 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000849 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
850 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100851 }
852
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000853 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000854 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100855 return next_location;
856}
857
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100858Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100859 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100860}
861
Serban Constantinescu579885a2015-02-22 20:51:33 +0000862CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
863 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100864 const CompilerOptions& compiler_options,
865 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100866 : CodeGenerator(graph,
867 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000868 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000869 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000870 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000871 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100872 compiler_options,
873 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100874 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800875 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100876 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000877 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000878 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000879 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100880 uint64_literals_(std::less<uint64_t>(),
881 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
882 method_patches_(MethodReferenceComparator(),
883 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
884 call_patches_(MethodReferenceComparator(),
885 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
886 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000887 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000888 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000889 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000890}
Alexandre Rames5319def2014-10-23 10:03:10 +0100891
Alexandre Rames67555f72014-11-18 10:55:16 +0000892#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100893
Zheng Xu3927c8b2015-11-18 17:46:25 +0800894void CodeGeneratorARM64::EmitJumpTables() {
895 for (auto jump_table : jump_tables_) {
896 jump_table->EmitTable(this);
897 }
898}
899
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000900void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800901 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000902 // Ensure we emit the literal pool.
903 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000904
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000905 CodeGenerator::Finalize(allocator);
906}
907
Zheng Xuad4450e2015-04-17 18:48:56 +0800908void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
909 // Note: There are 6 kinds of moves:
910 // 1. constant -> GPR/FPR (non-cycle)
911 // 2. constant -> stack (non-cycle)
912 // 3. GPR/FPR -> GPR/FPR
913 // 4. GPR/FPR -> stack
914 // 5. stack -> GPR/FPR
915 // 6. stack -> stack (non-cycle)
916 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
917 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
918 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
919 // dependency.
920 vixl_temps_.Open(GetVIXLAssembler());
921}
922
923void ParallelMoveResolverARM64::FinishEmitNativeCode() {
924 vixl_temps_.Close();
925}
926
927Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
928 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
929 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
930 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
931 Location scratch = GetScratchLocation(kind);
932 if (!scratch.Equals(Location::NoLocation())) {
933 return scratch;
934 }
935 // Allocate from VIXL temp registers.
936 if (kind == Location::kRegister) {
937 scratch = LocationFrom(vixl_temps_.AcquireX());
938 } else {
939 DCHECK(kind == Location::kFpuRegister);
940 scratch = LocationFrom(vixl_temps_.AcquireD());
941 }
942 AddScratchLocation(scratch);
943 return scratch;
944}
945
946void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
947 if (loc.IsRegister()) {
948 vixl_temps_.Release(XRegisterFrom(loc));
949 } else {
950 DCHECK(loc.IsFpuRegister());
951 vixl_temps_.Release(DRegisterFrom(loc));
952 }
953 RemoveScratchLocation(loc);
954}
955
Alexandre Rames3e69f162014-12-10 10:36:50 +0000956void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100957 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100958 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000959}
960
Alexandre Rames5319def2014-10-23 10:03:10 +0100961void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100962 MacroAssembler* masm = GetVIXLAssembler();
963 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000964 __ Bind(&frame_entry_label_);
965
Serban Constantinescu02164b32014-11-13 14:05:07 +0000966 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
967 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100968 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000969 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000970 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000971 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000972 __ Ldr(wzr, MemOperand(temp, 0));
973 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000974 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100975
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000976 if (!HasEmptyFrame()) {
977 int frame_size = GetFrameSize();
978 // Stack layout:
979 // sp[frame_size - 8] : lr.
980 // ... : other preserved core registers.
981 // ... : other preserved fp registers.
982 // ... : reserved frame space.
983 // sp[0] : current method.
984 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100985 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800986 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
987 frame_size - GetCoreSpillSize());
988 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
989 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000990 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100991}
992
993void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100994 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100995 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000996 if (!HasEmptyFrame()) {
997 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800998 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
999 frame_size - FrameEntrySpillSize());
1000 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1001 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001002 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001003 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001004 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001005 __ Ret();
1006 GetAssembler()->cfi().RestoreState();
1007 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001008}
1009
Zheng Xuda403092015-04-24 17:35:39 +08001010vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1011 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1012 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1013 core_spill_mask_);
1014}
1015
1016vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1017 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1018 GetNumberOfFloatingPointRegisters()));
1019 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1020 fpu_spill_mask_);
1021}
1022
Alexandre Rames5319def2014-10-23 10:03:10 +01001023void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1024 __ Bind(GetLabelOf(block));
1025}
1026
Alexandre Rames5319def2014-10-23 10:03:10 +01001027void CodeGeneratorARM64::Move(HInstruction* instruction,
1028 Location location,
1029 HInstruction* move_for) {
1030 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001031 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001032 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +01001033
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +01001034 if (instruction->IsFakeString()) {
1035 // The fake string is an alias for null.
1036 DCHECK(IsBaseline());
1037 instruction = locations->Out().GetConstant();
1038 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
1039 }
1040
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001041 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001042 MoveLocation(location,
1043 Location::DoubleStackSlot(kCurrentMethodStackOffset),
1044 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001045 } else if (locations != nullptr && locations->Out().Equals(location)) {
1046 return;
1047 } else if (instruction->IsIntConstant()
1048 || instruction->IsLongConstant()
1049 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001050 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +01001051 if (location.IsRegister()) {
1052 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001053 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +01001054 (instruction->IsLongConstant() && dst.Is64Bits()));
1055 __ Mov(dst, value);
1056 } else {
1057 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001058 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001059 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
1060 ? temps.AcquireW()
1061 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +01001062 __ Mov(temp, value);
1063 __ Str(temp, StackOperandFrom(location));
1064 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001065 } else if (instruction->IsTemporary()) {
1066 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001067 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001068 } else if (instruction->IsLoadLocal()) {
1069 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +00001070 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001071 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001072 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001073 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001074 }
1075
1076 } else {
1077 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001078 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001079 }
1080}
1081
Calin Juravle175dc732015-08-25 15:42:32 +01001082void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1083 DCHECK(location.IsRegister());
1084 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1085}
1086
Calin Juravlee460d1d2015-09-29 04:52:17 +01001087void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1088 if (location.IsRegister()) {
1089 locations->AddTemp(location);
1090 } else {
1091 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1092 }
1093}
1094
Alexandre Rames5319def2014-10-23 10:03:10 +01001095Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1096 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001097
Alexandre Rames5319def2014-10-23 10:03:10 +01001098 switch (type) {
1099 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001100 case Primitive::kPrimInt:
1101 case Primitive::kPrimFloat:
1102 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1103
1104 case Primitive::kPrimLong:
1105 case Primitive::kPrimDouble:
1106 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1107
Alexandre Rames5319def2014-10-23 10:03:10 +01001108 case Primitive::kPrimBoolean:
1109 case Primitive::kPrimByte:
1110 case Primitive::kPrimChar:
1111 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001112 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001113 LOG(FATAL) << "Unexpected type " << type;
1114 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001115
Alexandre Rames5319def2014-10-23 10:03:10 +01001116 LOG(FATAL) << "Unreachable";
1117 return Location::NoLocation();
1118}
1119
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001120void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001121 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001123 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001124 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001125 if (value_can_be_null) {
1126 __ Cbz(value, &done);
1127 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001128 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1129 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001130 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001131 if (value_can_be_null) {
1132 __ Bind(&done);
1133 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001134}
1135
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001136void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
1137 // Blocked core registers:
1138 // lr : Runtime reserved.
1139 // tr : Runtime reserved.
1140 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1141 // ip1 : VIXL core temp.
1142 // ip0 : VIXL core temp.
1143 //
1144 // Blocked fp registers:
1145 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001146 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1147 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001148 while (!reserved_core_registers.IsEmpty()) {
1149 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1150 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001151
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001152 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001153 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001154 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1155 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001156
1157 if (is_baseline) {
1158 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
1159 while (!reserved_core_baseline_registers.IsEmpty()) {
1160 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
1161 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001162 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001163
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001164 if (is_baseline || GetGraph()->IsDebuggable()) {
1165 // Stubs do not save callee-save floating point registers. If the graph
1166 // is debuggable, we need to deal with these registers differently. For
1167 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001168 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
1169 while (!reserved_fp_baseline_registers.IsEmpty()) {
1170 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
1171 }
1172 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001173}
1174
1175Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
1176 if (type == Primitive::kPrimVoid) {
1177 LOG(FATAL) << "Unreachable type " << type;
1178 }
1179
Alexandre Rames542361f2015-01-29 16:57:31 +00001180 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001181 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
1182 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001183 return Location::FpuRegisterLocation(reg);
1184 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001185 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
1186 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001187 return Location::RegisterLocation(reg);
1188 }
1189}
1190
Alexandre Rames3e69f162014-12-10 10:36:50 +00001191size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1192 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1193 __ Str(reg, MemOperand(sp, stack_index));
1194 return kArm64WordSize;
1195}
1196
1197size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1198 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1199 __ Ldr(reg, MemOperand(sp, stack_index));
1200 return kArm64WordSize;
1201}
1202
1203size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1204 FPRegister reg = FPRegister(reg_id, kDRegSize);
1205 __ Str(reg, MemOperand(sp, stack_index));
1206 return kArm64WordSize;
1207}
1208
1209size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1210 FPRegister reg = FPRegister(reg_id, kDRegSize);
1211 __ Ldr(reg, MemOperand(sp, stack_index));
1212 return kArm64WordSize;
1213}
1214
Alexandre Rames5319def2014-10-23 10:03:10 +01001215void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001216 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001217}
1218
1219void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001220 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001221}
1222
Alexandre Rames67555f72014-11-18 10:55:16 +00001223void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001224 if (constant->IsIntConstant()) {
1225 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1226 } else if (constant->IsLongConstant()) {
1227 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1228 } else if (constant->IsNullConstant()) {
1229 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001230 } else if (constant->IsFloatConstant()) {
1231 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1232 } else {
1233 DCHECK(constant->IsDoubleConstant());
1234 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1235 }
1236}
1237
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238
1239static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1240 DCHECK(constant.IsConstant());
1241 HConstant* cst = constant.GetConstant();
1242 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001243 // Null is mapped to a core W register, which we associate with kPrimInt.
1244 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1246 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1247 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1248}
1249
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250void CodeGeneratorARM64::MoveLocation(Location destination,
1251 Location source,
1252 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001253 if (source.Equals(destination)) {
1254 return;
1255 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001256
1257 // A valid move can always be inferred from the destination and source
1258 // locations. When moving from and to a register, the argument type can be
1259 // used to generate 32bit instead of 64bit moves. In debug mode we also
1260 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001262
1263 if (destination.IsRegister() || destination.IsFpuRegister()) {
1264 if (unspecified_type) {
1265 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1266 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001267 (src_cst != nullptr && (src_cst->IsIntConstant()
1268 || src_cst->IsFloatConstant()
1269 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001270 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001272 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001273 // If the source is a double stack slot or a 64bit constant, a 64bit
1274 // type is appropriate. Else the source is a register, and since the
1275 // type has not been specified, we chose a 64bit type to force a 64bit
1276 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001278 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001279 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1281 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1282 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001283 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1284 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1285 __ Ldr(dst, StackOperandFrom(source));
1286 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001288 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001290 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001292 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001293 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001294 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1295 ? Primitive::kPrimLong
1296 : Primitive::kPrimInt;
1297 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1298 }
1299 } else {
1300 DCHECK(source.IsFpuRegister());
1301 if (destination.IsRegister()) {
1302 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1303 ? Primitive::kPrimDouble
1304 : Primitive::kPrimFloat;
1305 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1306 } else {
1307 DCHECK(destination.IsFpuRegister());
1308 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001309 }
1310 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001311 } else { // The destination is not a register. It must be a stack slot.
1312 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1313 if (source.IsRegister() || source.IsFpuRegister()) {
1314 if (unspecified_type) {
1315 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001316 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001317 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001318 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001319 }
1320 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001321 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1322 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1323 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001324 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001325 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1326 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001327 UseScratchRegisterScope temps(GetVIXLAssembler());
1328 HConstant* src_cst = source.GetConstant();
1329 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001330 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001331 temp = temps.AcquireW();
1332 } else if (src_cst->IsLongConstant()) {
1333 temp = temps.AcquireX();
1334 } else if (src_cst->IsFloatConstant()) {
1335 temp = temps.AcquireS();
1336 } else {
1337 DCHECK(src_cst->IsDoubleConstant());
1338 temp = temps.AcquireD();
1339 }
1340 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001341 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001342 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001343 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001344 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001345 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001346 // There is generally less pressure on FP registers.
1347 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001348 __ Ldr(temp, StackOperandFrom(source));
1349 __ Str(temp, StackOperandFrom(destination));
1350 }
1351 }
1352}
1353
1354void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001355 CPURegister dst,
1356 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001357 switch (type) {
1358 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001359 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001360 break;
1361 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001362 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001363 break;
1364 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001365 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001366 break;
1367 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001368 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001369 break;
1370 case Primitive::kPrimInt:
1371 case Primitive::kPrimNot:
1372 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001373 case Primitive::kPrimFloat:
1374 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001375 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001376 __ Ldr(dst, src);
1377 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001378 case Primitive::kPrimVoid:
1379 LOG(FATAL) << "Unreachable type " << type;
1380 }
1381}
1382
Calin Juravle77520bc2015-01-12 18:45:46 +00001383void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 CPURegister dst,
1385 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001386 MacroAssembler* masm = GetVIXLAssembler();
1387 BlockPoolsScope block_pools(masm);
1388 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001389 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001390 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001392 DCHECK(!src.IsPreIndex());
1393 DCHECK(!src.IsPostIndex());
1394
1395 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001396 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001397 MemOperand base = MemOperand(temp_base);
1398 switch (type) {
1399 case Primitive::kPrimBoolean:
1400 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001401 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 break;
1403 case Primitive::kPrimByte:
1404 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001405 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1407 break;
1408 case Primitive::kPrimChar:
1409 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001410 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411 break;
1412 case Primitive::kPrimShort:
1413 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001414 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001415 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1416 break;
1417 case Primitive::kPrimInt:
1418 case Primitive::kPrimNot:
1419 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001420 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001422 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001423 break;
1424 case Primitive::kPrimFloat:
1425 case Primitive::kPrimDouble: {
1426 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001427 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001428
1429 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1430 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001431 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001432 __ Fmov(FPRegister(dst), temp);
1433 break;
1434 }
1435 case Primitive::kPrimVoid:
1436 LOG(FATAL) << "Unreachable type " << type;
1437 }
1438}
1439
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001440void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001441 CPURegister src,
1442 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001443 switch (type) {
1444 case Primitive::kPrimBoolean:
1445 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001446 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001447 break;
1448 case Primitive::kPrimChar:
1449 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001450 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001451 break;
1452 case Primitive::kPrimInt:
1453 case Primitive::kPrimNot:
1454 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001455 case Primitive::kPrimFloat:
1456 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001457 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001458 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001459 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001460 case Primitive::kPrimVoid:
1461 LOG(FATAL) << "Unreachable type " << type;
1462 }
1463}
1464
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001465void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1466 CPURegister src,
1467 const MemOperand& dst) {
1468 UseScratchRegisterScope temps(GetVIXLAssembler());
1469 Register temp_base = temps.AcquireX();
1470
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001471 DCHECK(!dst.IsPreIndex());
1472 DCHECK(!dst.IsPostIndex());
1473
1474 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001475 Operand op = OperandFromMemOperand(dst);
1476 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001477 MemOperand base = MemOperand(temp_base);
1478 switch (type) {
1479 case Primitive::kPrimBoolean:
1480 case Primitive::kPrimByte:
1481 __ Stlrb(Register(src), base);
1482 break;
1483 case Primitive::kPrimChar:
1484 case Primitive::kPrimShort:
1485 __ Stlrh(Register(src), base);
1486 break;
1487 case Primitive::kPrimInt:
1488 case Primitive::kPrimNot:
1489 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001490 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001491 __ Stlr(Register(src), base);
1492 break;
1493 case Primitive::kPrimFloat:
1494 case Primitive::kPrimDouble: {
1495 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001496 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001497
1498 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1499 __ Fmov(temp, FPRegister(src));
1500 __ Stlr(temp, base);
1501 break;
1502 }
1503 case Primitive::kPrimVoid:
1504 LOG(FATAL) << "Unreachable type " << type;
1505 }
1506}
1507
Calin Juravle175dc732015-08-25 15:42:32 +01001508void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1509 HInstruction* instruction,
1510 uint32_t dex_pc,
1511 SlowPathCode* slow_path) {
1512 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1513 instruction,
1514 dex_pc,
1515 slow_path);
1516}
1517
Alexandre Rames67555f72014-11-18 10:55:16 +00001518void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1519 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001520 uint32_t dex_pc,
1521 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001522 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001523 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001524 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1525 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001526 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001527}
1528
1529void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1530 vixl::Register class_reg) {
1531 UseScratchRegisterScope temps(GetVIXLAssembler());
1532 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001533 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001534 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001535
Serban Constantinescu02164b32014-11-13 14:05:07 +00001536 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001537 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001538 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1539 __ Add(temp, class_reg, status_offset);
1540 __ Ldar(temp, HeapOperand(temp));
1541 __ Cmp(temp, mirror::Class::kStatusInitialized);
1542 __ B(lt, slow_path->GetEntryLabel());
1543 } else {
1544 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1545 __ Cmp(temp, mirror::Class::kStatusInitialized);
1546 __ B(lt, slow_path->GetEntryLabel());
1547 __ Dmb(InnerShareable, BarrierReads);
1548 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001549 __ Bind(slow_path->GetExitLabel());
1550}
Alexandre Rames5319def2014-10-23 10:03:10 +01001551
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001552void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1553 BarrierType type = BarrierAll;
1554
1555 switch (kind) {
1556 case MemBarrierKind::kAnyAny:
1557 case MemBarrierKind::kAnyStore: {
1558 type = BarrierAll;
1559 break;
1560 }
1561 case MemBarrierKind::kLoadAny: {
1562 type = BarrierReads;
1563 break;
1564 }
1565 case MemBarrierKind::kStoreStore: {
1566 type = BarrierWrites;
1567 break;
1568 }
1569 default:
1570 LOG(FATAL) << "Unexpected memory barrier " << kind;
1571 }
1572 __ Dmb(InnerShareable, type);
1573}
1574
Serban Constantinescu02164b32014-11-13 14:05:07 +00001575void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1576 HBasicBlock* successor) {
1577 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001578 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1579 if (slow_path == nullptr) {
1580 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1581 instruction->SetSlowPath(slow_path);
1582 codegen_->AddSlowPath(slow_path);
1583 if (successor != nullptr) {
1584 DCHECK(successor->IsLoopHeader());
1585 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1586 }
1587 } else {
1588 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1589 }
1590
Serban Constantinescu02164b32014-11-13 14:05:07 +00001591 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1592 Register temp = temps.AcquireW();
1593
1594 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1595 if (successor == nullptr) {
1596 __ Cbnz(temp, slow_path->GetEntryLabel());
1597 __ Bind(slow_path->GetReturnLabel());
1598 } else {
1599 __ Cbz(temp, codegen_->GetLabelOf(successor));
1600 __ B(slow_path->GetEntryLabel());
1601 // slow_path will return to GetLabelOf(successor).
1602 }
1603}
1604
Alexandre Rames5319def2014-10-23 10:03:10 +01001605InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1606 CodeGeneratorARM64* codegen)
1607 : HGraphVisitor(graph),
1608 assembler_(codegen->GetAssembler()),
1609 codegen_(codegen) {}
1610
1611#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001612 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001613
1614#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1615
1616enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001617 // Using a base helps identify when we hit such breakpoints.
1618 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001619#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1620 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1621#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1622};
1623
1624#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001625 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001626 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1627 } \
1628 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1630 locations->SetOut(Location::Any()); \
1631 }
1632 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1633#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1634
1635#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001636#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001637
Alexandre Rames67555f72014-11-18 10:55:16 +00001638void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001639 DCHECK_EQ(instr->InputCount(), 2U);
1640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1641 Primitive::Type type = instr->GetResultType();
1642 switch (type) {
1643 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001644 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001646 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001648 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001649
1650 case Primitive::kPrimFloat:
1651 case Primitive::kPrimDouble:
1652 locations->SetInAt(0, Location::RequiresFpuRegister());
1653 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001654 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001655 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001656
Alexandre Rames5319def2014-10-23 10:03:10 +01001657 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001658 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001659 }
1660}
1661
Alexandre Rames09a99962015-04-15 11:47:56 +01001662void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001663 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1664
1665 bool object_field_get_with_read_barrier =
1666 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001667 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001668 new (GetGraph()->GetArena()) LocationSummary(instruction,
1669 object_field_get_with_read_barrier ?
1670 LocationSummary::kCallOnSlowPath :
1671 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001672 locations->SetInAt(0, Location::RequiresRegister());
1673 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1674 locations->SetOut(Location::RequiresFpuRegister());
1675 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001676 // The output overlaps for an object field get when read barriers
1677 // are enabled: we do not want the load to overwrite the object's
1678 // location, as we need it to emit the read barrier.
1679 locations->SetOut(
1680 Location::RequiresRegister(),
1681 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001682 }
1683}
1684
1685void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1686 const FieldInfo& field_info) {
1687 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001688 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001689 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001690
1691 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1692 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1693
1694 if (field_info.IsVolatile()) {
1695 if (use_acquire_release) {
1696 // NB: LoadAcquire will record the pc info if needed.
1697 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1698 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001699 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001700 codegen_->MaybeRecordImplicitNullCheck(instruction);
1701 // For IRIW sequential consistency kLoadAny is not sufficient.
1702 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1703 }
1704 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001705 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001706 codegen_->MaybeRecordImplicitNullCheck(instruction);
1707 }
Roland Levillain4d027112015-07-01 15:41:14 +01001708
1709 if (field_type == Primitive::kPrimNot) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001710 LocationSummary* locations = instruction->GetLocations();
1711 Location base = locations->InAt(0);
1712 Location out = locations->Out();
1713 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
1714 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01001715 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001716}
1717
1718void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1719 LocationSummary* locations =
1720 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1721 locations->SetInAt(0, Location::RequiresRegister());
1722 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1723 locations->SetInAt(1, Location::RequiresFpuRegister());
1724 } else {
1725 locations->SetInAt(1, Location::RequiresRegister());
1726 }
1727}
1728
1729void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001730 const FieldInfo& field_info,
1731 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001732 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001733 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001734
1735 Register obj = InputRegisterAt(instruction, 0);
1736 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001737 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001738 Offset offset = field_info.GetFieldOffset();
1739 Primitive::Type field_type = field_info.GetFieldType();
1740 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1741
Roland Levillain4d027112015-07-01 15:41:14 +01001742 {
1743 // We use a block to end the scratch scope before the write barrier, thus
1744 // freeing the temporary registers so they can be used in `MarkGCCard`.
1745 UseScratchRegisterScope temps(GetVIXLAssembler());
1746
1747 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1748 DCHECK(value.IsW());
1749 Register temp = temps.AcquireW();
1750 __ Mov(temp, value.W());
1751 GetAssembler()->PoisonHeapReference(temp.W());
1752 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001753 }
Roland Levillain4d027112015-07-01 15:41:14 +01001754
1755 if (field_info.IsVolatile()) {
1756 if (use_acquire_release) {
1757 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1758 codegen_->MaybeRecordImplicitNullCheck(instruction);
1759 } else {
1760 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1761 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1762 codegen_->MaybeRecordImplicitNullCheck(instruction);
1763 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1764 }
1765 } else {
1766 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1767 codegen_->MaybeRecordImplicitNullCheck(instruction);
1768 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001769 }
1770
1771 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001772 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001773 }
1774}
1775
Alexandre Rames67555f72014-11-18 10:55:16 +00001776void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001777 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001778
1779 switch (type) {
1780 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001781 case Primitive::kPrimLong: {
1782 Register dst = OutputRegister(instr);
1783 Register lhs = InputRegisterAt(instr, 0);
1784 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001785 if (instr->IsAdd()) {
1786 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001787 } else if (instr->IsAnd()) {
1788 __ And(dst, lhs, rhs);
1789 } else if (instr->IsOr()) {
1790 __ Orr(dst, lhs, rhs);
1791 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001792 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001793 } else {
1794 DCHECK(instr->IsXor());
1795 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001796 }
1797 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001798 }
1799 case Primitive::kPrimFloat:
1800 case Primitive::kPrimDouble: {
1801 FPRegister dst = OutputFPRegister(instr);
1802 FPRegister lhs = InputFPRegisterAt(instr, 0);
1803 FPRegister rhs = InputFPRegisterAt(instr, 1);
1804 if (instr->IsAdd()) {
1805 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001806 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001807 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001808 } else {
1809 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001810 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001811 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001812 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001813 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001814 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001815 }
1816}
1817
Serban Constantinescu02164b32014-11-13 14:05:07 +00001818void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1819 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1820
1821 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1822 Primitive::Type type = instr->GetResultType();
1823 switch (type) {
1824 case Primitive::kPrimInt:
1825 case Primitive::kPrimLong: {
1826 locations->SetInAt(0, Location::RequiresRegister());
1827 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1828 locations->SetOut(Location::RequiresRegister());
1829 break;
1830 }
1831 default:
1832 LOG(FATAL) << "Unexpected shift type " << type;
1833 }
1834}
1835
1836void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1837 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1838
1839 Primitive::Type type = instr->GetType();
1840 switch (type) {
1841 case Primitive::kPrimInt:
1842 case Primitive::kPrimLong: {
1843 Register dst = OutputRegister(instr);
1844 Register lhs = InputRegisterAt(instr, 0);
1845 Operand rhs = InputOperandAt(instr, 1);
1846 if (rhs.IsImmediate()) {
1847 uint32_t shift_value = (type == Primitive::kPrimInt)
1848 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1849 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1850 if (instr->IsShl()) {
1851 __ Lsl(dst, lhs, shift_value);
1852 } else if (instr->IsShr()) {
1853 __ Asr(dst, lhs, shift_value);
1854 } else {
1855 __ Lsr(dst, lhs, shift_value);
1856 }
1857 } else {
1858 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1859
1860 if (instr->IsShl()) {
1861 __ Lsl(dst, lhs, rhs_reg);
1862 } else if (instr->IsShr()) {
1863 __ Asr(dst, lhs, rhs_reg);
1864 } else {
1865 __ Lsr(dst, lhs, rhs_reg);
1866 }
1867 }
1868 break;
1869 }
1870 default:
1871 LOG(FATAL) << "Unexpected shift operation type " << type;
1872 }
1873}
1874
Alexandre Rames5319def2014-10-23 10:03:10 +01001875void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001876 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001877}
1878
1879void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001880 HandleBinaryOp(instruction);
1881}
1882
1883void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1884 HandleBinaryOp(instruction);
1885}
1886
1887void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1888 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001889}
1890
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001891void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
1892 LocationSummary* locations =
1893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1894 locations->SetInAt(0, Location::RequiresRegister());
1895 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1896 locations->SetOut(Location::RequiresRegister());
1897}
1898
1899void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1900 HArm64IntermediateAddress* instruction) {
1901 __ Add(OutputRegister(instruction),
1902 InputRegisterAt(instruction, 0),
1903 Operand(InputOperandAt(instruction, 1)));
1904}
1905
Alexandre Rames418318f2015-11-20 15:55:47 +00001906void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1907 LocationSummary* locations =
1908 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1909 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1910 Location::RequiresRegister());
1911 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1912 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1914}
1915
1916void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1917 Register res = OutputRegister(instr);
1918 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
1919 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
1920 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
1921
1922 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1923 // This fixup should be carried out for all multiply-accumulate instructions:
1924 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1925 if (instr->GetType() == Primitive::kPrimLong &&
1926 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1927 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
1928 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
1929 if (prev->IsLoadOrStore()) {
1930 // Make sure we emit only exactly one nop.
1931 vixl::CodeBufferCheckScope scope(masm,
1932 vixl::kInstructionSize,
1933 vixl::CodeBufferCheckScope::kCheck,
1934 vixl::CodeBufferCheckScope::kExactSize);
1935 __ nop();
1936 }
1937 }
1938
1939 if (instr->GetOpKind() == HInstruction::kAdd) {
1940 __ Madd(res, mul_left, mul_right, accumulator);
1941 } else {
1942 DCHECK(instr->GetOpKind() == HInstruction::kSub);
1943 __ Msub(res, mul_left, mul_right, accumulator);
1944 }
1945}
1946
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001947void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001948 bool object_array_get_with_read_barrier =
1949 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001950 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001951 new (GetGraph()->GetArena()) LocationSummary(instruction,
1952 object_array_get_with_read_barrier ?
1953 LocationSummary::kCallOnSlowPath :
1954 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001955 locations->SetInAt(0, Location::RequiresRegister());
1956 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001957 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1958 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1959 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001960 // The output overlaps in the case of an object array get with
1961 // read barriers enabled: we do not want the move to overwrite the
1962 // array's location, as we need it to emit the read barrier.
1963 locations->SetOut(
1964 Location::RequiresRegister(),
1965 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001966 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001967}
1968
1969void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001970 Primitive::Type type = instruction->GetType();
1971 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001972 LocationSummary* locations = instruction->GetLocations();
1973 Location index = locations->InAt(1);
1974 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001975 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001976 CPURegister dest = OutputCPURegister(instruction);
1977
Alexandre Ramesd921d642015-04-16 15:07:16 +01001978 MacroAssembler* masm = GetVIXLAssembler();
1979 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001980 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01001981 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001982
1983 if (index.IsConstant()) {
1984 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001985 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001986 } else {
1987 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001988 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1989 // We do not need to compute the intermediate address from the array: the
1990 // input instruction has done it already. See the comment in
1991 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1992 if (kIsDebugBuild) {
1993 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1994 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1995 }
1996 temp = obj;
1997 } else {
1998 __ Add(temp, obj, offset);
1999 }
Alexandre Rames82000b02015-07-07 11:34:16 +01002000 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002001 }
2002
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002003 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00002004 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002005
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002006 if (type == Primitive::kPrimNot) {
2007 static_assert(
2008 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2009 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2010 Location obj_loc = locations->InAt(0);
2011 Location out = locations->Out();
2012 if (index.IsConstant()) {
2013 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
2014 } else {
2015 // Note: when `obj_loc` is a HArm64IntermediateAddress, it does
2016 // not contain the base address of the array object, which is
2017 // needed by the read barrier entry point. So the read barrier
2018 // slow path will temporarily set back `obj_loc` to the right
2019 // address (see ReadBarrierForHeapReferenceSlowPathARM64::EmitNativeCode).
2020 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset, index);
2021 }
Roland Levillain4d027112015-07-01 15:41:14 +01002022 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002023}
2024
Alexandre Rames5319def2014-10-23 10:03:10 +01002025void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2027 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002029}
2030
2031void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002032 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002033 __ Ldr(OutputRegister(instruction),
2034 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002035 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002036}
2037
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002038void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002039 Primitive::Type value_type = instruction->GetComponentType();
2040
2041 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2042 bool object_array_set_with_read_barrier =
2043 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2045 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002046 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2047 LocationSummary::kCallOnSlowPath :
2048 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002049 locations->SetInAt(0, Location::RequiresRegister());
2050 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002051 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002052 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002053 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002054 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002055 }
2056}
2057
2058void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2059 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002060 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002061 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002062 bool needs_write_barrier =
2063 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002064
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002065 Register array = InputRegisterAt(instruction, 0);
2066 CPURegister value = InputCPURegisterAt(instruction, 2);
2067 CPURegister source = value;
2068 Location index = locations->InAt(1);
2069 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2070 MemOperand destination = HeapOperand(array);
2071 MacroAssembler* masm = GetVIXLAssembler();
2072 BlockPoolsScope block_pools(masm);
2073
2074 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002075 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002076 if (index.IsConstant()) {
2077 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2078 destination = HeapOperand(array, offset);
2079 } else {
2080 UseScratchRegisterScope temps(masm);
2081 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002082 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2083 // We do not need to compute the intermediate address from the array: the
2084 // input instruction has done it already. See the comment in
2085 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2086 if (kIsDebugBuild) {
2087 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2088 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2089 }
2090 temp = array;
2091 } else {
2092 __ Add(temp, array, offset);
2093 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002094 destination = HeapOperand(temp,
2095 XRegisterFrom(index),
2096 LSL,
2097 Primitive::ComponentSizeShift(value_type));
2098 }
2099 codegen_->Store(value_type, value, destination);
2100 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002101 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002102 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002103 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002104 vixl::Label done;
2105 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002106 {
2107 // We use a block to end the scratch scope before the write barrier, thus
2108 // freeing the temporary registers so they can be used in `MarkGCCard`.
2109 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002110 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002111 if (index.IsConstant()) {
2112 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002113 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002114 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002115 destination = HeapOperand(temp,
2116 XRegisterFrom(index),
2117 LSL,
2118 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002119 }
2120
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002121 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2122 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2123 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2124
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002125 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002126 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2127 codegen_->AddSlowPath(slow_path);
2128 if (instruction->GetValueCanBeNull()) {
2129 vixl::Label non_zero;
2130 __ Cbnz(Register(value), &non_zero);
2131 if (!index.IsConstant()) {
2132 __ Add(temp, array, offset);
2133 }
2134 __ Str(wzr, destination);
2135 codegen_->MaybeRecordImplicitNullCheck(instruction);
2136 __ B(&done);
2137 __ Bind(&non_zero);
2138 }
2139
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002140 if (kEmitCompilerReadBarrier) {
2141 // When read barriers are enabled, the type checking
2142 // instrumentation requires two read barriers:
2143 //
2144 // __ Mov(temp2, temp);
2145 // // /* HeapReference<Class> */ temp = temp->component_type_
2146 // __ Ldr(temp, HeapOperand(temp, component_offset));
2147 // codegen_->GenerateReadBarrier(
2148 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2149 //
2150 // // /* HeapReference<Class> */ temp2 = value->klass_
2151 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2152 // codegen_->GenerateReadBarrier(
2153 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2154 //
2155 // __ Cmp(temp, temp2);
2156 //
2157 // However, the second read barrier may trash `temp`, as it
2158 // is a temporary register, and as such would not be saved
2159 // along with live registers before calling the runtime (nor
2160 // restored afterwards). So in this case, we bail out and
2161 // delegate the work to the array set slow path.
2162 //
2163 // TODO: Extend the register allocator to support a new
2164 // "(locally) live temp" location so as to avoid always
2165 // going into the slow path when read barriers are enabled.
2166 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002167 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002168 Register temp2 = temps.AcquireSameSizeAs(array);
2169 // /* HeapReference<Class> */ temp = array->klass_
2170 __ Ldr(temp, HeapOperand(array, class_offset));
2171 codegen_->MaybeRecordImplicitNullCheck(instruction);
2172 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2173
2174 // /* HeapReference<Class> */ temp = temp->component_type_
2175 __ Ldr(temp, HeapOperand(temp, component_offset));
2176 // /* HeapReference<Class> */ temp2 = value->klass_
2177 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2178 // If heap poisoning is enabled, no need to unpoison `temp`
2179 // nor `temp2`, as we are comparing two poisoned references.
2180 __ Cmp(temp, temp2);
2181
2182 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2183 vixl::Label do_put;
2184 __ B(eq, &do_put);
2185 // If heap poisoning is enabled, the `temp` reference has
2186 // not been unpoisoned yet; unpoison it now.
2187 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2188
2189 // /* HeapReference<Class> */ temp = temp->super_class_
2190 __ Ldr(temp, HeapOperand(temp, super_offset));
2191 // If heap poisoning is enabled, no need to unpoison
2192 // `temp`, as we are comparing against null below.
2193 __ Cbnz(temp, slow_path->GetEntryLabel());
2194 __ Bind(&do_put);
2195 } else {
2196 __ B(ne, slow_path->GetEntryLabel());
2197 }
2198 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002199 }
2200 }
2201
2202 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002203 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002204 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002205 __ Mov(temp2, value.W());
2206 GetAssembler()->PoisonHeapReference(temp2);
2207 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002208 }
2209
2210 if (!index.IsConstant()) {
2211 __ Add(temp, array, offset);
2212 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002213 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002214
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002215 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002216 codegen_->MaybeRecordImplicitNullCheck(instruction);
2217 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002218 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002219
2220 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2221
2222 if (done.IsLinked()) {
2223 __ Bind(&done);
2224 }
2225
2226 if (slow_path != nullptr) {
2227 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002228 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002229 }
2230}
2231
Alexandre Rames67555f72014-11-18 10:55:16 +00002232void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002233 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2234 ? LocationSummary::kCallOnSlowPath
2235 : LocationSummary::kNoCall;
2236 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002237 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002238 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002239 if (instruction->HasUses()) {
2240 locations->SetOut(Location::SameAsFirstInput());
2241 }
2242}
2243
2244void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002245 BoundsCheckSlowPathARM64* slow_path =
2246 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002247 codegen_->AddSlowPath(slow_path);
2248
2249 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2250 __ B(slow_path->GetEntryLabel(), hs);
2251}
2252
Alexandre Rames67555f72014-11-18 10:55:16 +00002253void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2254 LocationSummary* locations =
2255 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2256 locations->SetInAt(0, Location::RequiresRegister());
2257 if (check->HasUses()) {
2258 locations->SetOut(Location::SameAsFirstInput());
2259 }
2260}
2261
2262void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2263 // We assume the class is not null.
2264 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2265 check->GetLoadClass(), check, check->GetDexPc(), true);
2266 codegen_->AddSlowPath(slow_path);
2267 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2268}
2269
Roland Levillain7f63c522015-07-13 15:54:55 +00002270static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2271 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2272 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2273}
2274
Serban Constantinescu02164b32014-11-13 14:05:07 +00002275void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002276 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002277 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2278 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002279 switch (in_type) {
2280 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002281 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002282 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2284 break;
2285 }
2286 case Primitive::kPrimFloat:
2287 case Primitive::kPrimDouble: {
2288 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002289 locations->SetInAt(1,
2290 IsFloatingPointZeroConstant(compare->InputAt(1))
2291 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2292 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002293 locations->SetOut(Location::RequiresRegister());
2294 break;
2295 }
2296 default:
2297 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2298 }
2299}
2300
2301void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2302 Primitive::Type in_type = compare->InputAt(0)->GetType();
2303
2304 // 0 if: left == right
2305 // 1 if: left > right
2306 // -1 if: left < right
2307 switch (in_type) {
2308 case Primitive::kPrimLong: {
2309 Register result = OutputRegister(compare);
2310 Register left = InputRegisterAt(compare, 0);
2311 Operand right = InputOperandAt(compare, 1);
2312
2313 __ Cmp(left, right);
2314 __ Cset(result, ne);
2315 __ Cneg(result, result, lt);
2316 break;
2317 }
2318 case Primitive::kPrimFloat:
2319 case Primitive::kPrimDouble: {
2320 Register result = OutputRegister(compare);
2321 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002322 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002323 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2324 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002325 __ Fcmp(left, 0.0);
2326 } else {
2327 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2328 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002329 if (compare->IsGtBias()) {
2330 __ Cset(result, ne);
2331 } else {
2332 __ Csetm(result, ne);
2333 }
2334 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01002335 break;
2336 }
2337 default:
2338 LOG(FATAL) << "Unimplemented compare type " << in_type;
2339 }
2340}
2341
2342void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
2343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002344
2345 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2346 locations->SetInAt(0, Location::RequiresFpuRegister());
2347 locations->SetInAt(1,
2348 IsFloatingPointZeroConstant(instruction->InputAt(1))
2349 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2350 : Location::RequiresFpuRegister());
2351 } else {
2352 // Integer cases.
2353 locations->SetInAt(0, Location::RequiresRegister());
2354 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2355 }
2356
Alexandre Rames5319def2014-10-23 10:03:10 +01002357 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002359 }
2360}
2361
2362void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
2363 if (!instruction->NeedsMaterialization()) {
2364 return;
2365 }
2366
2367 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002368 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002369 IfCondition if_cond = instruction->GetCondition();
2370 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002371
Roland Levillain7f63c522015-07-13 15:54:55 +00002372 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2373 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2374 if (locations->InAt(1).IsConstant()) {
2375 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2376 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2377 __ Fcmp(lhs, 0.0);
2378 } else {
2379 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2380 }
2381 __ Cset(res, arm64_cond);
2382 if (instruction->IsFPConditionTrueIfNaN()) {
2383 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
2384 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
2385 } else if (instruction->IsFPConditionFalseIfNaN()) {
2386 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
2387 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
2388 }
2389 } else {
2390 // Integer cases.
2391 Register lhs = InputRegisterAt(instruction, 0);
2392 Operand rhs = InputOperandAt(instruction, 1);
2393 __ Cmp(lhs, rhs);
2394 __ Cset(res, arm64_cond);
2395 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002396}
2397
2398#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2399 M(Equal) \
2400 M(NotEqual) \
2401 M(LessThan) \
2402 M(LessThanOrEqual) \
2403 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002404 M(GreaterThanOrEqual) \
2405 M(Below) \
2406 M(BelowOrEqual) \
2407 M(Above) \
2408 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002409#define DEFINE_CONDITION_VISITORS(Name) \
2410void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
2411void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
2412FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002413#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002414#undef FOR_EACH_CONDITION_INSTRUCTION
2415
Zheng Xuc6667102015-05-15 16:08:45 +08002416void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2417 DCHECK(instruction->IsDiv() || instruction->IsRem());
2418
2419 LocationSummary* locations = instruction->GetLocations();
2420 Location second = locations->InAt(1);
2421 DCHECK(second.IsConstant());
2422
2423 Register out = OutputRegister(instruction);
2424 Register dividend = InputRegisterAt(instruction, 0);
2425 int64_t imm = Int64FromConstant(second.GetConstant());
2426 DCHECK(imm == 1 || imm == -1);
2427
2428 if (instruction->IsRem()) {
2429 __ Mov(out, 0);
2430 } else {
2431 if (imm == 1) {
2432 __ Mov(out, dividend);
2433 } else {
2434 __ Neg(out, dividend);
2435 }
2436 }
2437}
2438
2439void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2440 DCHECK(instruction->IsDiv() || instruction->IsRem());
2441
2442 LocationSummary* locations = instruction->GetLocations();
2443 Location second = locations->InAt(1);
2444 DCHECK(second.IsConstant());
2445
2446 Register out = OutputRegister(instruction);
2447 Register dividend = InputRegisterAt(instruction, 0);
2448 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01002449 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002450 DCHECK(IsPowerOfTwo(abs_imm));
2451 int ctz_imm = CTZ(abs_imm);
2452
2453 UseScratchRegisterScope temps(GetVIXLAssembler());
2454 Register temp = temps.AcquireSameSizeAs(out);
2455
2456 if (instruction->IsDiv()) {
2457 __ Add(temp, dividend, abs_imm - 1);
2458 __ Cmp(dividend, 0);
2459 __ Csel(out, temp, dividend, lt);
2460 if (imm > 0) {
2461 __ Asr(out, out, ctz_imm);
2462 } else {
2463 __ Neg(out, Operand(out, ASR, ctz_imm));
2464 }
2465 } else {
2466 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2467 __ Asr(temp, dividend, bits - 1);
2468 __ Lsr(temp, temp, bits - ctz_imm);
2469 __ Add(out, dividend, temp);
2470 __ And(out, out, abs_imm - 1);
2471 __ Sub(out, out, temp);
2472 }
2473}
2474
2475void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2476 DCHECK(instruction->IsDiv() || instruction->IsRem());
2477
2478 LocationSummary* locations = instruction->GetLocations();
2479 Location second = locations->InAt(1);
2480 DCHECK(second.IsConstant());
2481
2482 Register out = OutputRegister(instruction);
2483 Register dividend = InputRegisterAt(instruction, 0);
2484 int64_t imm = Int64FromConstant(second.GetConstant());
2485
2486 Primitive::Type type = instruction->GetResultType();
2487 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2488
2489 int64_t magic;
2490 int shift;
2491 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2492
2493 UseScratchRegisterScope temps(GetVIXLAssembler());
2494 Register temp = temps.AcquireSameSizeAs(out);
2495
2496 // temp = get_high(dividend * magic)
2497 __ Mov(temp, magic);
2498 if (type == Primitive::kPrimLong) {
2499 __ Smulh(temp, dividend, temp);
2500 } else {
2501 __ Smull(temp.X(), dividend, temp);
2502 __ Lsr(temp.X(), temp.X(), 32);
2503 }
2504
2505 if (imm > 0 && magic < 0) {
2506 __ Add(temp, temp, dividend);
2507 } else if (imm < 0 && magic > 0) {
2508 __ Sub(temp, temp, dividend);
2509 }
2510
2511 if (shift != 0) {
2512 __ Asr(temp, temp, shift);
2513 }
2514
2515 if (instruction->IsDiv()) {
2516 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2517 } else {
2518 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2519 // TODO: Strength reduction for msub.
2520 Register temp_imm = temps.AcquireSameSizeAs(out);
2521 __ Mov(temp_imm, imm);
2522 __ Msub(out, temp, temp_imm, dividend);
2523 }
2524}
2525
2526void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2527 DCHECK(instruction->IsDiv() || instruction->IsRem());
2528 Primitive::Type type = instruction->GetResultType();
2529 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2530
2531 LocationSummary* locations = instruction->GetLocations();
2532 Register out = OutputRegister(instruction);
2533 Location second = locations->InAt(1);
2534
2535 if (second.IsConstant()) {
2536 int64_t imm = Int64FromConstant(second.GetConstant());
2537
2538 if (imm == 0) {
2539 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2540 } else if (imm == 1 || imm == -1) {
2541 DivRemOneOrMinusOne(instruction);
2542 } else if (IsPowerOfTwo(std::abs(imm))) {
2543 DivRemByPowerOfTwo(instruction);
2544 } else {
2545 DCHECK(imm <= -2 || imm >= 2);
2546 GenerateDivRemWithAnyConstant(instruction);
2547 }
2548 } else {
2549 Register dividend = InputRegisterAt(instruction, 0);
2550 Register divisor = InputRegisterAt(instruction, 1);
2551 if (instruction->IsDiv()) {
2552 __ Sdiv(out, dividend, divisor);
2553 } else {
2554 UseScratchRegisterScope temps(GetVIXLAssembler());
2555 Register temp = temps.AcquireSameSizeAs(out);
2556 __ Sdiv(temp, dividend, divisor);
2557 __ Msub(out, temp, divisor, dividend);
2558 }
2559 }
2560}
2561
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002562void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2565 switch (div->GetResultType()) {
2566 case Primitive::kPrimInt:
2567 case Primitive::kPrimLong:
2568 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002569 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2571 break;
2572
2573 case Primitive::kPrimFloat:
2574 case Primitive::kPrimDouble:
2575 locations->SetInAt(0, Location::RequiresFpuRegister());
2576 locations->SetInAt(1, Location::RequiresFpuRegister());
2577 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2578 break;
2579
2580 default:
2581 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2582 }
2583}
2584
2585void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2586 Primitive::Type type = div->GetResultType();
2587 switch (type) {
2588 case Primitive::kPrimInt:
2589 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002590 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002591 break;
2592
2593 case Primitive::kPrimFloat:
2594 case Primitive::kPrimDouble:
2595 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2596 break;
2597
2598 default:
2599 LOG(FATAL) << "Unexpected div type " << type;
2600 }
2601}
2602
Alexandre Rames67555f72014-11-18 10:55:16 +00002603void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002604 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2605 ? LocationSummary::kCallOnSlowPath
2606 : LocationSummary::kNoCall;
2607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002608 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2609 if (instruction->HasUses()) {
2610 locations->SetOut(Location::SameAsFirstInput());
2611 }
2612}
2613
2614void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2615 SlowPathCodeARM64* slow_path =
2616 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2617 codegen_->AddSlowPath(slow_path);
2618 Location value = instruction->GetLocations()->InAt(0);
2619
Alexandre Rames3e69f162014-12-10 10:36:50 +00002620 Primitive::Type type = instruction->GetType();
2621
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002622 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2623 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002624 return;
2625 }
2626
Alexandre Rames67555f72014-11-18 10:55:16 +00002627 if (value.IsConstant()) {
2628 int64_t divisor = Int64ConstantFrom(value);
2629 if (divisor == 0) {
2630 __ B(slow_path->GetEntryLabel());
2631 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002632 // A division by a non-null constant is valid. We don't need to perform
2633 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002634 }
2635 } else {
2636 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2637 }
2638}
2639
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002640void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2641 LocationSummary* locations =
2642 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2643 locations->SetOut(Location::ConstantLocation(constant));
2644}
2645
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002646void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2647 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002648 // Will be generated at use site.
2649}
2650
Alexandre Rames5319def2014-10-23 10:03:10 +01002651void LocationsBuilderARM64::VisitExit(HExit* exit) {
2652 exit->SetLocations(nullptr);
2653}
2654
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002655void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002656}
2657
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002658void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2659 LocationSummary* locations =
2660 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2661 locations->SetOut(Location::ConstantLocation(constant));
2662}
2663
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002664void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002665 // Will be generated at use site.
2666}
2667
David Brazdilfc6a86a2015-06-26 10:33:45 +00002668void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002669 DCHECK(!successor->IsExitBlock());
2670 HBasicBlock* block = got->GetBlock();
2671 HInstruction* previous = got->GetPrevious();
2672 HLoopInformation* info = block->GetLoopInformation();
2673
David Brazdil46e2a392015-03-16 17:31:52 +00002674 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002675 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2676 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2677 return;
2678 }
2679 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2680 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2681 }
2682 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002683 __ B(codegen_->GetLabelOf(successor));
2684 }
2685}
2686
David Brazdilfc6a86a2015-06-26 10:33:45 +00002687void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2688 got->SetLocations(nullptr);
2689}
2690
2691void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2692 HandleGoto(got, got->GetSuccessor());
2693}
2694
2695void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2696 try_boundary->SetLocations(nullptr);
2697}
2698
2699void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2700 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2701 if (!successor->IsExitBlock()) {
2702 HandleGoto(try_boundary, successor);
2703 }
2704}
2705
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002706void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002707 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002708 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002709 vixl::Label* false_target) {
2710 // FP branching requires both targets to be explicit. If either of the targets
2711 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2712 vixl::Label fallthrough_target;
2713 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002714
David Brazdil0debae72015-11-12 18:37:00 +00002715 if (true_target == nullptr && false_target == nullptr) {
2716 // Nothing to do. The code always falls through.
2717 return;
2718 } else if (cond->IsIntConstant()) {
2719 // Constant condition, statically compared against 1.
2720 if (cond->AsIntConstant()->IsOne()) {
2721 if (true_target != nullptr) {
2722 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002723 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002724 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002725 DCHECK(cond->AsIntConstant()->IsZero());
2726 if (false_target != nullptr) {
2727 __ B(false_target);
2728 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002729 }
David Brazdil0debae72015-11-12 18:37:00 +00002730 return;
2731 }
2732
2733 // The following code generates these patterns:
2734 // (1) true_target == nullptr && false_target != nullptr
2735 // - opposite condition true => branch to false_target
2736 // (2) true_target != nullptr && false_target == nullptr
2737 // - condition true => branch to true_target
2738 // (3) true_target != nullptr && false_target != nullptr
2739 // - condition true => branch to true_target
2740 // - branch to false_target
2741 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002742 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002743 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002744 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002745 if (true_target == nullptr) {
2746 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2747 } else {
2748 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2749 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002750 } else {
2751 // The condition instruction has not been materialized, use its inputs as
2752 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002753 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002754
David Brazdil0debae72015-11-12 18:37:00 +00002755 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002756 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002757 FPRegister lhs = InputFPRegisterAt(condition, 0);
2758 if (condition->GetLocations()->InAt(1).IsConstant()) {
2759 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2760 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2761 __ Fcmp(lhs, 0.0);
2762 } else {
2763 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2764 }
2765 if (condition->IsFPConditionTrueIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002766 __ B(vs, true_target == nullptr ? &fallthrough_target : true_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002767 } else if (condition->IsFPConditionFalseIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002768 __ B(vs, false_target == nullptr ? &fallthrough_target : false_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002769 }
David Brazdil0debae72015-11-12 18:37:00 +00002770 if (true_target == nullptr) {
2771 __ B(ARM64Condition(condition->GetOppositeCondition()), false_target);
2772 } else {
2773 __ B(ARM64Condition(condition->GetCondition()), true_target);
2774 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002775 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002776 // Integer cases.
2777 Register lhs = InputRegisterAt(condition, 0);
2778 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002779
2780 Condition arm64_cond;
2781 vixl::Label* non_fallthrough_target;
2782 if (true_target == nullptr) {
2783 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2784 non_fallthrough_target = false_target;
2785 } else {
2786 arm64_cond = ARM64Condition(condition->GetCondition());
2787 non_fallthrough_target = true_target;
2788 }
2789
Roland Levillain7f63c522015-07-13 15:54:55 +00002790 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2791 switch (arm64_cond) {
2792 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002793 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002794 break;
2795 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002796 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002797 break;
2798 case lt:
2799 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002800 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002801 break;
2802 case ge:
2803 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002804 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002805 break;
2806 default:
2807 // Without the `static_cast` the compiler throws an error for
2808 // `-Werror=sign-promo`.
2809 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2810 }
2811 } else {
2812 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002813 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002814 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002815 }
2816 }
David Brazdil0debae72015-11-12 18:37:00 +00002817
2818 // If neither branch falls through (case 3), the conditional branch to `true_target`
2819 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2820 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002821 __ B(false_target);
2822 }
David Brazdil0debae72015-11-12 18:37:00 +00002823
2824 if (fallthrough_target.IsLinked()) {
2825 __ Bind(&fallthrough_target);
2826 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002827}
2828
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002829void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2830 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002831 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002832 locations->SetInAt(0, Location::RequiresRegister());
2833 }
2834}
2835
2836void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002837 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2838 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2839 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2840 nullptr : codegen_->GetLabelOf(true_successor);
2841 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2842 nullptr : codegen_->GetLabelOf(false_successor);
2843 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002844}
2845
2846void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2847 LocationSummary* locations = new (GetGraph()->GetArena())
2848 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002849 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002850 locations->SetInAt(0, Location::RequiresRegister());
2851 }
2852}
2853
2854void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2855 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2856 DeoptimizationSlowPathARM64(deoptimize);
2857 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002858 GenerateTestAndBranch(deoptimize,
2859 /* condition_input_index */ 0,
2860 slow_path->GetEntryLabel(),
2861 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002862}
2863
Alexandre Rames5319def2014-10-23 10:03:10 +01002864void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002865 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002866}
2867
2868void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002869 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002870}
2871
2872void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002873 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002874}
2875
2876void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002877 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002878}
2879
Alexandre Rames67555f72014-11-18 10:55:16 +00002880void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002881 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002882 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2883 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002884 case TypeCheckKind::kExactCheck:
2885 case TypeCheckKind::kAbstractClassCheck:
2886 case TypeCheckKind::kClassHierarchyCheck:
2887 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002888 call_kind =
2889 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002890 break;
2891 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002892 case TypeCheckKind::kUnresolvedCheck:
2893 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002894 call_kind = LocationSummary::kCallOnSlowPath;
2895 break;
2896 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002897
Alexandre Rames67555f72014-11-18 10:55:16 +00002898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002899 locations->SetInAt(0, Location::RequiresRegister());
2900 locations->SetInAt(1, Location::RequiresRegister());
2901 // The "out" register is used as a temporary, so it overlaps with the inputs.
2902 // Note that TypeCheckSlowPathARM64 uses this register too.
2903 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2904 // When read barriers are enabled, we need a temporary register for
2905 // some cases.
2906 if (kEmitCompilerReadBarrier &&
2907 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2908 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2909 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2910 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002911 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002912}
2913
2914void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2915 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002916 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002917 Register obj = InputRegisterAt(instruction, 0);
2918 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002919 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00002920 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002921 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2922 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2923 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2924 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002925
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002926 vixl::Label done, zero;
2927 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002928
2929 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002930 // Avoid null check if we know `obj` is not null.
2931 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002932 __ Cbz(obj, &zero);
2933 }
2934
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002935 // /* HeapReference<Class> */ out = obj->klass_
2936 __ Ldr(out, HeapOperand(obj.W(), class_offset));
2937 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002938
2939 switch (instruction->GetTypeCheckKind()) {
2940 case TypeCheckKind::kExactCheck: {
2941 __ Cmp(out, cls);
2942 __ Cset(out, eq);
2943 if (zero.IsLinked()) {
2944 __ B(&done);
2945 }
2946 break;
2947 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002948
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002949 case TypeCheckKind::kAbstractClassCheck: {
2950 // If the class is abstract, we eagerly fetch the super class of the
2951 // object to avoid doing a comparison we know will fail.
2952 vixl::Label loop, success;
2953 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002954 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
2955 if (kEmitCompilerReadBarrier) {
2956 // Save the value of `out` into `temp` before overwriting it
2957 // in the following move operation, as we will need it for the
2958 // read barrier below.
2959 Register temp = WRegisterFrom(temp_loc);
2960 __ Mov(temp, out);
2961 }
2962 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002963 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002964 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002965 // If `out` is null, we use it for the result, and jump to `done`.
2966 __ Cbz(out, &done);
2967 __ Cmp(out, cls);
2968 __ B(ne, &loop);
2969 __ Mov(out, 1);
2970 if (zero.IsLinked()) {
2971 __ B(&done);
2972 }
2973 break;
2974 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002975
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002976 case TypeCheckKind::kClassHierarchyCheck: {
2977 // Walk over the class hierarchy to find a match.
2978 vixl::Label loop, success;
2979 __ Bind(&loop);
2980 __ Cmp(out, cls);
2981 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002982 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
2983 if (kEmitCompilerReadBarrier) {
2984 // Save the value of `out` into `temp` before overwriting it
2985 // in the following move operation, as we will need it for the
2986 // read barrier below.
2987 Register temp = WRegisterFrom(temp_loc);
2988 __ Mov(temp, out);
2989 }
2990 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002991 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002992 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002993 __ Cbnz(out, &loop);
2994 // If `out` is null, we use it for the result, and jump to `done`.
2995 __ B(&done);
2996 __ Bind(&success);
2997 __ Mov(out, 1);
2998 if (zero.IsLinked()) {
2999 __ B(&done);
3000 }
3001 break;
3002 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003003
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003004 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003005 // Do an exact check.
3006 vixl::Label exact_check;
3007 __ Cmp(out, cls);
3008 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003009 // Otherwise, we need to check that the object's class is a non-primitive array.
3010 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3011 if (kEmitCompilerReadBarrier) {
3012 // Save the value of `out` into `temp` before overwriting it
3013 // in the following move operation, as we will need it for the
3014 // read barrier below.
3015 Register temp = WRegisterFrom(temp_loc);
3016 __ Mov(temp, out);
3017 }
3018 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003019 __ Ldr(out, HeapOperand(out, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003020 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003021 // If `out` is null, we use it for the result, and jump to `done`.
3022 __ Cbz(out, &done);
3023 __ Ldrh(out, HeapOperand(out, primitive_offset));
3024 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3025 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003026 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003027 __ Mov(out, 1);
3028 __ B(&done);
3029 break;
3030 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003031
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003032 case TypeCheckKind::kArrayCheck: {
3033 __ Cmp(out, cls);
3034 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003035 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3036 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003037 codegen_->AddSlowPath(slow_path);
3038 __ B(ne, slow_path->GetEntryLabel());
3039 __ Mov(out, 1);
3040 if (zero.IsLinked()) {
3041 __ B(&done);
3042 }
3043 break;
3044 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003045
Calin Juravle98893e12015-10-02 21:05:03 +01003046 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003047 case TypeCheckKind::kInterfaceCheck: {
3048 // Note that we indeed only call on slow path, but we always go
3049 // into the slow path for the unresolved and interface check
3050 // cases.
3051 //
3052 // We cannot directly call the InstanceofNonTrivial runtime
3053 // entry point without resorting to a type checking slow path
3054 // here (i.e. by calling InvokeRuntime directly), as it would
3055 // require to assign fixed registers for the inputs of this
3056 // HInstanceOf instruction (following the runtime calling
3057 // convention), which might be cluttered by the potential first
3058 // read barrier emission at the beginning of this method.
3059 DCHECK(locations->OnlyCallsOnSlowPath());
3060 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3061 /* is_fatal */ false);
3062 codegen_->AddSlowPath(slow_path);
3063 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003064 if (zero.IsLinked()) {
3065 __ B(&done);
3066 }
3067 break;
3068 }
3069 }
3070
3071 if (zero.IsLinked()) {
3072 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003073 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003074 }
3075
3076 if (done.IsLinked()) {
3077 __ Bind(&done);
3078 }
3079
3080 if (slow_path != nullptr) {
3081 __ Bind(slow_path->GetExitLabel());
3082 }
3083}
3084
3085void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3086 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3087 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3088
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003089 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3090 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003091 case TypeCheckKind::kExactCheck:
3092 case TypeCheckKind::kAbstractClassCheck:
3093 case TypeCheckKind::kClassHierarchyCheck:
3094 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003095 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3096 LocationSummary::kCallOnSlowPath :
3097 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003098 break;
3099 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003100 case TypeCheckKind::kUnresolvedCheck:
3101 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003102 call_kind = LocationSummary::kCallOnSlowPath;
3103 break;
3104 }
3105
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3107 locations->SetInAt(0, Location::RequiresRegister());
3108 locations->SetInAt(1, Location::RequiresRegister());
3109 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3110 locations->AddTemp(Location::RequiresRegister());
3111 locations->AddTemp(Location::RequiresRegister());
3112 // When read barriers are enabled, we need an additional temporary
3113 // register for some cases.
3114 if (kEmitCompilerReadBarrier &&
3115 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3116 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3117 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3118 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003119 }
3120}
3121
3122void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
3123 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003124 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003125 Register obj = InputRegisterAt(instruction, 0);
3126 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003127 Location temp_loc = locations->GetTemp(0);
3128 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003129 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3130 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3131 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3132 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003133
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003134 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3135 bool is_type_check_slow_path_fatal =
3136 (type_check_kind == TypeCheckKind::kExactCheck ||
3137 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3138 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3139 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3140 !instruction->CanThrowIntoCatchBlock();
3141 SlowPathCodeARM64* type_check_slow_path =
3142 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3143 is_type_check_slow_path_fatal);
3144 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003145
3146 vixl::Label done;
3147 // Avoid null check if we know obj is not null.
3148 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003149 __ Cbz(obj, &done);
3150 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003151
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003152 // /* HeapReference<Class> */ temp = obj->klass_
3153 __ Ldr(temp, HeapOperand(obj, class_offset));
3154 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003155
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003156 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003157 case TypeCheckKind::kExactCheck:
3158 case TypeCheckKind::kArrayCheck: {
3159 __ Cmp(temp, cls);
3160 // Jump to slow path for throwing the exception or doing a
3161 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003162 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003163 break;
3164 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003166 case TypeCheckKind::kAbstractClassCheck: {
3167 // If the class is abstract, we eagerly fetch the super class of the
3168 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003169 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003170 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003171 Location temp2_loc =
3172 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3173 if (kEmitCompilerReadBarrier) {
3174 // Save the value of `temp` into `temp2` before overwriting it
3175 // in the following move operation, as we will need it for the
3176 // read barrier below.
3177 Register temp2 = WRegisterFrom(temp2_loc);
3178 __ Mov(temp2, temp);
3179 }
3180 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003181 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003182 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3183
3184 // If the class reference currently in `temp` is not null, jump
3185 // to the `compare_classes` label to compare it with the checked
3186 // class.
3187 __ Cbnz(temp, &compare_classes);
3188 // Otherwise, jump to the slow path to throw the exception.
3189 //
3190 // But before, move back the object's class into `temp` before
3191 // going into the slow path, as it has been overwritten in the
3192 // meantime.
3193 // /* HeapReference<Class> */ temp = obj->klass_
3194 __ Ldr(temp, HeapOperand(obj, class_offset));
3195 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3196 __ B(type_check_slow_path->GetEntryLabel());
3197
3198 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003199 __ Cmp(temp, cls);
3200 __ B(ne, &loop);
3201 break;
3202 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003204 case TypeCheckKind::kClassHierarchyCheck: {
3205 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003206 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003207 __ Bind(&loop);
3208 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003209 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210
3211 Location temp2_loc =
3212 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3213 if (kEmitCompilerReadBarrier) {
3214 // Save the value of `temp` into `temp2` before overwriting it
3215 // in the following move operation, as we will need it for the
3216 // read barrier below.
3217 Register temp2 = WRegisterFrom(temp2_loc);
3218 __ Mov(temp2, temp);
3219 }
3220 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003221 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003222 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3223
3224 // If the class reference currently in `temp` is not null, jump
3225 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003226 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003227 // Otherwise, jump to the slow path to throw the exception.
3228 //
3229 // But before, move back the object's class into `temp` before
3230 // going into the slow path, as it has been overwritten in the
3231 // meantime.
3232 // /* HeapReference<Class> */ temp = obj->klass_
3233 __ Ldr(temp, HeapOperand(obj, class_offset));
3234 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3235 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003236 break;
3237 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003239 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003240 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003241 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003242 __ Cmp(temp, cls);
3243 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003244
3245 // Otherwise, we need to check that the object's class is a non-primitive array.
3246 Location temp2_loc =
3247 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3248 if (kEmitCompilerReadBarrier) {
3249 // Save the value of `temp` into `temp2` before overwriting it
3250 // in the following move operation, as we will need it for the
3251 // read barrier below.
3252 Register temp2 = WRegisterFrom(temp2_loc);
3253 __ Mov(temp2, temp);
3254 }
3255 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003256 __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003257 codegen_->MaybeGenerateReadBarrier(
3258 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
3259
3260 // If the component type is not null (i.e. the object is indeed
3261 // an array), jump to label `check_non_primitive_component_type`
3262 // to further check that this component type is not a primitive
3263 // type.
3264 __ Cbnz(temp, &check_non_primitive_component_type);
3265 // Otherwise, jump to the slow path to throw the exception.
3266 //
3267 // But before, move back the object's class into `temp` before
3268 // going into the slow path, as it has been overwritten in the
3269 // meantime.
3270 // /* HeapReference<Class> */ temp = obj->klass_
3271 __ Ldr(temp, HeapOperand(obj, class_offset));
3272 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3273 __ B(type_check_slow_path->GetEntryLabel());
3274
3275 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003276 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3277 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003278 __ Cbz(temp, &done);
3279 // Same comment as above regarding `temp` and the slow path.
3280 // /* HeapReference<Class> */ temp = obj->klass_
3281 __ Ldr(temp, HeapOperand(obj, class_offset));
3282 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3283 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003284 break;
3285 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003286
Calin Juravle98893e12015-10-02 21:05:03 +01003287 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003288 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003289 // We always go into the type check slow path for the unresolved
3290 // and interface check cases.
3291 //
3292 // We cannot directly call the CheckCast runtime entry point
3293 // without resorting to a type checking slow path here (i.e. by
3294 // calling InvokeRuntime directly), as it would require to
3295 // assign fixed registers for the inputs of this HInstanceOf
3296 // instruction (following the runtime calling convention), which
3297 // might be cluttered by the potential first read barrier
3298 // emission at the beginning of this method.
3299 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003300 break;
3301 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003302 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003303
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003304 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003305}
3306
Alexandre Rames5319def2014-10-23 10:03:10 +01003307void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3308 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3309 locations->SetOut(Location::ConstantLocation(constant));
3310}
3311
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003312void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003313 // Will be generated at use site.
3314}
3315
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003316void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3318 locations->SetOut(Location::ConstantLocation(constant));
3319}
3320
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003321void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003322 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003323}
3324
Calin Juravle175dc732015-08-25 15:42:32 +01003325void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3326 // The trampoline uses the same calling convention as dex calling conventions,
3327 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3328 // the method_idx.
3329 HandleInvoke(invoke);
3330}
3331
3332void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3333 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3334}
3335
Alexandre Rames5319def2014-10-23 10:03:10 +01003336void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003337 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003338 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003339}
3340
Alexandre Rames67555f72014-11-18 10:55:16 +00003341void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3342 HandleInvoke(invoke);
3343}
3344
3345void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3346 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003347 LocationSummary* locations = invoke->GetLocations();
3348 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003349 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3350 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003351 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003352 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003353 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003354
3355 // The register ip1 is required to be used for the hidden argument in
3356 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003357 MacroAssembler* masm = GetVIXLAssembler();
3358 UseScratchRegisterScope scratch_scope(masm);
3359 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003360 scratch_scope.Exclude(ip1);
3361 __ Mov(ip1, invoke->GetDexMethodIndex());
3362
Alexandre Rames67555f72014-11-18 10:55:16 +00003363 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003364 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003365 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003366 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003367 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003369 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003370 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003371 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003372 // Instead of simply (possibly) unpoisoning `temp` here, we should
3373 // emit a read barrier for the previous class reference load.
3374 // However this is not required in practice, as this is an
3375 // intermediate/temporary reference and because the current
3376 // concurrent copying collector keeps the from-space memory
3377 // intact/accessible until the end of the marking phase (the
3378 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003379 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003380 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003381 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003382 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003383 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003384 // lr();
3385 __ Blr(lr);
3386 DCHECK(!codegen_->IsLeafMethod());
3387 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3388}
3389
3390void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003391 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3392 if (intrinsic.TryDispatch(invoke)) {
3393 return;
3394 }
3395
Alexandre Rames67555f72014-11-18 10:55:16 +00003396 HandleInvoke(invoke);
3397}
3398
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003399void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003400 // When we do not run baseline, explicit clinit checks triggered by static
3401 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3402 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003403
Andreas Gampe878d58c2015-01-15 23:24:00 -08003404 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3405 if (intrinsic.TryDispatch(invoke)) {
3406 return;
3407 }
3408
Alexandre Rames67555f72014-11-18 10:55:16 +00003409 HandleInvoke(invoke);
3410}
3411
Andreas Gampe878d58c2015-01-15 23:24:00 -08003412static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3413 if (invoke->GetLocations()->Intrinsified()) {
3414 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3415 intrinsic.Dispatch(invoke);
3416 return true;
3417 }
3418 return false;
3419}
3420
Vladimir Markodc151b22015-10-15 18:02:30 +01003421HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3422 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3423 MethodReference target_method ATTRIBUTE_UNUSED) {
3424 // On arm64 we support all dispatch types.
3425 return desired_dispatch_info;
3426}
3427
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003428void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003429 // For better instruction scheduling we load the direct code pointer before the method pointer.
3430 bool direct_code_loaded = false;
3431 switch (invoke->GetCodePtrLocation()) {
3432 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3433 // LR = code address from literal pool with link-time patch.
3434 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3435 direct_code_loaded = true;
3436 break;
3437 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3438 // LR = invoke->GetDirectCodePtr();
3439 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3440 direct_code_loaded = true;
3441 break;
3442 default:
3443 break;
3444 }
3445
Andreas Gampe878d58c2015-01-15 23:24:00 -08003446 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003447 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3448 switch (invoke->GetMethodLoadKind()) {
3449 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3450 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003451 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003452 break;
3453 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003454 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003455 break;
3456 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3457 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003458 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003459 break;
3460 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3461 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003462 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003463 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3464 break;
3465 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3466 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003467 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3468 invoke->GetDexCacheArrayOffset());
3469 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003470 {
3471 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003472 __ Bind(pc_insn_label);
3473 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003474 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003475 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003476 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003477 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3478 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003479 {
3480 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3481 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3482 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3483 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3484 }
Vladimir Marko58155012015-08-19 12:49:41 +00003485 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003486 }
Vladimir Marko58155012015-08-19 12:49:41 +00003487 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003488 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003489 Register reg = XRegisterFrom(temp);
3490 Register method_reg;
3491 if (current_method.IsRegister()) {
3492 method_reg = XRegisterFrom(current_method);
3493 } else {
3494 DCHECK(invoke->GetLocations()->Intrinsified());
3495 DCHECK(!current_method.IsValid());
3496 method_reg = reg;
3497 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3498 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003499
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003500 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003501 __ Ldr(reg.X(),
3502 MemOperand(method_reg.X(),
3503 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003504 // temp = temp[index_in_cache];
3505 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3506 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3507 break;
3508 }
3509 }
3510
3511 switch (invoke->GetCodePtrLocation()) {
3512 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3513 __ Bl(&frame_entry_label_);
3514 break;
3515 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3516 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3517 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003518 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3519 __ Bind(label);
3520 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003521 break;
3522 }
3523 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3524 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3525 // LR prepared above for better instruction scheduling.
3526 DCHECK(direct_code_loaded);
3527 // lr()
3528 __ Blr(lr);
3529 break;
3530 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3531 // LR = callee_method->entry_point_from_quick_compiled_code_;
3532 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003533 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003534 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3535 // lr()
3536 __ Blr(lr);
3537 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003538 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003539
Andreas Gampe878d58c2015-01-15 23:24:00 -08003540 DCHECK(!IsLeafMethod());
3541}
3542
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003543void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3544 LocationSummary* locations = invoke->GetLocations();
3545 Location receiver = locations->InAt(0);
3546 Register temp = XRegisterFrom(temp_in);
3547 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3548 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3549 Offset class_offset = mirror::Object::ClassOffset();
3550 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3551
3552 BlockPoolsScope block_pools(GetVIXLAssembler());
3553
3554 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003555 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003556 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
3557 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003558 // Instead of simply (possibly) unpoisoning `temp` here, we should
3559 // emit a read barrier for the previous class reference load.
3560 // However this is not required in practice, as this is an
3561 // intermediate/temporary reference and because the current
3562 // concurrent copying collector keeps the from-space memory
3563 // intact/accessible until the end of the marking phase (the
3564 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003565 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3566 // temp = temp->GetMethodAt(method_offset);
3567 __ Ldr(temp, MemOperand(temp, method_offset));
3568 // lr = temp->GetEntryPoint();
3569 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3570 // lr();
3571 __ Blr(lr);
3572}
3573
Vladimir Marko58155012015-08-19 12:49:41 +00003574void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3575 DCHECK(linker_patches->empty());
3576 size_t size =
3577 method_patches_.size() +
3578 call_patches_.size() +
3579 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003580 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003581 linker_patches->reserve(size);
3582 for (const auto& entry : method_patches_) {
3583 const MethodReference& target_method = entry.first;
3584 vixl::Literal<uint64_t>* literal = entry.second;
3585 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3586 target_method.dex_file,
3587 target_method.dex_method_index));
3588 }
3589 for (const auto& entry : call_patches_) {
3590 const MethodReference& target_method = entry.first;
3591 vixl::Literal<uint64_t>* literal = entry.second;
3592 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3593 target_method.dex_file,
3594 target_method.dex_method_index));
3595 }
3596 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003597 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003598 info.target_method.dex_file,
3599 info.target_method.dex_method_index));
3600 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003601 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003602 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003603 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003604 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003605 info.element_offset));
3606 }
3607}
3608
3609vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3610 // Look up the literal for value.
3611 auto lb = uint64_literals_.lower_bound(value);
3612 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3613 return lb->second;
3614 }
3615 // We don't have a literal for this value, insert a new one.
3616 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3617 uint64_literals_.PutBefore(lb, value, literal);
3618 return literal;
3619}
3620
3621vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3622 MethodReference target_method,
3623 MethodToLiteralMap* map) {
3624 // Look up the literal for target_method.
3625 auto lb = map->lower_bound(target_method);
3626 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3627 return lb->second;
3628 }
3629 // We don't have a literal for this method yet, insert a new one.
3630 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3631 map->PutBefore(lb, target_method, literal);
3632 return literal;
3633}
3634
3635vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3636 MethodReference target_method) {
3637 return DeduplicateMethodLiteral(target_method, &method_patches_);
3638}
3639
3640vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3641 MethodReference target_method) {
3642 return DeduplicateMethodLiteral(target_method, &call_patches_);
3643}
3644
3645
Andreas Gampe878d58c2015-01-15 23:24:00 -08003646void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003647 // When we do not run baseline, explicit clinit checks triggered by static
3648 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3649 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003650
Andreas Gampe878d58c2015-01-15 23:24:00 -08003651 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3652 return;
3653 }
3654
Alexandre Ramesd921d642015-04-16 15:07:16 +01003655 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003656 LocationSummary* locations = invoke->GetLocations();
3657 codegen_->GenerateStaticOrDirectCall(
3658 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003659 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003660}
3661
3662void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003663 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3664 return;
3665 }
3666
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003667 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003668 DCHECK(!codegen_->IsLeafMethod());
3669 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3670}
3671
Alexandre Rames67555f72014-11-18 10:55:16 +00003672void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003673 InvokeRuntimeCallingConvention calling_convention;
3674 CodeGenerator::CreateLoadClassLocationSummary(
3675 cls,
3676 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003677 LocationFrom(vixl::x0),
3678 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003679}
3680
3681void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003682 if (cls->NeedsAccessCheck()) {
3683 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3684 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3685 cls,
3686 cls->GetDexPc(),
3687 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01003688 return;
3689 }
3690
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003691 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003692 Register out = OutputRegister(cls);
3693 Register current_method = InputRegisterAt(cls, 0);
3694 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003695 DCHECK(!cls->CanCallRuntime());
3696 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003697 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3698 if (kEmitCompilerReadBarrier) {
3699 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3700 __ Add(out.X(), current_method.X(), declaring_class_offset);
3701 // /* mirror::Class* */ out = out->Read()
3702 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3703 } else {
3704 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3705 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3706 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003707 } else {
3708 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003709 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003710 // /* GcRoot<mirror::Class>[] */ out =
3711 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003712 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003713
3714 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
3715 if (kEmitCompilerReadBarrier) {
3716 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
3717 __ Add(out.X(), out.X(), cache_offset);
3718 // /* mirror::Class* */ out = out->Read()
3719 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3720 } else {
3721 // /* GcRoot<mirror::Class> */ out = out[type_index]
3722 __ Ldr(out, MemOperand(out.X(), cache_offset));
3723 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003724
3725 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3726 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3727 codegen_->AddSlowPath(slow_path);
3728 __ Cbz(out, slow_path->GetEntryLabel());
3729 if (cls->MustGenerateClinitCheck()) {
3730 GenerateClassInitializationCheck(slow_path, out);
3731 } else {
3732 __ Bind(slow_path->GetExitLabel());
3733 }
3734 }
3735}
3736
David Brazdilcb1c0552015-08-04 16:22:25 +01003737static MemOperand GetExceptionTlsAddress() {
3738 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3739}
3740
Alexandre Rames67555f72014-11-18 10:55:16 +00003741void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3742 LocationSummary* locations =
3743 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3744 locations->SetOut(Location::RequiresRegister());
3745}
3746
3747void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003748 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3749}
3750
3751void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3752 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3753}
3754
3755void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3756 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003757}
3758
Alexandre Rames5319def2014-10-23 10:03:10 +01003759void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3760 load->SetLocations(nullptr);
3761}
3762
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003763void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003764 // Nothing to do, this is driven by the code generator.
3765}
3766
Alexandre Rames67555f72014-11-18 10:55:16 +00003767void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3768 LocationSummary* locations =
3769 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003770 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003771 locations->SetOut(Location::RequiresRegister());
3772}
3773
3774void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3775 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3776 codegen_->AddSlowPath(slow_path);
3777
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003778 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003779 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003780 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003781
3782 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3783 if (kEmitCompilerReadBarrier) {
3784 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3785 __ Add(out.X(), current_method.X(), declaring_class_offset);
3786 // /* mirror::Class* */ out = out->Read()
3787 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3788 } else {
3789 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3790 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3791 }
3792
3793 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3794 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
3795
3796 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
3797 if (kEmitCompilerReadBarrier) {
3798 // /* GcRoot<mirror::String>* */ out = &out[string_index]
3799 __ Add(out.X(), out.X(), cache_offset);
3800 // /* mirror::String* */ out = out->Read()
3801 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3802 } else {
3803 // /* GcRoot<mirror::String> */ out = out[string_index]
3804 __ Ldr(out, MemOperand(out.X(), cache_offset));
3805 }
3806
Alexandre Rames67555f72014-11-18 10:55:16 +00003807 __ Cbz(out, slow_path->GetEntryLabel());
3808 __ Bind(slow_path->GetExitLabel());
3809}
3810
Alexandre Rames5319def2014-10-23 10:03:10 +01003811void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3812 local->SetLocations(nullptr);
3813}
3814
3815void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3816 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3817}
3818
3819void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3821 locations->SetOut(Location::ConstantLocation(constant));
3822}
3823
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003824void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003825 // Will be generated at use site.
3826}
3827
Alexandre Rames67555f72014-11-18 10:55:16 +00003828void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3829 LocationSummary* locations =
3830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3831 InvokeRuntimeCallingConvention calling_convention;
3832 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3833}
3834
3835void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3836 codegen_->InvokeRuntime(instruction->IsEnter()
3837 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3838 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003839 instruction->GetDexPc(),
3840 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003841 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003842}
3843
Alexandre Rames42d641b2014-10-27 14:00:51 +00003844void LocationsBuilderARM64::VisitMul(HMul* mul) {
3845 LocationSummary* locations =
3846 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3847 switch (mul->GetResultType()) {
3848 case Primitive::kPrimInt:
3849 case Primitive::kPrimLong:
3850 locations->SetInAt(0, Location::RequiresRegister());
3851 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003853 break;
3854
3855 case Primitive::kPrimFloat:
3856 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003857 locations->SetInAt(0, Location::RequiresFpuRegister());
3858 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003859 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003860 break;
3861
3862 default:
3863 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3864 }
3865}
3866
3867void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3868 switch (mul->GetResultType()) {
3869 case Primitive::kPrimInt:
3870 case Primitive::kPrimLong:
3871 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3872 break;
3873
3874 case Primitive::kPrimFloat:
3875 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003876 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003877 break;
3878
3879 default:
3880 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3881 }
3882}
3883
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003884void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3885 LocationSummary* locations =
3886 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3887 switch (neg->GetResultType()) {
3888 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003889 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003890 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003891 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003892 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003893
3894 case Primitive::kPrimFloat:
3895 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003896 locations->SetInAt(0, Location::RequiresFpuRegister());
3897 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003898 break;
3899
3900 default:
3901 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3902 }
3903}
3904
3905void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3906 switch (neg->GetResultType()) {
3907 case Primitive::kPrimInt:
3908 case Primitive::kPrimLong:
3909 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3910 break;
3911
3912 case Primitive::kPrimFloat:
3913 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003914 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003915 break;
3916
3917 default:
3918 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3919 }
3920}
3921
3922void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3923 LocationSummary* locations =
3924 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3925 InvokeRuntimeCallingConvention calling_convention;
3926 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003927 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003928 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003929 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003930 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003931 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003932}
3933
3934void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3935 LocationSummary* locations = instruction->GetLocations();
3936 InvokeRuntimeCallingConvention calling_convention;
3937 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3938 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003939 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003940 // Note: if heap poisoning is enabled, the entry point takes cares
3941 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003942 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3943 instruction,
3944 instruction->GetDexPc(),
3945 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003946 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003947}
3948
Alexandre Rames5319def2014-10-23 10:03:10 +01003949void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3950 LocationSummary* locations =
3951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3952 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003953 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3954 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003955 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003956 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003957}
3958
3959void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003960 // Note: if heap poisoning is enabled, the entry point takes cares
3961 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003962 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3963 instruction,
3964 instruction->GetDexPc(),
3965 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003966 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003967}
3968
3969void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003971 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003973}
3974
3975void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003976 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003977 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003978 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003979 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003980 break;
3981
3982 default:
3983 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3984 }
3985}
3986
David Brazdil66d126e2015-04-03 16:02:44 +01003987void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3988 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3989 locations->SetInAt(0, Location::RequiresRegister());
3990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3991}
3992
3993void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003994 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3995}
3996
Alexandre Rames5319def2014-10-23 10:03:10 +01003997void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003998 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3999 ? LocationSummary::kCallOnSlowPath
4000 : LocationSummary::kNoCall;
4001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004002 locations->SetInAt(0, Location::RequiresRegister());
4003 if (instruction->HasUses()) {
4004 locations->SetOut(Location::SameAsFirstInput());
4005 }
4006}
4007
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004008void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004009 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4010 return;
4011 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004012
Alexandre Ramesd921d642015-04-16 15:07:16 +01004013 BlockPoolsScope block_pools(GetVIXLAssembler());
4014 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004015 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4016 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4017}
4018
4019void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004020 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4021 codegen_->AddSlowPath(slow_path);
4022
4023 LocationSummary* locations = instruction->GetLocations();
4024 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004025
4026 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004027}
4028
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004029void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004030 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004031 GenerateImplicitNullCheck(instruction);
4032 } else {
4033 GenerateExplicitNullCheck(instruction);
4034 }
4035}
4036
Alexandre Rames67555f72014-11-18 10:55:16 +00004037void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4038 HandleBinaryOp(instruction);
4039}
4040
4041void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4042 HandleBinaryOp(instruction);
4043}
4044
Alexandre Rames3e69f162014-12-10 10:36:50 +00004045void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4046 LOG(FATAL) << "Unreachable";
4047}
4048
4049void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4050 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4051}
4052
Alexandre Rames5319def2014-10-23 10:03:10 +01004053void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4054 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4055 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4056 if (location.IsStackSlot()) {
4057 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4058 } else if (location.IsDoubleStackSlot()) {
4059 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4060 }
4061 locations->SetOut(location);
4062}
4063
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004064void InstructionCodeGeneratorARM64::VisitParameterValue(
4065 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004066 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004067}
4068
4069void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4070 LocationSummary* locations =
4071 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004072 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004073}
4074
4075void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4076 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4077 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004078}
4079
4080void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4081 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4082 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4083 locations->SetInAt(i, Location::Any());
4084 }
4085 locations->SetOut(Location::Any());
4086}
4087
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004088void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004089 LOG(FATAL) << "Unreachable";
4090}
4091
Serban Constantinescu02164b32014-11-13 14:05:07 +00004092void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004093 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004094 LocationSummary::CallKind call_kind =
4095 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4097
4098 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004099 case Primitive::kPrimInt:
4100 case Primitive::kPrimLong:
4101 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004102 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4104 break;
4105
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004106 case Primitive::kPrimFloat:
4107 case Primitive::kPrimDouble: {
4108 InvokeRuntimeCallingConvention calling_convention;
4109 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4110 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4111 locations->SetOut(calling_convention.GetReturnLocation(type));
4112
4113 break;
4114 }
4115
Serban Constantinescu02164b32014-11-13 14:05:07 +00004116 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004117 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004118 }
4119}
4120
4121void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4122 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004123
Serban Constantinescu02164b32014-11-13 14:05:07 +00004124 switch (type) {
4125 case Primitive::kPrimInt:
4126 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004127 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004128 break;
4129 }
4130
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004131 case Primitive::kPrimFloat:
4132 case Primitive::kPrimDouble: {
4133 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4134 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004135 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004136 break;
4137 }
4138
Serban Constantinescu02164b32014-11-13 14:05:07 +00004139 default:
4140 LOG(FATAL) << "Unexpected rem type " << type;
4141 }
4142}
4143
Calin Juravle27df7582015-04-17 19:12:31 +01004144void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4145 memory_barrier->SetLocations(nullptr);
4146}
4147
4148void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4149 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4150}
4151
Alexandre Rames5319def2014-10-23 10:03:10 +01004152void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4153 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4154 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004155 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004156}
4157
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004158void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004159 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004160}
4161
4162void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4163 instruction->SetLocations(nullptr);
4164}
4165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004166void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004167 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004168}
4169
Serban Constantinescu02164b32014-11-13 14:05:07 +00004170void LocationsBuilderARM64::VisitShl(HShl* shl) {
4171 HandleShift(shl);
4172}
4173
4174void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4175 HandleShift(shl);
4176}
4177
4178void LocationsBuilderARM64::VisitShr(HShr* shr) {
4179 HandleShift(shr);
4180}
4181
4182void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4183 HandleShift(shr);
4184}
4185
Alexandre Rames5319def2014-10-23 10:03:10 +01004186void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4188 Primitive::Type field_type = store->InputAt(1)->GetType();
4189 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004190 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004191 case Primitive::kPrimBoolean:
4192 case Primitive::kPrimByte:
4193 case Primitive::kPrimChar:
4194 case Primitive::kPrimShort:
4195 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004196 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004197 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4198 break;
4199
4200 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004201 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004202 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4203 break;
4204
4205 default:
4206 LOG(FATAL) << "Unimplemented local type " << field_type;
4207 }
4208}
4209
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004210void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004211}
4212
4213void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004214 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004215}
4216
4217void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004218 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004219}
4220
Alexandre Rames67555f72014-11-18 10:55:16 +00004221void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004222 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004223}
4224
4225void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004226 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004227}
4228
4229void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004230 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004231}
4232
Alexandre Rames67555f72014-11-18 10:55:16 +00004233void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004234 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004235}
4236
Calin Juravlee460d1d2015-09-29 04:52:17 +01004237void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4238 HUnresolvedInstanceFieldGet* instruction) {
4239 FieldAccessCallingConventionARM64 calling_convention;
4240 codegen_->CreateUnresolvedFieldLocationSummary(
4241 instruction, instruction->GetFieldType(), calling_convention);
4242}
4243
4244void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4245 HUnresolvedInstanceFieldGet* instruction) {
4246 FieldAccessCallingConventionARM64 calling_convention;
4247 codegen_->GenerateUnresolvedFieldAccess(instruction,
4248 instruction->GetFieldType(),
4249 instruction->GetFieldIndex(),
4250 instruction->GetDexPc(),
4251 calling_convention);
4252}
4253
4254void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4255 HUnresolvedInstanceFieldSet* instruction) {
4256 FieldAccessCallingConventionARM64 calling_convention;
4257 codegen_->CreateUnresolvedFieldLocationSummary(
4258 instruction, instruction->GetFieldType(), calling_convention);
4259}
4260
4261void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4262 HUnresolvedInstanceFieldSet* instruction) {
4263 FieldAccessCallingConventionARM64 calling_convention;
4264 codegen_->GenerateUnresolvedFieldAccess(instruction,
4265 instruction->GetFieldType(),
4266 instruction->GetFieldIndex(),
4267 instruction->GetDexPc(),
4268 calling_convention);
4269}
4270
4271void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4272 HUnresolvedStaticFieldGet* instruction) {
4273 FieldAccessCallingConventionARM64 calling_convention;
4274 codegen_->CreateUnresolvedFieldLocationSummary(
4275 instruction, instruction->GetFieldType(), calling_convention);
4276}
4277
4278void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4279 HUnresolvedStaticFieldGet* instruction) {
4280 FieldAccessCallingConventionARM64 calling_convention;
4281 codegen_->GenerateUnresolvedFieldAccess(instruction,
4282 instruction->GetFieldType(),
4283 instruction->GetFieldIndex(),
4284 instruction->GetDexPc(),
4285 calling_convention);
4286}
4287
4288void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4289 HUnresolvedStaticFieldSet* instruction) {
4290 FieldAccessCallingConventionARM64 calling_convention;
4291 codegen_->CreateUnresolvedFieldLocationSummary(
4292 instruction, instruction->GetFieldType(), calling_convention);
4293}
4294
4295void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4296 HUnresolvedStaticFieldSet* instruction) {
4297 FieldAccessCallingConventionARM64 calling_convention;
4298 codegen_->GenerateUnresolvedFieldAccess(instruction,
4299 instruction->GetFieldType(),
4300 instruction->GetFieldIndex(),
4301 instruction->GetDexPc(),
4302 calling_convention);
4303}
4304
Alexandre Rames5319def2014-10-23 10:03:10 +01004305void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4306 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4307}
4308
4309void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004310 HBasicBlock* block = instruction->GetBlock();
4311 if (block->GetLoopInformation() != nullptr) {
4312 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4313 // The back edge will generate the suspend check.
4314 return;
4315 }
4316 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4317 // The goto will generate the suspend check.
4318 return;
4319 }
4320 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004321}
4322
4323void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4324 temp->SetLocations(nullptr);
4325}
4326
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004327void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004328 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004329}
4330
Alexandre Rames67555f72014-11-18 10:55:16 +00004331void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4332 LocationSummary* locations =
4333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4334 InvokeRuntimeCallingConvention calling_convention;
4335 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4336}
4337
4338void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4339 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004340 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004341 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004342}
4343
4344void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4345 LocationSummary* locations =
4346 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4347 Primitive::Type input_type = conversion->GetInputType();
4348 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004349 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004350 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4351 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4352 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4353 }
4354
Alexandre Rames542361f2015-01-29 16:57:31 +00004355 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004356 locations->SetInAt(0, Location::RequiresFpuRegister());
4357 } else {
4358 locations->SetInAt(0, Location::RequiresRegister());
4359 }
4360
Alexandre Rames542361f2015-01-29 16:57:31 +00004361 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004362 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4363 } else {
4364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4365 }
4366}
4367
4368void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4369 Primitive::Type result_type = conversion->GetResultType();
4370 Primitive::Type input_type = conversion->GetInputType();
4371
4372 DCHECK_NE(input_type, result_type);
4373
Alexandre Rames542361f2015-01-29 16:57:31 +00004374 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004375 int result_size = Primitive::ComponentSize(result_type);
4376 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004377 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004378 Register output = OutputRegister(conversion);
4379 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004380 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
4381 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004382 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
4383 // 'int' values are used directly as W registers, discarding the top
4384 // bits, so we don't need to sign-extend and can just perform a move.
4385 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4386 // top 32 bits of the target register. We theoretically could leave those
4387 // bits unchanged, but we would have to make sure that no code uses a
4388 // 32bit input value as a 64bit value assuming that the top 32 bits are
4389 // zero.
4390 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00004391 } else if ((result_type == Primitive::kPrimChar) ||
4392 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
4393 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004394 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004395 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004396 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004397 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004398 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004399 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004400 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4401 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004402 } else if (Primitive::IsFloatingPointType(result_type) &&
4403 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004404 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4405 } else {
4406 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4407 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004408 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004409}
Alexandre Rames67555f72014-11-18 10:55:16 +00004410
Serban Constantinescu02164b32014-11-13 14:05:07 +00004411void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4412 HandleShift(ushr);
4413}
4414
4415void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4416 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004417}
4418
4419void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4420 HandleBinaryOp(instruction);
4421}
4422
4423void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4424 HandleBinaryOp(instruction);
4425}
4426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004427void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004428 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004429 LOG(FATAL) << "Unreachable";
4430}
4431
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004432void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004433 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004434 LOG(FATAL) << "Unreachable";
4435}
4436
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004437void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
4438 DCHECK(codegen_->IsBaseline());
4439 LocationSummary* locations =
4440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4441 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4442}
4443
4444void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4445 DCHECK(codegen_->IsBaseline());
4446 // Will be generated at use site.
4447}
4448
Mark Mendellfe57faa2015-09-18 09:26:15 -04004449// Simple implementation of packed switch - generate cascaded compare/jumps.
4450void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4451 LocationSummary* locations =
4452 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4453 locations->SetInAt(0, Location::RequiresRegister());
4454}
4455
4456void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4457 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004458 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004459 Register value_reg = InputRegisterAt(switch_instr, 0);
4460 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4461
Zheng Xu3927c8b2015-11-18 17:46:25 +08004462 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4463 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4464 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4465 // make sure we don't emit it if the target may run out of range.
4466 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4467 // ranges and emit the tables only as required.
4468 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004469
Zheng Xu3927c8b2015-11-18 17:46:25 +08004470 if (num_entries < kPackedSwitchJumpTableThreshold ||
4471 // Current instruction id is an upper bound of the number of HIRs in the graph.
4472 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4473 // Create a series of compare/jumps.
4474 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
4475 for (uint32_t i = 0; i < num_entries; i++) {
4476 int32_t case_value = lower_bound + i;
4477 vixl::Label* succ = codegen_->GetLabelOf(successors[i]);
4478 if (case_value == 0) {
4479 __ Cbz(value_reg, succ);
4480 } else {
4481 __ Cmp(value_reg, Operand(case_value));
4482 __ B(eq, succ);
4483 }
4484 }
4485
4486 // And the default for any other value.
4487 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4488 __ B(codegen_->GetLabelOf(default_block));
4489 }
4490 } else {
4491 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4492 codegen_->AddJumpTable(jump_table);
4493
4494 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4495
4496 // Below instructions should use at most one blocked register. Since there are two blocked
4497 // registers, we are free to block one.
4498 Register temp_w = temps.AcquireW();
4499 Register index;
4500 // Remove the bias.
4501 if (lower_bound != 0) {
4502 index = temp_w;
4503 __ Sub(index, value_reg, Operand(lower_bound));
4504 } else {
4505 index = value_reg;
4506 }
4507
4508 // Jump to default block if index is out of the range.
4509 __ Cmp(index, Operand(num_entries));
4510 __ B(hs, codegen_->GetLabelOf(default_block));
4511
4512 // In current VIXL implementation, it won't require any blocked registers to encode the
4513 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4514 // register pressure.
4515 Register table_base = temps.AcquireX();
4516 // Load jump offset from the table.
4517 __ Adr(table_base, jump_table->GetTableStartLabel());
4518 Register jump_offset = temp_w;
4519 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4520
4521 // Jump to target block by branching to table_base(pc related) + offset.
4522 Register target_address = table_base;
4523 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4524 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004525 }
4526}
4527
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004528void CodeGeneratorARM64::GenerateReadBarrier(HInstruction* instruction,
4529 Location out,
4530 Location ref,
4531 Location obj,
4532 uint32_t offset,
4533 Location index) {
4534 DCHECK(kEmitCompilerReadBarrier);
4535
4536 // If heap poisoning is enabled, the unpoisoning of the loaded
4537 // reference will be carried out by the runtime within the slow
4538 // path.
4539 //
4540 // Note that `ref` currently does not get unpoisoned (when heap
4541 // poisoning is enabled), which is alright as the `ref` argument is
4542 // not used by the artReadBarrierSlow entry point.
4543 //
4544 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4545 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4546 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4547 AddSlowPath(slow_path);
4548
4549 // TODO: When read barrier has a fast path, add it here.
4550 /* Currently the read barrier call is inserted after the original load.
4551 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
4552 * original load. This load-load ordering is required by the read barrier.
4553 * The fast path/slow path (for Baker's algorithm) should look like:
4554 *
4555 * bool isGray = obj.LockWord & kReadBarrierMask;
4556 * lfence; // load fence or artificial data dependence to prevent load-load reordering
4557 * ref = obj.field; // this is the original load
4558 * if (isGray) {
4559 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
4560 * }
4561 */
4562
4563 __ B(slow_path->GetEntryLabel());
4564 __ Bind(slow_path->GetExitLabel());
4565}
4566
4567void CodeGeneratorARM64::MaybeGenerateReadBarrier(HInstruction* instruction,
4568 Location out,
4569 Location ref,
4570 Location obj,
4571 uint32_t offset,
4572 Location index) {
4573 if (kEmitCompilerReadBarrier) {
4574 // If heap poisoning is enabled, unpoisoning will be taken care of
4575 // by the runtime within the slow path.
4576 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
4577 } else if (kPoisonHeapReferences) {
4578 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4579 }
4580}
4581
4582void CodeGeneratorARM64::GenerateReadBarrierForRoot(HInstruction* instruction,
4583 Location out,
4584 Location root) {
4585 DCHECK(kEmitCompilerReadBarrier);
4586
4587 // Note that GC roots are not affected by heap poisoning, so we do
4588 // not need to do anything special for this here.
4589 SlowPathCodeARM64* slow_path =
4590 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4591 AddSlowPath(slow_path);
4592
4593 // TODO: Implement a fast path for ReadBarrierForRoot, performing
4594 // the following operation (for Baker's algorithm):
4595 //
4596 // if (thread.tls32_.is_gc_marking) {
4597 // root = Mark(root);
4598 // }
4599
4600 __ B(slow_path->GetEntryLabel());
4601 __ Bind(slow_path->GetExitLabel());
4602}
4603
Alexandre Rames67555f72014-11-18 10:55:16 +00004604#undef __
4605#undef QUICK_ENTRY_POINT
4606
Alexandre Rames5319def2014-10-23 10:03:10 +01004607} // namespace arm64
4608} // namespace art