blob: 6ed2c5ab38f4eb9c9ba47db7d0b154c89f7dd167 [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;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000074// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080075// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000077static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Alexandre Ramesa89086e2014-11-07 17:13:25 +000096Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
98 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
99 // but we use the exact registers for clarity.
100 if (return_type == Primitive::kPrimFloat) {
101 return LocationFrom(s0);
102 } else if (return_type == Primitive::kPrimDouble) {
103 return LocationFrom(d0);
104 } else if (return_type == Primitive::kPrimLong) {
105 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100106 } else if (return_type == Primitive::kPrimVoid) {
107 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000108 } else {
109 return LocationFrom(w0);
110 }
111}
112
Alexandre Rames5319def2014-10-23 10:03:10 +0100113Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100115}
116
Alexandre Rames67555f72014-11-18 10:55:16 +0000117#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
118#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100119
Zheng Xuda403092015-04-24 17:35:39 +0800120// Calculate memory accessing operand for save/restore live registers.
121static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
122 RegisterSet* register_set,
123 int64_t spill_offset,
124 bool is_save) {
125 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
126 codegen->GetNumberOfCoreRegisters(),
127 register_set->GetFloatingPointRegisters(),
128 codegen->GetNumberOfFloatingPointRegisters()));
129
130 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
131 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000132 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
133 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800134
135 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
136 UseScratchRegisterScope temps(masm);
137
138 Register base = masm->StackPointer();
139 int64_t core_spill_size = core_list.TotalSizeInBytes();
140 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
141 int64_t reg_size = kXRegSizeInBytes;
142 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
143 uint32_t ls_access_size = WhichPowerOf2(reg_size);
144 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
145 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
146 // If the offset does not fit in the instruction's immediate field, use an alternate register
147 // to compute the base address(float point registers spill base address).
148 Register new_base = temps.AcquireSameSizeAs(base);
149 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
150 base = new_base;
151 spill_offset = -core_spill_size;
152 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
153 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
154 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
155 }
156
157 if (is_save) {
158 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
159 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
160 } else {
161 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
162 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
163 }
164}
165
166void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
167 RegisterSet* register_set = locations->GetLiveRegisters();
168 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
169 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
170 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
171 // If the register holds an object, update the stack mask.
172 if (locations->RegisterContainsObject(i)) {
173 locations->SetStackBit(stack_offset / kVRegSize);
174 }
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_core_stack_offsets_[i] = stack_offset;
178 stack_offset += kXRegSizeInBytes;
179 }
180 }
181
182 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
183 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
184 register_set->ContainsFloatingPointRegister(i)) {
185 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
186 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
187 saved_fpu_stack_offsets_[i] = stack_offset;
188 stack_offset += kDRegSizeInBytes;
189 }
190 }
191
192 SaveRestoreLiveRegistersHelper(codegen, register_set,
193 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
194}
195
196void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
197 RegisterSet* register_set = locations->GetLiveRegisters();
198 SaveRestoreLiveRegistersHelper(codegen, register_set,
199 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
200}
201
Alexandre Rames5319def2014-10-23 10:03:10 +0100202class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
203 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100205
Alexandre Rames67555f72014-11-18 10:55:16 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100207 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000208 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100209
Alexandre Rames5319def2014-10-23 10:03:10 +0100210 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000211 if (instruction_->CanThrowIntoCatchBlock()) {
212 // Live registers will be restored in the catch block if caught.
213 SaveLiveRegisters(codegen, instruction_->GetLocations());
214 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000215 // We're moving two locations to locations that could overlap, so we need a parallel
216 // move resolver.
217 InvokeRuntimeCallingConvention calling_convention;
218 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100219 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
220 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000222 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800223 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100224 }
225
Alexandre Rames8158f282015-08-07 10:26:17 +0100226 bool IsFatal() const OVERRIDE { return true; }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000231 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232
Alexandre Rames5319def2014-10-23 10:03:10 +0100233 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
234};
235
Alexandre Rames67555f72014-11-18 10:55:16 +0000236class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
237 public:
238 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
239
240 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
241 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
242 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000243 if (instruction_->CanThrowIntoCatchBlock()) {
244 // Live registers will be restored in the catch block if caught.
245 SaveLiveRegisters(codegen, instruction_->GetLocations());
246 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000247 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000248 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800249 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 }
251
Alexandre Rames8158f282015-08-07 10:26:17 +0100252 bool IsFatal() const OVERRIDE { return true; }
253
Alexandre Rames9931f312015-06-19 14:47:01 +0100254 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 private:
257 HDivZeroCheck* const instruction_;
258 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
259};
260
261class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
262 public:
263 LoadClassSlowPathARM64(HLoadClass* cls,
264 HInstruction* at,
265 uint32_t dex_pc,
266 bool do_clinit)
267 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
268 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
269 }
270
271 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
272 LocationSummary* locations = at_->GetLocations();
273 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
274
275 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
278 InvokeRuntimeCallingConvention calling_convention;
279 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
281 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000282 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800283 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100284 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800285 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100286 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800287 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000288
289 // Move the class to the desired location.
290 Location out = locations->Out();
291 if (out.IsValid()) {
292 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
293 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000294 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 }
296
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 __ B(GetExitLabel());
299 }
300
Alexandre Rames9931f312015-06-19 14:47:01 +0100301 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
302
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 private:
304 // The class this slow path will load.
305 HLoadClass* const cls_;
306
307 // The instruction where this slow path is happening.
308 // (Might be the load class or an initialization check).
309 HInstruction* const at_;
310
311 // The dex PC of `at_`.
312 const uint32_t dex_pc_;
313
314 // Whether to initialize the class.
315 const bool do_clinit_;
316
317 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
318};
319
320class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
321 public:
322 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
323
324 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
325 LocationSummary* locations = instruction_->GetLocations();
326 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
327 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
328
329 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000330 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000331
332 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800333 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000335 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100336 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000338 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000340 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 __ B(GetExitLabel());
342 }
343
Alexandre Rames9931f312015-06-19 14:47:01 +0100344 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
345
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 private:
347 HLoadString* const instruction_;
348
349 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
350};
351
Alexandre Rames5319def2014-10-23 10:03:10 +0100352class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
353 public:
354 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
355
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
357 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100358 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000359 if (instruction_->CanThrowIntoCatchBlock()) {
360 // Live registers will be restored in the catch block if caught.
361 SaveLiveRegisters(codegen, instruction_->GetLocations());
362 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000364 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800365 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100366 }
367
Alexandre Rames8158f282015-08-07 10:26:17 +0100368 bool IsFatal() const OVERRIDE { return true; }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
371
Alexandre Rames5319def2014-10-23 10:03:10 +0100372 private:
373 HNullCheck* const instruction_;
374
375 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
376};
377
378class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
379 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100380 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100381 : instruction_(instruction), successor_(successor) {}
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
384 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000386 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000388 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800389 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000390 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000391 if (successor_ == nullptr) {
392 __ B(GetReturnLabel());
393 } else {
394 __ B(arm64_codegen->GetLabelOf(successor_));
395 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100396 }
397
398 vixl::Label* GetReturnLabel() {
399 DCHECK(successor_ == nullptr);
400 return &return_label_;
401 }
402
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100403 HBasicBlock* GetSuccessor() const {
404 return successor_;
405 }
406
Alexandre Rames9931f312015-06-19 14:47:01 +0100407 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
408
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 private:
410 HSuspendCheck* const instruction_;
411 // If not null, the block to branch to after the suspend check.
412 HBasicBlock* const successor_;
413
414 // If `successor_` is null, the label to branch to after the suspend check.
415 vixl::Label return_label_;
416
417 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
418};
419
Alexandre Rames67555f72014-11-18 10:55:16 +0000420class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
421 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000422 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
423 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000424
425 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000426 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100427 Location class_to_check = locations->InAt(1);
428 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
429 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430 DCHECK(instruction_->IsCheckCast()
431 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
432 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100433 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434
Alexandre Rames67555f72014-11-18 10:55:16 +0000435 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000437 if (!is_fatal_) {
438 SaveLiveRegisters(codegen, locations);
439 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000440
441 // We're moving two locations to locations that could overlap, so we need a parallel
442 // move resolver.
443 InvokeRuntimeCallingConvention calling_convention;
444 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
446 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000447
448 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000449 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100450 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000451 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
452 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453 Primitive::Type ret_type = instruction_->GetType();
454 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
455 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
456 } else {
457 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100458 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800459 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000460 }
461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000462 if (!is_fatal_) {
463 RestoreLiveRegisters(codegen, locations);
464 __ B(GetExitLabel());
465 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 }
467
Alexandre Rames9931f312015-06-19 14:47:01 +0100468 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100470
Alexandre Rames67555f72014-11-18 10:55:16 +0000471 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000473 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474
Alexandre Rames67555f72014-11-18 10:55:16 +0000475 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
476};
477
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700478class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
479 public:
Aart Bik42249c32016-01-07 15:33:50 -0800480 explicit DeoptimizationSlowPathARM64(HDeoptimize* 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 {
Aart Bik42249c32016-01-07 15:33:50 -0800484 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700485 __ Bind(GetEntryLabel());
486 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800487 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
488 instruction_,
489 instruction_->GetDexPc(),
490 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000491 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700492 }
493
Alexandre Rames9931f312015-06-19 14:47:01 +0100494 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 private:
Aart Bik42249c32016-01-07 15:33:50 -0800497 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
499};
500
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100501class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
502 public:
503 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
504
505 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
506 LocationSummary* locations = instruction_->GetLocations();
507 __ Bind(GetEntryLabel());
508 SaveLiveRegisters(codegen, locations);
509
510 InvokeRuntimeCallingConvention calling_convention;
511 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
512 parallel_move.AddMove(
513 locations->InAt(0),
514 LocationFrom(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 nullptr);
517 parallel_move.AddMove(
518 locations->InAt(1),
519 LocationFrom(calling_convention.GetRegisterAt(1)),
520 Primitive::kPrimInt,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(2),
524 LocationFrom(calling_convention.GetRegisterAt(2)),
525 Primitive::kPrimNot,
526 nullptr);
527 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
528
529 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
530 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
531 instruction_,
532 instruction_->GetDexPc(),
533 this);
534 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
535 RestoreLiveRegisters(codegen, locations);
536 __ B(GetExitLabel());
537 }
538
539 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
540
541 private:
542 HInstruction* const instruction_;
543
544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
545};
546
Zheng Xu3927c8b2015-11-18 17:46:25 +0800547void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
548 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000549 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800550
551 // We are about to use the assembler to place literals directly. Make sure we have enough
552 // underlying code buffer and we have generated the jump table with right size.
553 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
554 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
555
556 __ Bind(&table_start_);
557 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
558 for (uint32_t i = 0; i < num_entries; i++) {
559 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
560 DCHECK(target_label->IsBound());
561 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
562 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
563 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
564 Literal<int32_t> literal(jump_offset);
565 __ place(&literal);
566 }
567}
568
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000569// Slow path generating a read barrier for a heap reference.
570class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
571 public:
572 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
573 Location out,
574 Location ref,
575 Location obj,
576 uint32_t offset,
577 Location index)
578 : instruction_(instruction),
579 out_(out),
580 ref_(ref),
581 obj_(obj),
582 offset_(offset),
583 index_(index) {
584 DCHECK(kEmitCompilerReadBarrier);
585 // If `obj` is equal to `out` or `ref`, it means the initial object
586 // has been overwritten by (or after) the heap object reference load
587 // to be instrumented, e.g.:
588 //
589 // __ Ldr(out, HeapOperand(out, class_offset);
590 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
591 //
592 // In that case, we have lost the information about the original
593 // object, and the emitted read barrier cannot work properly.
594 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
595 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
596 }
597
598 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
599 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
600 LocationSummary* locations = instruction_->GetLocations();
601 Primitive::Type type = Primitive::kPrimNot;
602 DCHECK(locations->CanCall());
603 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
604 DCHECK(!instruction_->IsInvoke() ||
605 (instruction_->IsInvokeStaticOrDirect() &&
606 instruction_->GetLocations()->Intrinsified()));
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000607 // The read barrier instrumentation does not support the
608 // HArm64IntermediateAddress instruction yet.
609 DCHECK(!(instruction_->IsArrayGet() &&
610 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000611
612 __ Bind(GetEntryLabel());
613
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000614 SaveLiveRegisters(codegen, locations);
615
616 // We may have to change the index's value, but as `index_` is a
617 // constant member (like other "inputs" of this slow path),
618 // introduce a copy of it, `index`.
619 Location index = index_;
620 if (index_.IsValid()) {
621 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
622 if (instruction_->IsArrayGet()) {
623 // Compute the actual memory offset and store it in `index`.
624 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
625 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
626 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
627 // We are about to change the value of `index_reg` (see the
628 // calls to vixl::MacroAssembler::Lsl and
629 // vixl::MacroAssembler::Mov below), but it has
630 // not been saved by the previous call to
631 // art::SlowPathCode::SaveLiveRegisters, as it is a
632 // callee-save register --
633 // art::SlowPathCode::SaveLiveRegisters does not consider
634 // callee-save registers, as it has been designed with the
635 // assumption that callee-save registers are supposed to be
636 // handled by the called function. So, as a callee-save
637 // register, `index_reg` _would_ eventually be saved onto
638 // the stack, but it would be too late: we would have
639 // changed its value earlier. Therefore, we manually save
640 // it here into another freely available register,
641 // `free_reg`, chosen of course among the caller-save
642 // registers (as a callee-save `free_reg` register would
643 // exhibit the same problem).
644 //
645 // Note we could have requested a temporary register from
646 // the register allocator instead; but we prefer not to, as
647 // this is a slow path, and we know we can find a
648 // caller-save register that is available.
649 Register free_reg = FindAvailableCallerSaveRegister(codegen);
650 __ Mov(free_reg.W(), index_reg);
651 index_reg = free_reg;
652 index = LocationFrom(index_reg);
653 } else {
654 // The initial register stored in `index_` has already been
655 // saved in the call to art::SlowPathCode::SaveLiveRegisters
656 // (as it is not a callee-save register), so we can freely
657 // use it.
658 }
659 // Shifting the index value contained in `index_reg` by the scale
660 // factor (2) cannot overflow in practice, as the runtime is
661 // unable to allocate object arrays with a size larger than
662 // 2^26 - 1 (that is, 2^28 - 4 bytes).
663 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
664 static_assert(
665 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
666 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
667 __ Add(index_reg, index_reg, Operand(offset_));
668 } else {
669 DCHECK(instruction_->IsInvoke());
670 DCHECK(instruction_->GetLocations()->Intrinsified());
671 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
672 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
673 << instruction_->AsInvoke()->GetIntrinsic();
674 DCHECK_EQ(offset_, 0U);
675 DCHECK(index_.IsRegisterPair());
676 // UnsafeGet's offset location is a register pair, the low
677 // part contains the correct offset.
678 index = index_.ToLow();
679 }
680 }
681
682 // We're moving two or three locations to locations that could
683 // overlap, so we need a parallel move resolver.
684 InvokeRuntimeCallingConvention calling_convention;
685 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
686 parallel_move.AddMove(ref_,
687 LocationFrom(calling_convention.GetRegisterAt(0)),
688 type,
689 nullptr);
690 parallel_move.AddMove(obj_,
691 LocationFrom(calling_convention.GetRegisterAt(1)),
692 type,
693 nullptr);
694 if (index.IsValid()) {
695 parallel_move.AddMove(index,
696 LocationFrom(calling_convention.GetRegisterAt(2)),
697 Primitive::kPrimInt,
698 nullptr);
699 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
700 } else {
701 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
702 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
703 }
704 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
705 instruction_,
706 instruction_->GetDexPc(),
707 this);
708 CheckEntrypointTypes<
709 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
710 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
711
712 RestoreLiveRegisters(codegen, locations);
713
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000714 __ B(GetExitLabel());
715 }
716
717 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
718
719 private:
720 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
721 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
722 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
723 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
724 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
725 return Register(VIXLRegCodeFromART(i), kXRegSize);
726 }
727 }
728 // We shall never fail to find a free caller-save register, as
729 // there are more than two core caller-save registers on ARM64
730 // (meaning it is possible to find one which is different from
731 // `ref` and `obj`).
732 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
733 LOG(FATAL) << "Could not find a free register";
734 UNREACHABLE();
735 }
736
737 HInstruction* const instruction_;
738 const Location out_;
739 const Location ref_;
740 const Location obj_;
741 const uint32_t offset_;
742 // An additional location containing an index to an array.
743 // Only used for HArrayGet and the UnsafeGetObject &
744 // UnsafeGetObjectVolatile intrinsics.
745 const Location index_;
746
747 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
748};
749
750// Slow path generating a read barrier for a GC root.
751class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
752 public:
753 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
754 : instruction_(instruction), out_(out), root_(root) {}
755
756 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
757 LocationSummary* locations = instruction_->GetLocations();
758 Primitive::Type type = Primitive::kPrimNot;
759 DCHECK(locations->CanCall());
760 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
761 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
762
763 __ Bind(GetEntryLabel());
764 SaveLiveRegisters(codegen, locations);
765
766 InvokeRuntimeCallingConvention calling_convention;
767 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
768 // The argument of the ReadBarrierForRootSlow is not a managed
769 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
770 // thus we need a 64-bit move here, and we cannot use
771 //
772 // arm64_codegen->MoveLocation(
773 // LocationFrom(calling_convention.GetRegisterAt(0)),
774 // root_,
775 // type);
776 //
777 // which would emit a 32-bit move, as `type` is a (32-bit wide)
778 // reference type (`Primitive::kPrimNot`).
779 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
780 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
781 instruction_,
782 instruction_->GetDexPc(),
783 this);
784 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
785 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
786
787 RestoreLiveRegisters(codegen, locations);
788 __ B(GetExitLabel());
789 }
790
791 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
792
793 private:
794 HInstruction* const instruction_;
795 const Location out_;
796 const Location root_;
797
798 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
799};
800
Alexandre Rames5319def2014-10-23 10:03:10 +0100801#undef __
802
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100803Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100804 Location next_location;
805 if (type == Primitive::kPrimVoid) {
806 LOG(FATAL) << "Unreachable type " << type;
807 }
808
Alexandre Rames542361f2015-01-29 16:57:31 +0000809 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100810 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
811 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000812 } else if (!Primitive::IsFloatingPointType(type) &&
813 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000814 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
815 } else {
816 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000817 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
818 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100819 }
820
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000821 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000822 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100823 return next_location;
824}
825
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100826Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100827 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100828}
829
Serban Constantinescu579885a2015-02-22 20:51:33 +0000830CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
831 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100832 const CompilerOptions& compiler_options,
833 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100834 : CodeGenerator(graph,
835 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000836 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000837 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000838 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000839 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100840 compiler_options,
841 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100842 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800843 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100844 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000845 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000846 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000847 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100848 uint64_literals_(std::less<uint64_t>(),
849 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
850 method_patches_(MethodReferenceComparator(),
851 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
852 call_patches_(MethodReferenceComparator(),
853 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
854 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000855 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000856 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000857 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000858}
Alexandre Rames5319def2014-10-23 10:03:10 +0100859
Alexandre Rames67555f72014-11-18 10:55:16 +0000860#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100861
Zheng Xu3927c8b2015-11-18 17:46:25 +0800862void CodeGeneratorARM64::EmitJumpTables() {
863 for (auto jump_table : jump_tables_) {
864 jump_table->EmitTable(this);
865 }
866}
867
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000868void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800869 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000870 // Ensure we emit the literal pool.
871 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000872
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000873 CodeGenerator::Finalize(allocator);
874}
875
Zheng Xuad4450e2015-04-17 18:48:56 +0800876void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
877 // Note: There are 6 kinds of moves:
878 // 1. constant -> GPR/FPR (non-cycle)
879 // 2. constant -> stack (non-cycle)
880 // 3. GPR/FPR -> GPR/FPR
881 // 4. GPR/FPR -> stack
882 // 5. stack -> GPR/FPR
883 // 6. stack -> stack (non-cycle)
884 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
885 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
886 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
887 // dependency.
888 vixl_temps_.Open(GetVIXLAssembler());
889}
890
891void ParallelMoveResolverARM64::FinishEmitNativeCode() {
892 vixl_temps_.Close();
893}
894
895Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
896 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
897 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
898 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
899 Location scratch = GetScratchLocation(kind);
900 if (!scratch.Equals(Location::NoLocation())) {
901 return scratch;
902 }
903 // Allocate from VIXL temp registers.
904 if (kind == Location::kRegister) {
905 scratch = LocationFrom(vixl_temps_.AcquireX());
906 } else {
907 DCHECK(kind == Location::kFpuRegister);
908 scratch = LocationFrom(vixl_temps_.AcquireD());
909 }
910 AddScratchLocation(scratch);
911 return scratch;
912}
913
914void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
915 if (loc.IsRegister()) {
916 vixl_temps_.Release(XRegisterFrom(loc));
917 } else {
918 DCHECK(loc.IsFpuRegister());
919 vixl_temps_.Release(DRegisterFrom(loc));
920 }
921 RemoveScratchLocation(loc);
922}
923
Alexandre Rames3e69f162014-12-10 10:36:50 +0000924void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100925 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100926 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000927}
928
Alexandre Rames5319def2014-10-23 10:03:10 +0100929void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100930 MacroAssembler* masm = GetVIXLAssembler();
931 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000932 __ Bind(&frame_entry_label_);
933
Serban Constantinescu02164b32014-11-13 14:05:07 +0000934 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
935 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100936 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000937 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000938 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000939 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000940 __ Ldr(wzr, MemOperand(temp, 0));
941 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000942 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100943
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000944 if (!HasEmptyFrame()) {
945 int frame_size = GetFrameSize();
946 // Stack layout:
947 // sp[frame_size - 8] : lr.
948 // ... : other preserved core registers.
949 // ... : other preserved fp registers.
950 // ... : reserved frame space.
951 // sp[0] : current method.
952 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100953 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800954 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
955 frame_size - GetCoreSpillSize());
956 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
957 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000958 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100959}
960
961void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100962 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100963 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000964 if (!HasEmptyFrame()) {
965 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800966 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
967 frame_size - FrameEntrySpillSize());
968 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
969 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000970 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100971 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000972 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100973 __ Ret();
974 GetAssembler()->cfi().RestoreState();
975 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100976}
977
Zheng Xuda403092015-04-24 17:35:39 +0800978vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
979 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
980 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
981 core_spill_mask_);
982}
983
984vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
985 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
986 GetNumberOfFloatingPointRegisters()));
987 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
988 fpu_spill_mask_);
989}
990
Alexandre Rames5319def2014-10-23 10:03:10 +0100991void CodeGeneratorARM64::Bind(HBasicBlock* block) {
992 __ Bind(GetLabelOf(block));
993}
994
Alexandre Rames5319def2014-10-23 10:03:10 +0100995void CodeGeneratorARM64::Move(HInstruction* instruction,
996 Location location,
997 HInstruction* move_for) {
998 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100999 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001000 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +01001001
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001002 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001003 MoveLocation(location,
1004 Location::DoubleStackSlot(kCurrentMethodStackOffset),
1005 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001006 } else if (locations != nullptr && locations->Out().Equals(location)) {
1007 return;
1008 } else if (instruction->IsIntConstant()
1009 || instruction->IsLongConstant()
1010 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001011 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +01001012 if (location.IsRegister()) {
1013 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001014 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +01001015 (instruction->IsLongConstant() && dst.Is64Bits()));
1016 __ Mov(dst, value);
1017 } else {
1018 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001019 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001020 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
1021 ? temps.AcquireW()
1022 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +01001023 __ Mov(temp, value);
1024 __ Str(temp, StackOperandFrom(location));
1025 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001026 } else if (instruction->IsTemporary()) {
1027 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001028 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001029 } else if (instruction->IsLoadLocal()) {
1030 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +00001031 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001032 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001033 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001034 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001035 }
1036
1037 } else {
1038 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001039 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001040 }
1041}
1042
Calin Juravle175dc732015-08-25 15:42:32 +01001043void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1044 DCHECK(location.IsRegister());
1045 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1046}
1047
Calin Juravlee460d1d2015-09-29 04:52:17 +01001048void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1049 if (location.IsRegister()) {
1050 locations->AddTemp(location);
1051 } else {
1052 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1053 }
1054}
1055
Alexandre Rames5319def2014-10-23 10:03:10 +01001056Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1057 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001058
Alexandre Rames5319def2014-10-23 10:03:10 +01001059 switch (type) {
1060 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001061 case Primitive::kPrimInt:
1062 case Primitive::kPrimFloat:
1063 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1064
1065 case Primitive::kPrimLong:
1066 case Primitive::kPrimDouble:
1067 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1068
Alexandre Rames5319def2014-10-23 10:03:10 +01001069 case Primitive::kPrimBoolean:
1070 case Primitive::kPrimByte:
1071 case Primitive::kPrimChar:
1072 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001073 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001074 LOG(FATAL) << "Unexpected type " << type;
1075 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001076
Alexandre Rames5319def2014-10-23 10:03:10 +01001077 LOG(FATAL) << "Unreachable";
1078 return Location::NoLocation();
1079}
1080
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001081void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001082 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001083 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001084 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001085 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001086 if (value_can_be_null) {
1087 __ Cbz(value, &done);
1088 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001089 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1090 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001091 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001092 if (value_can_be_null) {
1093 __ Bind(&done);
1094 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001095}
1096
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001097void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
1098 // Blocked core registers:
1099 // lr : Runtime reserved.
1100 // tr : Runtime reserved.
1101 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1102 // ip1 : VIXL core temp.
1103 // ip0 : VIXL core temp.
1104 //
1105 // Blocked fp registers:
1106 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1108 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001109 while (!reserved_core_registers.IsEmpty()) {
1110 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1111 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001112
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001113 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001114 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001115 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1116 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001117
1118 if (is_baseline) {
1119 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
1120 while (!reserved_core_baseline_registers.IsEmpty()) {
1121 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
1122 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001123 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001124
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001125 if (is_baseline || GetGraph()->IsDebuggable()) {
1126 // Stubs do not save callee-save floating point registers. If the graph
1127 // is debuggable, we need to deal with these registers differently. For
1128 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001129 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
1130 while (!reserved_fp_baseline_registers.IsEmpty()) {
1131 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
1132 }
1133 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001134}
1135
1136Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
1137 if (type == Primitive::kPrimVoid) {
1138 LOG(FATAL) << "Unreachable type " << type;
1139 }
1140
Alexandre Rames542361f2015-01-29 16:57:31 +00001141 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001142 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
1143 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001144 return Location::FpuRegisterLocation(reg);
1145 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001146 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
1147 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001148 return Location::RegisterLocation(reg);
1149 }
1150}
1151
Alexandre Rames3e69f162014-12-10 10:36:50 +00001152size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1153 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1154 __ Str(reg, MemOperand(sp, stack_index));
1155 return kArm64WordSize;
1156}
1157
1158size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1159 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1160 __ Ldr(reg, MemOperand(sp, stack_index));
1161 return kArm64WordSize;
1162}
1163
1164size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1165 FPRegister reg = FPRegister(reg_id, kDRegSize);
1166 __ Str(reg, MemOperand(sp, stack_index));
1167 return kArm64WordSize;
1168}
1169
1170size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1171 FPRegister reg = FPRegister(reg_id, kDRegSize);
1172 __ Ldr(reg, MemOperand(sp, stack_index));
1173 return kArm64WordSize;
1174}
1175
Alexandre Rames5319def2014-10-23 10:03:10 +01001176void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001177 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001178}
1179
1180void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001181 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001182}
1183
Alexandre Rames67555f72014-11-18 10:55:16 +00001184void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001185 if (constant->IsIntConstant()) {
1186 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1187 } else if (constant->IsLongConstant()) {
1188 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1189 } else if (constant->IsNullConstant()) {
1190 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001191 } else if (constant->IsFloatConstant()) {
1192 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1193 } else {
1194 DCHECK(constant->IsDoubleConstant());
1195 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1196 }
1197}
1198
Alexandre Rames3e69f162014-12-10 10:36:50 +00001199
1200static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1201 DCHECK(constant.IsConstant());
1202 HConstant* cst = constant.GetConstant();
1203 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001204 // Null is mapped to a core W register, which we associate with kPrimInt.
1205 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1207 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1208 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1209}
1210
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211void CodeGeneratorARM64::MoveLocation(Location destination,
1212 Location source,
1213 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001214 if (source.Equals(destination)) {
1215 return;
1216 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001217
1218 // A valid move can always be inferred from the destination and source
1219 // locations. When moving from and to a register, the argument type can be
1220 // used to generate 32bit instead of 64bit moves. In debug mode we also
1221 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001222 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001223
1224 if (destination.IsRegister() || destination.IsFpuRegister()) {
1225 if (unspecified_type) {
1226 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1227 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001228 (src_cst != nullptr && (src_cst->IsIntConstant()
1229 || src_cst->IsFloatConstant()
1230 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001231 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001232 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001233 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001234 // If the source is a double stack slot or a 64bit constant, a 64bit
1235 // type is appropriate. Else the source is a register, and since the
1236 // type has not been specified, we chose a 64bit type to force a 64bit
1237 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001238 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001239 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1242 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1243 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001244 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1245 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1246 __ Ldr(dst, StackOperandFrom(source));
1247 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001248 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001251 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001253 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001254 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1256 ? Primitive::kPrimLong
1257 : Primitive::kPrimInt;
1258 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1259 }
1260 } else {
1261 DCHECK(source.IsFpuRegister());
1262 if (destination.IsRegister()) {
1263 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1264 ? Primitive::kPrimDouble
1265 : Primitive::kPrimFloat;
1266 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1267 } else {
1268 DCHECK(destination.IsFpuRegister());
1269 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001270 }
1271 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001272 } else { // The destination is not a register. It must be a stack slot.
1273 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1274 if (source.IsRegister() || source.IsFpuRegister()) {
1275 if (unspecified_type) {
1276 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001278 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001279 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 }
1281 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1283 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1284 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001285 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1287 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001288 UseScratchRegisterScope temps(GetVIXLAssembler());
1289 HConstant* src_cst = source.GetConstant();
1290 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001291 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001292 temp = temps.AcquireW();
1293 } else if (src_cst->IsLongConstant()) {
1294 temp = temps.AcquireX();
1295 } else if (src_cst->IsFloatConstant()) {
1296 temp = temps.AcquireS();
1297 } else {
1298 DCHECK(src_cst->IsDoubleConstant());
1299 temp = temps.AcquireD();
1300 }
1301 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001303 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001304 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001305 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001306 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001307 // There is generally less pressure on FP registers.
1308 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001309 __ Ldr(temp, StackOperandFrom(source));
1310 __ Str(temp, StackOperandFrom(destination));
1311 }
1312 }
1313}
1314
1315void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001316 CPURegister dst,
1317 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001318 switch (type) {
1319 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001320 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001321 break;
1322 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001324 break;
1325 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 break;
1328 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001329 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001330 break;
1331 case Primitive::kPrimInt:
1332 case Primitive::kPrimNot:
1333 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001334 case Primitive::kPrimFloat:
1335 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001336 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001337 __ Ldr(dst, src);
1338 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 case Primitive::kPrimVoid:
1340 LOG(FATAL) << "Unreachable type " << type;
1341 }
1342}
1343
Calin Juravle77520bc2015-01-12 18:45:46 +00001344void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 CPURegister dst,
1346 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001347 MacroAssembler* masm = GetVIXLAssembler();
1348 BlockPoolsScope block_pools(masm);
1349 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001350 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001351 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001352
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001353 DCHECK(!src.IsPreIndex());
1354 DCHECK(!src.IsPostIndex());
1355
1356 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001357 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 MemOperand base = MemOperand(temp_base);
1359 switch (type) {
1360 case Primitive::kPrimBoolean:
1361 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001362 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001363 break;
1364 case Primitive::kPrimByte:
1365 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001366 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001367 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1368 break;
1369 case Primitive::kPrimChar:
1370 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001371 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001372 break;
1373 case Primitive::kPrimShort:
1374 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001375 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001376 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1377 break;
1378 case Primitive::kPrimInt:
1379 case Primitive::kPrimNot:
1380 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001381 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001382 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001383 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 break;
1385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble: {
1387 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001388 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001389
1390 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1391 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001392 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001393 __ Fmov(FPRegister(dst), temp);
1394 break;
1395 }
1396 case Primitive::kPrimVoid:
1397 LOG(FATAL) << "Unreachable type " << type;
1398 }
1399}
1400
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001401void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 CPURegister src,
1403 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001404 switch (type) {
1405 case Primitive::kPrimBoolean:
1406 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001407 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001408 break;
1409 case Primitive::kPrimChar:
1410 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001412 break;
1413 case Primitive::kPrimInt:
1414 case Primitive::kPrimNot:
1415 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001416 case Primitive::kPrimFloat:
1417 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001418 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001419 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001420 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421 case Primitive::kPrimVoid:
1422 LOG(FATAL) << "Unreachable type " << type;
1423 }
1424}
1425
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001426void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1427 CPURegister src,
1428 const MemOperand& dst) {
1429 UseScratchRegisterScope temps(GetVIXLAssembler());
1430 Register temp_base = temps.AcquireX();
1431
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001432 DCHECK(!dst.IsPreIndex());
1433 DCHECK(!dst.IsPostIndex());
1434
1435 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001436 Operand op = OperandFromMemOperand(dst);
1437 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001438 MemOperand base = MemOperand(temp_base);
1439 switch (type) {
1440 case Primitive::kPrimBoolean:
1441 case Primitive::kPrimByte:
1442 __ Stlrb(Register(src), base);
1443 break;
1444 case Primitive::kPrimChar:
1445 case Primitive::kPrimShort:
1446 __ Stlrh(Register(src), base);
1447 break;
1448 case Primitive::kPrimInt:
1449 case Primitive::kPrimNot:
1450 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001451 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001452 __ Stlr(Register(src), base);
1453 break;
1454 case Primitive::kPrimFloat:
1455 case Primitive::kPrimDouble: {
1456 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001457 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001458
1459 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1460 __ Fmov(temp, FPRegister(src));
1461 __ Stlr(temp, base);
1462 break;
1463 }
1464 case Primitive::kPrimVoid:
1465 LOG(FATAL) << "Unreachable type " << type;
1466 }
1467}
1468
Calin Juravle175dc732015-08-25 15:42:32 +01001469void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1470 HInstruction* instruction,
1471 uint32_t dex_pc,
1472 SlowPathCode* slow_path) {
1473 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1474 instruction,
1475 dex_pc,
1476 slow_path);
1477}
1478
Alexandre Rames67555f72014-11-18 10:55:16 +00001479void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1480 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001481 uint32_t dex_pc,
1482 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001483 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001484 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001485 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1486 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001487 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001488}
1489
1490void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1491 vixl::Register class_reg) {
1492 UseScratchRegisterScope temps(GetVIXLAssembler());
1493 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001494 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001495 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001496
Serban Constantinescu02164b32014-11-13 14:05:07 +00001497 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001498 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001499 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1500 __ Add(temp, class_reg, status_offset);
1501 __ Ldar(temp, HeapOperand(temp));
1502 __ Cmp(temp, mirror::Class::kStatusInitialized);
1503 __ B(lt, slow_path->GetEntryLabel());
1504 } else {
1505 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1506 __ Cmp(temp, mirror::Class::kStatusInitialized);
1507 __ B(lt, slow_path->GetEntryLabel());
1508 __ Dmb(InnerShareable, BarrierReads);
1509 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001510 __ Bind(slow_path->GetExitLabel());
1511}
Alexandre Rames5319def2014-10-23 10:03:10 +01001512
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001513void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1514 BarrierType type = BarrierAll;
1515
1516 switch (kind) {
1517 case MemBarrierKind::kAnyAny:
1518 case MemBarrierKind::kAnyStore: {
1519 type = BarrierAll;
1520 break;
1521 }
1522 case MemBarrierKind::kLoadAny: {
1523 type = BarrierReads;
1524 break;
1525 }
1526 case MemBarrierKind::kStoreStore: {
1527 type = BarrierWrites;
1528 break;
1529 }
1530 default:
1531 LOG(FATAL) << "Unexpected memory barrier " << kind;
1532 }
1533 __ Dmb(InnerShareable, type);
1534}
1535
Serban Constantinescu02164b32014-11-13 14:05:07 +00001536void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1537 HBasicBlock* successor) {
1538 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001539 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1540 if (slow_path == nullptr) {
1541 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1542 instruction->SetSlowPath(slow_path);
1543 codegen_->AddSlowPath(slow_path);
1544 if (successor != nullptr) {
1545 DCHECK(successor->IsLoopHeader());
1546 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1547 }
1548 } else {
1549 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1550 }
1551
Serban Constantinescu02164b32014-11-13 14:05:07 +00001552 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1553 Register temp = temps.AcquireW();
1554
1555 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1556 if (successor == nullptr) {
1557 __ Cbnz(temp, slow_path->GetEntryLabel());
1558 __ Bind(slow_path->GetReturnLabel());
1559 } else {
1560 __ Cbz(temp, codegen_->GetLabelOf(successor));
1561 __ B(slow_path->GetEntryLabel());
1562 // slow_path will return to GetLabelOf(successor).
1563 }
1564}
1565
Alexandre Rames5319def2014-10-23 10:03:10 +01001566InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1567 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001568 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001569 assembler_(codegen->GetAssembler()),
1570 codegen_(codegen) {}
1571
1572#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001573 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001574
1575#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1576
1577enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001578 // Using a base helps identify when we hit such breakpoints.
1579 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001580#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1581 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1582#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1583};
1584
1585#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001586 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001587 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1588 } \
1589 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1591 locations->SetOut(Location::Any()); \
1592 }
1593 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1594#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1595
1596#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001597#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001598
Alexandre Rames67555f72014-11-18 10:55:16 +00001599void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 DCHECK_EQ(instr->InputCount(), 2U);
1601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1602 Primitive::Type type = instr->GetResultType();
1603 switch (type) {
1604 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001605 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001606 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001607 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001608 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001609 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001610
1611 case Primitive::kPrimFloat:
1612 case Primitive::kPrimDouble:
1613 locations->SetInAt(0, Location::RequiresFpuRegister());
1614 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001615 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001616 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001617
Alexandre Rames5319def2014-10-23 10:03:10 +01001618 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001619 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001620 }
1621}
1622
Alexandre Rames09a99962015-04-15 11:47:56 +01001623void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001624 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1625
1626 bool object_field_get_with_read_barrier =
1627 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001628 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001629 new (GetGraph()->GetArena()) LocationSummary(instruction,
1630 object_field_get_with_read_barrier ?
1631 LocationSummary::kCallOnSlowPath :
1632 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001633 locations->SetInAt(0, Location::RequiresRegister());
1634 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1635 locations->SetOut(Location::RequiresFpuRegister());
1636 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001637 // The output overlaps for an object field get when read barriers
1638 // are enabled: we do not want the load to overwrite the object's
1639 // location, as we need it to emit the read barrier.
1640 locations->SetOut(
1641 Location::RequiresRegister(),
1642 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001643 }
1644}
1645
1646void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1647 const FieldInfo& field_info) {
1648 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001649 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001650 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001651
1652 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1653 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1654
1655 if (field_info.IsVolatile()) {
1656 if (use_acquire_release) {
1657 // NB: LoadAcquire will record the pc info if needed.
1658 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1659 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001660 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001661 codegen_->MaybeRecordImplicitNullCheck(instruction);
1662 // For IRIW sequential consistency kLoadAny is not sufficient.
1663 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1664 }
1665 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001666 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001667 codegen_->MaybeRecordImplicitNullCheck(instruction);
1668 }
Roland Levillain4d027112015-07-01 15:41:14 +01001669
1670 if (field_type == Primitive::kPrimNot) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001671 LocationSummary* locations = instruction->GetLocations();
1672 Location base = locations->InAt(0);
1673 Location out = locations->Out();
1674 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
1675 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01001676 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001677}
1678
1679void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1680 LocationSummary* locations =
1681 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1682 locations->SetInAt(0, Location::RequiresRegister());
1683 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1684 locations->SetInAt(1, Location::RequiresFpuRegister());
1685 } else {
1686 locations->SetInAt(1, Location::RequiresRegister());
1687 }
1688}
1689
1690void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001691 const FieldInfo& field_info,
1692 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001693 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001694 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001695
1696 Register obj = InputRegisterAt(instruction, 0);
1697 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001698 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001699 Offset offset = field_info.GetFieldOffset();
1700 Primitive::Type field_type = field_info.GetFieldType();
1701 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1702
Roland Levillain4d027112015-07-01 15:41:14 +01001703 {
1704 // We use a block to end the scratch scope before the write barrier, thus
1705 // freeing the temporary registers so they can be used in `MarkGCCard`.
1706 UseScratchRegisterScope temps(GetVIXLAssembler());
1707
1708 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1709 DCHECK(value.IsW());
1710 Register temp = temps.AcquireW();
1711 __ Mov(temp, value.W());
1712 GetAssembler()->PoisonHeapReference(temp.W());
1713 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001714 }
Roland Levillain4d027112015-07-01 15:41:14 +01001715
1716 if (field_info.IsVolatile()) {
1717 if (use_acquire_release) {
1718 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1719 codegen_->MaybeRecordImplicitNullCheck(instruction);
1720 } else {
1721 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1722 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1723 codegen_->MaybeRecordImplicitNullCheck(instruction);
1724 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1725 }
1726 } else {
1727 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1728 codegen_->MaybeRecordImplicitNullCheck(instruction);
1729 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001730 }
1731
1732 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001733 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001734 }
1735}
1736
Alexandre Rames67555f72014-11-18 10:55:16 +00001737void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001738 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001739
1740 switch (type) {
1741 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001742 case Primitive::kPrimLong: {
1743 Register dst = OutputRegister(instr);
1744 Register lhs = InputRegisterAt(instr, 0);
1745 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001746 if (instr->IsAdd()) {
1747 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 } else if (instr->IsAnd()) {
1749 __ And(dst, lhs, rhs);
1750 } else if (instr->IsOr()) {
1751 __ Orr(dst, lhs, rhs);
1752 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001753 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001754 } else if (instr->IsRor()) {
1755 if (rhs.IsImmediate()) {
1756 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1757 __ Ror(dst, lhs, shift);
1758 } else {
1759 // Ensure shift distance is in the same size register as the result. If
1760 // we are rotating a long and the shift comes in a w register originally,
1761 // we don't need to sxtw for use as an x since the shift distances are
1762 // all & reg_bits - 1.
1763 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1764 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001765 } else {
1766 DCHECK(instr->IsXor());
1767 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001768 }
1769 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001770 }
1771 case Primitive::kPrimFloat:
1772 case Primitive::kPrimDouble: {
1773 FPRegister dst = OutputFPRegister(instr);
1774 FPRegister lhs = InputFPRegisterAt(instr, 0);
1775 FPRegister rhs = InputFPRegisterAt(instr, 1);
1776 if (instr->IsAdd()) {
1777 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001778 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001779 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001780 } else {
1781 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001782 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001783 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001784 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001785 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001786 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001787 }
1788}
1789
Serban Constantinescu02164b32014-11-13 14:05:07 +00001790void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1791 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1792
1793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1794 Primitive::Type type = instr->GetResultType();
1795 switch (type) {
1796 case Primitive::kPrimInt:
1797 case Primitive::kPrimLong: {
1798 locations->SetInAt(0, Location::RequiresRegister());
1799 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1800 locations->SetOut(Location::RequiresRegister());
1801 break;
1802 }
1803 default:
1804 LOG(FATAL) << "Unexpected shift type " << type;
1805 }
1806}
1807
1808void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1809 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1810
1811 Primitive::Type type = instr->GetType();
1812 switch (type) {
1813 case Primitive::kPrimInt:
1814 case Primitive::kPrimLong: {
1815 Register dst = OutputRegister(instr);
1816 Register lhs = InputRegisterAt(instr, 0);
1817 Operand rhs = InputOperandAt(instr, 1);
1818 if (rhs.IsImmediate()) {
1819 uint32_t shift_value = (type == Primitive::kPrimInt)
1820 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1821 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1822 if (instr->IsShl()) {
1823 __ Lsl(dst, lhs, shift_value);
1824 } else if (instr->IsShr()) {
1825 __ Asr(dst, lhs, shift_value);
1826 } else {
1827 __ Lsr(dst, lhs, shift_value);
1828 }
1829 } else {
1830 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1831
1832 if (instr->IsShl()) {
1833 __ Lsl(dst, lhs, rhs_reg);
1834 } else if (instr->IsShr()) {
1835 __ Asr(dst, lhs, rhs_reg);
1836 } else {
1837 __ Lsr(dst, lhs, rhs_reg);
1838 }
1839 }
1840 break;
1841 }
1842 default:
1843 LOG(FATAL) << "Unexpected shift operation type " << type;
1844 }
1845}
1846
Alexandre Rames5319def2014-10-23 10:03:10 +01001847void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001848 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001849}
1850
1851void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001852 HandleBinaryOp(instruction);
1853}
1854
1855void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1856 HandleBinaryOp(instruction);
1857}
1858
1859void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1860 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001861}
1862
Alexandre Rames8626b742015-11-25 16:28:08 +00001863void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1864 HArm64DataProcWithShifterOp* instruction) {
1865 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1866 instruction->GetType() == Primitive::kPrimLong);
1867 LocationSummary* locations =
1868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1869 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1870 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1871 } else {
1872 locations->SetInAt(0, Location::RequiresRegister());
1873 }
1874 locations->SetInAt(1, Location::RequiresRegister());
1875 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1876}
1877
1878void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1879 HArm64DataProcWithShifterOp* instruction) {
1880 Primitive::Type type = instruction->GetType();
1881 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1882 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1883 Register out = OutputRegister(instruction);
1884 Register left;
1885 if (kind != HInstruction::kNeg) {
1886 left = InputRegisterAt(instruction, 0);
1887 }
1888 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1889 // shifter operand operation, the IR generating `right_reg` (input to the type
1890 // conversion) can have a different type from the current instruction's type,
1891 // so we manually indicate the type.
1892 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
1893 int64_t shift_amount = (type == Primitive::kPrimInt)
1894 ? static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxIntShiftValue)
1895 : static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxLongShiftValue);
1896
1897 Operand right_operand(0);
1898
1899 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1900 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1901 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1902 } else {
1903 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1904 }
1905
1906 // Logical binary operations do not support extension operations in the
1907 // operand. Note that VIXL would still manage if it was passed by generating
1908 // the extension as a separate instruction.
1909 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1910 DCHECK(!right_operand.IsExtendedRegister() ||
1911 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1912 kind != HInstruction::kNeg));
1913 switch (kind) {
1914 case HInstruction::kAdd:
1915 __ Add(out, left, right_operand);
1916 break;
1917 case HInstruction::kAnd:
1918 __ And(out, left, right_operand);
1919 break;
1920 case HInstruction::kNeg:
1921 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1922 __ Neg(out, right_operand);
1923 break;
1924 case HInstruction::kOr:
1925 __ Orr(out, left, right_operand);
1926 break;
1927 case HInstruction::kSub:
1928 __ Sub(out, left, right_operand);
1929 break;
1930 case HInstruction::kXor:
1931 __ Eor(out, left, right_operand);
1932 break;
1933 default:
1934 LOG(FATAL) << "Unexpected operation kind: " << kind;
1935 UNREACHABLE();
1936 }
1937}
1938
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001939void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001940 // The read barrier instrumentation does not support the
1941 // HArm64IntermediateAddress instruction yet.
1942 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001943 LocationSummary* locations =
1944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1945 locations->SetInAt(0, Location::RequiresRegister());
1946 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1947 locations->SetOut(Location::RequiresRegister());
1948}
1949
1950void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1951 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001952 // The read barrier instrumentation does not support the
1953 // HArm64IntermediateAddress instruction yet.
1954 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001955 __ Add(OutputRegister(instruction),
1956 InputRegisterAt(instruction, 0),
1957 Operand(InputOperandAt(instruction, 1)));
1958}
1959
Alexandre Rames418318f2015-11-20 15:55:47 +00001960void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1961 LocationSummary* locations =
1962 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1963 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1964 Location::RequiresRegister());
1965 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1966 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1967 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1968}
1969
1970void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1971 Register res = OutputRegister(instr);
1972 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
1973 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
1974 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
1975
1976 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1977 // This fixup should be carried out for all multiply-accumulate instructions:
1978 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1979 if (instr->GetType() == Primitive::kPrimLong &&
1980 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1981 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
1982 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
1983 if (prev->IsLoadOrStore()) {
1984 // Make sure we emit only exactly one nop.
1985 vixl::CodeBufferCheckScope scope(masm,
1986 vixl::kInstructionSize,
1987 vixl::CodeBufferCheckScope::kCheck,
1988 vixl::CodeBufferCheckScope::kExactSize);
1989 __ nop();
1990 }
1991 }
1992
1993 if (instr->GetOpKind() == HInstruction::kAdd) {
1994 __ Madd(res, mul_left, mul_right, accumulator);
1995 } else {
1996 DCHECK(instr->GetOpKind() == HInstruction::kSub);
1997 __ Msub(res, mul_left, mul_right, accumulator);
1998 }
1999}
2000
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002001void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002002 bool object_array_get_with_read_barrier =
2003 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002004 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002005 new (GetGraph()->GetArena()) LocationSummary(instruction,
2006 object_array_get_with_read_barrier ?
2007 LocationSummary::kCallOnSlowPath :
2008 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002009 locations->SetInAt(0, Location::RequiresRegister());
2010 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002011 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2012 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2013 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002014 // The output overlaps in the case of an object array get with
2015 // read barriers enabled: we do not want the move to overwrite the
2016 // array's location, as we need it to emit the read barrier.
2017 locations->SetOut(
2018 Location::RequiresRegister(),
2019 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002020 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002021}
2022
2023void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002024 Primitive::Type type = instruction->GetType();
2025 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002026 LocationSummary* locations = instruction->GetLocations();
2027 Location index = locations->InAt(1);
2028 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002029 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002030 CPURegister dest = OutputCPURegister(instruction);
2031
Alexandre Ramesd921d642015-04-16 15:07:16 +01002032 MacroAssembler* masm = GetVIXLAssembler();
2033 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002034 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002035 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002036
2037 if (index.IsConstant()) {
2038 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002039 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002040 } else {
2041 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002042 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002043 // The read barrier instrumentation does not support the
2044 // HArm64IntermediateAddress instruction yet.
2045 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002046 // We do not need to compute the intermediate address from the array: the
2047 // input instruction has done it already. See the comment in
2048 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2049 if (kIsDebugBuild) {
2050 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2051 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2052 }
2053 temp = obj;
2054 } else {
2055 __ Add(temp, obj, offset);
2056 }
Alexandre Rames82000b02015-07-07 11:34:16 +01002057 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002058 }
2059
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002060 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00002061 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002062
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002063 if (type == Primitive::kPrimNot) {
2064 static_assert(
2065 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2066 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2067 Location obj_loc = locations->InAt(0);
2068 Location out = locations->Out();
2069 if (index.IsConstant()) {
2070 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
2071 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002072 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset, index);
2073 }
Roland Levillain4d027112015-07-01 15:41:14 +01002074 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002075}
2076
Alexandre Rames5319def2014-10-23 10:03:10 +01002077void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2079 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002081}
2082
2083void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002084 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002085 __ Ldr(OutputRegister(instruction),
2086 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002087 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002088}
2089
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002090void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002091 Primitive::Type value_type = instruction->GetComponentType();
2092
2093 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2094 bool object_array_set_with_read_barrier =
2095 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2097 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002098 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2099 LocationSummary::kCallOnSlowPath :
2100 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002101 locations->SetInAt(0, Location::RequiresRegister());
2102 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002103 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002104 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002105 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002106 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002107 }
2108}
2109
2110void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2111 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002112 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002113 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002114 bool needs_write_barrier =
2115 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002116
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002117 Register array = InputRegisterAt(instruction, 0);
2118 CPURegister value = InputCPURegisterAt(instruction, 2);
2119 CPURegister source = value;
2120 Location index = locations->InAt(1);
2121 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2122 MemOperand destination = HeapOperand(array);
2123 MacroAssembler* masm = GetVIXLAssembler();
2124 BlockPoolsScope block_pools(masm);
2125
2126 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002127 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002128 if (index.IsConstant()) {
2129 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2130 destination = HeapOperand(array, offset);
2131 } else {
2132 UseScratchRegisterScope temps(masm);
2133 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002134 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002135 // The read barrier instrumentation does not support the
2136 // HArm64IntermediateAddress instruction yet.
2137 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002138 // We do not need to compute the intermediate address from the array: the
2139 // input instruction has done it already. See the comment in
2140 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2141 if (kIsDebugBuild) {
2142 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2143 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2144 }
2145 temp = array;
2146 } else {
2147 __ Add(temp, array, offset);
2148 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002149 destination = HeapOperand(temp,
2150 XRegisterFrom(index),
2151 LSL,
2152 Primitive::ComponentSizeShift(value_type));
2153 }
2154 codegen_->Store(value_type, value, destination);
2155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002156 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002157 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002158 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002159 vixl::Label done;
2160 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002161 {
2162 // We use a block to end the scratch scope before the write barrier, thus
2163 // freeing the temporary registers so they can be used in `MarkGCCard`.
2164 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002165 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002166 if (index.IsConstant()) {
2167 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002169 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002170 destination = HeapOperand(temp,
2171 XRegisterFrom(index),
2172 LSL,
2173 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002174 }
2175
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002176 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2177 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2178 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2179
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002180 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002181 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2182 codegen_->AddSlowPath(slow_path);
2183 if (instruction->GetValueCanBeNull()) {
2184 vixl::Label non_zero;
2185 __ Cbnz(Register(value), &non_zero);
2186 if (!index.IsConstant()) {
2187 __ Add(temp, array, offset);
2188 }
2189 __ Str(wzr, destination);
2190 codegen_->MaybeRecordImplicitNullCheck(instruction);
2191 __ B(&done);
2192 __ Bind(&non_zero);
2193 }
2194
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002195 if (kEmitCompilerReadBarrier) {
2196 // When read barriers are enabled, the type checking
2197 // instrumentation requires two read barriers:
2198 //
2199 // __ Mov(temp2, temp);
2200 // // /* HeapReference<Class> */ temp = temp->component_type_
2201 // __ Ldr(temp, HeapOperand(temp, component_offset));
2202 // codegen_->GenerateReadBarrier(
2203 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2204 //
2205 // // /* HeapReference<Class> */ temp2 = value->klass_
2206 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2207 // codegen_->GenerateReadBarrier(
2208 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2209 //
2210 // __ Cmp(temp, temp2);
2211 //
2212 // However, the second read barrier may trash `temp`, as it
2213 // is a temporary register, and as such would not be saved
2214 // along with live registers before calling the runtime (nor
2215 // restored afterwards). So in this case, we bail out and
2216 // delegate the work to the array set slow path.
2217 //
2218 // TODO: Extend the register allocator to support a new
2219 // "(locally) live temp" location so as to avoid always
2220 // going into the slow path when read barriers are enabled.
2221 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002222 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002223 Register temp2 = temps.AcquireSameSizeAs(array);
2224 // /* HeapReference<Class> */ temp = array->klass_
2225 __ Ldr(temp, HeapOperand(array, class_offset));
2226 codegen_->MaybeRecordImplicitNullCheck(instruction);
2227 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2228
2229 // /* HeapReference<Class> */ temp = temp->component_type_
2230 __ Ldr(temp, HeapOperand(temp, component_offset));
2231 // /* HeapReference<Class> */ temp2 = value->klass_
2232 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2233 // If heap poisoning is enabled, no need to unpoison `temp`
2234 // nor `temp2`, as we are comparing two poisoned references.
2235 __ Cmp(temp, temp2);
2236
2237 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2238 vixl::Label do_put;
2239 __ B(eq, &do_put);
2240 // If heap poisoning is enabled, the `temp` reference has
2241 // not been unpoisoned yet; unpoison it now.
2242 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2243
2244 // /* HeapReference<Class> */ temp = temp->super_class_
2245 __ Ldr(temp, HeapOperand(temp, super_offset));
2246 // If heap poisoning is enabled, no need to unpoison
2247 // `temp`, as we are comparing against null below.
2248 __ Cbnz(temp, slow_path->GetEntryLabel());
2249 __ Bind(&do_put);
2250 } else {
2251 __ B(ne, slow_path->GetEntryLabel());
2252 }
2253 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002254 }
2255 }
2256
2257 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002258 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002259 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002260 __ Mov(temp2, value.W());
2261 GetAssembler()->PoisonHeapReference(temp2);
2262 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002263 }
2264
2265 if (!index.IsConstant()) {
2266 __ Add(temp, array, offset);
2267 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002268 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002269
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002270 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002271 codegen_->MaybeRecordImplicitNullCheck(instruction);
2272 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002273 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002274
2275 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2276
2277 if (done.IsLinked()) {
2278 __ Bind(&done);
2279 }
2280
2281 if (slow_path != nullptr) {
2282 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002283 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002284 }
2285}
2286
Alexandre Rames67555f72014-11-18 10:55:16 +00002287void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002288 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2289 ? LocationSummary::kCallOnSlowPath
2290 : LocationSummary::kNoCall;
2291 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002292 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002293 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002294 if (instruction->HasUses()) {
2295 locations->SetOut(Location::SameAsFirstInput());
2296 }
2297}
2298
2299void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002300 BoundsCheckSlowPathARM64* slow_path =
2301 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002302 codegen_->AddSlowPath(slow_path);
2303
2304 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2305 __ B(slow_path->GetEntryLabel(), hs);
2306}
2307
Alexandre Rames67555f72014-11-18 10:55:16 +00002308void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2309 LocationSummary* locations =
2310 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2311 locations->SetInAt(0, Location::RequiresRegister());
2312 if (check->HasUses()) {
2313 locations->SetOut(Location::SameAsFirstInput());
2314 }
2315}
2316
2317void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2318 // We assume the class is not null.
2319 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2320 check->GetLoadClass(), check, check->GetDexPc(), true);
2321 codegen_->AddSlowPath(slow_path);
2322 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2323}
2324
Roland Levillain7f63c522015-07-13 15:54:55 +00002325static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2326 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2327 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2328}
2329
Serban Constantinescu02164b32014-11-13 14:05:07 +00002330void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002331 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002332 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2333 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002334 switch (in_type) {
2335 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002336 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002337 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2339 break;
2340 }
2341 case Primitive::kPrimFloat:
2342 case Primitive::kPrimDouble: {
2343 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002344 locations->SetInAt(1,
2345 IsFloatingPointZeroConstant(compare->InputAt(1))
2346 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2347 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002348 locations->SetOut(Location::RequiresRegister());
2349 break;
2350 }
2351 default:
2352 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2353 }
2354}
2355
2356void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2357 Primitive::Type in_type = compare->InputAt(0)->GetType();
2358
2359 // 0 if: left == right
2360 // 1 if: left > right
2361 // -1 if: left < right
2362 switch (in_type) {
2363 case Primitive::kPrimLong: {
2364 Register result = OutputRegister(compare);
2365 Register left = InputRegisterAt(compare, 0);
2366 Operand right = InputOperandAt(compare, 1);
2367
2368 __ Cmp(left, right);
2369 __ Cset(result, ne);
2370 __ Cneg(result, result, lt);
2371 break;
2372 }
2373 case Primitive::kPrimFloat:
2374 case Primitive::kPrimDouble: {
2375 Register result = OutputRegister(compare);
2376 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002377 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002378 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2379 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002380 __ Fcmp(left, 0.0);
2381 } else {
2382 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2383 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002384 if (compare->IsGtBias()) {
2385 __ Cset(result, ne);
2386 } else {
2387 __ Csetm(result, ne);
2388 }
2389 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01002390 break;
2391 }
2392 default:
2393 LOG(FATAL) << "Unimplemented compare type " << in_type;
2394 }
2395}
2396
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002397void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002399
2400 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2401 locations->SetInAt(0, Location::RequiresFpuRegister());
2402 locations->SetInAt(1,
2403 IsFloatingPointZeroConstant(instruction->InputAt(1))
2404 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2405 : Location::RequiresFpuRegister());
2406 } else {
2407 // Integer cases.
2408 locations->SetInAt(0, Location::RequiresRegister());
2409 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2410 }
2411
Alexandre Rames5319def2014-10-23 10:03:10 +01002412 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002414 }
2415}
2416
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002417void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002418 if (!instruction->NeedsMaterialization()) {
2419 return;
2420 }
2421
2422 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002423 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002424 IfCondition if_cond = instruction->GetCondition();
2425 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002426
Roland Levillain7f63c522015-07-13 15:54:55 +00002427 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2428 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2429 if (locations->InAt(1).IsConstant()) {
2430 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2431 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2432 __ Fcmp(lhs, 0.0);
2433 } else {
2434 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2435 }
2436 __ Cset(res, arm64_cond);
2437 if (instruction->IsFPConditionTrueIfNaN()) {
2438 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
2439 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
2440 } else if (instruction->IsFPConditionFalseIfNaN()) {
2441 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
2442 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
2443 }
2444 } else {
2445 // Integer cases.
2446 Register lhs = InputRegisterAt(instruction, 0);
2447 Operand rhs = InputOperandAt(instruction, 1);
2448 __ Cmp(lhs, rhs);
2449 __ Cset(res, arm64_cond);
2450 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002451}
2452
2453#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2454 M(Equal) \
2455 M(NotEqual) \
2456 M(LessThan) \
2457 M(LessThanOrEqual) \
2458 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002459 M(GreaterThanOrEqual) \
2460 M(Below) \
2461 M(BelowOrEqual) \
2462 M(Above) \
2463 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002464#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002465void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2466void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002467FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002468#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002469#undef FOR_EACH_CONDITION_INSTRUCTION
2470
Zheng Xuc6667102015-05-15 16:08:45 +08002471void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2472 DCHECK(instruction->IsDiv() || instruction->IsRem());
2473
2474 LocationSummary* locations = instruction->GetLocations();
2475 Location second = locations->InAt(1);
2476 DCHECK(second.IsConstant());
2477
2478 Register out = OutputRegister(instruction);
2479 Register dividend = InputRegisterAt(instruction, 0);
2480 int64_t imm = Int64FromConstant(second.GetConstant());
2481 DCHECK(imm == 1 || imm == -1);
2482
2483 if (instruction->IsRem()) {
2484 __ Mov(out, 0);
2485 } else {
2486 if (imm == 1) {
2487 __ Mov(out, dividend);
2488 } else {
2489 __ Neg(out, dividend);
2490 }
2491 }
2492}
2493
2494void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2495 DCHECK(instruction->IsDiv() || instruction->IsRem());
2496
2497 LocationSummary* locations = instruction->GetLocations();
2498 Location second = locations->InAt(1);
2499 DCHECK(second.IsConstant());
2500
2501 Register out = OutputRegister(instruction);
2502 Register dividend = InputRegisterAt(instruction, 0);
2503 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002504 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002505 int ctz_imm = CTZ(abs_imm);
2506
2507 UseScratchRegisterScope temps(GetVIXLAssembler());
2508 Register temp = temps.AcquireSameSizeAs(out);
2509
2510 if (instruction->IsDiv()) {
2511 __ Add(temp, dividend, abs_imm - 1);
2512 __ Cmp(dividend, 0);
2513 __ Csel(out, temp, dividend, lt);
2514 if (imm > 0) {
2515 __ Asr(out, out, ctz_imm);
2516 } else {
2517 __ Neg(out, Operand(out, ASR, ctz_imm));
2518 }
2519 } else {
2520 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2521 __ Asr(temp, dividend, bits - 1);
2522 __ Lsr(temp, temp, bits - ctz_imm);
2523 __ Add(out, dividend, temp);
2524 __ And(out, out, abs_imm - 1);
2525 __ Sub(out, out, temp);
2526 }
2527}
2528
2529void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2530 DCHECK(instruction->IsDiv() || instruction->IsRem());
2531
2532 LocationSummary* locations = instruction->GetLocations();
2533 Location second = locations->InAt(1);
2534 DCHECK(second.IsConstant());
2535
2536 Register out = OutputRegister(instruction);
2537 Register dividend = InputRegisterAt(instruction, 0);
2538 int64_t imm = Int64FromConstant(second.GetConstant());
2539
2540 Primitive::Type type = instruction->GetResultType();
2541 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2542
2543 int64_t magic;
2544 int shift;
2545 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2546
2547 UseScratchRegisterScope temps(GetVIXLAssembler());
2548 Register temp = temps.AcquireSameSizeAs(out);
2549
2550 // temp = get_high(dividend * magic)
2551 __ Mov(temp, magic);
2552 if (type == Primitive::kPrimLong) {
2553 __ Smulh(temp, dividend, temp);
2554 } else {
2555 __ Smull(temp.X(), dividend, temp);
2556 __ Lsr(temp.X(), temp.X(), 32);
2557 }
2558
2559 if (imm > 0 && magic < 0) {
2560 __ Add(temp, temp, dividend);
2561 } else if (imm < 0 && magic > 0) {
2562 __ Sub(temp, temp, dividend);
2563 }
2564
2565 if (shift != 0) {
2566 __ Asr(temp, temp, shift);
2567 }
2568
2569 if (instruction->IsDiv()) {
2570 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2571 } else {
2572 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2573 // TODO: Strength reduction for msub.
2574 Register temp_imm = temps.AcquireSameSizeAs(out);
2575 __ Mov(temp_imm, imm);
2576 __ Msub(out, temp, temp_imm, dividend);
2577 }
2578}
2579
2580void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2581 DCHECK(instruction->IsDiv() || instruction->IsRem());
2582 Primitive::Type type = instruction->GetResultType();
2583 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2584
2585 LocationSummary* locations = instruction->GetLocations();
2586 Register out = OutputRegister(instruction);
2587 Location second = locations->InAt(1);
2588
2589 if (second.IsConstant()) {
2590 int64_t imm = Int64FromConstant(second.GetConstant());
2591
2592 if (imm == 0) {
2593 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2594 } else if (imm == 1 || imm == -1) {
2595 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002596 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002597 DivRemByPowerOfTwo(instruction);
2598 } else {
2599 DCHECK(imm <= -2 || imm >= 2);
2600 GenerateDivRemWithAnyConstant(instruction);
2601 }
2602 } else {
2603 Register dividend = InputRegisterAt(instruction, 0);
2604 Register divisor = InputRegisterAt(instruction, 1);
2605 if (instruction->IsDiv()) {
2606 __ Sdiv(out, dividend, divisor);
2607 } else {
2608 UseScratchRegisterScope temps(GetVIXLAssembler());
2609 Register temp = temps.AcquireSameSizeAs(out);
2610 __ Sdiv(temp, dividend, divisor);
2611 __ Msub(out, temp, divisor, dividend);
2612 }
2613 }
2614}
2615
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002616void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2617 LocationSummary* locations =
2618 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2619 switch (div->GetResultType()) {
2620 case Primitive::kPrimInt:
2621 case Primitive::kPrimLong:
2622 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002623 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002624 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2625 break;
2626
2627 case Primitive::kPrimFloat:
2628 case Primitive::kPrimDouble:
2629 locations->SetInAt(0, Location::RequiresFpuRegister());
2630 locations->SetInAt(1, Location::RequiresFpuRegister());
2631 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2632 break;
2633
2634 default:
2635 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2636 }
2637}
2638
2639void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2640 Primitive::Type type = div->GetResultType();
2641 switch (type) {
2642 case Primitive::kPrimInt:
2643 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002644 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002645 break;
2646
2647 case Primitive::kPrimFloat:
2648 case Primitive::kPrimDouble:
2649 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2650 break;
2651
2652 default:
2653 LOG(FATAL) << "Unexpected div type " << type;
2654 }
2655}
2656
Alexandre Rames67555f72014-11-18 10:55:16 +00002657void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002658 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2659 ? LocationSummary::kCallOnSlowPath
2660 : LocationSummary::kNoCall;
2661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002662 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2663 if (instruction->HasUses()) {
2664 locations->SetOut(Location::SameAsFirstInput());
2665 }
2666}
2667
2668void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2669 SlowPathCodeARM64* slow_path =
2670 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2671 codegen_->AddSlowPath(slow_path);
2672 Location value = instruction->GetLocations()->InAt(0);
2673
Alexandre Rames3e69f162014-12-10 10:36:50 +00002674 Primitive::Type type = instruction->GetType();
2675
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002676 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2677 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002678 return;
2679 }
2680
Alexandre Rames67555f72014-11-18 10:55:16 +00002681 if (value.IsConstant()) {
2682 int64_t divisor = Int64ConstantFrom(value);
2683 if (divisor == 0) {
2684 __ B(slow_path->GetEntryLabel());
2685 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002686 // A division by a non-null constant is valid. We don't need to perform
2687 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002688 }
2689 } else {
2690 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2691 }
2692}
2693
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002694void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2697 locations->SetOut(Location::ConstantLocation(constant));
2698}
2699
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002700void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2701 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002702 // Will be generated at use site.
2703}
2704
Alexandre Rames5319def2014-10-23 10:03:10 +01002705void LocationsBuilderARM64::VisitExit(HExit* exit) {
2706 exit->SetLocations(nullptr);
2707}
2708
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002709void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002710}
2711
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002712void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2713 LocationSummary* locations =
2714 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2715 locations->SetOut(Location::ConstantLocation(constant));
2716}
2717
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002718void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002719 // Will be generated at use site.
2720}
2721
David Brazdilfc6a86a2015-06-26 10:33:45 +00002722void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002723 DCHECK(!successor->IsExitBlock());
2724 HBasicBlock* block = got->GetBlock();
2725 HInstruction* previous = got->GetPrevious();
2726 HLoopInformation* info = block->GetLoopInformation();
2727
David Brazdil46e2a392015-03-16 17:31:52 +00002728 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002729 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2730 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2731 return;
2732 }
2733 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2734 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2735 }
2736 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002737 __ B(codegen_->GetLabelOf(successor));
2738 }
2739}
2740
David Brazdilfc6a86a2015-06-26 10:33:45 +00002741void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2742 got->SetLocations(nullptr);
2743}
2744
2745void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2746 HandleGoto(got, got->GetSuccessor());
2747}
2748
2749void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2750 try_boundary->SetLocations(nullptr);
2751}
2752
2753void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2754 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2755 if (!successor->IsExitBlock()) {
2756 HandleGoto(try_boundary, successor);
2757 }
2758}
2759
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002760void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002761 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002762 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002763 vixl::Label* false_target) {
2764 // FP branching requires both targets to be explicit. If either of the targets
2765 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2766 vixl::Label fallthrough_target;
2767 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002768
David Brazdil0debae72015-11-12 18:37:00 +00002769 if (true_target == nullptr && false_target == nullptr) {
2770 // Nothing to do. The code always falls through.
2771 return;
2772 } else if (cond->IsIntConstant()) {
2773 // Constant condition, statically compared against 1.
2774 if (cond->AsIntConstant()->IsOne()) {
2775 if (true_target != nullptr) {
2776 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002777 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002778 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002779 DCHECK(cond->AsIntConstant()->IsZero());
2780 if (false_target != nullptr) {
2781 __ B(false_target);
2782 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002783 }
David Brazdil0debae72015-11-12 18:37:00 +00002784 return;
2785 }
2786
2787 // The following code generates these patterns:
2788 // (1) true_target == nullptr && false_target != nullptr
2789 // - opposite condition true => branch to false_target
2790 // (2) true_target != nullptr && false_target == nullptr
2791 // - condition true => branch to true_target
2792 // (3) true_target != nullptr && false_target != nullptr
2793 // - condition true => branch to true_target
2794 // - branch to false_target
2795 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002796 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002797 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002798 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002799 if (true_target == nullptr) {
2800 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2801 } else {
2802 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2803 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002804 } else {
2805 // The condition instruction has not been materialized, use its inputs as
2806 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002807 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002808
David Brazdil0debae72015-11-12 18:37:00 +00002809 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002810 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002811 FPRegister lhs = InputFPRegisterAt(condition, 0);
2812 if (condition->GetLocations()->InAt(1).IsConstant()) {
2813 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2814 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2815 __ Fcmp(lhs, 0.0);
2816 } else {
2817 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2818 }
2819 if (condition->IsFPConditionTrueIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002820 __ B(vs, true_target == nullptr ? &fallthrough_target : true_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002821 } else if (condition->IsFPConditionFalseIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002822 __ B(vs, false_target == nullptr ? &fallthrough_target : false_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002823 }
David Brazdil0debae72015-11-12 18:37:00 +00002824 if (true_target == nullptr) {
2825 __ B(ARM64Condition(condition->GetOppositeCondition()), false_target);
2826 } else {
2827 __ B(ARM64Condition(condition->GetCondition()), true_target);
2828 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002829 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002830 // Integer cases.
2831 Register lhs = InputRegisterAt(condition, 0);
2832 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002833
2834 Condition arm64_cond;
2835 vixl::Label* non_fallthrough_target;
2836 if (true_target == nullptr) {
2837 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2838 non_fallthrough_target = false_target;
2839 } else {
2840 arm64_cond = ARM64Condition(condition->GetCondition());
2841 non_fallthrough_target = true_target;
2842 }
2843
Roland Levillain7f63c522015-07-13 15:54:55 +00002844 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2845 switch (arm64_cond) {
2846 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002847 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002848 break;
2849 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002850 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002851 break;
2852 case lt:
2853 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002854 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002855 break;
2856 case ge:
2857 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002858 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002859 break;
2860 default:
2861 // Without the `static_cast` the compiler throws an error for
2862 // `-Werror=sign-promo`.
2863 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2864 }
2865 } else {
2866 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002867 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002868 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002869 }
2870 }
David Brazdil0debae72015-11-12 18:37:00 +00002871
2872 // If neither branch falls through (case 3), the conditional branch to `true_target`
2873 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2874 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002875 __ B(false_target);
2876 }
David Brazdil0debae72015-11-12 18:37:00 +00002877
2878 if (fallthrough_target.IsLinked()) {
2879 __ Bind(&fallthrough_target);
2880 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002881}
2882
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002883void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002885 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002886 locations->SetInAt(0, Location::RequiresRegister());
2887 }
2888}
2889
2890void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002891 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2892 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2893 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2894 nullptr : codegen_->GetLabelOf(true_successor);
2895 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2896 nullptr : codegen_->GetLabelOf(false_successor);
2897 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002898}
2899
2900void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2901 LocationSummary* locations = new (GetGraph()->GetArena())
2902 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002903 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002904 locations->SetInAt(0, Location::RequiresRegister());
2905 }
2906}
2907
2908void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002909 SlowPathCodeARM64* slow_path =
2910 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002911 GenerateTestAndBranch(deoptimize,
2912 /* condition_input_index */ 0,
2913 slow_path->GetEntryLabel(),
2914 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002915}
2916
David Srbecky0cf44932015-12-09 14:09:59 +00002917void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2918 new (GetGraph()->GetArena()) LocationSummary(info);
2919}
2920
2921void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002922 if (codegen_->HasStackMapAtCurrentPc()) {
2923 // Ensure that we do not collide with the stack map of the previous instruction.
2924 __ Nop();
2925 }
David Srbecky0cf44932015-12-09 14:09:59 +00002926 codegen_->RecordPcInfo(info, info->GetDexPc());
2927}
2928
Alexandre Rames5319def2014-10-23 10:03:10 +01002929void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002930 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002931}
2932
2933void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002934 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002935}
2936
2937void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002938 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002939}
2940
2941void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002942 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002943}
2944
Alexandre Rames67555f72014-11-18 10:55:16 +00002945void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002946 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002947 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2948 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002949 case TypeCheckKind::kExactCheck:
2950 case TypeCheckKind::kAbstractClassCheck:
2951 case TypeCheckKind::kClassHierarchyCheck:
2952 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002953 call_kind =
2954 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002955 break;
2956 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002957 case TypeCheckKind::kUnresolvedCheck:
2958 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002959 call_kind = LocationSummary::kCallOnSlowPath;
2960 break;
2961 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002962
Alexandre Rames67555f72014-11-18 10:55:16 +00002963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002964 locations->SetInAt(0, Location::RequiresRegister());
2965 locations->SetInAt(1, Location::RequiresRegister());
2966 // The "out" register is used as a temporary, so it overlaps with the inputs.
2967 // Note that TypeCheckSlowPathARM64 uses this register too.
2968 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2969 // When read barriers are enabled, we need a temporary register for
2970 // some cases.
2971 if (kEmitCompilerReadBarrier &&
2972 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2973 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2974 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2975 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002976 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002977}
2978
2979void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2980 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002981 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002982 Register obj = InputRegisterAt(instruction, 0);
2983 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002984 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00002985 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002986 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2987 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2988 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2989 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002990
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002991 vixl::Label done, zero;
2992 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002993
2994 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002995 // Avoid null check if we know `obj` is not null.
2996 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002997 __ Cbz(obj, &zero);
2998 }
2999
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003000 // /* HeapReference<Class> */ out = obj->klass_
3001 __ Ldr(out, HeapOperand(obj.W(), class_offset));
3002 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003003
3004 switch (instruction->GetTypeCheckKind()) {
3005 case TypeCheckKind::kExactCheck: {
3006 __ Cmp(out, cls);
3007 __ Cset(out, eq);
3008 if (zero.IsLinked()) {
3009 __ B(&done);
3010 }
3011 break;
3012 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003013
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003014 case TypeCheckKind::kAbstractClassCheck: {
3015 // If the class is abstract, we eagerly fetch the super class of the
3016 // object to avoid doing a comparison we know will fail.
3017 vixl::Label loop, success;
3018 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003019 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3020 if (kEmitCompilerReadBarrier) {
3021 // Save the value of `out` into `temp` before overwriting it
3022 // in the following move operation, as we will need it for the
3023 // read barrier below.
3024 Register temp = WRegisterFrom(temp_loc);
3025 __ Mov(temp, out);
3026 }
3027 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003028 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003029 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003030 // If `out` is null, we use it for the result, and jump to `done`.
3031 __ Cbz(out, &done);
3032 __ Cmp(out, cls);
3033 __ B(ne, &loop);
3034 __ Mov(out, 1);
3035 if (zero.IsLinked()) {
3036 __ B(&done);
3037 }
3038 break;
3039 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003040
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003041 case TypeCheckKind::kClassHierarchyCheck: {
3042 // Walk over the class hierarchy to find a match.
3043 vixl::Label loop, success;
3044 __ Bind(&loop);
3045 __ Cmp(out, cls);
3046 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003047 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3048 if (kEmitCompilerReadBarrier) {
3049 // Save the value of `out` into `temp` before overwriting it
3050 // in the following move operation, as we will need it for the
3051 // read barrier below.
3052 Register temp = WRegisterFrom(temp_loc);
3053 __ Mov(temp, out);
3054 }
3055 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003056 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003057 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003058 __ Cbnz(out, &loop);
3059 // If `out` is null, we use it for the result, and jump to `done`.
3060 __ B(&done);
3061 __ Bind(&success);
3062 __ Mov(out, 1);
3063 if (zero.IsLinked()) {
3064 __ B(&done);
3065 }
3066 break;
3067 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003068
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003069 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003070 // Do an exact check.
3071 vixl::Label exact_check;
3072 __ Cmp(out, cls);
3073 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003074 // Otherwise, we need to check that the object's class is a non-primitive array.
3075 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3076 if (kEmitCompilerReadBarrier) {
3077 // Save the value of `out` into `temp` before overwriting it
3078 // in the following move operation, as we will need it for the
3079 // read barrier below.
3080 Register temp = WRegisterFrom(temp_loc);
3081 __ Mov(temp, out);
3082 }
3083 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003084 __ Ldr(out, HeapOperand(out, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003085 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003086 // If `out` is null, we use it for the result, and jump to `done`.
3087 __ Cbz(out, &done);
3088 __ Ldrh(out, HeapOperand(out, primitive_offset));
3089 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3090 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003091 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003092 __ Mov(out, 1);
3093 __ B(&done);
3094 break;
3095 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003096
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003097 case TypeCheckKind::kArrayCheck: {
3098 __ Cmp(out, cls);
3099 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003100 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3101 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003102 codegen_->AddSlowPath(slow_path);
3103 __ B(ne, slow_path->GetEntryLabel());
3104 __ Mov(out, 1);
3105 if (zero.IsLinked()) {
3106 __ B(&done);
3107 }
3108 break;
3109 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003110
Calin Juravle98893e12015-10-02 21:05:03 +01003111 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003112 case TypeCheckKind::kInterfaceCheck: {
3113 // Note that we indeed only call on slow path, but we always go
3114 // into the slow path for the unresolved and interface check
3115 // cases.
3116 //
3117 // We cannot directly call the InstanceofNonTrivial runtime
3118 // entry point without resorting to a type checking slow path
3119 // here (i.e. by calling InvokeRuntime directly), as it would
3120 // require to assign fixed registers for the inputs of this
3121 // HInstanceOf instruction (following the runtime calling
3122 // convention), which might be cluttered by the potential first
3123 // read barrier emission at the beginning of this method.
3124 DCHECK(locations->OnlyCallsOnSlowPath());
3125 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3126 /* is_fatal */ false);
3127 codegen_->AddSlowPath(slow_path);
3128 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003129 if (zero.IsLinked()) {
3130 __ B(&done);
3131 }
3132 break;
3133 }
3134 }
3135
3136 if (zero.IsLinked()) {
3137 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003138 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 }
3140
3141 if (done.IsLinked()) {
3142 __ Bind(&done);
3143 }
3144
3145 if (slow_path != nullptr) {
3146 __ Bind(slow_path->GetExitLabel());
3147 }
3148}
3149
3150void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3151 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3152 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3153
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003154 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3155 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003156 case TypeCheckKind::kExactCheck:
3157 case TypeCheckKind::kAbstractClassCheck:
3158 case TypeCheckKind::kClassHierarchyCheck:
3159 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003160 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3161 LocationSummary::kCallOnSlowPath :
3162 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003163 break;
3164 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165 case TypeCheckKind::kUnresolvedCheck:
3166 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003167 call_kind = LocationSummary::kCallOnSlowPath;
3168 break;
3169 }
3170
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3172 locations->SetInAt(0, Location::RequiresRegister());
3173 locations->SetInAt(1, Location::RequiresRegister());
3174 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3175 locations->AddTemp(Location::RequiresRegister());
3176 locations->AddTemp(Location::RequiresRegister());
3177 // When read barriers are enabled, we need an additional temporary
3178 // register for some cases.
3179 if (kEmitCompilerReadBarrier &&
3180 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3181 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3182 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3183 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 }
3185}
3186
3187void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
3188 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003189 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003190 Register obj = InputRegisterAt(instruction, 0);
3191 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003192 Location temp_loc = locations->GetTemp(0);
3193 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003194 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3195 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3196 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3197 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003198
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003199 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3200 bool is_type_check_slow_path_fatal =
3201 (type_check_kind == TypeCheckKind::kExactCheck ||
3202 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3203 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3204 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3205 !instruction->CanThrowIntoCatchBlock();
3206 SlowPathCodeARM64* type_check_slow_path =
3207 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3208 is_type_check_slow_path_fatal);
3209 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003210
3211 vixl::Label done;
3212 // Avoid null check if we know obj is not null.
3213 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003214 __ Cbz(obj, &done);
3215 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003216
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003217 // /* HeapReference<Class> */ temp = obj->klass_
3218 __ Ldr(temp, HeapOperand(obj, class_offset));
3219 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003220
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003221 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003222 case TypeCheckKind::kExactCheck:
3223 case TypeCheckKind::kArrayCheck: {
3224 __ Cmp(temp, cls);
3225 // Jump to slow path for throwing the exception or doing a
3226 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003227 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003228 break;
3229 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003230
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003231 case TypeCheckKind::kAbstractClassCheck: {
3232 // If the class is abstract, we eagerly fetch the super class of the
3233 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003234 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003235 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003236 Location temp2_loc =
3237 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3238 if (kEmitCompilerReadBarrier) {
3239 // Save the value of `temp` into `temp2` before overwriting it
3240 // in the following move operation, as we will need it for the
3241 // read barrier below.
3242 Register temp2 = WRegisterFrom(temp2_loc);
3243 __ Mov(temp2, temp);
3244 }
3245 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003246 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003247 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3248
3249 // If the class reference currently in `temp` is not null, jump
3250 // to the `compare_classes` label to compare it with the checked
3251 // class.
3252 __ Cbnz(temp, &compare_classes);
3253 // Otherwise, jump to the slow path to throw the exception.
3254 //
3255 // But before, move back the object's class into `temp` before
3256 // going into the slow path, as it has been overwritten in the
3257 // meantime.
3258 // /* HeapReference<Class> */ temp = obj->klass_
3259 __ Ldr(temp, HeapOperand(obj, class_offset));
3260 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3261 __ B(type_check_slow_path->GetEntryLabel());
3262
3263 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003264 __ Cmp(temp, cls);
3265 __ B(ne, &loop);
3266 break;
3267 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003268
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003269 case TypeCheckKind::kClassHierarchyCheck: {
3270 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003271 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003272 __ Bind(&loop);
3273 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003274 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003275
3276 Location temp2_loc =
3277 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3278 if (kEmitCompilerReadBarrier) {
3279 // Save the value of `temp` into `temp2` before overwriting it
3280 // in the following move operation, as we will need it for the
3281 // read barrier below.
3282 Register temp2 = WRegisterFrom(temp2_loc);
3283 __ Mov(temp2, temp);
3284 }
3285 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003286 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003287 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3288
3289 // If the class reference currently in `temp` is not null, jump
3290 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003291 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003292 // Otherwise, jump to the slow path to throw the exception.
3293 //
3294 // But before, move back the object's class into `temp` before
3295 // going into the slow path, as it has been overwritten in the
3296 // meantime.
3297 // /* HeapReference<Class> */ temp = obj->klass_
3298 __ Ldr(temp, HeapOperand(obj, class_offset));
3299 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3300 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003301 break;
3302 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003304 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003305 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003306 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003307 __ Cmp(temp, cls);
3308 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003309
3310 // Otherwise, we need to check that the object's class is a non-primitive array.
3311 Location temp2_loc =
3312 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3313 if (kEmitCompilerReadBarrier) {
3314 // Save the value of `temp` into `temp2` before overwriting it
3315 // in the following move operation, as we will need it for the
3316 // read barrier below.
3317 Register temp2 = WRegisterFrom(temp2_loc);
3318 __ Mov(temp2, temp);
3319 }
3320 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003321 __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003322 codegen_->MaybeGenerateReadBarrier(
3323 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
3324
3325 // If the component type is not null (i.e. the object is indeed
3326 // an array), jump to label `check_non_primitive_component_type`
3327 // to further check that this component type is not a primitive
3328 // type.
3329 __ Cbnz(temp, &check_non_primitive_component_type);
3330 // Otherwise, jump to the slow path to throw the exception.
3331 //
3332 // But before, move back the object's class into `temp` before
3333 // going into the slow path, as it has been overwritten in the
3334 // meantime.
3335 // /* HeapReference<Class> */ temp = obj->klass_
3336 __ Ldr(temp, HeapOperand(obj, class_offset));
3337 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3338 __ B(type_check_slow_path->GetEntryLabel());
3339
3340 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003341 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3342 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003343 __ Cbz(temp, &done);
3344 // Same comment as above regarding `temp` and the slow path.
3345 // /* HeapReference<Class> */ temp = obj->klass_
3346 __ Ldr(temp, HeapOperand(obj, class_offset));
3347 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3348 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003349 break;
3350 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003351
Calin Juravle98893e12015-10-02 21:05:03 +01003352 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003353 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354 // We always go into the type check slow path for the unresolved
3355 // and interface check cases.
3356 //
3357 // We cannot directly call the CheckCast runtime entry point
3358 // without resorting to a type checking slow path here (i.e. by
3359 // calling InvokeRuntime directly), as it would require to
3360 // assign fixed registers for the inputs of this HInstanceOf
3361 // instruction (following the runtime calling convention), which
3362 // might be cluttered by the potential first read barrier
3363 // emission at the beginning of this method.
3364 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 break;
3366 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003367 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003368
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003370}
3371
Alexandre Rames5319def2014-10-23 10:03:10 +01003372void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3373 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3374 locations->SetOut(Location::ConstantLocation(constant));
3375}
3376
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003377void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003378 // Will be generated at use site.
3379}
3380
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003381void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3382 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3383 locations->SetOut(Location::ConstantLocation(constant));
3384}
3385
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003386void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003387 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003388}
3389
Calin Juravle175dc732015-08-25 15:42:32 +01003390void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3391 // The trampoline uses the same calling convention as dex calling conventions,
3392 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3393 // the method_idx.
3394 HandleInvoke(invoke);
3395}
3396
3397void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3398 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3399}
3400
Alexandre Rames5319def2014-10-23 10:03:10 +01003401void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003402 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003403 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003404}
3405
Alexandre Rames67555f72014-11-18 10:55:16 +00003406void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3407 HandleInvoke(invoke);
3408}
3409
3410void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3411 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003412 LocationSummary* locations = invoke->GetLocations();
3413 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003414 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3415 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003416 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003417 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003418 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003419
3420 // The register ip1 is required to be used for the hidden argument in
3421 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003422 MacroAssembler* masm = GetVIXLAssembler();
3423 UseScratchRegisterScope scratch_scope(masm);
3424 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003425 scratch_scope.Exclude(ip1);
3426 __ Mov(ip1, invoke->GetDexMethodIndex());
3427
Alexandre Rames67555f72014-11-18 10:55:16 +00003428 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003429 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003431 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003432 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003433 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003434 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003435 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003436 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003437 // Instead of simply (possibly) unpoisoning `temp` here, we should
3438 // emit a read barrier for the previous class reference load.
3439 // However this is not required in practice, as this is an
3440 // intermediate/temporary reference and because the current
3441 // concurrent copying collector keeps the from-space memory
3442 // intact/accessible until the end of the marking phase (the
3443 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003444 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003445 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003446 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003447 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003448 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003449 // lr();
3450 __ Blr(lr);
3451 DCHECK(!codegen_->IsLeafMethod());
3452 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3453}
3454
3455void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003456 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3457 if (intrinsic.TryDispatch(invoke)) {
3458 return;
3459 }
3460
Alexandre Rames67555f72014-11-18 10:55:16 +00003461 HandleInvoke(invoke);
3462}
3463
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003464void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003465 // When we do not run baseline, explicit clinit checks triggered by static
3466 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3467 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003468
Andreas Gampe878d58c2015-01-15 23:24:00 -08003469 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3470 if (intrinsic.TryDispatch(invoke)) {
3471 return;
3472 }
3473
Alexandre Rames67555f72014-11-18 10:55:16 +00003474 HandleInvoke(invoke);
3475}
3476
Andreas Gampe878d58c2015-01-15 23:24:00 -08003477static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3478 if (invoke->GetLocations()->Intrinsified()) {
3479 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3480 intrinsic.Dispatch(invoke);
3481 return true;
3482 }
3483 return false;
3484}
3485
Vladimir Markodc151b22015-10-15 18:02:30 +01003486HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3487 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3488 MethodReference target_method ATTRIBUTE_UNUSED) {
3489 // On arm64 we support all dispatch types.
3490 return desired_dispatch_info;
3491}
3492
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003493void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003494 // For better instruction scheduling we load the direct code pointer before the method pointer.
3495 bool direct_code_loaded = false;
3496 switch (invoke->GetCodePtrLocation()) {
3497 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3498 // LR = code address from literal pool with link-time patch.
3499 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3500 direct_code_loaded = true;
3501 break;
3502 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3503 // LR = invoke->GetDirectCodePtr();
3504 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3505 direct_code_loaded = true;
3506 break;
3507 default:
3508 break;
3509 }
3510
Andreas Gampe878d58c2015-01-15 23:24:00 -08003511 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003512 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3513 switch (invoke->GetMethodLoadKind()) {
3514 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3515 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003516 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003517 break;
3518 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003519 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003520 break;
3521 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3522 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003523 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003524 break;
3525 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3526 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003527 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003528 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3529 break;
3530 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3531 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003532 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3533 invoke->GetDexCacheArrayOffset());
3534 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003535 {
3536 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003537 __ Bind(pc_insn_label);
3538 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003539 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003540 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003541 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003542 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3543 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003544 {
3545 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3546 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3547 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3548 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3549 }
Vladimir Marko58155012015-08-19 12:49:41 +00003550 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003551 }
Vladimir Marko58155012015-08-19 12:49:41 +00003552 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003553 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003554 Register reg = XRegisterFrom(temp);
3555 Register method_reg;
3556 if (current_method.IsRegister()) {
3557 method_reg = XRegisterFrom(current_method);
3558 } else {
3559 DCHECK(invoke->GetLocations()->Intrinsified());
3560 DCHECK(!current_method.IsValid());
3561 method_reg = reg;
3562 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3563 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003564
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003565 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003566 __ Ldr(reg.X(),
3567 MemOperand(method_reg.X(),
3568 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003569 // temp = temp[index_in_cache];
3570 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3571 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3572 break;
3573 }
3574 }
3575
3576 switch (invoke->GetCodePtrLocation()) {
3577 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3578 __ Bl(&frame_entry_label_);
3579 break;
3580 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3581 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3582 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003583 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3584 __ Bind(label);
3585 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003586 break;
3587 }
3588 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3589 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3590 // LR prepared above for better instruction scheduling.
3591 DCHECK(direct_code_loaded);
3592 // lr()
3593 __ Blr(lr);
3594 break;
3595 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3596 // LR = callee_method->entry_point_from_quick_compiled_code_;
3597 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003598 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003599 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3600 // lr()
3601 __ Blr(lr);
3602 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003603 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003604
Andreas Gampe878d58c2015-01-15 23:24:00 -08003605 DCHECK(!IsLeafMethod());
3606}
3607
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003608void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003609 // Use the calling convention instead of the location of the receiver, as
3610 // intrinsics may have put the receiver in a different register. In the intrinsics
3611 // slow path, the arguments have been moved to the right place, so here we are
3612 // guaranteed that the receiver is the first register of the calling convention.
3613 InvokeDexCallingConvention calling_convention;
3614 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003615 Register temp = XRegisterFrom(temp_in);
3616 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3617 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3618 Offset class_offset = mirror::Object::ClassOffset();
3619 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3620
3621 BlockPoolsScope block_pools(GetVIXLAssembler());
3622
3623 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003624 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003625 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003626 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003627 // Instead of simply (possibly) unpoisoning `temp` here, we should
3628 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003629 // intermediate/temporary reference and because the current
3630 // concurrent copying collector keeps the from-space memory
3631 // intact/accessible until the end of the marking phase (the
3632 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003633 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3634 // temp = temp->GetMethodAt(method_offset);
3635 __ Ldr(temp, MemOperand(temp, method_offset));
3636 // lr = temp->GetEntryPoint();
3637 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3638 // lr();
3639 __ Blr(lr);
3640}
3641
Vladimir Marko58155012015-08-19 12:49:41 +00003642void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3643 DCHECK(linker_patches->empty());
3644 size_t size =
3645 method_patches_.size() +
3646 call_patches_.size() +
3647 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003648 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003649 linker_patches->reserve(size);
3650 for (const auto& entry : method_patches_) {
3651 const MethodReference& target_method = entry.first;
3652 vixl::Literal<uint64_t>* literal = entry.second;
3653 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3654 target_method.dex_file,
3655 target_method.dex_method_index));
3656 }
3657 for (const auto& entry : call_patches_) {
3658 const MethodReference& target_method = entry.first;
3659 vixl::Literal<uint64_t>* literal = entry.second;
3660 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3661 target_method.dex_file,
3662 target_method.dex_method_index));
3663 }
3664 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003665 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003666 info.target_method.dex_file,
3667 info.target_method.dex_method_index));
3668 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003669 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003670 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003671 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003672 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003673 info.element_offset));
3674 }
3675}
3676
3677vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3678 // Look up the literal for value.
3679 auto lb = uint64_literals_.lower_bound(value);
3680 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3681 return lb->second;
3682 }
3683 // We don't have a literal for this value, insert a new one.
3684 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3685 uint64_literals_.PutBefore(lb, value, literal);
3686 return literal;
3687}
3688
3689vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3690 MethodReference target_method,
3691 MethodToLiteralMap* map) {
3692 // Look up the literal for target_method.
3693 auto lb = map->lower_bound(target_method);
3694 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3695 return lb->second;
3696 }
3697 // We don't have a literal for this method yet, insert a new one.
3698 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3699 map->PutBefore(lb, target_method, literal);
3700 return literal;
3701}
3702
3703vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3704 MethodReference target_method) {
3705 return DeduplicateMethodLiteral(target_method, &method_patches_);
3706}
3707
3708vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3709 MethodReference target_method) {
3710 return DeduplicateMethodLiteral(target_method, &call_patches_);
3711}
3712
3713
Andreas Gampe878d58c2015-01-15 23:24:00 -08003714void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003715 // When we do not run baseline, explicit clinit checks triggered by static
3716 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3717 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003718
Andreas Gampe878d58c2015-01-15 23:24:00 -08003719 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3720 return;
3721 }
3722
Alexandre Ramesd921d642015-04-16 15:07:16 +01003723 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003724 LocationSummary* locations = invoke->GetLocations();
3725 codegen_->GenerateStaticOrDirectCall(
3726 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003727 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003728}
3729
3730void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003731 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3732 return;
3733 }
3734
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003735 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003736 DCHECK(!codegen_->IsLeafMethod());
3737 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3738}
3739
Alexandre Rames67555f72014-11-18 10:55:16 +00003740void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003741 InvokeRuntimeCallingConvention calling_convention;
3742 CodeGenerator::CreateLoadClassLocationSummary(
3743 cls,
3744 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003745 LocationFrom(vixl::x0),
3746 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003747}
3748
3749void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003750 if (cls->NeedsAccessCheck()) {
3751 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3752 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3753 cls,
3754 cls->GetDexPc(),
3755 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003756 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003757 return;
3758 }
3759
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003760 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003761 Register out = OutputRegister(cls);
3762 Register current_method = InputRegisterAt(cls, 0);
3763 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003764 DCHECK(!cls->CanCallRuntime());
3765 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003766 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3767 if (kEmitCompilerReadBarrier) {
3768 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3769 __ Add(out.X(), current_method.X(), declaring_class_offset);
3770 // /* mirror::Class* */ out = out->Read()
3771 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3772 } else {
3773 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3774 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3775 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003776 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003777 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003778 // /* GcRoot<mirror::Class>[] */ out =
3779 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003780 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003781
3782 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
3783 if (kEmitCompilerReadBarrier) {
3784 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
3785 __ Add(out.X(), out.X(), cache_offset);
3786 // /* mirror::Class* */ out = out->Read()
3787 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3788 } else {
3789 // /* GcRoot<mirror::Class> */ out = out[type_index]
3790 __ Ldr(out, MemOperand(out.X(), cache_offset));
3791 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003792
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003793 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3794 DCHECK(cls->CanCallRuntime());
3795 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3796 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3797 codegen_->AddSlowPath(slow_path);
3798 if (!cls->IsInDexCache()) {
3799 __ Cbz(out, slow_path->GetEntryLabel());
3800 }
3801 if (cls->MustGenerateClinitCheck()) {
3802 GenerateClassInitializationCheck(slow_path, out);
3803 } else {
3804 __ Bind(slow_path->GetExitLabel());
3805 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003806 }
3807 }
3808}
3809
David Brazdilcb1c0552015-08-04 16:22:25 +01003810static MemOperand GetExceptionTlsAddress() {
3811 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3812}
3813
Alexandre Rames67555f72014-11-18 10:55:16 +00003814void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3815 LocationSummary* locations =
3816 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3817 locations->SetOut(Location::RequiresRegister());
3818}
3819
3820void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003821 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3822}
3823
3824void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3825 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3826}
3827
3828void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3829 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003830}
3831
Alexandre Rames5319def2014-10-23 10:03:10 +01003832void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3833 load->SetLocations(nullptr);
3834}
3835
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003836void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003837 // Nothing to do, this is driven by the code generator.
3838}
3839
Alexandre Rames67555f72014-11-18 10:55:16 +00003840void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003841 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
3842 ? LocationSummary::kCallOnSlowPath
3843 : LocationSummary::kNoCall;
3844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003845 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003846 locations->SetOut(Location::RequiresRegister());
3847}
3848
3849void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003850 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003851 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003852 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003853
3854 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3855 if (kEmitCompilerReadBarrier) {
3856 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3857 __ Add(out.X(), current_method.X(), declaring_class_offset);
3858 // /* mirror::Class* */ out = out->Read()
3859 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3860 } else {
3861 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3862 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3863 }
3864
3865 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3866 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
3867
3868 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
3869 if (kEmitCompilerReadBarrier) {
3870 // /* GcRoot<mirror::String>* */ out = &out[string_index]
3871 __ Add(out.X(), out.X(), cache_offset);
3872 // /* mirror::String* */ out = out->Read()
3873 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3874 } else {
3875 // /* GcRoot<mirror::String> */ out = out[string_index]
3876 __ Ldr(out, MemOperand(out.X(), cache_offset));
3877 }
3878
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003879 if (!load->IsInDexCache()) {
3880 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3881 codegen_->AddSlowPath(slow_path);
3882 __ Cbz(out, slow_path->GetEntryLabel());
3883 __ Bind(slow_path->GetExitLabel());
3884 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003885}
3886
Alexandre Rames5319def2014-10-23 10:03:10 +01003887void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3888 local->SetLocations(nullptr);
3889}
3890
3891void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3892 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3893}
3894
3895void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3897 locations->SetOut(Location::ConstantLocation(constant));
3898}
3899
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003900void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003901 // Will be generated at use site.
3902}
3903
Alexandre Rames67555f72014-11-18 10:55:16 +00003904void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3905 LocationSummary* locations =
3906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3907 InvokeRuntimeCallingConvention calling_convention;
3908 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3909}
3910
3911void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3912 codegen_->InvokeRuntime(instruction->IsEnter()
3913 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3914 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003915 instruction->GetDexPc(),
3916 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003917 if (instruction->IsEnter()) {
3918 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3919 } else {
3920 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3921 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003922}
3923
Alexandre Rames42d641b2014-10-27 14:00:51 +00003924void LocationsBuilderARM64::VisitMul(HMul* mul) {
3925 LocationSummary* locations =
3926 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3927 switch (mul->GetResultType()) {
3928 case Primitive::kPrimInt:
3929 case Primitive::kPrimLong:
3930 locations->SetInAt(0, Location::RequiresRegister());
3931 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003933 break;
3934
3935 case Primitive::kPrimFloat:
3936 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003937 locations->SetInAt(0, Location::RequiresFpuRegister());
3938 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003939 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003940 break;
3941
3942 default:
3943 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3944 }
3945}
3946
3947void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3948 switch (mul->GetResultType()) {
3949 case Primitive::kPrimInt:
3950 case Primitive::kPrimLong:
3951 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3952 break;
3953
3954 case Primitive::kPrimFloat:
3955 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003956 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003957 break;
3958
3959 default:
3960 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3961 }
3962}
3963
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003964void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3965 LocationSummary* locations =
3966 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3967 switch (neg->GetResultType()) {
3968 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003969 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003970 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003971 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003972 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003973
3974 case Primitive::kPrimFloat:
3975 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003976 locations->SetInAt(0, Location::RequiresFpuRegister());
3977 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003978 break;
3979
3980 default:
3981 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3982 }
3983}
3984
3985void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3986 switch (neg->GetResultType()) {
3987 case Primitive::kPrimInt:
3988 case Primitive::kPrimLong:
3989 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3990 break;
3991
3992 case Primitive::kPrimFloat:
3993 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003994 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003995 break;
3996
3997 default:
3998 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3999 }
4000}
4001
4002void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4003 LocationSummary* locations =
4004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4005 InvokeRuntimeCallingConvention calling_convention;
4006 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004007 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004008 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004009 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004010}
4011
4012void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4013 LocationSummary* locations = instruction->GetLocations();
4014 InvokeRuntimeCallingConvention calling_convention;
4015 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4016 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004017 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004018 // Note: if heap poisoning is enabled, the entry point takes cares
4019 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004020 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4021 instruction,
4022 instruction->GetDexPc(),
4023 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004024 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004025}
4026
Alexandre Rames5319def2014-10-23 10:03:10 +01004027void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4028 LocationSummary* locations =
4029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4030 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004031 if (instruction->IsStringAlloc()) {
4032 locations->AddTemp(LocationFrom(kArtMethodRegister));
4033 } else {
4034 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4035 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4036 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004037 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4038}
4039
4040void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004041 // Note: if heap poisoning is enabled, the entry point takes cares
4042 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004043 if (instruction->IsStringAlloc()) {
4044 // String is allocated through StringFactory. Call NewEmptyString entry point.
4045 Location temp = instruction->GetLocations()->GetTemp(0);
4046 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4047 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4048 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4049 __ Blr(lr);
4050 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4051 } else {
4052 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4053 instruction,
4054 instruction->GetDexPc(),
4055 nullptr);
4056 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4057 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004058}
4059
4060void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004062 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004064}
4065
4066void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004067 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004068 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004069 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004070 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004071 break;
4072
4073 default:
4074 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4075 }
4076}
4077
David Brazdil66d126e2015-04-03 16:02:44 +01004078void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4080 locations->SetInAt(0, Location::RequiresRegister());
4081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4082}
4083
4084void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004085 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4086}
4087
Alexandre Rames5319def2014-10-23 10:03:10 +01004088void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004089 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4090 ? LocationSummary::kCallOnSlowPath
4091 : LocationSummary::kNoCall;
4092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004093 locations->SetInAt(0, Location::RequiresRegister());
4094 if (instruction->HasUses()) {
4095 locations->SetOut(Location::SameAsFirstInput());
4096 }
4097}
4098
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004099void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004100 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4101 return;
4102 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004103
Alexandre Ramesd921d642015-04-16 15:07:16 +01004104 BlockPoolsScope block_pools(GetVIXLAssembler());
4105 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004106 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4107 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4108}
4109
4110void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004111 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4112 codegen_->AddSlowPath(slow_path);
4113
4114 LocationSummary* locations = instruction->GetLocations();
4115 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004116
4117 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004118}
4119
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004120void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004121 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004122 GenerateImplicitNullCheck(instruction);
4123 } else {
4124 GenerateExplicitNullCheck(instruction);
4125 }
4126}
4127
Alexandre Rames67555f72014-11-18 10:55:16 +00004128void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4129 HandleBinaryOp(instruction);
4130}
4131
4132void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4133 HandleBinaryOp(instruction);
4134}
4135
Alexandre Rames3e69f162014-12-10 10:36:50 +00004136void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4137 LOG(FATAL) << "Unreachable";
4138}
4139
4140void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4141 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4142}
4143
Alexandre Rames5319def2014-10-23 10:03:10 +01004144void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4146 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4147 if (location.IsStackSlot()) {
4148 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4149 } else if (location.IsDoubleStackSlot()) {
4150 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4151 }
4152 locations->SetOut(location);
4153}
4154
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004155void InstructionCodeGeneratorARM64::VisitParameterValue(
4156 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004157 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004158}
4159
4160void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4161 LocationSummary* locations =
4162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004163 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004164}
4165
4166void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4167 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4168 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004169}
4170
4171void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4173 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4174 locations->SetInAt(i, Location::Any());
4175 }
4176 locations->SetOut(Location::Any());
4177}
4178
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004179void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004180 LOG(FATAL) << "Unreachable";
4181}
4182
Serban Constantinescu02164b32014-11-13 14:05:07 +00004183void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004184 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004185 LocationSummary::CallKind call_kind =
4186 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4188
4189 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004190 case Primitive::kPrimInt:
4191 case Primitive::kPrimLong:
4192 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004193 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004194 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4195 break;
4196
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004197 case Primitive::kPrimFloat:
4198 case Primitive::kPrimDouble: {
4199 InvokeRuntimeCallingConvention calling_convention;
4200 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4201 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4202 locations->SetOut(calling_convention.GetReturnLocation(type));
4203
4204 break;
4205 }
4206
Serban Constantinescu02164b32014-11-13 14:05:07 +00004207 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004208 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004209 }
4210}
4211
4212void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4213 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004214
Serban Constantinescu02164b32014-11-13 14:05:07 +00004215 switch (type) {
4216 case Primitive::kPrimInt:
4217 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004218 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004219 break;
4220 }
4221
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004222 case Primitive::kPrimFloat:
4223 case Primitive::kPrimDouble: {
4224 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4225 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004226 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004227 if (type == Primitive::kPrimFloat) {
4228 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4229 } else {
4230 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4231 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004232 break;
4233 }
4234
Serban Constantinescu02164b32014-11-13 14:05:07 +00004235 default:
4236 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004237 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004238 }
4239}
4240
Calin Juravle27df7582015-04-17 19:12:31 +01004241void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4242 memory_barrier->SetLocations(nullptr);
4243}
4244
4245void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4246 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4247}
4248
Alexandre Rames5319def2014-10-23 10:03:10 +01004249void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4250 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4251 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004252 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004253}
4254
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004255void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004256 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004257}
4258
4259void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4260 instruction->SetLocations(nullptr);
4261}
4262
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004263void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004264 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004265}
4266
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004267void LocationsBuilderARM64::VisitRor(HRor* ror) {
4268 HandleBinaryOp(ror);
4269}
4270
4271void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4272 HandleBinaryOp(ror);
4273}
4274
Serban Constantinescu02164b32014-11-13 14:05:07 +00004275void LocationsBuilderARM64::VisitShl(HShl* shl) {
4276 HandleShift(shl);
4277}
4278
4279void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4280 HandleShift(shl);
4281}
4282
4283void LocationsBuilderARM64::VisitShr(HShr* shr) {
4284 HandleShift(shr);
4285}
4286
4287void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4288 HandleShift(shr);
4289}
4290
Alexandre Rames5319def2014-10-23 10:03:10 +01004291void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4293 Primitive::Type field_type = store->InputAt(1)->GetType();
4294 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004295 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004296 case Primitive::kPrimBoolean:
4297 case Primitive::kPrimByte:
4298 case Primitive::kPrimChar:
4299 case Primitive::kPrimShort:
4300 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004301 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004302 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4303 break;
4304
4305 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004306 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004307 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4308 break;
4309
4310 default:
4311 LOG(FATAL) << "Unimplemented local type " << field_type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004312 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +01004313 }
4314}
4315
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004316void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004317}
4318
4319void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004320 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004321}
4322
4323void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004324 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004325}
4326
Alexandre Rames67555f72014-11-18 10:55:16 +00004327void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004328 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004329}
4330
4331void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004332 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004333}
4334
4335void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004336 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004337}
4338
Alexandre Rames67555f72014-11-18 10:55:16 +00004339void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004340 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004341}
4342
Calin Juravlee460d1d2015-09-29 04:52:17 +01004343void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4344 HUnresolvedInstanceFieldGet* instruction) {
4345 FieldAccessCallingConventionARM64 calling_convention;
4346 codegen_->CreateUnresolvedFieldLocationSummary(
4347 instruction, instruction->GetFieldType(), calling_convention);
4348}
4349
4350void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4351 HUnresolvedInstanceFieldGet* instruction) {
4352 FieldAccessCallingConventionARM64 calling_convention;
4353 codegen_->GenerateUnresolvedFieldAccess(instruction,
4354 instruction->GetFieldType(),
4355 instruction->GetFieldIndex(),
4356 instruction->GetDexPc(),
4357 calling_convention);
4358}
4359
4360void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4361 HUnresolvedInstanceFieldSet* instruction) {
4362 FieldAccessCallingConventionARM64 calling_convention;
4363 codegen_->CreateUnresolvedFieldLocationSummary(
4364 instruction, instruction->GetFieldType(), calling_convention);
4365}
4366
4367void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4368 HUnresolvedInstanceFieldSet* instruction) {
4369 FieldAccessCallingConventionARM64 calling_convention;
4370 codegen_->GenerateUnresolvedFieldAccess(instruction,
4371 instruction->GetFieldType(),
4372 instruction->GetFieldIndex(),
4373 instruction->GetDexPc(),
4374 calling_convention);
4375}
4376
4377void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4378 HUnresolvedStaticFieldGet* instruction) {
4379 FieldAccessCallingConventionARM64 calling_convention;
4380 codegen_->CreateUnresolvedFieldLocationSummary(
4381 instruction, instruction->GetFieldType(), calling_convention);
4382}
4383
4384void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4385 HUnresolvedStaticFieldGet* instruction) {
4386 FieldAccessCallingConventionARM64 calling_convention;
4387 codegen_->GenerateUnresolvedFieldAccess(instruction,
4388 instruction->GetFieldType(),
4389 instruction->GetFieldIndex(),
4390 instruction->GetDexPc(),
4391 calling_convention);
4392}
4393
4394void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4395 HUnresolvedStaticFieldSet* instruction) {
4396 FieldAccessCallingConventionARM64 calling_convention;
4397 codegen_->CreateUnresolvedFieldLocationSummary(
4398 instruction, instruction->GetFieldType(), calling_convention);
4399}
4400
4401void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4402 HUnresolvedStaticFieldSet* instruction) {
4403 FieldAccessCallingConventionARM64 calling_convention;
4404 codegen_->GenerateUnresolvedFieldAccess(instruction,
4405 instruction->GetFieldType(),
4406 instruction->GetFieldIndex(),
4407 instruction->GetDexPc(),
4408 calling_convention);
4409}
4410
Alexandre Rames5319def2014-10-23 10:03:10 +01004411void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4412 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4413}
4414
4415void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004416 HBasicBlock* block = instruction->GetBlock();
4417 if (block->GetLoopInformation() != nullptr) {
4418 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4419 // The back edge will generate the suspend check.
4420 return;
4421 }
4422 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4423 // The goto will generate the suspend check.
4424 return;
4425 }
4426 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004427}
4428
4429void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4430 temp->SetLocations(nullptr);
4431}
4432
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004433void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004434 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004435}
4436
Alexandre Rames67555f72014-11-18 10:55:16 +00004437void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4438 LocationSummary* locations =
4439 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4440 InvokeRuntimeCallingConvention calling_convention;
4441 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4442}
4443
4444void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4445 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004446 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004447 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004448}
4449
4450void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4451 LocationSummary* locations =
4452 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4453 Primitive::Type input_type = conversion->GetInputType();
4454 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004455 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004456 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4457 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4458 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4459 }
4460
Alexandre Rames542361f2015-01-29 16:57:31 +00004461 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004462 locations->SetInAt(0, Location::RequiresFpuRegister());
4463 } else {
4464 locations->SetInAt(0, Location::RequiresRegister());
4465 }
4466
Alexandre Rames542361f2015-01-29 16:57:31 +00004467 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004468 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4469 } else {
4470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4471 }
4472}
4473
4474void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4475 Primitive::Type result_type = conversion->GetResultType();
4476 Primitive::Type input_type = conversion->GetInputType();
4477
4478 DCHECK_NE(input_type, result_type);
4479
Alexandre Rames542361f2015-01-29 16:57:31 +00004480 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004481 int result_size = Primitive::ComponentSize(result_type);
4482 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004483 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004484 Register output = OutputRegister(conversion);
4485 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004486 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004487 // 'int' values are used directly as W registers, discarding the top
4488 // bits, so we don't need to sign-extend and can just perform a move.
4489 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4490 // top 32 bits of the target register. We theoretically could leave those
4491 // bits unchanged, but we would have to make sure that no code uses a
4492 // 32bit input value as a 64bit value assuming that the top 32 bits are
4493 // zero.
4494 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004495 } else if (result_type == Primitive::kPrimChar ||
4496 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4497 __ Ubfx(output,
4498 output.IsX() ? source.X() : source.W(),
4499 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004500 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004501 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004502 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004503 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004504 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004505 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004506 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4507 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004508 } else if (Primitive::IsFloatingPointType(result_type) &&
4509 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004510 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4511 } else {
4512 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4513 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004514 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004515}
Alexandre Rames67555f72014-11-18 10:55:16 +00004516
Serban Constantinescu02164b32014-11-13 14:05:07 +00004517void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4518 HandleShift(ushr);
4519}
4520
4521void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4522 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004523}
4524
4525void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4526 HandleBinaryOp(instruction);
4527}
4528
4529void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4530 HandleBinaryOp(instruction);
4531}
4532
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004533void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004534 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004535 LOG(FATAL) << "Unreachable";
4536}
4537
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004538void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004539 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004540 LOG(FATAL) << "Unreachable";
4541}
4542
Mark Mendellfe57faa2015-09-18 09:26:15 -04004543// Simple implementation of packed switch - generate cascaded compare/jumps.
4544void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4545 LocationSummary* locations =
4546 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4547 locations->SetInAt(0, Location::RequiresRegister());
4548}
4549
4550void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4551 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004552 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004553 Register value_reg = InputRegisterAt(switch_instr, 0);
4554 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4555
Zheng Xu3927c8b2015-11-18 17:46:25 +08004556 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4557 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4558 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4559 // make sure we don't emit it if the target may run out of range.
4560 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4561 // ranges and emit the tables only as required.
4562 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004563
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004564 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004565 // Current instruction id is an upper bound of the number of HIRs in the graph.
4566 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4567 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004568 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4569 Register temp = temps.AcquireW();
4570 __ Subs(temp, value_reg, Operand(lower_bound));
4571
Zheng Xu3927c8b2015-11-18 17:46:25 +08004572 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004573 // Jump to successors[0] if value == lower_bound.
4574 __ B(eq, codegen_->GetLabelOf(successors[0]));
4575 int32_t last_index = 0;
4576 for (; num_entries - last_index > 2; last_index += 2) {
4577 __ Subs(temp, temp, Operand(2));
4578 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4579 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4580 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4581 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4582 }
4583 if (num_entries - last_index == 2) {
4584 // The last missing case_value.
4585 __ Cmp(temp, Operand(1));
4586 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004587 }
4588
4589 // And the default for any other value.
4590 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4591 __ B(codegen_->GetLabelOf(default_block));
4592 }
4593 } else {
4594 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4595 codegen_->AddJumpTable(jump_table);
4596
4597 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4598
4599 // Below instructions should use at most one blocked register. Since there are two blocked
4600 // registers, we are free to block one.
4601 Register temp_w = temps.AcquireW();
4602 Register index;
4603 // Remove the bias.
4604 if (lower_bound != 0) {
4605 index = temp_w;
4606 __ Sub(index, value_reg, Operand(lower_bound));
4607 } else {
4608 index = value_reg;
4609 }
4610
4611 // Jump to default block if index is out of the range.
4612 __ Cmp(index, Operand(num_entries));
4613 __ B(hs, codegen_->GetLabelOf(default_block));
4614
4615 // In current VIXL implementation, it won't require any blocked registers to encode the
4616 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4617 // register pressure.
4618 Register table_base = temps.AcquireX();
4619 // Load jump offset from the table.
4620 __ Adr(table_base, jump_table->GetTableStartLabel());
4621 Register jump_offset = temp_w;
4622 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4623
4624 // Jump to target block by branching to table_base(pc related) + offset.
4625 Register target_address = table_base;
4626 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4627 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004628 }
4629}
4630
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004631void CodeGeneratorARM64::GenerateReadBarrier(HInstruction* instruction,
4632 Location out,
4633 Location ref,
4634 Location obj,
4635 uint32_t offset,
4636 Location index) {
4637 DCHECK(kEmitCompilerReadBarrier);
4638
4639 // If heap poisoning is enabled, the unpoisoning of the loaded
4640 // reference will be carried out by the runtime within the slow
4641 // path.
4642 //
4643 // Note that `ref` currently does not get unpoisoned (when heap
4644 // poisoning is enabled), which is alright as the `ref` argument is
4645 // not used by the artReadBarrierSlow entry point.
4646 //
4647 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4648 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4649 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4650 AddSlowPath(slow_path);
4651
4652 // TODO: When read barrier has a fast path, add it here.
4653 /* Currently the read barrier call is inserted after the original load.
4654 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
4655 * original load. This load-load ordering is required by the read barrier.
4656 * The fast path/slow path (for Baker's algorithm) should look like:
4657 *
4658 * bool isGray = obj.LockWord & kReadBarrierMask;
4659 * lfence; // load fence or artificial data dependence to prevent load-load reordering
4660 * ref = obj.field; // this is the original load
4661 * if (isGray) {
4662 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
4663 * }
4664 */
4665
4666 __ B(slow_path->GetEntryLabel());
4667 __ Bind(slow_path->GetExitLabel());
4668}
4669
4670void CodeGeneratorARM64::MaybeGenerateReadBarrier(HInstruction* instruction,
4671 Location out,
4672 Location ref,
4673 Location obj,
4674 uint32_t offset,
4675 Location index) {
4676 if (kEmitCompilerReadBarrier) {
4677 // If heap poisoning is enabled, unpoisoning will be taken care of
4678 // by the runtime within the slow path.
4679 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
4680 } else if (kPoisonHeapReferences) {
4681 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4682 }
4683}
4684
4685void CodeGeneratorARM64::GenerateReadBarrierForRoot(HInstruction* instruction,
4686 Location out,
4687 Location root) {
4688 DCHECK(kEmitCompilerReadBarrier);
4689
4690 // Note that GC roots are not affected by heap poisoning, so we do
4691 // not need to do anything special for this here.
4692 SlowPathCodeARM64* slow_path =
4693 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4694 AddSlowPath(slow_path);
4695
4696 // TODO: Implement a fast path for ReadBarrierForRoot, performing
4697 // the following operation (for Baker's algorithm):
4698 //
4699 // if (thread.tls32_.is_gc_marking) {
4700 // root = Mark(root);
4701 // }
4702
4703 __ B(slow_path->GetEntryLabel());
4704 __ Bind(slow_path->GetExitLabel());
4705}
4706
Alexandre Rames67555f72014-11-18 10:55:16 +00004707#undef __
4708#undef QUICK_ENTRY_POINT
4709
Alexandre Rames5319def2014-10-23 10:03:10 +01004710} // namespace arm64
4711} // namespace art