blob: b8ac421935fe19c0b96377e2466e2c980bcc0ac4 [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"
Andreas Gampe878d58c2015-01-15 23:24:00 -080022#include "common_arm64.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;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010080 }
Roland Levillain7f63c522015-07-13 15:54:55 +000081 LOG(FATAL) << "Unreachable";
82 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010083}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010095 } else if (return_type == Primitive::kPrimVoid) {
96 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Zheng Xuda403092015-04-24 17:35:39 +0800109// Calculate memory accessing operand for save/restore live registers.
110static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
111 RegisterSet* register_set,
112 int64_t spill_offset,
113 bool is_save) {
114 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
115 codegen->GetNumberOfCoreRegisters(),
116 register_set->GetFloatingPointRegisters(),
117 codegen->GetNumberOfFloatingPointRegisters()));
118
119 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
120 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000193 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
194 Location index_location,
195 Location length_location)
196 : instruction_(instruction),
197 index_location_(index_location),
198 length_location_(length_location) {}
199
Alexandre Rames5319def2014-10-23 10:03:10 +0100200
Alexandre Rames67555f72014-11-18 10:55:16 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000202 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100203 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100208 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames8158f282015-08-07 10:26:17 +0100215 bool IsFatal() const OVERRIDE { return true; }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 HBoundsCheck* const instruction_;
221 const Location index_location_;
222 const Location length_location_;
223
Alexandre Rames5319def2014-10-23 10:03:10 +0100224 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
225};
226
Alexandre Rames67555f72014-11-18 10:55:16 +0000227class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
228 public:
229 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
233 __ Bind(GetEntryLabel());
234 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000235 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800236 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000237 }
238
Alexandre Rames8158f282015-08-07 10:26:17 +0100239 bool IsFatal() const OVERRIDE { return true; }
240
Alexandre Rames9931f312015-06-19 14:47:01 +0100241 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
242
Alexandre Rames67555f72014-11-18 10:55:16 +0000243 private:
244 HDivZeroCheck* const instruction_;
245 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
246};
247
248class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
249 public:
250 LoadClassSlowPathARM64(HLoadClass* cls,
251 HInstruction* at,
252 uint32_t dex_pc,
253 bool do_clinit)
254 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
255 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
256 }
257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 LocationSummary* locations = at_->GetLocations();
260 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
261
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000264
265 InvokeRuntimeCallingConvention calling_convention;
266 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
268 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800270 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100271 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100273 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000275
276 // Move the class to the desired location.
277 Location out = locations->Out();
278 if (out.IsValid()) {
279 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
280 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000281 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000282 }
283
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 __ B(GetExitLabel());
286 }
287
Alexandre Rames9931f312015-06-19 14:47:01 +0100288 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 private:
291 // The class this slow path will load.
292 HLoadClass* const cls_;
293
294 // The instruction where this slow path is happening.
295 // (Might be the load class or an initialization check).
296 HInstruction* const at_;
297
298 // The dex PC of `at_`.
299 const uint32_t dex_pc_;
300
301 // Whether to initialize the class.
302 const bool do_clinit_;
303
304 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
305};
306
307class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
308 public:
309 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
310
311 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
312 LocationSummary* locations = instruction_->GetLocations();
313 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
314 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
315
316 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000318
319 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800320 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000322 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100323 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000325 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000326
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000327 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328 __ B(GetExitLabel());
329 }
330
Alexandre Rames9931f312015-06-19 14:47:01 +0100331 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 private:
334 HLoadString* const instruction_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
337};
338
Alexandre Rames5319def2014-10-23 10:03:10 +0100339class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
341 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
342
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100345 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000347 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800348 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100349 }
350
Alexandre Rames8158f282015-08-07 10:26:17 +0100351 bool IsFatal() const OVERRIDE { return true; }
352
Alexandre Rames9931f312015-06-19 14:47:01 +0100353 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
354
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 private:
356 HNullCheck* const instruction_;
357
358 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
359};
360
361class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
362 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100363 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100364 : instruction_(instruction), successor_(successor) {}
365
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
367 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100368 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000369 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000370 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000371 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800372 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000373 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 if (successor_ == nullptr) {
375 __ B(GetReturnLabel());
376 } else {
377 __ B(arm64_codegen->GetLabelOf(successor_));
378 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100379 }
380
381 vixl::Label* GetReturnLabel() {
382 DCHECK(successor_ == nullptr);
383 return &return_label_;
384 }
385
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100386 HBasicBlock* GetSuccessor() const {
387 return successor_;
388 }
389
Alexandre Rames9931f312015-06-19 14:47:01 +0100390 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
391
Alexandre Rames5319def2014-10-23 10:03:10 +0100392 private:
393 HSuspendCheck* const instruction_;
394 // If not null, the block to branch to after the suspend check.
395 HBasicBlock* const successor_;
396
397 // If `successor_` is null, the label to branch to after the suspend check.
398 vixl::Label return_label_;
399
400 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
401};
402
Alexandre Rames67555f72014-11-18 10:55:16 +0000403class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
404 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000405 TypeCheckSlowPathARM64(HInstruction* instruction,
406 Location class_to_check,
407 Location object_class,
408 uint32_t dex_pc)
409 : instruction_(instruction),
410 class_to_check_(class_to_check),
411 object_class_(object_class),
412 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 LocationSummary* locations = instruction_->GetLocations();
416 DCHECK(instruction_->IsCheckCast()
417 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
418 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
419
Alexandre Rames67555f72014-11-18 10:55:16 +0000420 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000421 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000422
423 // We're moving two locations to locations that could overlap, so we need a parallel
424 // move resolver.
425 InvokeRuntimeCallingConvention calling_convention;
426 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100427 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
428 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000429
430 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000431 arm64_codegen->InvokeRuntime(
432 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000433 Primitive::Type ret_type = instruction_->GetType();
434 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
435 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800436 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
437 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438 } else {
439 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000440 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800441 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442 }
443
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000444 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000445 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000446 }
447
Alexandre Rames9931f312015-06-19 14:47:01 +0100448 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
449
Alexandre Rames67555f72014-11-18 10:55:16 +0000450 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451 HInstruction* const instruction_;
452 const Location class_to_check_;
453 const Location object_class_;
454 uint32_t dex_pc_;
455
Alexandre Rames67555f72014-11-18 10:55:16 +0000456 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
457};
458
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700459class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
460 public:
461 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
462 : instruction_(instruction) {}
463
464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 __ Bind(GetEntryLabel());
466 SaveLiveRegisters(codegen, instruction_->GetLocations());
467 DCHECK(instruction_->IsDeoptimize());
468 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
469 uint32_t dex_pc = deoptimize->GetDexPc();
470 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
471 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
472 }
473
Alexandre Rames9931f312015-06-19 14:47:01 +0100474 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
475
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700476 private:
477 HInstruction* const instruction_;
478 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
479};
480
Alexandre Rames5319def2014-10-23 10:03:10 +0100481#undef __
482
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100483Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 Location next_location;
485 if (type == Primitive::kPrimVoid) {
486 LOG(FATAL) << "Unreachable type " << type;
487 }
488
Alexandre Rames542361f2015-01-29 16:57:31 +0000489 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100490 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
491 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000492 } else if (!Primitive::IsFloatingPointType(type) &&
493 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000494 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
495 } else {
496 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000497 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
498 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100499 }
500
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000501 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000502 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100503 return next_location;
504}
505
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100506Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100507 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100508}
509
Serban Constantinescu579885a2015-02-22 20:51:33 +0000510CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
511 const Arm64InstructionSetFeatures& isa_features,
512 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100513 : CodeGenerator(graph,
514 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000515 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000516 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000517 callee_saved_core_registers.list(),
518 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000519 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 block_labels_(nullptr),
521 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000522 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000523 move_resolver_(graph->GetArena(), this),
Vladimir Markob2c431e2015-08-19 12:45:42 +0000524 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000525 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000526 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000527}
Alexandre Rames5319def2014-10-23 10:03:10 +0100528
Alexandre Rames67555f72014-11-18 10:55:16 +0000529#undef __
530#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100531
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000532void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
533 // Ensure we emit the literal pool.
534 __ FinalizeCode();
535 CodeGenerator::Finalize(allocator);
536}
537
Zheng Xuad4450e2015-04-17 18:48:56 +0800538void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
539 // Note: There are 6 kinds of moves:
540 // 1. constant -> GPR/FPR (non-cycle)
541 // 2. constant -> stack (non-cycle)
542 // 3. GPR/FPR -> GPR/FPR
543 // 4. GPR/FPR -> stack
544 // 5. stack -> GPR/FPR
545 // 6. stack -> stack (non-cycle)
546 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
547 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
548 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
549 // dependency.
550 vixl_temps_.Open(GetVIXLAssembler());
551}
552
553void ParallelMoveResolverARM64::FinishEmitNativeCode() {
554 vixl_temps_.Close();
555}
556
557Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
558 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
559 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
560 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
561 Location scratch = GetScratchLocation(kind);
562 if (!scratch.Equals(Location::NoLocation())) {
563 return scratch;
564 }
565 // Allocate from VIXL temp registers.
566 if (kind == Location::kRegister) {
567 scratch = LocationFrom(vixl_temps_.AcquireX());
568 } else {
569 DCHECK(kind == Location::kFpuRegister);
570 scratch = LocationFrom(vixl_temps_.AcquireD());
571 }
572 AddScratchLocation(scratch);
573 return scratch;
574}
575
576void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
577 if (loc.IsRegister()) {
578 vixl_temps_.Release(XRegisterFrom(loc));
579 } else {
580 DCHECK(loc.IsFpuRegister());
581 vixl_temps_.Release(DRegisterFrom(loc));
582 }
583 RemoveScratchLocation(loc);
584}
585
Alexandre Rames3e69f162014-12-10 10:36:50 +0000586void ParallelMoveResolverARM64::EmitMove(size_t index) {
587 MoveOperands* move = moves_.Get(index);
588 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
589}
590
Alexandre Rames5319def2014-10-23 10:03:10 +0100591void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100592 MacroAssembler* masm = GetVIXLAssembler();
593 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000594 __ Bind(&frame_entry_label_);
595
Serban Constantinescu02164b32014-11-13 14:05:07 +0000596 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
597 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100598 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000599 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000600 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000601 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000602 __ Ldr(wzr, MemOperand(temp, 0));
603 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000604 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100605
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000606 if (!HasEmptyFrame()) {
607 int frame_size = GetFrameSize();
608 // Stack layout:
609 // sp[frame_size - 8] : lr.
610 // ... : other preserved core registers.
611 // ... : other preserved fp registers.
612 // ... : reserved frame space.
613 // sp[0] : current method.
614 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100615 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800616 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
617 frame_size - GetCoreSpillSize());
618 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
619 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000620 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100621}
622
623void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100624 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100625 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000626 if (!HasEmptyFrame()) {
627 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800628 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
629 frame_size - FrameEntrySpillSize());
630 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
631 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000632 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100633 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000634 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100635 __ Ret();
636 GetAssembler()->cfi().RestoreState();
637 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100638}
639
Zheng Xuda403092015-04-24 17:35:39 +0800640vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
641 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
642 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
643 core_spill_mask_);
644}
645
646vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
647 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
648 GetNumberOfFloatingPointRegisters()));
649 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
650 fpu_spill_mask_);
651}
652
Alexandre Rames5319def2014-10-23 10:03:10 +0100653void CodeGeneratorARM64::Bind(HBasicBlock* block) {
654 __ Bind(GetLabelOf(block));
655}
656
Alexandre Rames5319def2014-10-23 10:03:10 +0100657void CodeGeneratorARM64::Move(HInstruction* instruction,
658 Location location,
659 HInstruction* move_for) {
660 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100661 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000662 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100663
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100664 if (instruction->IsFakeString()) {
665 // The fake string is an alias for null.
666 DCHECK(IsBaseline());
667 instruction = locations->Out().GetConstant();
668 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
669 }
670
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100671 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700672 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100673 } else if (locations != nullptr && locations->Out().Equals(location)) {
674 return;
675 } else if (instruction->IsIntConstant()
676 || instruction->IsLongConstant()
677 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000678 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100679 if (location.IsRegister()) {
680 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000681 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100682 (instruction->IsLongConstant() && dst.Is64Bits()));
683 __ Mov(dst, value);
684 } else {
685 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000686 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000687 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
688 ? temps.AcquireW()
689 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100690 __ Mov(temp, value);
691 __ Str(temp, StackOperandFrom(location));
692 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000693 } else if (instruction->IsTemporary()) {
694 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000695 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100696 } else if (instruction->IsLoadLocal()) {
697 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000698 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000699 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000700 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000701 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100702 }
703
704 } else {
705 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000706 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100707 }
708}
709
Alexandre Rames5319def2014-10-23 10:03:10 +0100710Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
711 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000712
Alexandre Rames5319def2014-10-23 10:03:10 +0100713 switch (type) {
714 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000715 case Primitive::kPrimInt:
716 case Primitive::kPrimFloat:
717 return Location::StackSlot(GetStackSlot(load->GetLocal()));
718
719 case Primitive::kPrimLong:
720 case Primitive::kPrimDouble:
721 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
722
Alexandre Rames5319def2014-10-23 10:03:10 +0100723 case Primitive::kPrimBoolean:
724 case Primitive::kPrimByte:
725 case Primitive::kPrimChar:
726 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100727 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100728 LOG(FATAL) << "Unexpected type " << type;
729 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000730
Alexandre Rames5319def2014-10-23 10:03:10 +0100731 LOG(FATAL) << "Unreachable";
732 return Location::NoLocation();
733}
734
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100735void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000736 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100737 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000738 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100739 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100740 if (value_can_be_null) {
741 __ Cbz(value, &done);
742 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100743 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
744 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000745 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100746 if (value_can_be_null) {
747 __ Bind(&done);
748 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100749}
750
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000751void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
752 // Blocked core registers:
753 // lr : Runtime reserved.
754 // tr : Runtime reserved.
755 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
756 // ip1 : VIXL core temp.
757 // ip0 : VIXL core temp.
758 //
759 // Blocked fp registers:
760 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100761 CPURegList reserved_core_registers = vixl_reserved_core_registers;
762 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100763 while (!reserved_core_registers.IsEmpty()) {
764 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
765 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000766
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000767 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800768 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000769 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
770 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000771
772 if (is_baseline) {
773 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
774 while (!reserved_core_baseline_registers.IsEmpty()) {
775 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
776 }
777
778 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
779 while (!reserved_fp_baseline_registers.IsEmpty()) {
780 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
781 }
782 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100783}
784
785Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
786 if (type == Primitive::kPrimVoid) {
787 LOG(FATAL) << "Unreachable type " << type;
788 }
789
Alexandre Rames542361f2015-01-29 16:57:31 +0000790 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000791 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
792 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100793 return Location::FpuRegisterLocation(reg);
794 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000795 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
796 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100797 return Location::RegisterLocation(reg);
798 }
799}
800
Alexandre Rames3e69f162014-12-10 10:36:50 +0000801size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
802 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
803 __ Str(reg, MemOperand(sp, stack_index));
804 return kArm64WordSize;
805}
806
807size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
808 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
809 __ Ldr(reg, MemOperand(sp, stack_index));
810 return kArm64WordSize;
811}
812
813size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
814 FPRegister reg = FPRegister(reg_id, kDRegSize);
815 __ Str(reg, MemOperand(sp, stack_index));
816 return kArm64WordSize;
817}
818
819size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
820 FPRegister reg = FPRegister(reg_id, kDRegSize);
821 __ Ldr(reg, MemOperand(sp, stack_index));
822 return kArm64WordSize;
823}
824
Alexandre Rames5319def2014-10-23 10:03:10 +0100825void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100826 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100827}
828
829void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100830 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100831}
832
Alexandre Rames67555f72014-11-18 10:55:16 +0000833void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000834 if (constant->IsIntConstant()) {
835 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
836 } else if (constant->IsLongConstant()) {
837 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
838 } else if (constant->IsNullConstant()) {
839 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000840 } else if (constant->IsFloatConstant()) {
841 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
842 } else {
843 DCHECK(constant->IsDoubleConstant());
844 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
845 }
846}
847
Alexandre Rames3e69f162014-12-10 10:36:50 +0000848
849static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
850 DCHECK(constant.IsConstant());
851 HConstant* cst = constant.GetConstant();
852 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000853 // Null is mapped to a core W register, which we associate with kPrimInt.
854 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000855 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
856 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
857 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
858}
859
860void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000861 if (source.Equals(destination)) {
862 return;
863 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000864
865 // A valid move can always be inferred from the destination and source
866 // locations. When moving from and to a register, the argument type can be
867 // used to generate 32bit instead of 64bit moves. In debug mode we also
868 // checks the coherency of the locations and the type.
869 bool unspecified_type = (type == Primitive::kPrimVoid);
870
871 if (destination.IsRegister() || destination.IsFpuRegister()) {
872 if (unspecified_type) {
873 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
874 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000875 (src_cst != nullptr && (src_cst->IsIntConstant()
876 || src_cst->IsFloatConstant()
877 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000878 // For stack slots and 32bit constants, a 64bit type is appropriate.
879 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000880 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000881 // If the source is a double stack slot or a 64bit constant, a 64bit
882 // type is appropriate. Else the source is a register, and since the
883 // type has not been specified, we chose a 64bit type to force a 64bit
884 // move.
885 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000886 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000887 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000888 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
889 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 CPURegister dst = CPURegisterFrom(destination, type);
891 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
892 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
893 __ Ldr(dst, StackOperandFrom(source));
894 } else if (source.IsConstant()) {
895 DCHECK(CoherentConstantAndType(source, type));
896 MoveConstant(dst, source.GetConstant());
897 } else {
898 if (destination.IsRegister()) {
899 __ Mov(Register(dst), RegisterFrom(source, type));
900 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800901 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000902 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
903 }
904 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000905 } else { // The destination is not a register. It must be a stack slot.
906 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
907 if (source.IsRegister() || source.IsFpuRegister()) {
908 if (unspecified_type) {
909 if (source.IsRegister()) {
910 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
911 } else {
912 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
913 }
914 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000915 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
916 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000917 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
918 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100919 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000920 UseScratchRegisterScope temps(GetVIXLAssembler());
921 HConstant* src_cst = source.GetConstant();
922 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000923 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000924 temp = temps.AcquireW();
925 } else if (src_cst->IsLongConstant()) {
926 temp = temps.AcquireX();
927 } else if (src_cst->IsFloatConstant()) {
928 temp = temps.AcquireS();
929 } else {
930 DCHECK(src_cst->IsDoubleConstant());
931 temp = temps.AcquireD();
932 }
933 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000934 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000935 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000936 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000937 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000938 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000939 // There is generally less pressure on FP registers.
940 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000941 __ Ldr(temp, StackOperandFrom(source));
942 __ Str(temp, StackOperandFrom(destination));
943 }
944 }
945}
946
947void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000948 CPURegister dst,
949 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000950 switch (type) {
951 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000952 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000953 break;
954 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000955 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000956 break;
957 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000958 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000959 break;
960 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000961 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000962 break;
963 case Primitive::kPrimInt:
964 case Primitive::kPrimNot:
965 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000966 case Primitive::kPrimFloat:
967 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000968 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000969 __ Ldr(dst, src);
970 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000971 case Primitive::kPrimVoid:
972 LOG(FATAL) << "Unreachable type " << type;
973 }
974}
975
Calin Juravle77520bc2015-01-12 18:45:46 +0000976void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000977 CPURegister dst,
978 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100979 MacroAssembler* masm = GetVIXLAssembler();
980 BlockPoolsScope block_pools(masm);
981 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000982 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000983 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000984
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000985 DCHECK(!src.IsPreIndex());
986 DCHECK(!src.IsPostIndex());
987
988 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800989 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990 MemOperand base = MemOperand(temp_base);
991 switch (type) {
992 case Primitive::kPrimBoolean:
993 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000994 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000995 break;
996 case Primitive::kPrimByte:
997 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000998 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1000 break;
1001 case Primitive::kPrimChar:
1002 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001003 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001004 break;
1005 case Primitive::kPrimShort:
1006 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001007 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001008 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1009 break;
1010 case Primitive::kPrimInt:
1011 case Primitive::kPrimNot:
1012 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001013 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001014 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001015 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001016 break;
1017 case Primitive::kPrimFloat:
1018 case Primitive::kPrimDouble: {
1019 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001020 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001021
1022 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1023 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001024 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001025 __ Fmov(FPRegister(dst), temp);
1026 break;
1027 }
1028 case Primitive::kPrimVoid:
1029 LOG(FATAL) << "Unreachable type " << type;
1030 }
1031}
1032
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001033void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 CPURegister src,
1035 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001036 switch (type) {
1037 case Primitive::kPrimBoolean:
1038 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001039 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001040 break;
1041 case Primitive::kPrimChar:
1042 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001044 break;
1045 case Primitive::kPrimInt:
1046 case Primitive::kPrimNot:
1047 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001048 case Primitive::kPrimFloat:
1049 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001050 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001051 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001052 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001053 case Primitive::kPrimVoid:
1054 LOG(FATAL) << "Unreachable type " << type;
1055 }
1056}
1057
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001058void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1059 CPURegister src,
1060 const MemOperand& dst) {
1061 UseScratchRegisterScope temps(GetVIXLAssembler());
1062 Register temp_base = temps.AcquireX();
1063
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001064 DCHECK(!dst.IsPreIndex());
1065 DCHECK(!dst.IsPostIndex());
1066
1067 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001068 Operand op = OperandFromMemOperand(dst);
1069 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001070 MemOperand base = MemOperand(temp_base);
1071 switch (type) {
1072 case Primitive::kPrimBoolean:
1073 case Primitive::kPrimByte:
1074 __ Stlrb(Register(src), base);
1075 break;
1076 case Primitive::kPrimChar:
1077 case Primitive::kPrimShort:
1078 __ Stlrh(Register(src), base);
1079 break;
1080 case Primitive::kPrimInt:
1081 case Primitive::kPrimNot:
1082 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001083 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001084 __ Stlr(Register(src), base);
1085 break;
1086 case Primitive::kPrimFloat:
1087 case Primitive::kPrimDouble: {
1088 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001089 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001090
1091 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1092 __ Fmov(temp, FPRegister(src));
1093 __ Stlr(temp, base);
1094 break;
1095 }
1096 case Primitive::kPrimVoid:
1097 LOG(FATAL) << "Unreachable type " << type;
1098 }
1099}
1100
Alexandre Rames67555f72014-11-18 10:55:16 +00001101void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1102 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001103 uint32_t dex_pc,
1104 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001105 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001106 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001107 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1108 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001109 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001110}
1111
1112void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1113 vixl::Register class_reg) {
1114 UseScratchRegisterScope temps(GetVIXLAssembler());
1115 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001116 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001117 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001118
Serban Constantinescu02164b32014-11-13 14:05:07 +00001119 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001120 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001121 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1122 __ Add(temp, class_reg, status_offset);
1123 __ Ldar(temp, HeapOperand(temp));
1124 __ Cmp(temp, mirror::Class::kStatusInitialized);
1125 __ B(lt, slow_path->GetEntryLabel());
1126 } else {
1127 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1128 __ Cmp(temp, mirror::Class::kStatusInitialized);
1129 __ B(lt, slow_path->GetEntryLabel());
1130 __ Dmb(InnerShareable, BarrierReads);
1131 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001132 __ Bind(slow_path->GetExitLabel());
1133}
Alexandre Rames5319def2014-10-23 10:03:10 +01001134
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001135void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1136 BarrierType type = BarrierAll;
1137
1138 switch (kind) {
1139 case MemBarrierKind::kAnyAny:
1140 case MemBarrierKind::kAnyStore: {
1141 type = BarrierAll;
1142 break;
1143 }
1144 case MemBarrierKind::kLoadAny: {
1145 type = BarrierReads;
1146 break;
1147 }
1148 case MemBarrierKind::kStoreStore: {
1149 type = BarrierWrites;
1150 break;
1151 }
1152 default:
1153 LOG(FATAL) << "Unexpected memory barrier " << kind;
1154 }
1155 __ Dmb(InnerShareable, type);
1156}
1157
Serban Constantinescu02164b32014-11-13 14:05:07 +00001158void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1159 HBasicBlock* successor) {
1160 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001161 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1162 if (slow_path == nullptr) {
1163 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1164 instruction->SetSlowPath(slow_path);
1165 codegen_->AddSlowPath(slow_path);
1166 if (successor != nullptr) {
1167 DCHECK(successor->IsLoopHeader());
1168 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1169 }
1170 } else {
1171 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1172 }
1173
Serban Constantinescu02164b32014-11-13 14:05:07 +00001174 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1175 Register temp = temps.AcquireW();
1176
1177 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1178 if (successor == nullptr) {
1179 __ Cbnz(temp, slow_path->GetEntryLabel());
1180 __ Bind(slow_path->GetReturnLabel());
1181 } else {
1182 __ Cbz(temp, codegen_->GetLabelOf(successor));
1183 __ B(slow_path->GetEntryLabel());
1184 // slow_path will return to GetLabelOf(successor).
1185 }
1186}
1187
Alexandre Rames5319def2014-10-23 10:03:10 +01001188InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1189 CodeGeneratorARM64* codegen)
1190 : HGraphVisitor(graph),
1191 assembler_(codegen->GetAssembler()),
1192 codegen_(codegen) {}
1193
1194#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001196
1197#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1198
1199enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001200 // Using a base helps identify when we hit such breakpoints.
1201 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001202#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1203 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1204#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1205};
1206
1207#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1208 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001209 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001210 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1211 } \
1212 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1214 locations->SetOut(Location::Any()); \
1215 }
1216 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1217#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1218
1219#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001220#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001221
Alexandre Rames67555f72014-11-18 10:55:16 +00001222void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001223 DCHECK_EQ(instr->InputCount(), 2U);
1224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1225 Primitive::Type type = instr->GetResultType();
1226 switch (type) {
1227 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001228 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001229 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001230 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001232 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001233
1234 case Primitive::kPrimFloat:
1235 case Primitive::kPrimDouble:
1236 locations->SetInAt(0, Location::RequiresFpuRegister());
1237 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001238 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001239 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001240
Alexandre Rames5319def2014-10-23 10:03:10 +01001241 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001242 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001243 }
1244}
1245
Alexandre Rames09a99962015-04-15 11:47:56 +01001246void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1247 LocationSummary* locations =
1248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1249 locations->SetInAt(0, Location::RequiresRegister());
1250 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1251 locations->SetOut(Location::RequiresFpuRegister());
1252 } else {
1253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1254 }
1255}
1256
1257void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1258 const FieldInfo& field_info) {
1259 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001260 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001261 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001262
1263 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1264 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1265
1266 if (field_info.IsVolatile()) {
1267 if (use_acquire_release) {
1268 // NB: LoadAcquire will record the pc info if needed.
1269 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1270 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001271 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001272 codegen_->MaybeRecordImplicitNullCheck(instruction);
1273 // For IRIW sequential consistency kLoadAny is not sufficient.
1274 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1275 }
1276 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001277 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001278 codegen_->MaybeRecordImplicitNullCheck(instruction);
1279 }
Roland Levillain4d027112015-07-01 15:41:14 +01001280
1281 if (field_type == Primitive::kPrimNot) {
1282 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1283 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001284}
1285
1286void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1287 LocationSummary* locations =
1288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1289 locations->SetInAt(0, Location::RequiresRegister());
1290 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1291 locations->SetInAt(1, Location::RequiresFpuRegister());
1292 } else {
1293 locations->SetInAt(1, Location::RequiresRegister());
1294 }
1295}
1296
1297void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001298 const FieldInfo& field_info,
1299 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001300 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001301 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001302
1303 Register obj = InputRegisterAt(instruction, 0);
1304 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001305 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001306 Offset offset = field_info.GetFieldOffset();
1307 Primitive::Type field_type = field_info.GetFieldType();
1308 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1309
Roland Levillain4d027112015-07-01 15:41:14 +01001310 {
1311 // We use a block to end the scratch scope before the write barrier, thus
1312 // freeing the temporary registers so they can be used in `MarkGCCard`.
1313 UseScratchRegisterScope temps(GetVIXLAssembler());
1314
1315 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1316 DCHECK(value.IsW());
1317 Register temp = temps.AcquireW();
1318 __ Mov(temp, value.W());
1319 GetAssembler()->PoisonHeapReference(temp.W());
1320 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001321 }
Roland Levillain4d027112015-07-01 15:41:14 +01001322
1323 if (field_info.IsVolatile()) {
1324 if (use_acquire_release) {
1325 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1326 codegen_->MaybeRecordImplicitNullCheck(instruction);
1327 } else {
1328 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1329 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1330 codegen_->MaybeRecordImplicitNullCheck(instruction);
1331 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1332 }
1333 } else {
1334 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1335 codegen_->MaybeRecordImplicitNullCheck(instruction);
1336 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001337 }
1338
1339 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001340 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001341 }
1342}
1343
Alexandre Rames67555f72014-11-18 10:55:16 +00001344void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001345 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001346
1347 switch (type) {
1348 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001349 case Primitive::kPrimLong: {
1350 Register dst = OutputRegister(instr);
1351 Register lhs = InputRegisterAt(instr, 0);
1352 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001353 if (instr->IsAdd()) {
1354 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001355 } else if (instr->IsAnd()) {
1356 __ And(dst, lhs, rhs);
1357 } else if (instr->IsOr()) {
1358 __ Orr(dst, lhs, rhs);
1359 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001361 } else {
1362 DCHECK(instr->IsXor());
1363 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 }
1365 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001366 }
1367 case Primitive::kPrimFloat:
1368 case Primitive::kPrimDouble: {
1369 FPRegister dst = OutputFPRegister(instr);
1370 FPRegister lhs = InputFPRegisterAt(instr, 0);
1371 FPRegister rhs = InputFPRegisterAt(instr, 1);
1372 if (instr->IsAdd()) {
1373 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001374 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001375 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001376 } else {
1377 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001378 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001379 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001380 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001382 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001383 }
1384}
1385
Serban Constantinescu02164b32014-11-13 14:05:07 +00001386void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1387 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1388
1389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1390 Primitive::Type type = instr->GetResultType();
1391 switch (type) {
1392 case Primitive::kPrimInt:
1393 case Primitive::kPrimLong: {
1394 locations->SetInAt(0, Location::RequiresRegister());
1395 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1396 locations->SetOut(Location::RequiresRegister());
1397 break;
1398 }
1399 default:
1400 LOG(FATAL) << "Unexpected shift type " << type;
1401 }
1402}
1403
1404void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1405 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1406
1407 Primitive::Type type = instr->GetType();
1408 switch (type) {
1409 case Primitive::kPrimInt:
1410 case Primitive::kPrimLong: {
1411 Register dst = OutputRegister(instr);
1412 Register lhs = InputRegisterAt(instr, 0);
1413 Operand rhs = InputOperandAt(instr, 1);
1414 if (rhs.IsImmediate()) {
1415 uint32_t shift_value = (type == Primitive::kPrimInt)
1416 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1417 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1418 if (instr->IsShl()) {
1419 __ Lsl(dst, lhs, shift_value);
1420 } else if (instr->IsShr()) {
1421 __ Asr(dst, lhs, shift_value);
1422 } else {
1423 __ Lsr(dst, lhs, shift_value);
1424 }
1425 } else {
1426 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1427
1428 if (instr->IsShl()) {
1429 __ Lsl(dst, lhs, rhs_reg);
1430 } else if (instr->IsShr()) {
1431 __ Asr(dst, lhs, rhs_reg);
1432 } else {
1433 __ Lsr(dst, lhs, rhs_reg);
1434 }
1435 }
1436 break;
1437 }
1438 default:
1439 LOG(FATAL) << "Unexpected shift operation type " << type;
1440 }
1441}
1442
Alexandre Rames5319def2014-10-23 10:03:10 +01001443void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001444 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001445}
1446
1447void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001448 HandleBinaryOp(instruction);
1449}
1450
1451void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1452 HandleBinaryOp(instruction);
1453}
1454
1455void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1456 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001457}
1458
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001459void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1460 LocationSummary* locations =
1461 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1462 locations->SetInAt(0, Location::RequiresRegister());
1463 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001464 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1465 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1466 } else {
1467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1468 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001469}
1470
1471void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1472 LocationSummary* locations = instruction->GetLocations();
1473 Primitive::Type type = instruction->GetType();
1474 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001475 Location index = locations->InAt(1);
1476 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001477 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001478 MacroAssembler* masm = GetVIXLAssembler();
1479 UseScratchRegisterScope temps(masm);
1480 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001481
1482 if (index.IsConstant()) {
1483 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001484 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001485 } else {
1486 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001487 __ Add(temp, obj, offset);
1488 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001489 }
1490
Alexandre Rames67555f72014-11-18 10:55:16 +00001491 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001492 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001493
1494 if (type == Primitive::kPrimNot) {
1495 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1496 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001497}
1498
Alexandre Rames5319def2014-10-23 10:03:10 +01001499void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1501 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001502 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001503}
1504
1505void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001506 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001507 __ Ldr(OutputRegister(instruction),
1508 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001509 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001510}
1511
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001512void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001513 if (instruction->NeedsTypeCheck()) {
1514 LocationSummary* locations =
1515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001516 InvokeRuntimeCallingConvention calling_convention;
1517 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1518 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1519 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1520 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001521 LocationSummary* locations =
1522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001523 locations->SetInAt(0, Location::RequiresRegister());
1524 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001525 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1526 locations->SetInAt(2, Location::RequiresFpuRegister());
1527 } else {
1528 locations->SetInAt(2, Location::RequiresRegister());
1529 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001530 }
1531}
1532
1533void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1534 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001535 LocationSummary* locations = instruction->GetLocations();
1536 bool needs_runtime_call = locations->WillCall();
1537
1538 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001539 // Note: if heap poisoning is enabled, pAputObject takes cares
1540 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001541 codegen_->InvokeRuntime(
1542 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001543 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001544 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001545 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001546 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001547 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001548 Location index = locations->InAt(1);
1549 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001550 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001551 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001552 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001553 {
1554 // We use a block to end the scratch scope before the write barrier, thus
1555 // freeing the temporary registers so they can be used in `MarkGCCard`.
1556 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557
Roland Levillain4d027112015-07-01 15:41:14 +01001558 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1559 DCHECK(value.IsW());
1560 Register temp = temps.AcquireW();
1561 __ Mov(temp, value.W());
1562 GetAssembler()->PoisonHeapReference(temp.W());
1563 source = temp;
1564 }
1565
Alexandre Rames97833a02015-04-16 15:07:12 +01001566 if (index.IsConstant()) {
1567 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1568 destination = HeapOperand(obj, offset);
1569 } else {
1570 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001571 __ Add(temp, obj, offset);
1572 destination = HeapOperand(temp,
1573 XRegisterFrom(index),
1574 LSL,
1575 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001576 }
1577
Roland Levillain4d027112015-07-01 15:41:14 +01001578 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001579 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001580 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001581 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001582 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001583 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001584 }
1585}
1586
Alexandre Rames67555f72014-11-18 10:55:16 +00001587void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1588 LocationSummary* locations =
1589 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1590 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001591 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 if (instruction->HasUses()) {
1593 locations->SetOut(Location::SameAsFirstInput());
1594 }
1595}
1596
1597void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001598 LocationSummary* locations = instruction->GetLocations();
1599 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1600 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001601 codegen_->AddSlowPath(slow_path);
1602
1603 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1604 __ B(slow_path->GetEntryLabel(), hs);
1605}
1606
1607void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1609 instruction, LocationSummary::kCallOnSlowPath);
1610 locations->SetInAt(0, Location::RequiresRegister());
1611 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001612 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001613}
1614
1615void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001616 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001617 Register obj = InputRegisterAt(instruction, 0);;
1618 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001619 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001620
Alexandre Rames3e69f162014-12-10 10:36:50 +00001621 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1622 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001623 codegen_->AddSlowPath(slow_path);
1624
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001625 // Avoid null check if we know obj is not null.
1626 if (instruction->MustDoNullCheck()) {
1627 __ Cbz(obj, slow_path->GetExitLabel());
1628 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001630 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001631 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001632 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001633 // The checkcast succeeds if the classes are equal (fast path).
1634 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001635 __ B(ne, slow_path->GetEntryLabel());
1636 __ Bind(slow_path->GetExitLabel());
1637}
1638
1639void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1640 LocationSummary* locations =
1641 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1642 locations->SetInAt(0, Location::RequiresRegister());
1643 if (check->HasUses()) {
1644 locations->SetOut(Location::SameAsFirstInput());
1645 }
1646}
1647
1648void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1649 // We assume the class is not null.
1650 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1651 check->GetLoadClass(), check, check->GetDexPc(), true);
1652 codegen_->AddSlowPath(slow_path);
1653 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1654}
1655
Roland Levillain7f63c522015-07-13 15:54:55 +00001656static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1657 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1658 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1659}
1660
Serban Constantinescu02164b32014-11-13 14:05:07 +00001661void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001662 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001663 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1664 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 switch (in_type) {
1666 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001667 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001668 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1670 break;
1671 }
1672 case Primitive::kPrimFloat:
1673 case Primitive::kPrimDouble: {
1674 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001675 locations->SetInAt(1,
1676 IsFloatingPointZeroConstant(compare->InputAt(1))
1677 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1678 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001679 locations->SetOut(Location::RequiresRegister());
1680 break;
1681 }
1682 default:
1683 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1684 }
1685}
1686
1687void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1688 Primitive::Type in_type = compare->InputAt(0)->GetType();
1689
1690 // 0 if: left == right
1691 // 1 if: left > right
1692 // -1 if: left < right
1693 switch (in_type) {
1694 case Primitive::kPrimLong: {
1695 Register result = OutputRegister(compare);
1696 Register left = InputRegisterAt(compare, 0);
1697 Operand right = InputOperandAt(compare, 1);
1698
1699 __ Cmp(left, right);
1700 __ Cset(result, ne);
1701 __ Cneg(result, result, lt);
1702 break;
1703 }
1704 case Primitive::kPrimFloat:
1705 case Primitive::kPrimDouble: {
1706 Register result = OutputRegister(compare);
1707 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001708 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001709 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1710 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001711 __ Fcmp(left, 0.0);
1712 } else {
1713 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1714 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001715 if (compare->IsGtBias()) {
1716 __ Cset(result, ne);
1717 } else {
1718 __ Csetm(result, ne);
1719 }
1720 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001721 break;
1722 }
1723 default:
1724 LOG(FATAL) << "Unimplemented compare type " << in_type;
1725 }
1726}
1727
1728void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1729 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001730
1731 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1732 locations->SetInAt(0, Location::RequiresFpuRegister());
1733 locations->SetInAt(1,
1734 IsFloatingPointZeroConstant(instruction->InputAt(1))
1735 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1736 : Location::RequiresFpuRegister());
1737 } else {
1738 // Integer cases.
1739 locations->SetInAt(0, Location::RequiresRegister());
1740 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1741 }
1742
Alexandre Rames5319def2014-10-23 10:03:10 +01001743 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001745 }
1746}
1747
1748void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1749 if (!instruction->NeedsMaterialization()) {
1750 return;
1751 }
1752
1753 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001754 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001755 IfCondition if_cond = instruction->GetCondition();
1756 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001757
Roland Levillain7f63c522015-07-13 15:54:55 +00001758 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1759 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1760 if (locations->InAt(1).IsConstant()) {
1761 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1762 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1763 __ Fcmp(lhs, 0.0);
1764 } else {
1765 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1766 }
1767 __ Cset(res, arm64_cond);
1768 if (instruction->IsFPConditionTrueIfNaN()) {
1769 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1770 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1771 } else if (instruction->IsFPConditionFalseIfNaN()) {
1772 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1773 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1774 }
1775 } else {
1776 // Integer cases.
1777 Register lhs = InputRegisterAt(instruction, 0);
1778 Operand rhs = InputOperandAt(instruction, 1);
1779 __ Cmp(lhs, rhs);
1780 __ Cset(res, arm64_cond);
1781 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001782}
1783
1784#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1785 M(Equal) \
1786 M(NotEqual) \
1787 M(LessThan) \
1788 M(LessThanOrEqual) \
1789 M(GreaterThan) \
1790 M(GreaterThanOrEqual)
1791#define DEFINE_CONDITION_VISITORS(Name) \
1792void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1793void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1794FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001795#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001796#undef FOR_EACH_CONDITION_INSTRUCTION
1797
Zheng Xuc6667102015-05-15 16:08:45 +08001798void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1799 DCHECK(instruction->IsDiv() || instruction->IsRem());
1800
1801 LocationSummary* locations = instruction->GetLocations();
1802 Location second = locations->InAt(1);
1803 DCHECK(second.IsConstant());
1804
1805 Register out = OutputRegister(instruction);
1806 Register dividend = InputRegisterAt(instruction, 0);
1807 int64_t imm = Int64FromConstant(second.GetConstant());
1808 DCHECK(imm == 1 || imm == -1);
1809
1810 if (instruction->IsRem()) {
1811 __ Mov(out, 0);
1812 } else {
1813 if (imm == 1) {
1814 __ Mov(out, dividend);
1815 } else {
1816 __ Neg(out, dividend);
1817 }
1818 }
1819}
1820
1821void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1822 DCHECK(instruction->IsDiv() || instruction->IsRem());
1823
1824 LocationSummary* locations = instruction->GetLocations();
1825 Location second = locations->InAt(1);
1826 DCHECK(second.IsConstant());
1827
1828 Register out = OutputRegister(instruction);
1829 Register dividend = InputRegisterAt(instruction, 0);
1830 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001831 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001832 DCHECK(IsPowerOfTwo(abs_imm));
1833 int ctz_imm = CTZ(abs_imm);
1834
1835 UseScratchRegisterScope temps(GetVIXLAssembler());
1836 Register temp = temps.AcquireSameSizeAs(out);
1837
1838 if (instruction->IsDiv()) {
1839 __ Add(temp, dividend, abs_imm - 1);
1840 __ Cmp(dividend, 0);
1841 __ Csel(out, temp, dividend, lt);
1842 if (imm > 0) {
1843 __ Asr(out, out, ctz_imm);
1844 } else {
1845 __ Neg(out, Operand(out, ASR, ctz_imm));
1846 }
1847 } else {
1848 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1849 __ Asr(temp, dividend, bits - 1);
1850 __ Lsr(temp, temp, bits - ctz_imm);
1851 __ Add(out, dividend, temp);
1852 __ And(out, out, abs_imm - 1);
1853 __ Sub(out, out, temp);
1854 }
1855}
1856
1857void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1858 DCHECK(instruction->IsDiv() || instruction->IsRem());
1859
1860 LocationSummary* locations = instruction->GetLocations();
1861 Location second = locations->InAt(1);
1862 DCHECK(second.IsConstant());
1863
1864 Register out = OutputRegister(instruction);
1865 Register dividend = InputRegisterAt(instruction, 0);
1866 int64_t imm = Int64FromConstant(second.GetConstant());
1867
1868 Primitive::Type type = instruction->GetResultType();
1869 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1870
1871 int64_t magic;
1872 int shift;
1873 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1874
1875 UseScratchRegisterScope temps(GetVIXLAssembler());
1876 Register temp = temps.AcquireSameSizeAs(out);
1877
1878 // temp = get_high(dividend * magic)
1879 __ Mov(temp, magic);
1880 if (type == Primitive::kPrimLong) {
1881 __ Smulh(temp, dividend, temp);
1882 } else {
1883 __ Smull(temp.X(), dividend, temp);
1884 __ Lsr(temp.X(), temp.X(), 32);
1885 }
1886
1887 if (imm > 0 && magic < 0) {
1888 __ Add(temp, temp, dividend);
1889 } else if (imm < 0 && magic > 0) {
1890 __ Sub(temp, temp, dividend);
1891 }
1892
1893 if (shift != 0) {
1894 __ Asr(temp, temp, shift);
1895 }
1896
1897 if (instruction->IsDiv()) {
1898 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1899 } else {
1900 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1901 // TODO: Strength reduction for msub.
1902 Register temp_imm = temps.AcquireSameSizeAs(out);
1903 __ Mov(temp_imm, imm);
1904 __ Msub(out, temp, temp_imm, dividend);
1905 }
1906}
1907
1908void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1909 DCHECK(instruction->IsDiv() || instruction->IsRem());
1910 Primitive::Type type = instruction->GetResultType();
1911 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1912
1913 LocationSummary* locations = instruction->GetLocations();
1914 Register out = OutputRegister(instruction);
1915 Location second = locations->InAt(1);
1916
1917 if (second.IsConstant()) {
1918 int64_t imm = Int64FromConstant(second.GetConstant());
1919
1920 if (imm == 0) {
1921 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1922 } else if (imm == 1 || imm == -1) {
1923 DivRemOneOrMinusOne(instruction);
1924 } else if (IsPowerOfTwo(std::abs(imm))) {
1925 DivRemByPowerOfTwo(instruction);
1926 } else {
1927 DCHECK(imm <= -2 || imm >= 2);
1928 GenerateDivRemWithAnyConstant(instruction);
1929 }
1930 } else {
1931 Register dividend = InputRegisterAt(instruction, 0);
1932 Register divisor = InputRegisterAt(instruction, 1);
1933 if (instruction->IsDiv()) {
1934 __ Sdiv(out, dividend, divisor);
1935 } else {
1936 UseScratchRegisterScope temps(GetVIXLAssembler());
1937 Register temp = temps.AcquireSameSizeAs(out);
1938 __ Sdiv(temp, dividend, divisor);
1939 __ Msub(out, temp, divisor, dividend);
1940 }
1941 }
1942}
1943
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001944void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1945 LocationSummary* locations =
1946 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1947 switch (div->GetResultType()) {
1948 case Primitive::kPrimInt:
1949 case Primitive::kPrimLong:
1950 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001951 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1953 break;
1954
1955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble:
1957 locations->SetInAt(0, Location::RequiresFpuRegister());
1958 locations->SetInAt(1, Location::RequiresFpuRegister());
1959 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1960 break;
1961
1962 default:
1963 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1964 }
1965}
1966
1967void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1968 Primitive::Type type = div->GetResultType();
1969 switch (type) {
1970 case Primitive::kPrimInt:
1971 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001972 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001973 break;
1974
1975 case Primitive::kPrimFloat:
1976 case Primitive::kPrimDouble:
1977 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1978 break;
1979
1980 default:
1981 LOG(FATAL) << "Unexpected div type " << type;
1982 }
1983}
1984
Alexandre Rames67555f72014-11-18 10:55:16 +00001985void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1986 LocationSummary* locations =
1987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1988 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1989 if (instruction->HasUses()) {
1990 locations->SetOut(Location::SameAsFirstInput());
1991 }
1992}
1993
1994void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1995 SlowPathCodeARM64* slow_path =
1996 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1997 codegen_->AddSlowPath(slow_path);
1998 Location value = instruction->GetLocations()->InAt(0);
1999
Alexandre Rames3e69f162014-12-10 10:36:50 +00002000 Primitive::Type type = instruction->GetType();
2001
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002002 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2003 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002004 return;
2005 }
2006
Alexandre Rames67555f72014-11-18 10:55:16 +00002007 if (value.IsConstant()) {
2008 int64_t divisor = Int64ConstantFrom(value);
2009 if (divisor == 0) {
2010 __ B(slow_path->GetEntryLabel());
2011 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002012 // A division by a non-null constant is valid. We don't need to perform
2013 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002014 }
2015 } else {
2016 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2017 }
2018}
2019
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002020void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2023 locations->SetOut(Location::ConstantLocation(constant));
2024}
2025
2026void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2027 UNUSED(constant);
2028 // Will be generated at use site.
2029}
2030
Alexandre Rames5319def2014-10-23 10:03:10 +01002031void LocationsBuilderARM64::VisitExit(HExit* exit) {
2032 exit->SetLocations(nullptr);
2033}
2034
2035void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002036 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002037}
2038
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002039void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2040 LocationSummary* locations =
2041 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2042 locations->SetOut(Location::ConstantLocation(constant));
2043}
2044
2045void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2046 UNUSED(constant);
2047 // Will be generated at use site.
2048}
2049
David Brazdilfc6a86a2015-06-26 10:33:45 +00002050void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002051 DCHECK(!successor->IsExitBlock());
2052 HBasicBlock* block = got->GetBlock();
2053 HInstruction* previous = got->GetPrevious();
2054 HLoopInformation* info = block->GetLoopInformation();
2055
David Brazdil46e2a392015-03-16 17:31:52 +00002056 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002057 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2058 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2059 return;
2060 }
2061 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2062 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2063 }
2064 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002065 __ B(codegen_->GetLabelOf(successor));
2066 }
2067}
2068
David Brazdilfc6a86a2015-06-26 10:33:45 +00002069void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2070 got->SetLocations(nullptr);
2071}
2072
2073void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2074 HandleGoto(got, got->GetSuccessor());
2075}
2076
2077void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2078 try_boundary->SetLocations(nullptr);
2079}
2080
2081void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2082 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2083 if (!successor->IsExitBlock()) {
2084 HandleGoto(try_boundary, successor);
2085 }
2086}
2087
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002088void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2089 vixl::Label* true_target,
2090 vixl::Label* false_target,
2091 vixl::Label* always_true_target) {
2092 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002093 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002094
Serban Constantinescu02164b32014-11-13 14:05:07 +00002095 if (cond->IsIntConstant()) {
2096 int32_t cond_value = cond->AsIntConstant()->GetValue();
2097 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002098 if (always_true_target != nullptr) {
2099 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002100 }
2101 return;
2102 } else {
2103 DCHECK_EQ(cond_value, 0);
2104 }
2105 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002106 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002107 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002108 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002109 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002110 } else {
2111 // The condition instruction has not been materialized, use its inputs as
2112 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002113 Primitive::Type type =
2114 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2115
2116 if (Primitive::IsFloatingPointType(type)) {
2117 // FP compares don't like null false_targets.
2118 if (false_target == nullptr) {
2119 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002120 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002121 FPRegister lhs = InputFPRegisterAt(condition, 0);
2122 if (condition->GetLocations()->InAt(1).IsConstant()) {
2123 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2124 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2125 __ Fcmp(lhs, 0.0);
2126 } else {
2127 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2128 }
2129 if (condition->IsFPConditionTrueIfNaN()) {
2130 __ B(vs, true_target); // VS for unordered.
2131 } else if (condition->IsFPConditionFalseIfNaN()) {
2132 __ B(vs, false_target); // VS for unordered.
2133 }
2134 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002135 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002136 // Integer cases.
2137 Register lhs = InputRegisterAt(condition, 0);
2138 Operand rhs = InputOperandAt(condition, 1);
2139 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2140 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2141 switch (arm64_cond) {
2142 case eq:
2143 __ Cbz(lhs, true_target);
2144 break;
2145 case ne:
2146 __ Cbnz(lhs, true_target);
2147 break;
2148 case lt:
2149 // Test the sign bit and branch accordingly.
2150 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2151 break;
2152 case ge:
2153 // Test the sign bit and branch accordingly.
2154 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2155 break;
2156 default:
2157 // Without the `static_cast` the compiler throws an error for
2158 // `-Werror=sign-promo`.
2159 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2160 }
2161 } else {
2162 __ Cmp(lhs, rhs);
2163 __ B(arm64_cond, true_target);
2164 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002165 }
2166 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002167 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002168 __ B(false_target);
2169 }
2170}
2171
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002172void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2173 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2174 HInstruction* cond = if_instr->InputAt(0);
2175 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2176 locations->SetInAt(0, Location::RequiresRegister());
2177 }
2178}
2179
2180void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2181 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2182 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2183 vixl::Label* always_true_target = true_target;
2184 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2185 if_instr->IfTrueSuccessor())) {
2186 always_true_target = nullptr;
2187 }
2188 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2189 if_instr->IfFalseSuccessor())) {
2190 false_target = nullptr;
2191 }
2192 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2193}
2194
2195void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2196 LocationSummary* locations = new (GetGraph()->GetArena())
2197 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2198 HInstruction* cond = deoptimize->InputAt(0);
2199 DCHECK(cond->IsCondition());
2200 if (cond->AsCondition()->NeedsMaterialization()) {
2201 locations->SetInAt(0, Location::RequiresRegister());
2202 }
2203}
2204
2205void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2206 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2207 DeoptimizationSlowPathARM64(deoptimize);
2208 codegen_->AddSlowPath(slow_path);
2209 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2210 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2211}
2212
Alexandre Rames5319def2014-10-23 10:03:10 +01002213void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002214 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002215}
2216
2217void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002218 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002219}
2220
2221void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002222 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002223}
2224
2225void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002226 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002227}
2228
Alexandre Rames67555f72014-11-18 10:55:16 +00002229void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2230 LocationSummary::CallKind call_kind =
2231 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2233 locations->SetInAt(0, Location::RequiresRegister());
2234 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002235 // The output does overlap inputs.
2236 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002237}
2238
2239void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2240 LocationSummary* locations = instruction->GetLocations();
2241 Register obj = InputRegisterAt(instruction, 0);;
2242 Register cls = InputRegisterAt(instruction, 1);;
2243 Register out = OutputRegister(instruction);
2244
2245 vixl::Label done;
2246
2247 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002248 // Avoid null check if we know `obj` is not null.
2249 if (instruction->MustDoNullCheck()) {
2250 __ Mov(out, 0);
2251 __ Cbz(obj, &done);
2252 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002253
2254 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002255 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002256 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002257 __ Cmp(out, cls);
2258 if (instruction->IsClassFinal()) {
2259 // Classes must be equal for the instanceof to succeed.
2260 __ Cset(out, eq);
2261 } else {
2262 // If the classes are not equal, we go into a slow path.
2263 DCHECK(locations->OnlyCallsOnSlowPath());
2264 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002265 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2266 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002267 codegen_->AddSlowPath(slow_path);
2268 __ B(ne, slow_path->GetEntryLabel());
2269 __ Mov(out, 1);
2270 __ Bind(slow_path->GetExitLabel());
2271 }
2272
2273 __ Bind(&done);
2274}
2275
Alexandre Rames5319def2014-10-23 10:03:10 +01002276void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2278 locations->SetOut(Location::ConstantLocation(constant));
2279}
2280
2281void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2282 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002283 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002284}
2285
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002286void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2287 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2288 locations->SetOut(Location::ConstantLocation(constant));
2289}
2290
2291void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2292 // Will be generated at use site.
2293 UNUSED(constant);
2294}
2295
Alexandre Rames5319def2014-10-23 10:03:10 +01002296void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002297 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002298 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002299}
2300
Alexandre Rames67555f72014-11-18 10:55:16 +00002301void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2302 HandleInvoke(invoke);
2303}
2304
2305void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2306 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002307 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2308 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2309 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002310 Location receiver = invoke->GetLocations()->InAt(0);
2311 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002312 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002313
2314 // The register ip1 is required to be used for the hidden argument in
2315 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002316 MacroAssembler* masm = GetVIXLAssembler();
2317 UseScratchRegisterScope scratch_scope(masm);
2318 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002319 scratch_scope.Exclude(ip1);
2320 __ Mov(ip1, invoke->GetDexMethodIndex());
2321
2322 // temp = object->GetClass();
2323 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002324 __ Ldr(temp.W(), StackOperandFrom(receiver));
2325 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002326 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002327 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002328 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002329 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002330 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002331 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002332 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002333 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002334 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002335 // lr();
2336 __ Blr(lr);
2337 DCHECK(!codegen_->IsLeafMethod());
2338 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2339}
2340
2341void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002342 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2343 if (intrinsic.TryDispatch(invoke)) {
2344 return;
2345 }
2346
Alexandre Rames67555f72014-11-18 10:55:16 +00002347 HandleInvoke(invoke);
2348}
2349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002350void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002351 // When we do not run baseline, explicit clinit checks triggered by static
2352 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2353 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002354
Andreas Gampe878d58c2015-01-15 23:24:00 -08002355 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2356 if (intrinsic.TryDispatch(invoke)) {
2357 return;
2358 }
2359
Alexandre Rames67555f72014-11-18 10:55:16 +00002360 HandleInvoke(invoke);
2361}
2362
Andreas Gampe878d58c2015-01-15 23:24:00 -08002363static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2364 if (invoke->GetLocations()->Intrinsified()) {
2365 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2366 intrinsic.Dispatch(invoke);
2367 return true;
2368 }
2369 return false;
2370}
2371
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002372void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002373 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Markob2c431e2015-08-19 12:45:42 +00002374 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002375
Vladimir Markob2c431e2015-08-19 12:45:42 +00002376 // TODO: Implement all kinds of calls:
2377 // 1) boot -> boot
2378 // 2) app -> boot
2379 // 3) app -> app
2380 //
2381 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Vladimir Marko9b688a02015-05-06 14:12:42 +01002382
Vladimir Markob2c431e2015-08-19 12:45:42 +00002383 if (invoke->IsStringInit()) {
2384 Register reg = XRegisterFrom(temp);
2385 // temp = thread->string_init_entrypoint
2386 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
2387 // LR = temp->entry_point_from_quick_compiled_code_;
2388 __ Ldr(lr, MemOperand(
2389 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2390 // lr()
2391 __ Blr(lr);
2392 } else if (invoke->IsRecursive()) {
2393 __ Bl(&frame_entry_label_);
2394 } else {
2395 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2396 Register reg = XRegisterFrom(temp);
2397 Register method_reg;
2398 if (current_method.IsRegister()) {
2399 method_reg = XRegisterFrom(current_method);
2400 } else {
2401 DCHECK(invoke->GetLocations()->Intrinsified());
2402 DCHECK(!current_method.IsValid());
2403 method_reg = reg;
2404 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
Vladimir Marko9b688a02015-05-06 14:12:42 +01002405 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002406
2407 // temp = current_method->dex_cache_resolved_methods_;
2408 __ Ldr(reg.W(), MemOperand(method_reg.X(),
2409 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2410 // temp = temp[index_in_cache];
2411 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2412 // lr = temp->entry_point_from_quick_compiled_code_;
2413 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2414 kArm64WordSize).Int32Value()));
2415 // lr();
2416 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002417 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002418
Andreas Gampe878d58c2015-01-15 23:24:00 -08002419 DCHECK(!IsLeafMethod());
2420}
2421
2422void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002423 // When we do not run baseline, explicit clinit checks triggered by static
2424 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2425 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002426
Andreas Gampe878d58c2015-01-15 23:24:00 -08002427 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2428 return;
2429 }
2430
Alexandre Ramesd921d642015-04-16 15:07:16 +01002431 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002432 LocationSummary* locations = invoke->GetLocations();
2433 codegen_->GenerateStaticOrDirectCall(
2434 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002435 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002436}
2437
2438void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002439 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2440 return;
2441 }
2442
Alexandre Rames5319def2014-10-23 10:03:10 +01002443 LocationSummary* locations = invoke->GetLocations();
2444 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002445 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2446 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2447 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002448 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002449 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002450
Alexandre Ramesd921d642015-04-16 15:07:16 +01002451 BlockPoolsScope block_pools(GetVIXLAssembler());
2452
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002453 DCHECK(receiver.IsRegister());
2454 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002455 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002456 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002457 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002458 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002459 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002460 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002461 // lr();
2462 __ Blr(lr);
2463 DCHECK(!codegen_->IsLeafMethod());
2464 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2465}
2466
Alexandre Rames67555f72014-11-18 10:55:16 +00002467void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2468 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2469 : LocationSummary::kNoCall;
2470 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002471 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002472 locations->SetOut(Location::RequiresRegister());
2473}
2474
2475void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2476 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002477 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002478 if (cls->IsReferrersClass()) {
2479 DCHECK(!cls->CanCallRuntime());
2480 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002481 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002482 } else {
2483 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002484 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002485 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002486 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002487
2488 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2489 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2490 codegen_->AddSlowPath(slow_path);
2491 __ Cbz(out, slow_path->GetEntryLabel());
2492 if (cls->MustGenerateClinitCheck()) {
2493 GenerateClassInitializationCheck(slow_path, out);
2494 } else {
2495 __ Bind(slow_path->GetExitLabel());
2496 }
2497 }
2498}
2499
David Brazdilcb1c0552015-08-04 16:22:25 +01002500static MemOperand GetExceptionTlsAddress() {
2501 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2502}
2503
Alexandre Rames67555f72014-11-18 10:55:16 +00002504void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2505 LocationSummary* locations =
2506 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2507 locations->SetOut(Location::RequiresRegister());
2508}
2509
2510void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002511 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2512}
2513
2514void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2515 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2516}
2517
2518void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2519 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002520}
2521
Alexandre Rames5319def2014-10-23 10:03:10 +01002522void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2523 load->SetLocations(nullptr);
2524}
2525
2526void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2527 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002528 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002529}
2530
Alexandre Rames67555f72014-11-18 10:55:16 +00002531void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2532 LocationSummary* locations =
2533 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002534 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002535 locations->SetOut(Location::RequiresRegister());
2536}
2537
2538void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2539 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2540 codegen_->AddSlowPath(slow_path);
2541
2542 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002543 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002544 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002545 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002546 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002547 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002548 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002549 __ Cbz(out, slow_path->GetEntryLabel());
2550 __ Bind(slow_path->GetExitLabel());
2551}
2552
Alexandre Rames5319def2014-10-23 10:03:10 +01002553void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2554 local->SetLocations(nullptr);
2555}
2556
2557void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2558 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2559}
2560
2561void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2563 locations->SetOut(Location::ConstantLocation(constant));
2564}
2565
2566void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2567 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002568 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002569}
2570
Alexandre Rames67555f72014-11-18 10:55:16 +00002571void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2572 LocationSummary* locations =
2573 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2574 InvokeRuntimeCallingConvention calling_convention;
2575 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2576}
2577
2578void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2579 codegen_->InvokeRuntime(instruction->IsEnter()
2580 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2581 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002582 instruction->GetDexPc(),
2583 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002584 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002585}
2586
Alexandre Rames42d641b2014-10-27 14:00:51 +00002587void LocationsBuilderARM64::VisitMul(HMul* mul) {
2588 LocationSummary* locations =
2589 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2590 switch (mul->GetResultType()) {
2591 case Primitive::kPrimInt:
2592 case Primitive::kPrimLong:
2593 locations->SetInAt(0, Location::RequiresRegister());
2594 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002596 break;
2597
2598 case Primitive::kPrimFloat:
2599 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002600 locations->SetInAt(0, Location::RequiresFpuRegister());
2601 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002602 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002603 break;
2604
2605 default:
2606 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2607 }
2608}
2609
2610void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2611 switch (mul->GetResultType()) {
2612 case Primitive::kPrimInt:
2613 case Primitive::kPrimLong:
2614 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2615 break;
2616
2617 case Primitive::kPrimFloat:
2618 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002619 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002620 break;
2621
2622 default:
2623 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2624 }
2625}
2626
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002627void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2628 LocationSummary* locations =
2629 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2630 switch (neg->GetResultType()) {
2631 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002632 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002633 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002634 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002635 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002636
2637 case Primitive::kPrimFloat:
2638 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002639 locations->SetInAt(0, Location::RequiresFpuRegister());
2640 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002641 break;
2642
2643 default:
2644 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2645 }
2646}
2647
2648void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2649 switch (neg->GetResultType()) {
2650 case Primitive::kPrimInt:
2651 case Primitive::kPrimLong:
2652 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2653 break;
2654
2655 case Primitive::kPrimFloat:
2656 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002657 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002658 break;
2659
2660 default:
2661 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2662 }
2663}
2664
2665void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2666 LocationSummary* locations =
2667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2668 InvokeRuntimeCallingConvention calling_convention;
2669 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002670 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002671 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002672 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002673 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002674 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002675}
2676
2677void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2678 LocationSummary* locations = instruction->GetLocations();
2679 InvokeRuntimeCallingConvention calling_convention;
2680 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2681 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002682 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002683 // Note: if heap poisoning is enabled, the entry point takes cares
2684 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002685 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002686 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2687 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002688 instruction->GetDexPc(),
2689 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002690 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002691}
2692
Alexandre Rames5319def2014-10-23 10:03:10 +01002693void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2694 LocationSummary* locations =
2695 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2696 InvokeRuntimeCallingConvention calling_convention;
2697 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002698 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002699 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002700 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002701}
2702
2703void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2704 LocationSummary* locations = instruction->GetLocations();
2705 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2706 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002707 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002708 // Note: if heap poisoning is enabled, the entry point takes cares
2709 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002710 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002711 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2712 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002713 instruction->GetDexPc(),
2714 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002715 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002716}
2717
2718void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002720 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002721 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002722}
2723
2724void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002725 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002726 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002727 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002728 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002729 break;
2730
2731 default:
2732 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2733 }
2734}
2735
David Brazdil66d126e2015-04-03 16:02:44 +01002736void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2738 locations->SetInAt(0, Location::RequiresRegister());
2739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2740}
2741
2742void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002743 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2744}
2745
Alexandre Rames5319def2014-10-23 10:03:10 +01002746void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2747 LocationSummary* locations =
2748 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2749 locations->SetInAt(0, Location::RequiresRegister());
2750 if (instruction->HasUses()) {
2751 locations->SetOut(Location::SameAsFirstInput());
2752 }
2753}
2754
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002755void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002756 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2757 return;
2758 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002759
Alexandre Ramesd921d642015-04-16 15:07:16 +01002760 BlockPoolsScope block_pools(GetVIXLAssembler());
2761 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002762 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2763 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2764}
2765
2766void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002767 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2768 codegen_->AddSlowPath(slow_path);
2769
2770 LocationSummary* locations = instruction->GetLocations();
2771 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002772
2773 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002774}
2775
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002776void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2777 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2778 GenerateImplicitNullCheck(instruction);
2779 } else {
2780 GenerateExplicitNullCheck(instruction);
2781 }
2782}
2783
Alexandre Rames67555f72014-11-18 10:55:16 +00002784void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2785 HandleBinaryOp(instruction);
2786}
2787
2788void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2789 HandleBinaryOp(instruction);
2790}
2791
Alexandre Rames3e69f162014-12-10 10:36:50 +00002792void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2793 LOG(FATAL) << "Unreachable";
2794}
2795
2796void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2797 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2798}
2799
Alexandre Rames5319def2014-10-23 10:03:10 +01002800void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2802 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2803 if (location.IsStackSlot()) {
2804 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2805 } else if (location.IsDoubleStackSlot()) {
2806 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2807 }
2808 locations->SetOut(location);
2809}
2810
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002811void InstructionCodeGeneratorARM64::VisitParameterValue(
2812 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002813 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002814}
2815
2816void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2817 LocationSummary* locations =
2818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002819 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002820}
2821
2822void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2823 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2824 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002825}
2826
2827void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2828 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2829 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2830 locations->SetInAt(i, Location::Any());
2831 }
2832 locations->SetOut(Location::Any());
2833}
2834
2835void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002836 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002837 LOG(FATAL) << "Unreachable";
2838}
2839
Serban Constantinescu02164b32014-11-13 14:05:07 +00002840void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002841 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002842 LocationSummary::CallKind call_kind =
2843 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2845
2846 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002847 case Primitive::kPrimInt:
2848 case Primitive::kPrimLong:
2849 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002850 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2852 break;
2853
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002854 case Primitive::kPrimFloat:
2855 case Primitive::kPrimDouble: {
2856 InvokeRuntimeCallingConvention calling_convention;
2857 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2858 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2859 locations->SetOut(calling_convention.GetReturnLocation(type));
2860
2861 break;
2862 }
2863
Serban Constantinescu02164b32014-11-13 14:05:07 +00002864 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002865 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002866 }
2867}
2868
2869void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2870 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002871
Serban Constantinescu02164b32014-11-13 14:05:07 +00002872 switch (type) {
2873 case Primitive::kPrimInt:
2874 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002875 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002876 break;
2877 }
2878
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002879 case Primitive::kPrimFloat:
2880 case Primitive::kPrimDouble: {
2881 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2882 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002883 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002884 break;
2885 }
2886
Serban Constantinescu02164b32014-11-13 14:05:07 +00002887 default:
2888 LOG(FATAL) << "Unexpected rem type " << type;
2889 }
2890}
2891
Calin Juravle27df7582015-04-17 19:12:31 +01002892void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2893 memory_barrier->SetLocations(nullptr);
2894}
2895
2896void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2897 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2898}
2899
Alexandre Rames5319def2014-10-23 10:03:10 +01002900void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2902 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002903 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002904}
2905
2906void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002907 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002908 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002909}
2910
2911void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2912 instruction->SetLocations(nullptr);
2913}
2914
2915void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002916 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002917 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002918}
2919
Serban Constantinescu02164b32014-11-13 14:05:07 +00002920void LocationsBuilderARM64::VisitShl(HShl* shl) {
2921 HandleShift(shl);
2922}
2923
2924void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2925 HandleShift(shl);
2926}
2927
2928void LocationsBuilderARM64::VisitShr(HShr* shr) {
2929 HandleShift(shr);
2930}
2931
2932void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2933 HandleShift(shr);
2934}
2935
Alexandre Rames5319def2014-10-23 10:03:10 +01002936void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2938 Primitive::Type field_type = store->InputAt(1)->GetType();
2939 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002940 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002941 case Primitive::kPrimBoolean:
2942 case Primitive::kPrimByte:
2943 case Primitive::kPrimChar:
2944 case Primitive::kPrimShort:
2945 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002946 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002947 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2948 break;
2949
2950 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002951 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002952 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2953 break;
2954
2955 default:
2956 LOG(FATAL) << "Unimplemented local type " << field_type;
2957 }
2958}
2959
2960void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002961 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002962}
2963
2964void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002965 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002966}
2967
2968void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002969 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002970}
2971
Alexandre Rames67555f72014-11-18 10:55:16 +00002972void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002973 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002974}
2975
2976void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002977 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002978}
2979
2980void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002981 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002982}
2983
Alexandre Rames67555f72014-11-18 10:55:16 +00002984void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002985 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002986}
2987
2988void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2990}
2991
2992void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002993 HBasicBlock* block = instruction->GetBlock();
2994 if (block->GetLoopInformation() != nullptr) {
2995 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2996 // The back edge will generate the suspend check.
2997 return;
2998 }
2999 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3000 // The goto will generate the suspend check.
3001 return;
3002 }
3003 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003004}
3005
3006void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3007 temp->SetLocations(nullptr);
3008}
3009
3010void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3011 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003012 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003013}
3014
Alexandre Rames67555f72014-11-18 10:55:16 +00003015void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3016 LocationSummary* locations =
3017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3018 InvokeRuntimeCallingConvention calling_convention;
3019 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3020}
3021
3022void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3023 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003024 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003025 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003026}
3027
3028void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3029 LocationSummary* locations =
3030 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3031 Primitive::Type input_type = conversion->GetInputType();
3032 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003033 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003034 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3035 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3036 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3037 }
3038
Alexandre Rames542361f2015-01-29 16:57:31 +00003039 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003040 locations->SetInAt(0, Location::RequiresFpuRegister());
3041 } else {
3042 locations->SetInAt(0, Location::RequiresRegister());
3043 }
3044
Alexandre Rames542361f2015-01-29 16:57:31 +00003045 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003046 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3047 } else {
3048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3049 }
3050}
3051
3052void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3053 Primitive::Type result_type = conversion->GetResultType();
3054 Primitive::Type input_type = conversion->GetInputType();
3055
3056 DCHECK_NE(input_type, result_type);
3057
Alexandre Rames542361f2015-01-29 16:57:31 +00003058 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003059 int result_size = Primitive::ComponentSize(result_type);
3060 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003061 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003062 Register output = OutputRegister(conversion);
3063 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003064 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3065 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3066 } else if ((result_type == Primitive::kPrimChar) ||
3067 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3068 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003069 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003070 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003071 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003072 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003073 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003074 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003075 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3076 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003077 } else if (Primitive::IsFloatingPointType(result_type) &&
3078 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003079 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3080 } else {
3081 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3082 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003083 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003084}
Alexandre Rames67555f72014-11-18 10:55:16 +00003085
Serban Constantinescu02164b32014-11-13 14:05:07 +00003086void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3087 HandleShift(ushr);
3088}
3089
3090void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3091 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003092}
3093
3094void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3095 HandleBinaryOp(instruction);
3096}
3097
3098void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3099 HandleBinaryOp(instruction);
3100}
3101
Calin Juravleb1498f62015-02-16 13:13:29 +00003102void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3103 // Nothing to do, this should be removed during prepare for register allocator.
3104 UNUSED(instruction);
3105 LOG(FATAL) << "Unreachable";
3106}
3107
3108void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3109 // Nothing to do, this should be removed during prepare for register allocator.
3110 UNUSED(instruction);
3111 LOG(FATAL) << "Unreachable";
3112}
3113
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003114void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3115 DCHECK(codegen_->IsBaseline());
3116 LocationSummary* locations =
3117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3118 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3119}
3120
3121void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3122 DCHECK(codegen_->IsBaseline());
3123 // Will be generated at use site.
3124}
3125
Alexandre Rames67555f72014-11-18 10:55:16 +00003126#undef __
3127#undef QUICK_ENTRY_POINT
3128
Alexandre Rames5319def2014-10-23 10:03:10 +01003129} // namespace arm64
3130} // namespace art