blob: d1bddf673a906ecb298b3c793a9ceeae8251798b [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
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
Zheng Xu3927c8b2015-11-18 17:46:25 +080071// The compare/jump sequence will generate about (2 * num_entries + 1) instructions. While jump
72// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
73// generates less code/data with a small num_entries.
74static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
Alexandre Rames5319def2014-10-23 10:03:10 +010075
Alexandre Rames5319def2014-10-23 10:03:10 +010076inline Condition ARM64Condition(IfCondition cond) {
77 switch (cond) {
78 case kCondEQ: return eq;
79 case kCondNE: return ne;
80 case kCondLT: return lt;
81 case kCondLE: return le;
82 case kCondGT: return gt;
83 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070084 case kCondB: return lo;
85 case kCondBE: return ls;
86 case kCondA: return hi;
87 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010088 }
Roland Levillain7f63c522015-07-13 15:54:55 +000089 LOG(FATAL) << "Unreachable";
90 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010091}
92
Alexandre Ramesa89086e2014-11-07 17:13:25 +000093Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000094 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
95 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
96 // but we use the exact registers for clarity.
97 if (return_type == Primitive::kPrimFloat) {
98 return LocationFrom(s0);
99 } else if (return_type == Primitive::kPrimDouble) {
100 return LocationFrom(d0);
101 } else if (return_type == Primitive::kPrimLong) {
102 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100103 } else if (return_type == Primitive::kPrimVoid) {
104 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000105 } else {
106 return LocationFrom(w0);
107 }
108}
109
Alexandre Rames5319def2014-10-23 10:03:10 +0100110Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000111 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100112}
113
Alexandre Rames67555f72014-11-18 10:55:16 +0000114#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
115#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100116
Zheng Xuda403092015-04-24 17:35:39 +0800117// Calculate memory accessing operand for save/restore live registers.
118static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
119 RegisterSet* register_set,
120 int64_t spill_offset,
121 bool is_save) {
122 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
123 codegen->GetNumberOfCoreRegisters(),
124 register_set->GetFloatingPointRegisters(),
125 codegen->GetNumberOfFloatingPointRegisters()));
126
127 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
128 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000129 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
130 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800131
132 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
133 UseScratchRegisterScope temps(masm);
134
135 Register base = masm->StackPointer();
136 int64_t core_spill_size = core_list.TotalSizeInBytes();
137 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
138 int64_t reg_size = kXRegSizeInBytes;
139 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
140 uint32_t ls_access_size = WhichPowerOf2(reg_size);
141 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
142 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
143 // If the offset does not fit in the instruction's immediate field, use an alternate register
144 // to compute the base address(float point registers spill base address).
145 Register new_base = temps.AcquireSameSizeAs(base);
146 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
147 base = new_base;
148 spill_offset = -core_spill_size;
149 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
150 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
151 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
152 }
153
154 if (is_save) {
155 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
156 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
157 } else {
158 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
159 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
160 }
161}
162
163void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
164 RegisterSet* register_set = locations->GetLiveRegisters();
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
167 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
168 // If the register holds an object, update the stack mask.
169 if (locations->RegisterContainsObject(i)) {
170 locations->SetStackBit(stack_offset / kVRegSize);
171 }
172 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
173 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
174 saved_core_stack_offsets_[i] = stack_offset;
175 stack_offset += kXRegSizeInBytes;
176 }
177 }
178
179 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
180 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
181 register_set->ContainsFloatingPointRegister(i)) {
182 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
183 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
184 saved_fpu_stack_offsets_[i] = stack_offset;
185 stack_offset += kDRegSizeInBytes;
186 }
187 }
188
189 SaveRestoreLiveRegistersHelper(codegen, register_set,
190 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
191}
192
193void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
194 RegisterSet* register_set = locations->GetLiveRegisters();
195 SaveRestoreLiveRegistersHelper(codegen, register_set,
196 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
197}
198
Alexandre Rames5319def2014-10-23 10:03:10 +0100199class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
200 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100201 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100202
Alexandre Rames67555f72014-11-18 10:55:16 +0000203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000205 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100206
Alexandre Rames5319def2014-10-23 10:03:10 +0100207 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000208 if (instruction_->CanThrowIntoCatchBlock()) {
209 // Live registers will be restored in the catch block if caught.
210 SaveLiveRegisters(codegen, instruction_->GetLocations());
211 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000212 // We're moving two locations to locations that could overlap, so we need a parallel
213 // move resolver.
214 InvokeRuntimeCallingConvention calling_convention;
215 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100216 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
217 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000218 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000219 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800220 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100221 }
222
Alexandre Rames8158f282015-08-07 10:26:17 +0100223 bool IsFatal() const OVERRIDE { return true; }
224
Alexandre Rames9931f312015-06-19 14:47:01 +0100225 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000228 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
231};
232
Alexandre Rames67555f72014-11-18 10:55:16 +0000233class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
234 public:
235 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
236
237 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
238 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
239 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000240 if (instruction_->CanThrowIntoCatchBlock()) {
241 // Live registers will be restored in the catch block if caught.
242 SaveLiveRegisters(codegen, instruction_->GetLocations());
243 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000244 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000245 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000247 }
248
Alexandre Rames8158f282015-08-07 10:26:17 +0100249 bool IsFatal() const OVERRIDE { return true; }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 private:
254 HDivZeroCheck* const instruction_;
255 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
256};
257
258class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
259 public:
260 LoadClassSlowPathARM64(HLoadClass* cls,
261 HInstruction* at,
262 uint32_t dex_pc,
263 bool do_clinit)
264 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
265 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
266 }
267
268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 LocationSummary* locations = at_->GetLocations();
270 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
271
272 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000274
275 InvokeRuntimeCallingConvention calling_convention;
276 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
278 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000279 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800280 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800282 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100283 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800284 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 // Move the class to the desired location.
287 Location out = locations->Out();
288 if (out.IsValid()) {
289 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
290 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000291 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 }
293
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 __ B(GetExitLabel());
296 }
297
Alexandre Rames9931f312015-06-19 14:47:01 +0100298 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
299
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 private:
301 // The class this slow path will load.
302 HLoadClass* const cls_;
303
304 // The instruction where this slow path is happening.
305 // (Might be the load class or an initialization check).
306 HInstruction* const at_;
307
308 // The dex PC of `at_`.
309 const uint32_t dex_pc_;
310
311 // Whether to initialize the class.
312 const bool do_clinit_;
313
314 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
315};
316
317class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
318 public:
319 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
322 LocationSummary* locations = instruction_->GetLocations();
323 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
324 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
325
326 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000327 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
329 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800330 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000331 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000332 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100333 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000335 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000336
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000337 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000338 __ B(GetExitLabel());
339 }
340
Alexandre Rames9931f312015-06-19 14:47:01 +0100341 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
342
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 private:
344 HLoadString* const instruction_;
345
346 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
347};
348
Alexandre Rames5319def2014-10-23 10:03:10 +0100349class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
350 public:
351 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
352
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
354 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000356 if (instruction_->CanThrowIntoCatchBlock()) {
357 // Live registers will be restored in the catch block if caught.
358 SaveLiveRegisters(codegen, instruction_->GetLocations());
359 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000361 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800362 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100363 }
364
Alexandre Rames8158f282015-08-07 10:26:17 +0100365 bool IsFatal() const OVERRIDE { return true; }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
368
Alexandre Rames5319def2014-10-23 10:03:10 +0100369 private:
370 HNullCheck* const instruction_;
371
372 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
373};
374
375class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
376 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100377 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100378 : instruction_(instruction), successor_(successor) {}
379
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
381 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000384 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000385 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800386 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000387 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000388 if (successor_ == nullptr) {
389 __ B(GetReturnLabel());
390 } else {
391 __ B(arm64_codegen->GetLabelOf(successor_));
392 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 }
394
395 vixl::Label* GetReturnLabel() {
396 DCHECK(successor_ == nullptr);
397 return &return_label_;
398 }
399
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100400 HBasicBlock* GetSuccessor() const {
401 return successor_;
402 }
403
Alexandre Rames9931f312015-06-19 14:47:01 +0100404 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
405
Alexandre Rames5319def2014-10-23 10:03:10 +0100406 private:
407 HSuspendCheck* const instruction_;
408 // If not null, the block to branch to after the suspend check.
409 HBasicBlock* const successor_;
410
411 // If `successor_` is null, the label to branch to after the suspend check.
412 vixl::Label return_label_;
413
414 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
415};
416
Alexandre Rames67555f72014-11-18 10:55:16 +0000417class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000419 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
420 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000421
422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100424 Location class_to_check = locations->InAt(1);
425 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
426 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000427 DCHECK(instruction_->IsCheckCast()
428 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
429 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100430 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000431
Alexandre Rames67555f72014-11-18 10:55:16 +0000432 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000433
434 if (instruction_->IsCheckCast()) {
435 // The codegen for the instruction overwrites `temp`, so put it back in place.
436 Register obj = InputRegisterAt(instruction_, 0);
437 Register temp = WRegisterFrom(locations->GetTemp(0));
438 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
439 __ Ldr(temp, HeapOperand(obj, class_offset));
440 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
441 }
442
443 if (!is_fatal_) {
444 SaveLiveRegisters(codegen, locations);
445 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
447 // We're moving two locations to locations that could overlap, so we need a parallel
448 // move resolver.
449 InvokeRuntimeCallingConvention calling_convention;
450 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100451 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
452 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453
454 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000455 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100456 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000457 Primitive::Type ret_type = instruction_->GetType();
458 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
459 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800460 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
461 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000462 } else {
463 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100464 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800465 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000466 }
467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000468 if (!is_fatal_) {
469 RestoreLiveRegisters(codegen, locations);
470 __ B(GetExitLabel());
471 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000472 }
473
Alexandre Rames9931f312015-06-19 14:47:01 +0100474 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100476
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000479 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000480
Alexandre Rames67555f72014-11-18 10:55:16 +0000481 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
482};
483
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700484class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
485 public:
486 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100487 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700488
489 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
490 __ Bind(GetEntryLabel());
491 SaveLiveRegisters(codegen, instruction_->GetLocations());
492 DCHECK(instruction_->IsDeoptimize());
493 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
494 uint32_t dex_pc = deoptimize->GetDexPc();
495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
496 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
497 }
498
Alexandre Rames9931f312015-06-19 14:47:01 +0100499 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
500
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700501 private:
502 HInstruction* const instruction_;
503 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
504};
505
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100506class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
507 public:
508 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
509
510 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
511 LocationSummary* locations = instruction_->GetLocations();
512 __ Bind(GetEntryLabel());
513 SaveLiveRegisters(codegen, locations);
514
515 InvokeRuntimeCallingConvention calling_convention;
516 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
517 parallel_move.AddMove(
518 locations->InAt(0),
519 LocationFrom(calling_convention.GetRegisterAt(0)),
520 Primitive::kPrimNot,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(1),
524 LocationFrom(calling_convention.GetRegisterAt(1)),
525 Primitive::kPrimInt,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(2),
529 LocationFrom(calling_convention.GetRegisterAt(2)),
530 Primitive::kPrimNot,
531 nullptr);
532 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
533
534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
535 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
536 instruction_,
537 instruction_->GetDexPc(),
538 this);
539 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
540 RestoreLiveRegisters(codegen, locations);
541 __ B(GetExitLabel());
542 }
543
544 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
545
546 private:
547 HInstruction* const instruction_;
548
549 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
550};
551
Zheng Xu3927c8b2015-11-18 17:46:25 +0800552void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
553 uint32_t num_entries = switch_instr_->GetNumEntries();
554 DCHECK_GE(num_entries, kPackedSwitchJumpTableThreshold);
555
556 // We are about to use the assembler to place literals directly. Make sure we have enough
557 // underlying code buffer and we have generated the jump table with right size.
558 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
559 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
560
561 __ Bind(&table_start_);
562 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
563 for (uint32_t i = 0; i < num_entries; i++) {
564 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
565 DCHECK(target_label->IsBound());
566 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
567 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
568 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
569 Literal<int32_t> literal(jump_offset);
570 __ place(&literal);
571 }
572}
573
Alexandre Rames5319def2014-10-23 10:03:10 +0100574#undef __
575
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100576Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100577 Location next_location;
578 if (type == Primitive::kPrimVoid) {
579 LOG(FATAL) << "Unreachable type " << type;
580 }
581
Alexandre Rames542361f2015-01-29 16:57:31 +0000582 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100583 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
584 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000585 } else if (!Primitive::IsFloatingPointType(type) &&
586 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000587 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
588 } else {
589 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000590 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
591 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100592 }
593
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000594 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000595 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100596 return next_location;
597}
598
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100599Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100600 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100601}
602
Serban Constantinescu579885a2015-02-22 20:51:33 +0000603CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
604 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100605 const CompilerOptions& compiler_options,
606 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100607 : CodeGenerator(graph,
608 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000609 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000610 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000611 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000612 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100613 compiler_options,
614 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100615 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800616 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100617 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000618 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000619 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000620 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100621 uint64_literals_(std::less<uint64_t>(),
622 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
623 method_patches_(MethodReferenceComparator(),
624 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
625 call_patches_(MethodReferenceComparator(),
626 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
627 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000628 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000629 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000630 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000631}
Alexandre Rames5319def2014-10-23 10:03:10 +0100632
Alexandre Rames67555f72014-11-18 10:55:16 +0000633#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100634
Zheng Xu3927c8b2015-11-18 17:46:25 +0800635void CodeGeneratorARM64::EmitJumpTables() {
636 for (auto jump_table : jump_tables_) {
637 jump_table->EmitTable(this);
638 }
639}
640
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000641void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800642 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000643 // Ensure we emit the literal pool.
644 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000645
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000646 CodeGenerator::Finalize(allocator);
647}
648
Zheng Xuad4450e2015-04-17 18:48:56 +0800649void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
650 // Note: There are 6 kinds of moves:
651 // 1. constant -> GPR/FPR (non-cycle)
652 // 2. constant -> stack (non-cycle)
653 // 3. GPR/FPR -> GPR/FPR
654 // 4. GPR/FPR -> stack
655 // 5. stack -> GPR/FPR
656 // 6. stack -> stack (non-cycle)
657 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
658 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
659 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
660 // dependency.
661 vixl_temps_.Open(GetVIXLAssembler());
662}
663
664void ParallelMoveResolverARM64::FinishEmitNativeCode() {
665 vixl_temps_.Close();
666}
667
668Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
669 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
670 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
671 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
672 Location scratch = GetScratchLocation(kind);
673 if (!scratch.Equals(Location::NoLocation())) {
674 return scratch;
675 }
676 // Allocate from VIXL temp registers.
677 if (kind == Location::kRegister) {
678 scratch = LocationFrom(vixl_temps_.AcquireX());
679 } else {
680 DCHECK(kind == Location::kFpuRegister);
681 scratch = LocationFrom(vixl_temps_.AcquireD());
682 }
683 AddScratchLocation(scratch);
684 return scratch;
685}
686
687void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
688 if (loc.IsRegister()) {
689 vixl_temps_.Release(XRegisterFrom(loc));
690 } else {
691 DCHECK(loc.IsFpuRegister());
692 vixl_temps_.Release(DRegisterFrom(loc));
693 }
694 RemoveScratchLocation(loc);
695}
696
Alexandre Rames3e69f162014-12-10 10:36:50 +0000697void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100698 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100699 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000700}
701
Alexandre Rames5319def2014-10-23 10:03:10 +0100702void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100703 MacroAssembler* masm = GetVIXLAssembler();
704 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000705 __ Bind(&frame_entry_label_);
706
Serban Constantinescu02164b32014-11-13 14:05:07 +0000707 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
708 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100709 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000710 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000711 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000712 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000713 __ Ldr(wzr, MemOperand(temp, 0));
714 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000715 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100716
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000717 if (!HasEmptyFrame()) {
718 int frame_size = GetFrameSize();
719 // Stack layout:
720 // sp[frame_size - 8] : lr.
721 // ... : other preserved core registers.
722 // ... : other preserved fp registers.
723 // ... : reserved frame space.
724 // sp[0] : current method.
725 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100726 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800727 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
728 frame_size - GetCoreSpillSize());
729 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
730 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000731 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100732}
733
734void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100735 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100736 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000737 if (!HasEmptyFrame()) {
738 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800739 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
740 frame_size - FrameEntrySpillSize());
741 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
742 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000743 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100744 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000745 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100746 __ Ret();
747 GetAssembler()->cfi().RestoreState();
748 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100749}
750
Zheng Xuda403092015-04-24 17:35:39 +0800751vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
752 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
753 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
754 core_spill_mask_);
755}
756
757vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
758 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
759 GetNumberOfFloatingPointRegisters()));
760 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
761 fpu_spill_mask_);
762}
763
Alexandre Rames5319def2014-10-23 10:03:10 +0100764void CodeGeneratorARM64::Bind(HBasicBlock* block) {
765 __ Bind(GetLabelOf(block));
766}
767
Alexandre Rames5319def2014-10-23 10:03:10 +0100768void CodeGeneratorARM64::Move(HInstruction* instruction,
769 Location location,
770 HInstruction* move_for) {
771 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000773 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100774
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100775 if (instruction->IsFakeString()) {
776 // The fake string is an alias for null.
777 DCHECK(IsBaseline());
778 instruction = locations->Out().GetConstant();
779 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
780 }
781
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100782 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100783 MoveLocation(location,
784 Location::DoubleStackSlot(kCurrentMethodStackOffset),
785 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100786 } else if (locations != nullptr && locations->Out().Equals(location)) {
787 return;
788 } else if (instruction->IsIntConstant()
789 || instruction->IsLongConstant()
790 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000791 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100792 if (location.IsRegister()) {
793 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000794 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100795 (instruction->IsLongConstant() && dst.Is64Bits()));
796 __ Mov(dst, value);
797 } else {
798 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000799 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000800 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
801 ? temps.AcquireW()
802 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100803 __ Mov(temp, value);
804 __ Str(temp, StackOperandFrom(location));
805 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000806 } else if (instruction->IsTemporary()) {
807 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000808 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100809 } else if (instruction->IsLoadLocal()) {
810 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000811 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000812 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000813 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000814 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100815 }
816
817 } else {
818 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000819 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100820 }
821}
822
Calin Juravle175dc732015-08-25 15:42:32 +0100823void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
824 DCHECK(location.IsRegister());
825 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
826}
827
Calin Juravlee460d1d2015-09-29 04:52:17 +0100828void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
829 if (location.IsRegister()) {
830 locations->AddTemp(location);
831 } else {
832 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
833 }
834}
835
Alexandre Rames5319def2014-10-23 10:03:10 +0100836Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
837 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000838
Alexandre Rames5319def2014-10-23 10:03:10 +0100839 switch (type) {
840 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000841 case Primitive::kPrimInt:
842 case Primitive::kPrimFloat:
843 return Location::StackSlot(GetStackSlot(load->GetLocal()));
844
845 case Primitive::kPrimLong:
846 case Primitive::kPrimDouble:
847 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
848
Alexandre Rames5319def2014-10-23 10:03:10 +0100849 case Primitive::kPrimBoolean:
850 case Primitive::kPrimByte:
851 case Primitive::kPrimChar:
852 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100853 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100854 LOG(FATAL) << "Unexpected type " << type;
855 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000856
Alexandre Rames5319def2014-10-23 10:03:10 +0100857 LOG(FATAL) << "Unreachable";
858 return Location::NoLocation();
859}
860
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100861void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000862 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100863 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000864 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100866 if (value_can_be_null) {
867 __ Cbz(value, &done);
868 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100869 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
870 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000871 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100872 if (value_can_be_null) {
873 __ Bind(&done);
874 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100875}
876
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000877void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
878 // Blocked core registers:
879 // lr : Runtime reserved.
880 // tr : Runtime reserved.
881 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
882 // ip1 : VIXL core temp.
883 // ip0 : VIXL core temp.
884 //
885 // Blocked fp registers:
886 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100887 CPURegList reserved_core_registers = vixl_reserved_core_registers;
888 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100889 while (!reserved_core_registers.IsEmpty()) {
890 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
891 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000892
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000893 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800894 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
896 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000897
898 if (is_baseline) {
899 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
900 while (!reserved_core_baseline_registers.IsEmpty()) {
901 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
902 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100903 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000904
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100905 if (is_baseline || GetGraph()->IsDebuggable()) {
906 // Stubs do not save callee-save floating point registers. If the graph
907 // is debuggable, we need to deal with these registers differently. For
908 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000909 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
910 while (!reserved_fp_baseline_registers.IsEmpty()) {
911 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
912 }
913 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100914}
915
916Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
917 if (type == Primitive::kPrimVoid) {
918 LOG(FATAL) << "Unreachable type " << type;
919 }
920
Alexandre Rames542361f2015-01-29 16:57:31 +0000921 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000922 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
923 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100924 return Location::FpuRegisterLocation(reg);
925 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000926 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
927 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100928 return Location::RegisterLocation(reg);
929 }
930}
931
Alexandre Rames3e69f162014-12-10 10:36:50 +0000932size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
933 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
934 __ Str(reg, MemOperand(sp, stack_index));
935 return kArm64WordSize;
936}
937
938size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
939 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
940 __ Ldr(reg, MemOperand(sp, stack_index));
941 return kArm64WordSize;
942}
943
944size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
945 FPRegister reg = FPRegister(reg_id, kDRegSize);
946 __ Str(reg, MemOperand(sp, stack_index));
947 return kArm64WordSize;
948}
949
950size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
951 FPRegister reg = FPRegister(reg_id, kDRegSize);
952 __ Ldr(reg, MemOperand(sp, stack_index));
953 return kArm64WordSize;
954}
955
Alexandre Rames5319def2014-10-23 10:03:10 +0100956void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100957 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100958}
959
960void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100961 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100962}
963
Alexandre Rames67555f72014-11-18 10:55:16 +0000964void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000965 if (constant->IsIntConstant()) {
966 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
967 } else if (constant->IsLongConstant()) {
968 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
969 } else if (constant->IsNullConstant()) {
970 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000971 } else if (constant->IsFloatConstant()) {
972 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
973 } else {
974 DCHECK(constant->IsDoubleConstant());
975 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
976 }
977}
978
Alexandre Rames3e69f162014-12-10 10:36:50 +0000979
980static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
981 DCHECK(constant.IsConstant());
982 HConstant* cst = constant.GetConstant();
983 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000984 // Null is mapped to a core W register, which we associate with kPrimInt.
985 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000986 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
987 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
988 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
989}
990
Calin Juravlee460d1d2015-09-29 04:52:17 +0100991void CodeGeneratorARM64::MoveLocation(Location destination,
992 Location source,
993 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000994 if (source.Equals(destination)) {
995 return;
996 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000997
998 // A valid move can always be inferred from the destination and source
999 // locations. When moving from and to a register, the argument type can be
1000 // used to generate 32bit instead of 64bit moves. In debug mode we also
1001 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001002 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001003
1004 if (destination.IsRegister() || destination.IsFpuRegister()) {
1005 if (unspecified_type) {
1006 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1007 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001008 (src_cst != nullptr && (src_cst->IsIntConstant()
1009 || src_cst->IsFloatConstant()
1010 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001011 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001012 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001013 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001014 // If the source is a double stack slot or a 64bit constant, a 64bit
1015 // type is appropriate. Else the source is a register, and since the
1016 // type has not been specified, we chose a 64bit type to force a 64bit
1017 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001018 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001019 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001020 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1022 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1023 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001024 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1025 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1026 __ Ldr(dst, StackOperandFrom(source));
1027 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001028 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001029 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001030 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001031 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001032 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001033 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001034 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001035 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1036 ? Primitive::kPrimLong
1037 : Primitive::kPrimInt;
1038 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1039 }
1040 } else {
1041 DCHECK(source.IsFpuRegister());
1042 if (destination.IsRegister()) {
1043 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1044 ? Primitive::kPrimDouble
1045 : Primitive::kPrimFloat;
1046 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1047 } else {
1048 DCHECK(destination.IsFpuRegister());
1049 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001050 }
1051 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001052 } else { // The destination is not a register. It must be a stack slot.
1053 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1054 if (source.IsRegister() || source.IsFpuRegister()) {
1055 if (unspecified_type) {
1056 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001057 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001058 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001059 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001060 }
1061 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001062 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1063 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1064 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001065 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1067 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001068 UseScratchRegisterScope temps(GetVIXLAssembler());
1069 HConstant* src_cst = source.GetConstant();
1070 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001071 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001072 temp = temps.AcquireW();
1073 } else if (src_cst->IsLongConstant()) {
1074 temp = temps.AcquireX();
1075 } else if (src_cst->IsFloatConstant()) {
1076 temp = temps.AcquireS();
1077 } else {
1078 DCHECK(src_cst->IsDoubleConstant());
1079 temp = temps.AcquireD();
1080 }
1081 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001082 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001083 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001084 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001085 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001086 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001087 // There is generally less pressure on FP registers.
1088 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001089 __ Ldr(temp, StackOperandFrom(source));
1090 __ Str(temp, StackOperandFrom(destination));
1091 }
1092 }
1093}
1094
1095void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001096 CPURegister dst,
1097 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001098 switch (type) {
1099 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001100 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001101 break;
1102 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001103 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001104 break;
1105 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001106 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001107 break;
1108 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001109 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001110 break;
1111 case Primitive::kPrimInt:
1112 case Primitive::kPrimNot:
1113 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001114 case Primitive::kPrimFloat:
1115 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001116 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001117 __ Ldr(dst, src);
1118 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001119 case Primitive::kPrimVoid:
1120 LOG(FATAL) << "Unreachable type " << type;
1121 }
1122}
1123
Calin Juravle77520bc2015-01-12 18:45:46 +00001124void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125 CPURegister dst,
1126 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001127 MacroAssembler* masm = GetVIXLAssembler();
1128 BlockPoolsScope block_pools(masm);
1129 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001130 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001131 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001132
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001133 DCHECK(!src.IsPreIndex());
1134 DCHECK(!src.IsPostIndex());
1135
1136 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001137 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001138 MemOperand base = MemOperand(temp_base);
1139 switch (type) {
1140 case Primitive::kPrimBoolean:
1141 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001142 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001143 break;
1144 case Primitive::kPrimByte:
1145 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001146 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001147 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1148 break;
1149 case Primitive::kPrimChar:
1150 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001151 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001152 break;
1153 case Primitive::kPrimShort:
1154 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001155 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001156 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1157 break;
1158 case Primitive::kPrimInt:
1159 case Primitive::kPrimNot:
1160 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001161 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001162 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001163 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001164 break;
1165 case Primitive::kPrimFloat:
1166 case Primitive::kPrimDouble: {
1167 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001168 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001169
1170 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1171 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001172 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001173 __ Fmov(FPRegister(dst), temp);
1174 break;
1175 }
1176 case Primitive::kPrimVoid:
1177 LOG(FATAL) << "Unreachable type " << type;
1178 }
1179}
1180
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001181void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001182 CPURegister src,
1183 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001184 switch (type) {
1185 case Primitive::kPrimBoolean:
1186 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001187 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001188 break;
1189 case Primitive::kPrimChar:
1190 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001191 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001192 break;
1193 case Primitive::kPrimInt:
1194 case Primitive::kPrimNot:
1195 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001196 case Primitive::kPrimFloat:
1197 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001198 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001199 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001200 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001201 case Primitive::kPrimVoid:
1202 LOG(FATAL) << "Unreachable type " << type;
1203 }
1204}
1205
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001206void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1207 CPURegister src,
1208 const MemOperand& dst) {
1209 UseScratchRegisterScope temps(GetVIXLAssembler());
1210 Register temp_base = temps.AcquireX();
1211
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001212 DCHECK(!dst.IsPreIndex());
1213 DCHECK(!dst.IsPostIndex());
1214
1215 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001216 Operand op = OperandFromMemOperand(dst);
1217 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001218 MemOperand base = MemOperand(temp_base);
1219 switch (type) {
1220 case Primitive::kPrimBoolean:
1221 case Primitive::kPrimByte:
1222 __ Stlrb(Register(src), base);
1223 break;
1224 case Primitive::kPrimChar:
1225 case Primitive::kPrimShort:
1226 __ Stlrh(Register(src), base);
1227 break;
1228 case Primitive::kPrimInt:
1229 case Primitive::kPrimNot:
1230 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001231 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001232 __ Stlr(Register(src), base);
1233 break;
1234 case Primitive::kPrimFloat:
1235 case Primitive::kPrimDouble: {
1236 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001237 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001238
1239 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1240 __ Fmov(temp, FPRegister(src));
1241 __ Stlr(temp, base);
1242 break;
1243 }
1244 case Primitive::kPrimVoid:
1245 LOG(FATAL) << "Unreachable type " << type;
1246 }
1247}
1248
Calin Juravle175dc732015-08-25 15:42:32 +01001249void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1250 HInstruction* instruction,
1251 uint32_t dex_pc,
1252 SlowPathCode* slow_path) {
1253 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1254 instruction,
1255 dex_pc,
1256 slow_path);
1257}
1258
Alexandre Rames67555f72014-11-18 10:55:16 +00001259void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1260 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001261 uint32_t dex_pc,
1262 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001263 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001264 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001265 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1266 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001267 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001268}
1269
1270void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1271 vixl::Register class_reg) {
1272 UseScratchRegisterScope temps(GetVIXLAssembler());
1273 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001274 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001275 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001276
Serban Constantinescu02164b32014-11-13 14:05:07 +00001277 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001278 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001279 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1280 __ Add(temp, class_reg, status_offset);
1281 __ Ldar(temp, HeapOperand(temp));
1282 __ Cmp(temp, mirror::Class::kStatusInitialized);
1283 __ B(lt, slow_path->GetEntryLabel());
1284 } else {
1285 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1286 __ Cmp(temp, mirror::Class::kStatusInitialized);
1287 __ B(lt, slow_path->GetEntryLabel());
1288 __ Dmb(InnerShareable, BarrierReads);
1289 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001290 __ Bind(slow_path->GetExitLabel());
1291}
Alexandre Rames5319def2014-10-23 10:03:10 +01001292
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001293void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1294 BarrierType type = BarrierAll;
1295
1296 switch (kind) {
1297 case MemBarrierKind::kAnyAny:
1298 case MemBarrierKind::kAnyStore: {
1299 type = BarrierAll;
1300 break;
1301 }
1302 case MemBarrierKind::kLoadAny: {
1303 type = BarrierReads;
1304 break;
1305 }
1306 case MemBarrierKind::kStoreStore: {
1307 type = BarrierWrites;
1308 break;
1309 }
1310 default:
1311 LOG(FATAL) << "Unexpected memory barrier " << kind;
1312 }
1313 __ Dmb(InnerShareable, type);
1314}
1315
Serban Constantinescu02164b32014-11-13 14:05:07 +00001316void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1317 HBasicBlock* successor) {
1318 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001319 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1320 if (slow_path == nullptr) {
1321 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1322 instruction->SetSlowPath(slow_path);
1323 codegen_->AddSlowPath(slow_path);
1324 if (successor != nullptr) {
1325 DCHECK(successor->IsLoopHeader());
1326 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1327 }
1328 } else {
1329 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1330 }
1331
Serban Constantinescu02164b32014-11-13 14:05:07 +00001332 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1333 Register temp = temps.AcquireW();
1334
1335 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1336 if (successor == nullptr) {
1337 __ Cbnz(temp, slow_path->GetEntryLabel());
1338 __ Bind(slow_path->GetReturnLabel());
1339 } else {
1340 __ Cbz(temp, codegen_->GetLabelOf(successor));
1341 __ B(slow_path->GetEntryLabel());
1342 // slow_path will return to GetLabelOf(successor).
1343 }
1344}
1345
Alexandre Rames5319def2014-10-23 10:03:10 +01001346InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1347 CodeGeneratorARM64* codegen)
1348 : HGraphVisitor(graph),
1349 assembler_(codegen->GetAssembler()),
1350 codegen_(codegen) {}
1351
1352#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001353 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001354
1355#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1356
1357enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001358 // Using a base helps identify when we hit such breakpoints.
1359 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001360#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1361 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1362#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1363};
1364
1365#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001366 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001367 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1368 } \
1369 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1370 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1371 locations->SetOut(Location::Any()); \
1372 }
1373 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1374#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1375
1376#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001377#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001378
Alexandre Rames67555f72014-11-18 10:55:16 +00001379void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 DCHECK_EQ(instr->InputCount(), 2U);
1381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1382 Primitive::Type type = instr->GetResultType();
1383 switch (type) {
1384 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001385 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001386 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001387 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001389 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001390
1391 case Primitive::kPrimFloat:
1392 case Primitive::kPrimDouble:
1393 locations->SetInAt(0, Location::RequiresFpuRegister());
1394 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001395 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001396 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001397
Alexandre Rames5319def2014-10-23 10:03:10 +01001398 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001399 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001400 }
1401}
1402
Alexandre Rames09a99962015-04-15 11:47:56 +01001403void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1404 LocationSummary* locations =
1405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1406 locations->SetInAt(0, Location::RequiresRegister());
1407 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1408 locations->SetOut(Location::RequiresFpuRegister());
1409 } else {
1410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1411 }
1412}
1413
1414void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1415 const FieldInfo& field_info) {
1416 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001417 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001418 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001419
1420 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1421 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1422
1423 if (field_info.IsVolatile()) {
1424 if (use_acquire_release) {
1425 // NB: LoadAcquire will record the pc info if needed.
1426 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1427 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001428 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001429 codegen_->MaybeRecordImplicitNullCheck(instruction);
1430 // For IRIW sequential consistency kLoadAny is not sufficient.
1431 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1432 }
1433 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001434 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001435 codegen_->MaybeRecordImplicitNullCheck(instruction);
1436 }
Roland Levillain4d027112015-07-01 15:41:14 +01001437
1438 if (field_type == Primitive::kPrimNot) {
1439 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1440 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001441}
1442
1443void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1444 LocationSummary* locations =
1445 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1446 locations->SetInAt(0, Location::RequiresRegister());
1447 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1448 locations->SetInAt(1, Location::RequiresFpuRegister());
1449 } else {
1450 locations->SetInAt(1, Location::RequiresRegister());
1451 }
1452}
1453
1454void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001455 const FieldInfo& field_info,
1456 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001457 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001458 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001459
1460 Register obj = InputRegisterAt(instruction, 0);
1461 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001462 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001463 Offset offset = field_info.GetFieldOffset();
1464 Primitive::Type field_type = field_info.GetFieldType();
1465 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1466
Roland Levillain4d027112015-07-01 15:41:14 +01001467 {
1468 // We use a block to end the scratch scope before the write barrier, thus
1469 // freeing the temporary registers so they can be used in `MarkGCCard`.
1470 UseScratchRegisterScope temps(GetVIXLAssembler());
1471
1472 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1473 DCHECK(value.IsW());
1474 Register temp = temps.AcquireW();
1475 __ Mov(temp, value.W());
1476 GetAssembler()->PoisonHeapReference(temp.W());
1477 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001478 }
Roland Levillain4d027112015-07-01 15:41:14 +01001479
1480 if (field_info.IsVolatile()) {
1481 if (use_acquire_release) {
1482 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1483 codegen_->MaybeRecordImplicitNullCheck(instruction);
1484 } else {
1485 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1486 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1487 codegen_->MaybeRecordImplicitNullCheck(instruction);
1488 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1489 }
1490 } else {
1491 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1492 codegen_->MaybeRecordImplicitNullCheck(instruction);
1493 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001494 }
1495
1496 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001497 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001498 }
1499}
1500
Alexandre Rames67555f72014-11-18 10:55:16 +00001501void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001502 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001503
1504 switch (type) {
1505 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001506 case Primitive::kPrimLong: {
1507 Register dst = OutputRegister(instr);
1508 Register lhs = InputRegisterAt(instr, 0);
1509 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001510 if (instr->IsAdd()) {
1511 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001512 } else if (instr->IsAnd()) {
1513 __ And(dst, lhs, rhs);
1514 } else if (instr->IsOr()) {
1515 __ Orr(dst, lhs, rhs);
1516 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001517 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001518 } else {
1519 DCHECK(instr->IsXor());
1520 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001521 }
1522 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001523 }
1524 case Primitive::kPrimFloat:
1525 case Primitive::kPrimDouble: {
1526 FPRegister dst = OutputFPRegister(instr);
1527 FPRegister lhs = InputFPRegisterAt(instr, 0);
1528 FPRegister rhs = InputFPRegisterAt(instr, 1);
1529 if (instr->IsAdd()) {
1530 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001531 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001532 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001533 } else {
1534 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001535 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001536 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001537 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001538 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001539 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001540 }
1541}
1542
Serban Constantinescu02164b32014-11-13 14:05:07 +00001543void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1544 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1545
1546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1547 Primitive::Type type = instr->GetResultType();
1548 switch (type) {
1549 case Primitive::kPrimInt:
1550 case Primitive::kPrimLong: {
1551 locations->SetInAt(0, Location::RequiresRegister());
1552 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1553 locations->SetOut(Location::RequiresRegister());
1554 break;
1555 }
1556 default:
1557 LOG(FATAL) << "Unexpected shift type " << type;
1558 }
1559}
1560
1561void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1562 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1563
1564 Primitive::Type type = instr->GetType();
1565 switch (type) {
1566 case Primitive::kPrimInt:
1567 case Primitive::kPrimLong: {
1568 Register dst = OutputRegister(instr);
1569 Register lhs = InputRegisterAt(instr, 0);
1570 Operand rhs = InputOperandAt(instr, 1);
1571 if (rhs.IsImmediate()) {
1572 uint32_t shift_value = (type == Primitive::kPrimInt)
1573 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1574 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1575 if (instr->IsShl()) {
1576 __ Lsl(dst, lhs, shift_value);
1577 } else if (instr->IsShr()) {
1578 __ Asr(dst, lhs, shift_value);
1579 } else {
1580 __ Lsr(dst, lhs, shift_value);
1581 }
1582 } else {
1583 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1584
1585 if (instr->IsShl()) {
1586 __ Lsl(dst, lhs, rhs_reg);
1587 } else if (instr->IsShr()) {
1588 __ Asr(dst, lhs, rhs_reg);
1589 } else {
1590 __ Lsr(dst, lhs, rhs_reg);
1591 }
1592 }
1593 break;
1594 }
1595 default:
1596 LOG(FATAL) << "Unexpected shift operation type " << type;
1597 }
1598}
1599
Alexandre Rames5319def2014-10-23 10:03:10 +01001600void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001601 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001602}
1603
1604void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001605 HandleBinaryOp(instruction);
1606}
1607
1608void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1609 HandleBinaryOp(instruction);
1610}
1611
1612void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1613 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001614}
1615
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001616void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
1617 LocationSummary* locations =
1618 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1619 locations->SetInAt(0, Location::RequiresRegister());
1620 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1621 locations->SetOut(Location::RequiresRegister());
1622}
1623
1624void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1625 HArm64IntermediateAddress* instruction) {
1626 __ Add(OutputRegister(instruction),
1627 InputRegisterAt(instruction, 0),
1628 Operand(InputOperandAt(instruction, 1)));
1629}
1630
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001631void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1632 LocationSummary* locations =
1633 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1634 locations->SetInAt(0, Location::RequiresRegister());
1635 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001636 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1637 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1638 } else {
1639 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1640 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001641}
1642
1643void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001644 Primitive::Type type = instruction->GetType();
1645 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001646 Location index = instruction->GetLocations()->InAt(1);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001648 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001649 CPURegister dest = OutputCPURegister(instruction);
1650
Alexandre Ramesd921d642015-04-16 15:07:16 +01001651 MacroAssembler* masm = GetVIXLAssembler();
1652 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001653 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01001654 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001655
1656 if (index.IsConstant()) {
1657 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001658 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001659 } else {
1660 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001661 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1662 // We do not need to compute the intermediate address from the array: the
1663 // input instruction has done it already. See the comment in
1664 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1665 if (kIsDebugBuild) {
1666 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1667 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1668 }
1669 temp = obj;
1670 } else {
1671 __ Add(temp, obj, offset);
1672 }
Alexandre Rames82000b02015-07-07 11:34:16 +01001673 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001674 }
1675
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001676 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001678
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001679 if (instruction->GetType() == Primitive::kPrimNot) {
1680 GetAssembler()->MaybeUnpoisonHeapReference(dest.W());
Roland Levillain4d027112015-07-01 15:41:14 +01001681 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001682}
1683
Alexandre Rames5319def2014-10-23 10:03:10 +01001684void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1686 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001688}
1689
1690void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001691 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001692 __ Ldr(OutputRegister(instruction),
1693 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001695}
1696
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001697void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1699 instruction,
1700 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1701 locations->SetInAt(0, Location::RequiresRegister());
1702 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1703 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1704 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001705 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001706 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001707 }
1708}
1709
1710void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1711 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001712 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001713 bool may_need_runtime_call = locations->CanCall();
1714 bool needs_write_barrier =
1715 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001716
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001717 Register array = InputRegisterAt(instruction, 0);
1718 CPURegister value = InputCPURegisterAt(instruction, 2);
1719 CPURegister source = value;
1720 Location index = locations->InAt(1);
1721 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1722 MemOperand destination = HeapOperand(array);
1723 MacroAssembler* masm = GetVIXLAssembler();
1724 BlockPoolsScope block_pools(masm);
1725
1726 if (!needs_write_barrier) {
1727 DCHECK(!may_need_runtime_call);
1728 if (index.IsConstant()) {
1729 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1730 destination = HeapOperand(array, offset);
1731 } else {
1732 UseScratchRegisterScope temps(masm);
1733 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001734 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1735 // We do not need to compute the intermediate address from the array: the
1736 // input instruction has done it already. See the comment in
1737 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1738 if (kIsDebugBuild) {
1739 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1740 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1741 }
1742 temp = array;
1743 } else {
1744 __ Add(temp, array, offset);
1745 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001746 destination = HeapOperand(temp,
1747 XRegisterFrom(index),
1748 LSL,
1749 Primitive::ComponentSizeShift(value_type));
1750 }
1751 codegen_->Store(value_type, value, destination);
1752 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001753 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001754 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001755 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001756 vixl::Label done;
1757 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001758 {
1759 // We use a block to end the scratch scope before the write barrier, thus
1760 // freeing the temporary registers so they can be used in `MarkGCCard`.
1761 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001762 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001763 if (index.IsConstant()) {
1764 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001765 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001766 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001767 destination = HeapOperand(temp,
1768 XRegisterFrom(index),
1769 LSL,
1770 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001771 }
1772
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001773 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1774 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1775 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1776
1777 if (may_need_runtime_call) {
1778 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1779 codegen_->AddSlowPath(slow_path);
1780 if (instruction->GetValueCanBeNull()) {
1781 vixl::Label non_zero;
1782 __ Cbnz(Register(value), &non_zero);
1783 if (!index.IsConstant()) {
1784 __ Add(temp, array, offset);
1785 }
1786 __ Str(wzr, destination);
1787 codegen_->MaybeRecordImplicitNullCheck(instruction);
1788 __ B(&done);
1789 __ Bind(&non_zero);
1790 }
1791
1792 Register temp2 = temps.AcquireSameSizeAs(array);
1793 __ Ldr(temp, HeapOperand(array, class_offset));
1794 codegen_->MaybeRecordImplicitNullCheck(instruction);
1795 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1796 __ Ldr(temp, HeapOperand(temp, component_offset));
1797 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1798 // No need to poison/unpoison, we're comparing two poisoned references.
1799 __ Cmp(temp, temp2);
1800 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1801 vixl::Label do_put;
1802 __ B(eq, &do_put);
1803 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1804 __ Ldr(temp, HeapOperand(temp, super_offset));
1805 // No need to unpoison, we're comparing against null.
1806 __ Cbnz(temp, slow_path->GetEntryLabel());
1807 __ Bind(&do_put);
1808 } else {
1809 __ B(ne, slow_path->GetEntryLabel());
1810 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001811 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001812 }
1813
1814 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001815 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001816 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001817 __ Mov(temp2, value.W());
1818 GetAssembler()->PoisonHeapReference(temp2);
1819 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001820 }
1821
1822 if (!index.IsConstant()) {
1823 __ Add(temp, array, offset);
1824 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001825 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001826
1827 if (!may_need_runtime_call) {
1828 codegen_->MaybeRecordImplicitNullCheck(instruction);
1829 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001830 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001831
1832 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1833
1834 if (done.IsLinked()) {
1835 __ Bind(&done);
1836 }
1837
1838 if (slow_path != nullptr) {
1839 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001840 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001841 }
1842}
1843
Alexandre Rames67555f72014-11-18 10:55:16 +00001844void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001845 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1846 ? LocationSummary::kCallOnSlowPath
1847 : LocationSummary::kNoCall;
1848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001849 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001850 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001851 if (instruction->HasUses()) {
1852 locations->SetOut(Location::SameAsFirstInput());
1853 }
1854}
1855
1856void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001857 BoundsCheckSlowPathARM64* slow_path =
1858 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001859 codegen_->AddSlowPath(slow_path);
1860
1861 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1862 __ B(slow_path->GetEntryLabel(), hs);
1863}
1864
Alexandre Rames67555f72014-11-18 10:55:16 +00001865void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1866 LocationSummary* locations =
1867 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1868 locations->SetInAt(0, Location::RequiresRegister());
1869 if (check->HasUses()) {
1870 locations->SetOut(Location::SameAsFirstInput());
1871 }
1872}
1873
1874void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1875 // We assume the class is not null.
1876 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1877 check->GetLoadClass(), check, check->GetDexPc(), true);
1878 codegen_->AddSlowPath(slow_path);
1879 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1880}
1881
Roland Levillain7f63c522015-07-13 15:54:55 +00001882static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1883 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1884 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1885}
1886
Serban Constantinescu02164b32014-11-13 14:05:07 +00001887void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001888 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001889 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1890 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001891 switch (in_type) {
1892 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001893 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001894 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1896 break;
1897 }
1898 case Primitive::kPrimFloat:
1899 case Primitive::kPrimDouble: {
1900 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001901 locations->SetInAt(1,
1902 IsFloatingPointZeroConstant(compare->InputAt(1))
1903 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1904 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001905 locations->SetOut(Location::RequiresRegister());
1906 break;
1907 }
1908 default:
1909 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1910 }
1911}
1912
1913void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1914 Primitive::Type in_type = compare->InputAt(0)->GetType();
1915
1916 // 0 if: left == right
1917 // 1 if: left > right
1918 // -1 if: left < right
1919 switch (in_type) {
1920 case Primitive::kPrimLong: {
1921 Register result = OutputRegister(compare);
1922 Register left = InputRegisterAt(compare, 0);
1923 Operand right = InputOperandAt(compare, 1);
1924
1925 __ Cmp(left, right);
1926 __ Cset(result, ne);
1927 __ Cneg(result, result, lt);
1928 break;
1929 }
1930 case Primitive::kPrimFloat:
1931 case Primitive::kPrimDouble: {
1932 Register result = OutputRegister(compare);
1933 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001934 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001935 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1936 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001937 __ Fcmp(left, 0.0);
1938 } else {
1939 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1940 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001941 if (compare->IsGtBias()) {
1942 __ Cset(result, ne);
1943 } else {
1944 __ Csetm(result, ne);
1945 }
1946 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001947 break;
1948 }
1949 default:
1950 LOG(FATAL) << "Unimplemented compare type " << in_type;
1951 }
1952}
1953
1954void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1955 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001956
1957 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1958 locations->SetInAt(0, Location::RequiresFpuRegister());
1959 locations->SetInAt(1,
1960 IsFloatingPointZeroConstant(instruction->InputAt(1))
1961 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1962 : Location::RequiresFpuRegister());
1963 } else {
1964 // Integer cases.
1965 locations->SetInAt(0, Location::RequiresRegister());
1966 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1967 }
1968
Alexandre Rames5319def2014-10-23 10:03:10 +01001969 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001971 }
1972}
1973
1974void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1975 if (!instruction->NeedsMaterialization()) {
1976 return;
1977 }
1978
1979 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001980 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001981 IfCondition if_cond = instruction->GetCondition();
1982 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001983
Roland Levillain7f63c522015-07-13 15:54:55 +00001984 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1985 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1986 if (locations->InAt(1).IsConstant()) {
1987 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1988 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1989 __ Fcmp(lhs, 0.0);
1990 } else {
1991 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1992 }
1993 __ Cset(res, arm64_cond);
1994 if (instruction->IsFPConditionTrueIfNaN()) {
1995 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1996 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1997 } else if (instruction->IsFPConditionFalseIfNaN()) {
1998 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1999 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
2000 }
2001 } else {
2002 // Integer cases.
2003 Register lhs = InputRegisterAt(instruction, 0);
2004 Operand rhs = InputOperandAt(instruction, 1);
2005 __ Cmp(lhs, rhs);
2006 __ Cset(res, arm64_cond);
2007 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002008}
2009
2010#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2011 M(Equal) \
2012 M(NotEqual) \
2013 M(LessThan) \
2014 M(LessThanOrEqual) \
2015 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002016 M(GreaterThanOrEqual) \
2017 M(Below) \
2018 M(BelowOrEqual) \
2019 M(Above) \
2020 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002021#define DEFINE_CONDITION_VISITORS(Name) \
2022void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
2023void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
2024FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002025#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002026#undef FOR_EACH_CONDITION_INSTRUCTION
2027
Zheng Xuc6667102015-05-15 16:08:45 +08002028void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2029 DCHECK(instruction->IsDiv() || instruction->IsRem());
2030
2031 LocationSummary* locations = instruction->GetLocations();
2032 Location second = locations->InAt(1);
2033 DCHECK(second.IsConstant());
2034
2035 Register out = OutputRegister(instruction);
2036 Register dividend = InputRegisterAt(instruction, 0);
2037 int64_t imm = Int64FromConstant(second.GetConstant());
2038 DCHECK(imm == 1 || imm == -1);
2039
2040 if (instruction->IsRem()) {
2041 __ Mov(out, 0);
2042 } else {
2043 if (imm == 1) {
2044 __ Mov(out, dividend);
2045 } else {
2046 __ Neg(out, dividend);
2047 }
2048 }
2049}
2050
2051void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2052 DCHECK(instruction->IsDiv() || instruction->IsRem());
2053
2054 LocationSummary* locations = instruction->GetLocations();
2055 Location second = locations->InAt(1);
2056 DCHECK(second.IsConstant());
2057
2058 Register out = OutputRegister(instruction);
2059 Register dividend = InputRegisterAt(instruction, 0);
2060 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01002061 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002062 DCHECK(IsPowerOfTwo(abs_imm));
2063 int ctz_imm = CTZ(abs_imm);
2064
2065 UseScratchRegisterScope temps(GetVIXLAssembler());
2066 Register temp = temps.AcquireSameSizeAs(out);
2067
2068 if (instruction->IsDiv()) {
2069 __ Add(temp, dividend, abs_imm - 1);
2070 __ Cmp(dividend, 0);
2071 __ Csel(out, temp, dividend, lt);
2072 if (imm > 0) {
2073 __ Asr(out, out, ctz_imm);
2074 } else {
2075 __ Neg(out, Operand(out, ASR, ctz_imm));
2076 }
2077 } else {
2078 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2079 __ Asr(temp, dividend, bits - 1);
2080 __ Lsr(temp, temp, bits - ctz_imm);
2081 __ Add(out, dividend, temp);
2082 __ And(out, out, abs_imm - 1);
2083 __ Sub(out, out, temp);
2084 }
2085}
2086
2087void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2088 DCHECK(instruction->IsDiv() || instruction->IsRem());
2089
2090 LocationSummary* locations = instruction->GetLocations();
2091 Location second = locations->InAt(1);
2092 DCHECK(second.IsConstant());
2093
2094 Register out = OutputRegister(instruction);
2095 Register dividend = InputRegisterAt(instruction, 0);
2096 int64_t imm = Int64FromConstant(second.GetConstant());
2097
2098 Primitive::Type type = instruction->GetResultType();
2099 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2100
2101 int64_t magic;
2102 int shift;
2103 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2104
2105 UseScratchRegisterScope temps(GetVIXLAssembler());
2106 Register temp = temps.AcquireSameSizeAs(out);
2107
2108 // temp = get_high(dividend * magic)
2109 __ Mov(temp, magic);
2110 if (type == Primitive::kPrimLong) {
2111 __ Smulh(temp, dividend, temp);
2112 } else {
2113 __ Smull(temp.X(), dividend, temp);
2114 __ Lsr(temp.X(), temp.X(), 32);
2115 }
2116
2117 if (imm > 0 && magic < 0) {
2118 __ Add(temp, temp, dividend);
2119 } else if (imm < 0 && magic > 0) {
2120 __ Sub(temp, temp, dividend);
2121 }
2122
2123 if (shift != 0) {
2124 __ Asr(temp, temp, shift);
2125 }
2126
2127 if (instruction->IsDiv()) {
2128 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2129 } else {
2130 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2131 // TODO: Strength reduction for msub.
2132 Register temp_imm = temps.AcquireSameSizeAs(out);
2133 __ Mov(temp_imm, imm);
2134 __ Msub(out, temp, temp_imm, dividend);
2135 }
2136}
2137
2138void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2139 DCHECK(instruction->IsDiv() || instruction->IsRem());
2140 Primitive::Type type = instruction->GetResultType();
2141 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2142
2143 LocationSummary* locations = instruction->GetLocations();
2144 Register out = OutputRegister(instruction);
2145 Location second = locations->InAt(1);
2146
2147 if (second.IsConstant()) {
2148 int64_t imm = Int64FromConstant(second.GetConstant());
2149
2150 if (imm == 0) {
2151 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2152 } else if (imm == 1 || imm == -1) {
2153 DivRemOneOrMinusOne(instruction);
2154 } else if (IsPowerOfTwo(std::abs(imm))) {
2155 DivRemByPowerOfTwo(instruction);
2156 } else {
2157 DCHECK(imm <= -2 || imm >= 2);
2158 GenerateDivRemWithAnyConstant(instruction);
2159 }
2160 } else {
2161 Register dividend = InputRegisterAt(instruction, 0);
2162 Register divisor = InputRegisterAt(instruction, 1);
2163 if (instruction->IsDiv()) {
2164 __ Sdiv(out, dividend, divisor);
2165 } else {
2166 UseScratchRegisterScope temps(GetVIXLAssembler());
2167 Register temp = temps.AcquireSameSizeAs(out);
2168 __ Sdiv(temp, dividend, divisor);
2169 __ Msub(out, temp, divisor, dividend);
2170 }
2171 }
2172}
2173
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002174void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2175 LocationSummary* locations =
2176 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2177 switch (div->GetResultType()) {
2178 case Primitive::kPrimInt:
2179 case Primitive::kPrimLong:
2180 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002181 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2183 break;
2184
2185 case Primitive::kPrimFloat:
2186 case Primitive::kPrimDouble:
2187 locations->SetInAt(0, Location::RequiresFpuRegister());
2188 locations->SetInAt(1, Location::RequiresFpuRegister());
2189 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2190 break;
2191
2192 default:
2193 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2194 }
2195}
2196
2197void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2198 Primitive::Type type = div->GetResultType();
2199 switch (type) {
2200 case Primitive::kPrimInt:
2201 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002202 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002203 break;
2204
2205 case Primitive::kPrimFloat:
2206 case Primitive::kPrimDouble:
2207 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2208 break;
2209
2210 default:
2211 LOG(FATAL) << "Unexpected div type " << type;
2212 }
2213}
2214
Alexandre Rames67555f72014-11-18 10:55:16 +00002215void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002216 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2217 ? LocationSummary::kCallOnSlowPath
2218 : LocationSummary::kNoCall;
2219 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002220 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2221 if (instruction->HasUses()) {
2222 locations->SetOut(Location::SameAsFirstInput());
2223 }
2224}
2225
2226void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2227 SlowPathCodeARM64* slow_path =
2228 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2229 codegen_->AddSlowPath(slow_path);
2230 Location value = instruction->GetLocations()->InAt(0);
2231
Alexandre Rames3e69f162014-12-10 10:36:50 +00002232 Primitive::Type type = instruction->GetType();
2233
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002234 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2235 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002236 return;
2237 }
2238
Alexandre Rames67555f72014-11-18 10:55:16 +00002239 if (value.IsConstant()) {
2240 int64_t divisor = Int64ConstantFrom(value);
2241 if (divisor == 0) {
2242 __ B(slow_path->GetEntryLabel());
2243 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002244 // A division by a non-null constant is valid. We don't need to perform
2245 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002246 }
2247 } else {
2248 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2249 }
2250}
2251
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002252void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2253 LocationSummary* locations =
2254 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2255 locations->SetOut(Location::ConstantLocation(constant));
2256}
2257
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002258void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2259 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002260 // Will be generated at use site.
2261}
2262
Alexandre Rames5319def2014-10-23 10:03:10 +01002263void LocationsBuilderARM64::VisitExit(HExit* exit) {
2264 exit->SetLocations(nullptr);
2265}
2266
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002267void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002268}
2269
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002270void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2271 LocationSummary* locations =
2272 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2273 locations->SetOut(Location::ConstantLocation(constant));
2274}
2275
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002276void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002277 // Will be generated at use site.
2278}
2279
David Brazdilfc6a86a2015-06-26 10:33:45 +00002280void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002281 DCHECK(!successor->IsExitBlock());
2282 HBasicBlock* block = got->GetBlock();
2283 HInstruction* previous = got->GetPrevious();
2284 HLoopInformation* info = block->GetLoopInformation();
2285
David Brazdil46e2a392015-03-16 17:31:52 +00002286 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002287 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2288 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2289 return;
2290 }
2291 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2292 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2293 }
2294 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002295 __ B(codegen_->GetLabelOf(successor));
2296 }
2297}
2298
David Brazdilfc6a86a2015-06-26 10:33:45 +00002299void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2300 got->SetLocations(nullptr);
2301}
2302
2303void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2304 HandleGoto(got, got->GetSuccessor());
2305}
2306
2307void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2308 try_boundary->SetLocations(nullptr);
2309}
2310
2311void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2312 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2313 if (!successor->IsExitBlock()) {
2314 HandleGoto(try_boundary, successor);
2315 }
2316}
2317
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002318void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002319 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002320 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002321 vixl::Label* false_target) {
2322 // FP branching requires both targets to be explicit. If either of the targets
2323 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2324 vixl::Label fallthrough_target;
2325 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002326
David Brazdil0debae72015-11-12 18:37:00 +00002327 if (true_target == nullptr && false_target == nullptr) {
2328 // Nothing to do. The code always falls through.
2329 return;
2330 } else if (cond->IsIntConstant()) {
2331 // Constant condition, statically compared against 1.
2332 if (cond->AsIntConstant()->IsOne()) {
2333 if (true_target != nullptr) {
2334 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002335 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002336 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002337 DCHECK(cond->AsIntConstant()->IsZero());
2338 if (false_target != nullptr) {
2339 __ B(false_target);
2340 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002341 }
David Brazdil0debae72015-11-12 18:37:00 +00002342 return;
2343 }
2344
2345 // The following code generates these patterns:
2346 // (1) true_target == nullptr && false_target != nullptr
2347 // - opposite condition true => branch to false_target
2348 // (2) true_target != nullptr && false_target == nullptr
2349 // - condition true => branch to true_target
2350 // (3) true_target != nullptr && false_target != nullptr
2351 // - condition true => branch to true_target
2352 // - branch to false_target
2353 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002354 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002355 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002356 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002357 if (true_target == nullptr) {
2358 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2359 } else {
2360 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2361 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002362 } else {
2363 // The condition instruction has not been materialized, use its inputs as
2364 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002365 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002366
David Brazdil0debae72015-11-12 18:37:00 +00002367 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002368 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002369 FPRegister lhs = InputFPRegisterAt(condition, 0);
2370 if (condition->GetLocations()->InAt(1).IsConstant()) {
2371 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2372 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2373 __ Fcmp(lhs, 0.0);
2374 } else {
2375 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2376 }
2377 if (condition->IsFPConditionTrueIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002378 __ B(vs, true_target == nullptr ? &fallthrough_target : true_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002379 } else if (condition->IsFPConditionFalseIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002380 __ B(vs, false_target == nullptr ? &fallthrough_target : false_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002381 }
David Brazdil0debae72015-11-12 18:37:00 +00002382 if (true_target == nullptr) {
2383 __ B(ARM64Condition(condition->GetOppositeCondition()), false_target);
2384 } else {
2385 __ B(ARM64Condition(condition->GetCondition()), true_target);
2386 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002387 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002388 // Integer cases.
2389 Register lhs = InputRegisterAt(condition, 0);
2390 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002391
2392 Condition arm64_cond;
2393 vixl::Label* non_fallthrough_target;
2394 if (true_target == nullptr) {
2395 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2396 non_fallthrough_target = false_target;
2397 } else {
2398 arm64_cond = ARM64Condition(condition->GetCondition());
2399 non_fallthrough_target = true_target;
2400 }
2401
Roland Levillain7f63c522015-07-13 15:54:55 +00002402 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2403 switch (arm64_cond) {
2404 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002405 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002406 break;
2407 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002408 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002409 break;
2410 case lt:
2411 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002412 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002413 break;
2414 case ge:
2415 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002416 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002417 break;
2418 default:
2419 // Without the `static_cast` the compiler throws an error for
2420 // `-Werror=sign-promo`.
2421 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2422 }
2423 } else {
2424 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002425 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002426 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002427 }
2428 }
David Brazdil0debae72015-11-12 18:37:00 +00002429
2430 // If neither branch falls through (case 3), the conditional branch to `true_target`
2431 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2432 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002433 __ B(false_target);
2434 }
David Brazdil0debae72015-11-12 18:37:00 +00002435
2436 if (fallthrough_target.IsLinked()) {
2437 __ Bind(&fallthrough_target);
2438 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002439}
2440
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002441void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002443 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002444 locations->SetInAt(0, Location::RequiresRegister());
2445 }
2446}
2447
2448void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002449 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2450 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2451 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2452 nullptr : codegen_->GetLabelOf(true_successor);
2453 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2454 nullptr : codegen_->GetLabelOf(false_successor);
2455 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002456}
2457
2458void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2459 LocationSummary* locations = new (GetGraph()->GetArena())
2460 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002461 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002462 locations->SetInAt(0, Location::RequiresRegister());
2463 }
2464}
2465
2466void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2467 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2468 DeoptimizationSlowPathARM64(deoptimize);
2469 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002470 GenerateTestAndBranch(deoptimize,
2471 /* condition_input_index */ 0,
2472 slow_path->GetEntryLabel(),
2473 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002474}
2475
Alexandre Rames5319def2014-10-23 10:03:10 +01002476void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002477 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002478}
2479
2480void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002481 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002482}
2483
2484void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002485 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002486}
2487
2488void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002489 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002490}
2491
Alexandre Rames67555f72014-11-18 10:55:16 +00002492void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002493 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2494 switch (instruction->GetTypeCheckKind()) {
2495 case TypeCheckKind::kExactCheck:
2496 case TypeCheckKind::kAbstractClassCheck:
2497 case TypeCheckKind::kClassHierarchyCheck:
2498 case TypeCheckKind::kArrayObjectCheck:
2499 call_kind = LocationSummary::kNoCall;
2500 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002501 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002502 case TypeCheckKind::kInterfaceCheck:
2503 call_kind = LocationSummary::kCall;
2504 break;
2505 case TypeCheckKind::kArrayCheck:
2506 call_kind = LocationSummary::kCallOnSlowPath;
2507 break;
2508 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002510 if (call_kind != LocationSummary::kCall) {
2511 locations->SetInAt(0, Location::RequiresRegister());
2512 locations->SetInAt(1, Location::RequiresRegister());
2513 // The out register is used as a temporary, so it overlaps with the inputs.
2514 // Note that TypeCheckSlowPathARM64 uses this register too.
2515 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2516 } else {
2517 InvokeRuntimeCallingConvention calling_convention;
2518 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2519 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2520 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2521 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002522}
2523
2524void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2525 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002526 Register obj = InputRegisterAt(instruction, 0);
2527 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002528 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002529 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2530 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2531 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2532 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002533
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002534 vixl::Label done, zero;
2535 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002536
2537 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002538 // Avoid null check if we know `obj` is not null.
2539 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002540 __ Cbz(obj, &zero);
2541 }
2542
Calin Juravle98893e12015-10-02 21:05:03 +01002543 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002544 // This is safe, as the register is caller-save, and the object must be in another
2545 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01002546 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
2547 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002548 ? obj
2549 : out;
2550 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2551 GetAssembler()->MaybeUnpoisonHeapReference(target);
2552
2553 switch (instruction->GetTypeCheckKind()) {
2554 case TypeCheckKind::kExactCheck: {
2555 __ Cmp(out, cls);
2556 __ Cset(out, eq);
2557 if (zero.IsLinked()) {
2558 __ B(&done);
2559 }
2560 break;
2561 }
2562 case TypeCheckKind::kAbstractClassCheck: {
2563 // If the class is abstract, we eagerly fetch the super class of the
2564 // object to avoid doing a comparison we know will fail.
2565 vixl::Label loop, success;
2566 __ Bind(&loop);
2567 __ Ldr(out, HeapOperand(out, super_offset));
2568 GetAssembler()->MaybeUnpoisonHeapReference(out);
2569 // If `out` is null, we use it for the result, and jump to `done`.
2570 __ Cbz(out, &done);
2571 __ Cmp(out, cls);
2572 __ B(ne, &loop);
2573 __ Mov(out, 1);
2574 if (zero.IsLinked()) {
2575 __ B(&done);
2576 }
2577 break;
2578 }
2579 case TypeCheckKind::kClassHierarchyCheck: {
2580 // Walk over the class hierarchy to find a match.
2581 vixl::Label loop, success;
2582 __ Bind(&loop);
2583 __ Cmp(out, cls);
2584 __ B(eq, &success);
2585 __ Ldr(out, HeapOperand(out, super_offset));
2586 GetAssembler()->MaybeUnpoisonHeapReference(out);
2587 __ Cbnz(out, &loop);
2588 // If `out` is null, we use it for the result, and jump to `done`.
2589 __ B(&done);
2590 __ Bind(&success);
2591 __ Mov(out, 1);
2592 if (zero.IsLinked()) {
2593 __ B(&done);
2594 }
2595 break;
2596 }
2597 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002598 // Do an exact check.
2599 vixl::Label exact_check;
2600 __ Cmp(out, cls);
2601 __ B(eq, &exact_check);
2602 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002603 __ Ldr(out, HeapOperand(out, component_offset));
2604 GetAssembler()->MaybeUnpoisonHeapReference(out);
2605 // If `out` is null, we use it for the result, and jump to `done`.
2606 __ Cbz(out, &done);
2607 __ Ldrh(out, HeapOperand(out, primitive_offset));
2608 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2609 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002610 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002611 __ Mov(out, 1);
2612 __ B(&done);
2613 break;
2614 }
2615 case TypeCheckKind::kArrayCheck: {
2616 __ Cmp(out, cls);
2617 DCHECK(locations->OnlyCallsOnSlowPath());
2618 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2619 instruction, /* is_fatal */ false);
2620 codegen_->AddSlowPath(slow_path);
2621 __ B(ne, slow_path->GetEntryLabel());
2622 __ Mov(out, 1);
2623 if (zero.IsLinked()) {
2624 __ B(&done);
2625 }
2626 break;
2627 }
Calin Juravle98893e12015-10-02 21:05:03 +01002628 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002629 case TypeCheckKind::kInterfaceCheck:
2630 default: {
2631 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2632 instruction,
2633 instruction->GetDexPc(),
2634 nullptr);
2635 if (zero.IsLinked()) {
2636 __ B(&done);
2637 }
2638 break;
2639 }
2640 }
2641
2642 if (zero.IsLinked()) {
2643 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002644 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002645 }
2646
2647 if (done.IsLinked()) {
2648 __ Bind(&done);
2649 }
2650
2651 if (slow_path != nullptr) {
2652 __ Bind(slow_path->GetExitLabel());
2653 }
2654}
2655
2656void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2657 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2658 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2659
2660 switch (instruction->GetTypeCheckKind()) {
2661 case TypeCheckKind::kExactCheck:
2662 case TypeCheckKind::kAbstractClassCheck:
2663 case TypeCheckKind::kClassHierarchyCheck:
2664 case TypeCheckKind::kArrayObjectCheck:
2665 call_kind = throws_into_catch
2666 ? LocationSummary::kCallOnSlowPath
2667 : LocationSummary::kNoCall;
2668 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002669 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002670 case TypeCheckKind::kInterfaceCheck:
2671 call_kind = LocationSummary::kCall;
2672 break;
2673 case TypeCheckKind::kArrayCheck:
2674 call_kind = LocationSummary::kCallOnSlowPath;
2675 break;
2676 }
2677
2678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2679 instruction, call_kind);
2680 if (call_kind != LocationSummary::kCall) {
2681 locations->SetInAt(0, Location::RequiresRegister());
2682 locations->SetInAt(1, Location::RequiresRegister());
2683 // Note that TypeCheckSlowPathARM64 uses this register too.
2684 locations->AddTemp(Location::RequiresRegister());
2685 } else {
2686 InvokeRuntimeCallingConvention calling_convention;
2687 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2688 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2689 }
2690}
2691
2692void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2693 LocationSummary* locations = instruction->GetLocations();
2694 Register obj = InputRegisterAt(instruction, 0);
2695 Register cls = InputRegisterAt(instruction, 1);
2696 Register temp;
2697 if (!locations->WillCall()) {
2698 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2699 }
2700
2701 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2702 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2703 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2704 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2705 SlowPathCodeARM64* slow_path = nullptr;
2706
2707 if (!locations->WillCall()) {
2708 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2709 instruction, !locations->CanCall());
2710 codegen_->AddSlowPath(slow_path);
2711 }
2712
2713 vixl::Label done;
2714 // Avoid null check if we know obj is not null.
2715 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002716 __ Cbz(obj, &done);
2717 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002718
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002719 if (locations->WillCall()) {
2720 __ Ldr(obj, HeapOperand(obj, class_offset));
2721 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002722 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002723 __ Ldr(temp, HeapOperand(obj, class_offset));
2724 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002725 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002726
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002727 switch (instruction->GetTypeCheckKind()) {
2728 case TypeCheckKind::kExactCheck:
2729 case TypeCheckKind::kArrayCheck: {
2730 __ Cmp(temp, cls);
2731 // Jump to slow path for throwing the exception or doing a
2732 // more involved array check.
2733 __ B(ne, slow_path->GetEntryLabel());
2734 break;
2735 }
2736 case TypeCheckKind::kAbstractClassCheck: {
2737 // If the class is abstract, we eagerly fetch the super class of the
2738 // object to avoid doing a comparison we know will fail.
2739 vixl::Label loop;
2740 __ Bind(&loop);
2741 __ Ldr(temp, HeapOperand(temp, super_offset));
2742 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2743 // Jump to the slow path to throw the exception.
2744 __ Cbz(temp, slow_path->GetEntryLabel());
2745 __ Cmp(temp, cls);
2746 __ B(ne, &loop);
2747 break;
2748 }
2749 case TypeCheckKind::kClassHierarchyCheck: {
2750 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002751 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002752 __ Bind(&loop);
2753 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002754 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002755 __ Ldr(temp, HeapOperand(temp, super_offset));
2756 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2757 __ Cbnz(temp, &loop);
2758 // Jump to the slow path to throw the exception.
2759 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002760 break;
2761 }
2762 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002763 // Do an exact check.
2764 __ Cmp(temp, cls);
2765 __ B(eq, &done);
2766 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002767 __ Ldr(temp, HeapOperand(temp, component_offset));
2768 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2769 __ Cbz(temp, slow_path->GetEntryLabel());
2770 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2771 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2772 __ Cbnz(temp, slow_path->GetEntryLabel());
2773 break;
2774 }
Calin Juravle98893e12015-10-02 21:05:03 +01002775 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002776 case TypeCheckKind::kInterfaceCheck:
2777 default:
2778 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2779 instruction,
2780 instruction->GetDexPc(),
2781 nullptr);
2782 break;
2783 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002784 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002785
2786 if (slow_path != nullptr) {
2787 __ Bind(slow_path->GetExitLabel());
2788 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002789}
2790
Alexandre Rames5319def2014-10-23 10:03:10 +01002791void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2792 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2793 locations->SetOut(Location::ConstantLocation(constant));
2794}
2795
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002796void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002797 // Will be generated at use site.
2798}
2799
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002800void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2802 locations->SetOut(Location::ConstantLocation(constant));
2803}
2804
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002805void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002806 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002807}
2808
Calin Juravle175dc732015-08-25 15:42:32 +01002809void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2810 // The trampoline uses the same calling convention as dex calling conventions,
2811 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2812 // the method_idx.
2813 HandleInvoke(invoke);
2814}
2815
2816void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2817 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2818}
2819
Alexandre Rames5319def2014-10-23 10:03:10 +01002820void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002821 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002822 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002823}
2824
Alexandre Rames67555f72014-11-18 10:55:16 +00002825void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2826 HandleInvoke(invoke);
2827}
2828
2829void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2830 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002831 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2832 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2833 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002834 Location receiver = invoke->GetLocations()->InAt(0);
2835 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002836 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002837
2838 // The register ip1 is required to be used for the hidden argument in
2839 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002840 MacroAssembler* masm = GetVIXLAssembler();
2841 UseScratchRegisterScope scratch_scope(masm);
2842 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002843 scratch_scope.Exclude(ip1);
2844 __ Mov(ip1, invoke->GetDexMethodIndex());
2845
2846 // temp = object->GetClass();
2847 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002848 __ Ldr(temp.W(), StackOperandFrom(receiver));
2849 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002850 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002851 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002852 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002853 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002854 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002855 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002856 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002857 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002858 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002859 // lr();
2860 __ Blr(lr);
2861 DCHECK(!codegen_->IsLeafMethod());
2862 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2863}
2864
2865void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002866 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2867 if (intrinsic.TryDispatch(invoke)) {
2868 return;
2869 }
2870
Alexandre Rames67555f72014-11-18 10:55:16 +00002871 HandleInvoke(invoke);
2872}
2873
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002874void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002875 // When we do not run baseline, explicit clinit checks triggered by static
2876 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2877 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002878
Andreas Gampe878d58c2015-01-15 23:24:00 -08002879 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2880 if (intrinsic.TryDispatch(invoke)) {
2881 return;
2882 }
2883
Alexandre Rames67555f72014-11-18 10:55:16 +00002884 HandleInvoke(invoke);
2885}
2886
Andreas Gampe878d58c2015-01-15 23:24:00 -08002887static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2888 if (invoke->GetLocations()->Intrinsified()) {
2889 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2890 intrinsic.Dispatch(invoke);
2891 return true;
2892 }
2893 return false;
2894}
2895
Vladimir Markodc151b22015-10-15 18:02:30 +01002896HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
2897 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
2898 MethodReference target_method ATTRIBUTE_UNUSED) {
2899 // On arm64 we support all dispatch types.
2900 return desired_dispatch_info;
2901}
2902
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002903void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002904 // For better instruction scheduling we load the direct code pointer before the method pointer.
2905 bool direct_code_loaded = false;
2906 switch (invoke->GetCodePtrLocation()) {
2907 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2908 // LR = code address from literal pool with link-time patch.
2909 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2910 direct_code_loaded = true;
2911 break;
2912 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2913 // LR = invoke->GetDirectCodePtr();
2914 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2915 direct_code_loaded = true;
2916 break;
2917 default:
2918 break;
2919 }
2920
Andreas Gampe878d58c2015-01-15 23:24:00 -08002921 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002922 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2923 switch (invoke->GetMethodLoadKind()) {
2924 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2925 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00002926 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00002927 break;
2928 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2929 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2930 break;
2931 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2932 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00002933 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00002934 break;
2935 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2936 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00002937 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00002938 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2939 break;
2940 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2941 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002942 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2943 invoke->GetDexCacheArrayOffset());
2944 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00002945 {
2946 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00002947 __ Bind(pc_insn_label);
2948 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00002949 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002950 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00002951 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002952 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2953 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00002954 {
2955 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2956 __ Bind(&pc_relative_dex_cache_patches_.back().label);
2957 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
2958 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2959 }
Vladimir Marko58155012015-08-19 12:49:41 +00002960 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002961 }
Vladimir Marko58155012015-08-19 12:49:41 +00002962 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2963 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2964 Register reg = XRegisterFrom(temp);
2965 Register method_reg;
2966 if (current_method.IsRegister()) {
2967 method_reg = XRegisterFrom(current_method);
2968 } else {
2969 DCHECK(invoke->GetLocations()->Intrinsified());
2970 DCHECK(!current_method.IsValid());
2971 method_reg = reg;
2972 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2973 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002974
Vladimir Marko58155012015-08-19 12:49:41 +00002975 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002976 __ Ldr(reg.X(),
2977 MemOperand(method_reg.X(),
2978 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002979 // temp = temp[index_in_cache];
2980 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2981 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2982 break;
2983 }
2984 }
2985
2986 switch (invoke->GetCodePtrLocation()) {
2987 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2988 __ Bl(&frame_entry_label_);
2989 break;
2990 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2991 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2992 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00002993 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2994 __ Bind(label);
2995 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00002996 break;
2997 }
2998 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2999 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3000 // LR prepared above for better instruction scheduling.
3001 DCHECK(direct_code_loaded);
3002 // lr()
3003 __ Blr(lr);
3004 break;
3005 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3006 // LR = callee_method->entry_point_from_quick_compiled_code_;
3007 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003008 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003009 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3010 // lr()
3011 __ Blr(lr);
3012 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003013 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003014
Andreas Gampe878d58c2015-01-15 23:24:00 -08003015 DCHECK(!IsLeafMethod());
3016}
3017
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003018void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3019 LocationSummary* locations = invoke->GetLocations();
3020 Location receiver = locations->InAt(0);
3021 Register temp = XRegisterFrom(temp_in);
3022 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3023 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3024 Offset class_offset = mirror::Object::ClassOffset();
3025 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3026
3027 BlockPoolsScope block_pools(GetVIXLAssembler());
3028
3029 DCHECK(receiver.IsRegister());
3030 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
3031 MaybeRecordImplicitNullCheck(invoke);
3032 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3033 // temp = temp->GetMethodAt(method_offset);
3034 __ Ldr(temp, MemOperand(temp, method_offset));
3035 // lr = temp->GetEntryPoint();
3036 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3037 // lr();
3038 __ Blr(lr);
3039}
3040
Vladimir Marko58155012015-08-19 12:49:41 +00003041void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3042 DCHECK(linker_patches->empty());
3043 size_t size =
3044 method_patches_.size() +
3045 call_patches_.size() +
3046 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003047 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003048 linker_patches->reserve(size);
3049 for (const auto& entry : method_patches_) {
3050 const MethodReference& target_method = entry.first;
3051 vixl::Literal<uint64_t>* literal = entry.second;
3052 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3053 target_method.dex_file,
3054 target_method.dex_method_index));
3055 }
3056 for (const auto& entry : call_patches_) {
3057 const MethodReference& target_method = entry.first;
3058 vixl::Literal<uint64_t>* literal = entry.second;
3059 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3060 target_method.dex_file,
3061 target_method.dex_method_index));
3062 }
3063 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003064 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003065 info.target_method.dex_file,
3066 info.target_method.dex_method_index));
3067 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003068 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003069 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003070 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003071 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003072 info.element_offset));
3073 }
3074}
3075
3076vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3077 // Look up the literal for value.
3078 auto lb = uint64_literals_.lower_bound(value);
3079 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3080 return lb->second;
3081 }
3082 // We don't have a literal for this value, insert a new one.
3083 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3084 uint64_literals_.PutBefore(lb, value, literal);
3085 return literal;
3086}
3087
3088vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3089 MethodReference target_method,
3090 MethodToLiteralMap* map) {
3091 // Look up the literal for target_method.
3092 auto lb = map->lower_bound(target_method);
3093 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3094 return lb->second;
3095 }
3096 // We don't have a literal for this method yet, insert a new one.
3097 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3098 map->PutBefore(lb, target_method, literal);
3099 return literal;
3100}
3101
3102vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3103 MethodReference target_method) {
3104 return DeduplicateMethodLiteral(target_method, &method_patches_);
3105}
3106
3107vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3108 MethodReference target_method) {
3109 return DeduplicateMethodLiteral(target_method, &call_patches_);
3110}
3111
3112
Andreas Gampe878d58c2015-01-15 23:24:00 -08003113void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003114 // When we do not run baseline, explicit clinit checks triggered by static
3115 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3116 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003117
Andreas Gampe878d58c2015-01-15 23:24:00 -08003118 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3119 return;
3120 }
3121
Alexandre Ramesd921d642015-04-16 15:07:16 +01003122 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003123 LocationSummary* locations = invoke->GetLocations();
3124 codegen_->GenerateStaticOrDirectCall(
3125 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003126 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003127}
3128
3129void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003130 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3131 return;
3132 }
3133
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003134 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003135 DCHECK(!codegen_->IsLeafMethod());
3136 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3137}
3138
Alexandre Rames67555f72014-11-18 10:55:16 +00003139void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003140 InvokeRuntimeCallingConvention calling_convention;
3141 CodeGenerator::CreateLoadClassLocationSummary(
3142 cls,
3143 LocationFrom(calling_convention.GetRegisterAt(0)),
3144 LocationFrom(vixl::x0));
Alexandre Rames67555f72014-11-18 10:55:16 +00003145}
3146
3147void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003148 if (cls->NeedsAccessCheck()) {
3149 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3150 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3151 cls,
3152 cls->GetDexPc(),
3153 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01003154 return;
3155 }
3156
3157 Register out = OutputRegister(cls);
3158 Register current_method = InputRegisterAt(cls, 0);
3159 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003160 DCHECK(!cls->CanCallRuntime());
3161 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003162 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003163 } else {
3164 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003165 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3166 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3167 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3168 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003169
3170 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3171 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3172 codegen_->AddSlowPath(slow_path);
3173 __ Cbz(out, slow_path->GetEntryLabel());
3174 if (cls->MustGenerateClinitCheck()) {
3175 GenerateClassInitializationCheck(slow_path, out);
3176 } else {
3177 __ Bind(slow_path->GetExitLabel());
3178 }
3179 }
3180}
3181
David Brazdilcb1c0552015-08-04 16:22:25 +01003182static MemOperand GetExceptionTlsAddress() {
3183 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3184}
3185
Alexandre Rames67555f72014-11-18 10:55:16 +00003186void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3187 LocationSummary* locations =
3188 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3189 locations->SetOut(Location::RequiresRegister());
3190}
3191
3192void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003193 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3194}
3195
3196void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3197 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3198}
3199
3200void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3201 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003202}
3203
Alexandre Rames5319def2014-10-23 10:03:10 +01003204void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3205 load->SetLocations(nullptr);
3206}
3207
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003208void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003209 // Nothing to do, this is driven by the code generator.
3210}
3211
Alexandre Rames67555f72014-11-18 10:55:16 +00003212void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3213 LocationSummary* locations =
3214 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003215 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003216 locations->SetOut(Location::RequiresRegister());
3217}
3218
3219void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3220 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3221 codegen_->AddSlowPath(slow_path);
3222
3223 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003224 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003225 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003226 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3227 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3228 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003229 __ Cbz(out, slow_path->GetEntryLabel());
3230 __ Bind(slow_path->GetExitLabel());
3231}
3232
Alexandre Rames5319def2014-10-23 10:03:10 +01003233void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3234 local->SetLocations(nullptr);
3235}
3236
3237void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3238 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3239}
3240
3241void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3243 locations->SetOut(Location::ConstantLocation(constant));
3244}
3245
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003246void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003247 // Will be generated at use site.
3248}
3249
Alexandre Rames67555f72014-11-18 10:55:16 +00003250void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3251 LocationSummary* locations =
3252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3253 InvokeRuntimeCallingConvention calling_convention;
3254 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3255}
3256
3257void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3258 codegen_->InvokeRuntime(instruction->IsEnter()
3259 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3260 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003261 instruction->GetDexPc(),
3262 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003263 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003264}
3265
Alexandre Rames42d641b2014-10-27 14:00:51 +00003266void LocationsBuilderARM64::VisitMul(HMul* mul) {
3267 LocationSummary* locations =
3268 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3269 switch (mul->GetResultType()) {
3270 case Primitive::kPrimInt:
3271 case Primitive::kPrimLong:
3272 locations->SetInAt(0, Location::RequiresRegister());
3273 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003275 break;
3276
3277 case Primitive::kPrimFloat:
3278 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003279 locations->SetInAt(0, Location::RequiresFpuRegister());
3280 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003281 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003282 break;
3283
3284 default:
3285 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3286 }
3287}
3288
3289void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3290 switch (mul->GetResultType()) {
3291 case Primitive::kPrimInt:
3292 case Primitive::kPrimLong:
3293 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3294 break;
3295
3296 case Primitive::kPrimFloat:
3297 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003298 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003299 break;
3300
3301 default:
3302 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3303 }
3304}
3305
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003306void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3307 LocationSummary* locations =
3308 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3309 switch (neg->GetResultType()) {
3310 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003311 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003312 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003314 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003315
3316 case Primitive::kPrimFloat:
3317 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003318 locations->SetInAt(0, Location::RequiresFpuRegister());
3319 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003320 break;
3321
3322 default:
3323 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3324 }
3325}
3326
3327void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3328 switch (neg->GetResultType()) {
3329 case Primitive::kPrimInt:
3330 case Primitive::kPrimLong:
3331 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3332 break;
3333
3334 case Primitive::kPrimFloat:
3335 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003336 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003337 break;
3338
3339 default:
3340 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3341 }
3342}
3343
3344void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3345 LocationSummary* locations =
3346 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3347 InvokeRuntimeCallingConvention calling_convention;
3348 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003349 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003350 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003351 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003352 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003353 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003354}
3355
3356void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3357 LocationSummary* locations = instruction->GetLocations();
3358 InvokeRuntimeCallingConvention calling_convention;
3359 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3360 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003361 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003362 // Note: if heap poisoning is enabled, the entry point takes cares
3363 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003364 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3365 instruction,
3366 instruction->GetDexPc(),
3367 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003368 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003369}
3370
Alexandre Rames5319def2014-10-23 10:03:10 +01003371void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3374 InvokeRuntimeCallingConvention calling_convention;
3375 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003376 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003377 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003378 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003379}
3380
3381void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3382 LocationSummary* locations = instruction->GetLocations();
3383 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3384 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003385 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003386 // Note: if heap poisoning is enabled, the entry point takes cares
3387 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003388 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3389 instruction,
3390 instruction->GetDexPc(),
3391 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003392 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003393}
3394
3395void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3396 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003397 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003399}
3400
3401void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003402 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003403 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003404 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003405 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003406 break;
3407
3408 default:
3409 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3410 }
3411}
3412
David Brazdil66d126e2015-04-03 16:02:44 +01003413void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3415 locations->SetInAt(0, Location::RequiresRegister());
3416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3417}
3418
3419void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003420 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3421}
3422
Alexandre Rames5319def2014-10-23 10:03:10 +01003423void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003424 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3425 ? LocationSummary::kCallOnSlowPath
3426 : LocationSummary::kNoCall;
3427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003428 locations->SetInAt(0, Location::RequiresRegister());
3429 if (instruction->HasUses()) {
3430 locations->SetOut(Location::SameAsFirstInput());
3431 }
3432}
3433
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003434void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003435 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3436 return;
3437 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003438
Alexandre Ramesd921d642015-04-16 15:07:16 +01003439 BlockPoolsScope block_pools(GetVIXLAssembler());
3440 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003441 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3442 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3443}
3444
3445void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003446 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3447 codegen_->AddSlowPath(slow_path);
3448
3449 LocationSummary* locations = instruction->GetLocations();
3450 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003451
3452 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003453}
3454
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003455void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003456 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003457 GenerateImplicitNullCheck(instruction);
3458 } else {
3459 GenerateExplicitNullCheck(instruction);
3460 }
3461}
3462
Alexandre Rames67555f72014-11-18 10:55:16 +00003463void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3464 HandleBinaryOp(instruction);
3465}
3466
3467void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3468 HandleBinaryOp(instruction);
3469}
3470
Alexandre Rames3e69f162014-12-10 10:36:50 +00003471void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3472 LOG(FATAL) << "Unreachable";
3473}
3474
3475void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3476 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3477}
3478
Alexandre Rames5319def2014-10-23 10:03:10 +01003479void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3480 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3481 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3482 if (location.IsStackSlot()) {
3483 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3484 } else if (location.IsDoubleStackSlot()) {
3485 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3486 }
3487 locations->SetOut(location);
3488}
3489
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003490void InstructionCodeGeneratorARM64::VisitParameterValue(
3491 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003492 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003493}
3494
3495void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3496 LocationSummary* locations =
3497 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003498 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003499}
3500
3501void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3502 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3503 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003504}
3505
3506void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3508 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3509 locations->SetInAt(i, Location::Any());
3510 }
3511 locations->SetOut(Location::Any());
3512}
3513
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003514void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003515 LOG(FATAL) << "Unreachable";
3516}
3517
Serban Constantinescu02164b32014-11-13 14:05:07 +00003518void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003519 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003520 LocationSummary::CallKind call_kind =
3521 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003522 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3523
3524 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003525 case Primitive::kPrimInt:
3526 case Primitive::kPrimLong:
3527 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003528 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3530 break;
3531
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003532 case Primitive::kPrimFloat:
3533 case Primitive::kPrimDouble: {
3534 InvokeRuntimeCallingConvention calling_convention;
3535 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3536 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3537 locations->SetOut(calling_convention.GetReturnLocation(type));
3538
3539 break;
3540 }
3541
Serban Constantinescu02164b32014-11-13 14:05:07 +00003542 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003543 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003544 }
3545}
3546
3547void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3548 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003549
Serban Constantinescu02164b32014-11-13 14:05:07 +00003550 switch (type) {
3551 case Primitive::kPrimInt:
3552 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003553 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003554 break;
3555 }
3556
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003557 case Primitive::kPrimFloat:
3558 case Primitive::kPrimDouble: {
3559 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3560 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003561 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003562 break;
3563 }
3564
Serban Constantinescu02164b32014-11-13 14:05:07 +00003565 default:
3566 LOG(FATAL) << "Unexpected rem type " << type;
3567 }
3568}
3569
Calin Juravle27df7582015-04-17 19:12:31 +01003570void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3571 memory_barrier->SetLocations(nullptr);
3572}
3573
3574void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3575 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3576}
3577
Alexandre Rames5319def2014-10-23 10:03:10 +01003578void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3580 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003581 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003582}
3583
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003584void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003585 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003586}
3587
3588void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3589 instruction->SetLocations(nullptr);
3590}
3591
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003592void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003593 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003594}
3595
Serban Constantinescu02164b32014-11-13 14:05:07 +00003596void LocationsBuilderARM64::VisitShl(HShl* shl) {
3597 HandleShift(shl);
3598}
3599
3600void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3601 HandleShift(shl);
3602}
3603
3604void LocationsBuilderARM64::VisitShr(HShr* shr) {
3605 HandleShift(shr);
3606}
3607
3608void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3609 HandleShift(shr);
3610}
3611
Alexandre Rames5319def2014-10-23 10:03:10 +01003612void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3614 Primitive::Type field_type = store->InputAt(1)->GetType();
3615 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003616 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003617 case Primitive::kPrimBoolean:
3618 case Primitive::kPrimByte:
3619 case Primitive::kPrimChar:
3620 case Primitive::kPrimShort:
3621 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003622 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003623 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3624 break;
3625
3626 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003627 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003628 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3629 break;
3630
3631 default:
3632 LOG(FATAL) << "Unimplemented local type " << field_type;
3633 }
3634}
3635
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003636void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003637}
3638
3639void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003640 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003641}
3642
3643void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003644 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003645}
3646
Alexandre Rames67555f72014-11-18 10:55:16 +00003647void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003648 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003649}
3650
3651void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003652 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003653}
3654
3655void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003656 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003657}
3658
Alexandre Rames67555f72014-11-18 10:55:16 +00003659void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003660 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003661}
3662
Calin Juravlee460d1d2015-09-29 04:52:17 +01003663void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3664 HUnresolvedInstanceFieldGet* instruction) {
3665 FieldAccessCallingConventionARM64 calling_convention;
3666 codegen_->CreateUnresolvedFieldLocationSummary(
3667 instruction, instruction->GetFieldType(), calling_convention);
3668}
3669
3670void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3671 HUnresolvedInstanceFieldGet* instruction) {
3672 FieldAccessCallingConventionARM64 calling_convention;
3673 codegen_->GenerateUnresolvedFieldAccess(instruction,
3674 instruction->GetFieldType(),
3675 instruction->GetFieldIndex(),
3676 instruction->GetDexPc(),
3677 calling_convention);
3678}
3679
3680void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3681 HUnresolvedInstanceFieldSet* instruction) {
3682 FieldAccessCallingConventionARM64 calling_convention;
3683 codegen_->CreateUnresolvedFieldLocationSummary(
3684 instruction, instruction->GetFieldType(), calling_convention);
3685}
3686
3687void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3688 HUnresolvedInstanceFieldSet* instruction) {
3689 FieldAccessCallingConventionARM64 calling_convention;
3690 codegen_->GenerateUnresolvedFieldAccess(instruction,
3691 instruction->GetFieldType(),
3692 instruction->GetFieldIndex(),
3693 instruction->GetDexPc(),
3694 calling_convention);
3695}
3696
3697void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3698 HUnresolvedStaticFieldGet* instruction) {
3699 FieldAccessCallingConventionARM64 calling_convention;
3700 codegen_->CreateUnresolvedFieldLocationSummary(
3701 instruction, instruction->GetFieldType(), calling_convention);
3702}
3703
3704void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3705 HUnresolvedStaticFieldGet* instruction) {
3706 FieldAccessCallingConventionARM64 calling_convention;
3707 codegen_->GenerateUnresolvedFieldAccess(instruction,
3708 instruction->GetFieldType(),
3709 instruction->GetFieldIndex(),
3710 instruction->GetDexPc(),
3711 calling_convention);
3712}
3713
3714void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3715 HUnresolvedStaticFieldSet* instruction) {
3716 FieldAccessCallingConventionARM64 calling_convention;
3717 codegen_->CreateUnresolvedFieldLocationSummary(
3718 instruction, instruction->GetFieldType(), calling_convention);
3719}
3720
3721void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3722 HUnresolvedStaticFieldSet* instruction) {
3723 FieldAccessCallingConventionARM64 calling_convention;
3724 codegen_->GenerateUnresolvedFieldAccess(instruction,
3725 instruction->GetFieldType(),
3726 instruction->GetFieldIndex(),
3727 instruction->GetDexPc(),
3728 calling_convention);
3729}
3730
Alexandre Rames5319def2014-10-23 10:03:10 +01003731void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3733}
3734
3735void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003736 HBasicBlock* block = instruction->GetBlock();
3737 if (block->GetLoopInformation() != nullptr) {
3738 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3739 // The back edge will generate the suspend check.
3740 return;
3741 }
3742 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3743 // The goto will generate the suspend check.
3744 return;
3745 }
3746 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003747}
3748
3749void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3750 temp->SetLocations(nullptr);
3751}
3752
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003753void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003754 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01003755}
3756
Alexandre Rames67555f72014-11-18 10:55:16 +00003757void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3758 LocationSummary* locations =
3759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3760 InvokeRuntimeCallingConvention calling_convention;
3761 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3762}
3763
3764void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3765 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003766 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003767 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003768}
3769
3770void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3771 LocationSummary* locations =
3772 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3773 Primitive::Type input_type = conversion->GetInputType();
3774 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003775 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003776 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3777 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3778 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3779 }
3780
Alexandre Rames542361f2015-01-29 16:57:31 +00003781 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003782 locations->SetInAt(0, Location::RequiresFpuRegister());
3783 } else {
3784 locations->SetInAt(0, Location::RequiresRegister());
3785 }
3786
Alexandre Rames542361f2015-01-29 16:57:31 +00003787 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003788 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3789 } else {
3790 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3791 }
3792}
3793
3794void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3795 Primitive::Type result_type = conversion->GetResultType();
3796 Primitive::Type input_type = conversion->GetInputType();
3797
3798 DCHECK_NE(input_type, result_type);
3799
Alexandre Rames542361f2015-01-29 16:57:31 +00003800 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003801 int result_size = Primitive::ComponentSize(result_type);
3802 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003803 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003804 Register output = OutputRegister(conversion);
3805 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003806 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3807 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003808 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3809 // 'int' values are used directly as W registers, discarding the top
3810 // bits, so we don't need to sign-extend and can just perform a move.
3811 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3812 // top 32 bits of the target register. We theoretically could leave those
3813 // bits unchanged, but we would have to make sure that no code uses a
3814 // 32bit input value as a 64bit value assuming that the top 32 bits are
3815 // zero.
3816 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003817 } else if ((result_type == Primitive::kPrimChar) ||
3818 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3819 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003820 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003821 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003822 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003823 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003824 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003825 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003826 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3827 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003828 } else if (Primitive::IsFloatingPointType(result_type) &&
3829 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003830 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3831 } else {
3832 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3833 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003834 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003835}
Alexandre Rames67555f72014-11-18 10:55:16 +00003836
Serban Constantinescu02164b32014-11-13 14:05:07 +00003837void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3838 HandleShift(ushr);
3839}
3840
3841void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3842 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003843}
3844
3845void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3846 HandleBinaryOp(instruction);
3847}
3848
3849void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3850 HandleBinaryOp(instruction);
3851}
3852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003853void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00003854 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00003855 LOG(FATAL) << "Unreachable";
3856}
3857
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003858void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00003859 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00003860 LOG(FATAL) << "Unreachable";
3861}
3862
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003863void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3864 DCHECK(codegen_->IsBaseline());
3865 LocationSummary* locations =
3866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3867 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3868}
3869
3870void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3871 DCHECK(codegen_->IsBaseline());
3872 // Will be generated at use site.
3873}
3874
Mark Mendellfe57faa2015-09-18 09:26:15 -04003875// Simple implementation of packed switch - generate cascaded compare/jumps.
3876void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3877 LocationSummary* locations =
3878 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3879 locations->SetInAt(0, Location::RequiresRegister());
3880}
3881
3882void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3883 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08003884 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04003885 Register value_reg = InputRegisterAt(switch_instr, 0);
3886 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3887
Zheng Xu3927c8b2015-11-18 17:46:25 +08003888 // Roughly set 16 as max average assemblies generated per HIR in a graph.
3889 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
3890 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
3891 // make sure we don't emit it if the target may run out of range.
3892 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
3893 // ranges and emit the tables only as required.
3894 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003895
Zheng Xu3927c8b2015-11-18 17:46:25 +08003896 if (num_entries < kPackedSwitchJumpTableThreshold ||
3897 // Current instruction id is an upper bound of the number of HIRs in the graph.
3898 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
3899 // Create a series of compare/jumps.
3900 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3901 for (uint32_t i = 0; i < num_entries; i++) {
3902 int32_t case_value = lower_bound + i;
3903 vixl::Label* succ = codegen_->GetLabelOf(successors[i]);
3904 if (case_value == 0) {
3905 __ Cbz(value_reg, succ);
3906 } else {
3907 __ Cmp(value_reg, Operand(case_value));
3908 __ B(eq, succ);
3909 }
3910 }
3911
3912 // And the default for any other value.
3913 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3914 __ B(codegen_->GetLabelOf(default_block));
3915 }
3916 } else {
3917 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
3918 codegen_->AddJumpTable(jump_table);
3919
3920 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
3921
3922 // Below instructions should use at most one blocked register. Since there are two blocked
3923 // registers, we are free to block one.
3924 Register temp_w = temps.AcquireW();
3925 Register index;
3926 // Remove the bias.
3927 if (lower_bound != 0) {
3928 index = temp_w;
3929 __ Sub(index, value_reg, Operand(lower_bound));
3930 } else {
3931 index = value_reg;
3932 }
3933
3934 // Jump to default block if index is out of the range.
3935 __ Cmp(index, Operand(num_entries));
3936 __ B(hs, codegen_->GetLabelOf(default_block));
3937
3938 // In current VIXL implementation, it won't require any blocked registers to encode the
3939 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
3940 // register pressure.
3941 Register table_base = temps.AcquireX();
3942 // Load jump offset from the table.
3943 __ Adr(table_base, jump_table->GetTableStartLabel());
3944 Register jump_offset = temp_w;
3945 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
3946
3947 // Jump to target block by branching to table_base(pc related) + offset.
3948 Register target_address = table_base;
3949 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
3950 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04003951 }
3952}
3953
Alexandre Rames67555f72014-11-18 10:55:16 +00003954#undef __
3955#undef QUICK_ENTRY_POINT
3956
Alexandre Rames5319def2014-10-23 10:03:10 +01003957} // namespace arm64
3958} // namespace art