blob: 7fab5cfcaf1baf6f033732766e719bf96d70006d [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:
363 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
364 HBasicBlock* successor)
365 : instruction_(instruction), successor_(successor) {}
366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
368 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100369 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000370 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000372 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800373 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000374 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 if (successor_ == nullptr) {
376 __ B(GetReturnLabel());
377 } else {
378 __ B(arm64_codegen->GetLabelOf(successor_));
379 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 }
381
382 vixl::Label* GetReturnLabel() {
383 DCHECK(successor_ == nullptr);
384 return &return_label_;
385 }
386
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100387 HBasicBlock* GetSuccessor() const {
388 return successor_;
389 }
390
Alexandre Rames9931f312015-06-19 14:47:01 +0100391 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
392
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 private:
394 HSuspendCheck* const instruction_;
395 // If not null, the block to branch to after the suspend check.
396 HBasicBlock* const successor_;
397
398 // If `successor_` is null, the label to branch to after the suspend check.
399 vixl::Label return_label_;
400
401 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
402};
403
Alexandre Rames67555f72014-11-18 10:55:16 +0000404class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
405 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000406 TypeCheckSlowPathARM64(HInstruction* instruction,
407 Location class_to_check,
408 Location object_class,
409 uint32_t dex_pc)
410 : instruction_(instruction),
411 class_to_check_(class_to_check),
412 object_class_(object_class),
413 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000416 LocationSummary* locations = instruction_->GetLocations();
417 DCHECK(instruction_->IsCheckCast()
418 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
419 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
420
Alexandre Rames67555f72014-11-18 10:55:16 +0000421 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000422 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
424 // We're moving two locations to locations that could overlap, so we need a parallel
425 // move resolver.
426 InvokeRuntimeCallingConvention calling_convention;
427 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100428 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
429 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430
431 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000432 arm64_codegen->InvokeRuntime(
433 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 Primitive::Type ret_type = instruction_->GetType();
435 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
436 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800437 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
438 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000439 } else {
440 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000441 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800442 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443 }
444
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000445 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000446 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 }
448
Alexandre Rames9931f312015-06-19 14:47:01 +0100449 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
450
Alexandre Rames67555f72014-11-18 10:55:16 +0000451 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452 HInstruction* const instruction_;
453 const Location class_to_check_;
454 const Location object_class_;
455 uint32_t dex_pc_;
456
Alexandre Rames67555f72014-11-18 10:55:16 +0000457 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
458};
459
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700460class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
462 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
463 : instruction_(instruction) {}
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 __ Bind(GetEntryLabel());
467 SaveLiveRegisters(codegen, instruction_->GetLocations());
468 DCHECK(instruction_->IsDeoptimize());
469 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
470 uint32_t dex_pc = deoptimize->GetDexPc();
471 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
472 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
473 }
474
Alexandre Rames9931f312015-06-19 14:47:01 +0100475 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700477 private:
478 HInstruction* const instruction_;
479 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
480};
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482#undef __
483
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100484Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 Location next_location;
486 if (type == Primitive::kPrimVoid) {
487 LOG(FATAL) << "Unreachable type " << type;
488 }
489
Alexandre Rames542361f2015-01-29 16:57:31 +0000490 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100491 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
492 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000493 } else if (!Primitive::IsFloatingPointType(type) &&
494 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000495 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
496 } else {
497 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000498 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
499 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100500 }
501
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000502 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000503 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 return next_location;
505}
506
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100507Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100508 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100509}
510
Serban Constantinescu579885a2015-02-22 20:51:33 +0000511CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
512 const Arm64InstructionSetFeatures& isa_features,
513 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100514 : CodeGenerator(graph,
515 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000516 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000517 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000518 callee_saved_core_registers.list(),
519 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000520 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100521 block_labels_(nullptr),
522 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000523 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000524 move_resolver_(graph->GetArena(), this),
525 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000526 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000527 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000528}
Alexandre Rames5319def2014-10-23 10:03:10 +0100529
Alexandre Rames67555f72014-11-18 10:55:16 +0000530#undef __
531#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100532
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000533void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
534 // Ensure we emit the literal pool.
535 __ FinalizeCode();
536 CodeGenerator::Finalize(allocator);
537}
538
Zheng Xuad4450e2015-04-17 18:48:56 +0800539void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
540 // Note: There are 6 kinds of moves:
541 // 1. constant -> GPR/FPR (non-cycle)
542 // 2. constant -> stack (non-cycle)
543 // 3. GPR/FPR -> GPR/FPR
544 // 4. GPR/FPR -> stack
545 // 5. stack -> GPR/FPR
546 // 6. stack -> stack (non-cycle)
547 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
548 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
549 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
550 // dependency.
551 vixl_temps_.Open(GetVIXLAssembler());
552}
553
554void ParallelMoveResolverARM64::FinishEmitNativeCode() {
555 vixl_temps_.Close();
556}
557
558Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
559 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
560 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
561 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
562 Location scratch = GetScratchLocation(kind);
563 if (!scratch.Equals(Location::NoLocation())) {
564 return scratch;
565 }
566 // Allocate from VIXL temp registers.
567 if (kind == Location::kRegister) {
568 scratch = LocationFrom(vixl_temps_.AcquireX());
569 } else {
570 DCHECK(kind == Location::kFpuRegister);
571 scratch = LocationFrom(vixl_temps_.AcquireD());
572 }
573 AddScratchLocation(scratch);
574 return scratch;
575}
576
577void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
578 if (loc.IsRegister()) {
579 vixl_temps_.Release(XRegisterFrom(loc));
580 } else {
581 DCHECK(loc.IsFpuRegister());
582 vixl_temps_.Release(DRegisterFrom(loc));
583 }
584 RemoveScratchLocation(loc);
585}
586
Alexandre Rames3e69f162014-12-10 10:36:50 +0000587void ParallelMoveResolverARM64::EmitMove(size_t index) {
588 MoveOperands* move = moves_.Get(index);
589 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
590}
591
Alexandre Rames5319def2014-10-23 10:03:10 +0100592void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100593 MacroAssembler* masm = GetVIXLAssembler();
594 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000595 __ Bind(&frame_entry_label_);
596
Serban Constantinescu02164b32014-11-13 14:05:07 +0000597 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
598 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100599 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000600 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000601 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000602 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000603 __ Ldr(wzr, MemOperand(temp, 0));
604 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000605 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100606
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000607 if (!HasEmptyFrame()) {
608 int frame_size = GetFrameSize();
609 // Stack layout:
610 // sp[frame_size - 8] : lr.
611 // ... : other preserved core registers.
612 // ... : other preserved fp registers.
613 // ... : reserved frame space.
614 // sp[0] : current method.
615 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100616 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800617 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
618 frame_size - GetCoreSpillSize());
619 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
620 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000621 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100622}
623
624void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100625 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100626 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000627 if (!HasEmptyFrame()) {
628 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800629 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
630 frame_size - FrameEntrySpillSize());
631 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
632 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000633 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100634 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000635 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100636 __ Ret();
637 GetAssembler()->cfi().RestoreState();
638 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100639}
640
Zheng Xuda403092015-04-24 17:35:39 +0800641vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
642 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
643 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
644 core_spill_mask_);
645}
646
647vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
648 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
649 GetNumberOfFloatingPointRegisters()));
650 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
651 fpu_spill_mask_);
652}
653
Alexandre Rames5319def2014-10-23 10:03:10 +0100654void CodeGeneratorARM64::Bind(HBasicBlock* block) {
655 __ Bind(GetLabelOf(block));
656}
657
Alexandre Rames5319def2014-10-23 10:03:10 +0100658void CodeGeneratorARM64::Move(HInstruction* instruction,
659 Location location,
660 HInstruction* move_for) {
661 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100662 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000663 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100664
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100665 if (instruction->IsFakeString()) {
666 // The fake string is an alias for null.
667 DCHECK(IsBaseline());
668 instruction = locations->Out().GetConstant();
669 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
670 }
671
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100672 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700673 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100674 } else if (locations != nullptr && locations->Out().Equals(location)) {
675 return;
676 } else if (instruction->IsIntConstant()
677 || instruction->IsLongConstant()
678 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000679 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100680 if (location.IsRegister()) {
681 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000682 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100683 (instruction->IsLongConstant() && dst.Is64Bits()));
684 __ Mov(dst, value);
685 } else {
686 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000687 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000688 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
689 ? temps.AcquireW()
690 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 __ Mov(temp, value);
692 __ Str(temp, StackOperandFrom(location));
693 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000694 } else if (instruction->IsTemporary()) {
695 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000696 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100697 } else if (instruction->IsLoadLocal()) {
698 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000699 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000700 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000701 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000702 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100703 }
704
705 } else {
706 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000707 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100708 }
709}
710
Alexandre Rames5319def2014-10-23 10:03:10 +0100711Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
712 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000713
Alexandre Rames5319def2014-10-23 10:03:10 +0100714 switch (type) {
715 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000716 case Primitive::kPrimInt:
717 case Primitive::kPrimFloat:
718 return Location::StackSlot(GetStackSlot(load->GetLocal()));
719
720 case Primitive::kPrimLong:
721 case Primitive::kPrimDouble:
722 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
723
Alexandre Rames5319def2014-10-23 10:03:10 +0100724 case Primitive::kPrimBoolean:
725 case Primitive::kPrimByte:
726 case Primitive::kPrimChar:
727 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100728 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100729 LOG(FATAL) << "Unexpected type " << type;
730 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000731
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 LOG(FATAL) << "Unreachable";
733 return Location::NoLocation();
734}
735
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100736void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000737 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100738 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000739 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100740 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100741 if (value_can_be_null) {
742 __ Cbz(value, &done);
743 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100744 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
745 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000746 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100747 if (value_can_be_null) {
748 __ Bind(&done);
749 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100750}
751
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000752void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
753 // Blocked core registers:
754 // lr : Runtime reserved.
755 // tr : Runtime reserved.
756 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
757 // ip1 : VIXL core temp.
758 // ip0 : VIXL core temp.
759 //
760 // Blocked fp registers:
761 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100762 CPURegList reserved_core_registers = vixl_reserved_core_registers;
763 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100764 while (!reserved_core_registers.IsEmpty()) {
765 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
766 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000767
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000768 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800769 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000770 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
771 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000772
773 if (is_baseline) {
774 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
775 while (!reserved_core_baseline_registers.IsEmpty()) {
776 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
777 }
778
779 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
780 while (!reserved_fp_baseline_registers.IsEmpty()) {
781 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
782 }
783 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100784}
785
786Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
787 if (type == Primitive::kPrimVoid) {
788 LOG(FATAL) << "Unreachable type " << type;
789 }
790
Alexandre Rames542361f2015-01-29 16:57:31 +0000791 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000792 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
793 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100794 return Location::FpuRegisterLocation(reg);
795 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000796 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
797 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100798 return Location::RegisterLocation(reg);
799 }
800}
801
Alexandre Rames3e69f162014-12-10 10:36:50 +0000802size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
803 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
804 __ Str(reg, MemOperand(sp, stack_index));
805 return kArm64WordSize;
806}
807
808size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
809 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
810 __ Ldr(reg, MemOperand(sp, stack_index));
811 return kArm64WordSize;
812}
813
814size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
815 FPRegister reg = FPRegister(reg_id, kDRegSize);
816 __ Str(reg, MemOperand(sp, stack_index));
817 return kArm64WordSize;
818}
819
820size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
821 FPRegister reg = FPRegister(reg_id, kDRegSize);
822 __ Ldr(reg, MemOperand(sp, stack_index));
823 return kArm64WordSize;
824}
825
Alexandre Rames5319def2014-10-23 10:03:10 +0100826void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100827 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100828}
829
830void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100831 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100832}
833
Alexandre Rames67555f72014-11-18 10:55:16 +0000834void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000835 if (constant->IsIntConstant()) {
836 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
837 } else if (constant->IsLongConstant()) {
838 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
839 } else if (constant->IsNullConstant()) {
840 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000841 } else if (constant->IsFloatConstant()) {
842 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
843 } else {
844 DCHECK(constant->IsDoubleConstant());
845 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
846 }
847}
848
Alexandre Rames3e69f162014-12-10 10:36:50 +0000849
850static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
851 DCHECK(constant.IsConstant());
852 HConstant* cst = constant.GetConstant();
853 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000854 // Null is mapped to a core W register, which we associate with kPrimInt.
855 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000856 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
857 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
858 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
859}
860
861void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000862 if (source.Equals(destination)) {
863 return;
864 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000865
866 // A valid move can always be inferred from the destination and source
867 // locations. When moving from and to a register, the argument type can be
868 // used to generate 32bit instead of 64bit moves. In debug mode we also
869 // checks the coherency of the locations and the type.
870 bool unspecified_type = (type == Primitive::kPrimVoid);
871
872 if (destination.IsRegister() || destination.IsFpuRegister()) {
873 if (unspecified_type) {
874 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
875 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000876 (src_cst != nullptr && (src_cst->IsIntConstant()
877 || src_cst->IsFloatConstant()
878 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000879 // For stack slots and 32bit constants, a 64bit type is appropriate.
880 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000881 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000882 // If the source is a double stack slot or a 64bit constant, a 64bit
883 // type is appropriate. Else the source is a register, and since the
884 // type has not been specified, we chose a 64bit type to force a 64bit
885 // move.
886 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000887 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000888 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000889 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
890 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000891 CPURegister dst = CPURegisterFrom(destination, type);
892 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
893 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
894 __ Ldr(dst, StackOperandFrom(source));
895 } else if (source.IsConstant()) {
896 DCHECK(CoherentConstantAndType(source, type));
897 MoveConstant(dst, source.GetConstant());
898 } else {
899 if (destination.IsRegister()) {
900 __ Mov(Register(dst), RegisterFrom(source, type));
901 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800902 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000903 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
904 }
905 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000906 } else { // The destination is not a register. It must be a stack slot.
907 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
908 if (source.IsRegister() || source.IsFpuRegister()) {
909 if (unspecified_type) {
910 if (source.IsRegister()) {
911 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
912 } else {
913 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
914 }
915 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000916 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
917 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000918 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
919 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100920 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000921 UseScratchRegisterScope temps(GetVIXLAssembler());
922 HConstant* src_cst = source.GetConstant();
923 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000924 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000925 temp = temps.AcquireW();
926 } else if (src_cst->IsLongConstant()) {
927 temp = temps.AcquireX();
928 } else if (src_cst->IsFloatConstant()) {
929 temp = temps.AcquireS();
930 } else {
931 DCHECK(src_cst->IsDoubleConstant());
932 temp = temps.AcquireD();
933 }
934 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000935 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000936 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000937 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000938 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000939 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000940 // There is generally less pressure on FP registers.
941 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000942 __ Ldr(temp, StackOperandFrom(source));
943 __ Str(temp, StackOperandFrom(destination));
944 }
945 }
946}
947
948void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000949 CPURegister dst,
950 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000951 switch (type) {
952 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000953 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000954 break;
955 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000956 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000957 break;
958 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000959 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000960 break;
961 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000962 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000963 break;
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot:
966 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000967 case Primitive::kPrimFloat:
968 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000969 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000970 __ Ldr(dst, src);
971 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000972 case Primitive::kPrimVoid:
973 LOG(FATAL) << "Unreachable type " << type;
974 }
975}
976
Calin Juravle77520bc2015-01-12 18:45:46 +0000977void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000978 CPURegister dst,
979 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100980 MacroAssembler* masm = GetVIXLAssembler();
981 BlockPoolsScope block_pools(masm);
982 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000983 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000984 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000985
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000986 DCHECK(!src.IsPreIndex());
987 DCHECK(!src.IsPostIndex());
988
989 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800990 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000991 MemOperand base = MemOperand(temp_base);
992 switch (type) {
993 case Primitive::kPrimBoolean:
994 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000995 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000996 break;
997 case Primitive::kPrimByte:
998 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000999 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001000 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1001 break;
1002 case Primitive::kPrimChar:
1003 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001004 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001005 break;
1006 case Primitive::kPrimShort:
1007 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001008 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1010 break;
1011 case Primitive::kPrimInt:
1012 case Primitive::kPrimNot:
1013 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001014 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001015 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001016 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001017 break;
1018 case Primitive::kPrimFloat:
1019 case Primitive::kPrimDouble: {
1020 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001021 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001022
1023 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1024 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001025 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001026 __ Fmov(FPRegister(dst), temp);
1027 break;
1028 }
1029 case Primitive::kPrimVoid:
1030 LOG(FATAL) << "Unreachable type " << type;
1031 }
1032}
1033
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001034void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001035 CPURegister src,
1036 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001037 switch (type) {
1038 case Primitive::kPrimBoolean:
1039 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001040 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001041 break;
1042 case Primitive::kPrimChar:
1043 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001044 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001045 break;
1046 case Primitive::kPrimInt:
1047 case Primitive::kPrimNot:
1048 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001049 case Primitive::kPrimFloat:
1050 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001051 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001053 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001054 case Primitive::kPrimVoid:
1055 LOG(FATAL) << "Unreachable type " << type;
1056 }
1057}
1058
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001059void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1060 CPURegister src,
1061 const MemOperand& dst) {
1062 UseScratchRegisterScope temps(GetVIXLAssembler());
1063 Register temp_base = temps.AcquireX();
1064
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001065 DCHECK(!dst.IsPreIndex());
1066 DCHECK(!dst.IsPostIndex());
1067
1068 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001069 Operand op = OperandFromMemOperand(dst);
1070 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001071 MemOperand base = MemOperand(temp_base);
1072 switch (type) {
1073 case Primitive::kPrimBoolean:
1074 case Primitive::kPrimByte:
1075 __ Stlrb(Register(src), base);
1076 break;
1077 case Primitive::kPrimChar:
1078 case Primitive::kPrimShort:
1079 __ Stlrh(Register(src), base);
1080 break;
1081 case Primitive::kPrimInt:
1082 case Primitive::kPrimNot:
1083 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001084 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001085 __ Stlr(Register(src), base);
1086 break;
1087 case Primitive::kPrimFloat:
1088 case Primitive::kPrimDouble: {
1089 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001090 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001091
1092 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1093 __ Fmov(temp, FPRegister(src));
1094 __ Stlr(temp, base);
1095 break;
1096 }
1097 case Primitive::kPrimVoid:
1098 LOG(FATAL) << "Unreachable type " << type;
1099 }
1100}
1101
Alexandre Rames67555f72014-11-18 10:55:16 +00001102void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1103 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001104 uint32_t dex_pc,
1105 SlowPathCode* slow_path) {
Alexandre Rames8158f282015-08-07 10:26:17 +01001106 // Ensure that the call kind indication given to the register allocator is
1107 // coherent with the runtime call generated.
1108 if (slow_path == nullptr) {
1109 DCHECK(instruction->GetLocations()->WillCall());
1110 } else {
1111 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal());
1112 }
1113
Alexandre Ramesd921d642015-04-16 15:07:16 +01001114 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001115 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1116 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001117 RecordPcInfo(instruction, dex_pc, slow_path);
1118 DCHECK(instruction->IsSuspendCheck()
1119 || instruction->IsBoundsCheck()
1120 || instruction->IsNullCheck()
1121 || instruction->IsDivZeroCheck()
1122 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001123}
1124
1125void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1126 vixl::Register class_reg) {
1127 UseScratchRegisterScope temps(GetVIXLAssembler());
1128 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001129 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001130 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001131
Serban Constantinescu02164b32014-11-13 14:05:07 +00001132 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001133 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001134 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1135 __ Add(temp, class_reg, status_offset);
1136 __ Ldar(temp, HeapOperand(temp));
1137 __ Cmp(temp, mirror::Class::kStatusInitialized);
1138 __ B(lt, slow_path->GetEntryLabel());
1139 } else {
1140 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1141 __ Cmp(temp, mirror::Class::kStatusInitialized);
1142 __ B(lt, slow_path->GetEntryLabel());
1143 __ Dmb(InnerShareable, BarrierReads);
1144 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001145 __ Bind(slow_path->GetExitLabel());
1146}
Alexandre Rames5319def2014-10-23 10:03:10 +01001147
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001148void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1149 BarrierType type = BarrierAll;
1150
1151 switch (kind) {
1152 case MemBarrierKind::kAnyAny:
1153 case MemBarrierKind::kAnyStore: {
1154 type = BarrierAll;
1155 break;
1156 }
1157 case MemBarrierKind::kLoadAny: {
1158 type = BarrierReads;
1159 break;
1160 }
1161 case MemBarrierKind::kStoreStore: {
1162 type = BarrierWrites;
1163 break;
1164 }
1165 default:
1166 LOG(FATAL) << "Unexpected memory barrier " << kind;
1167 }
1168 __ Dmb(InnerShareable, type);
1169}
1170
Serban Constantinescu02164b32014-11-13 14:05:07 +00001171void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1172 HBasicBlock* successor) {
1173 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001174 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1175 if (slow_path == nullptr) {
1176 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1177 instruction->SetSlowPath(slow_path);
1178 codegen_->AddSlowPath(slow_path);
1179 if (successor != nullptr) {
1180 DCHECK(successor->IsLoopHeader());
1181 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1182 }
1183 } else {
1184 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1185 }
1186
Serban Constantinescu02164b32014-11-13 14:05:07 +00001187 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1188 Register temp = temps.AcquireW();
1189
1190 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1191 if (successor == nullptr) {
1192 __ Cbnz(temp, slow_path->GetEntryLabel());
1193 __ Bind(slow_path->GetReturnLabel());
1194 } else {
1195 __ Cbz(temp, codegen_->GetLabelOf(successor));
1196 __ B(slow_path->GetEntryLabel());
1197 // slow_path will return to GetLabelOf(successor).
1198 }
1199}
1200
Alexandre Rames5319def2014-10-23 10:03:10 +01001201InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1202 CodeGeneratorARM64* codegen)
1203 : HGraphVisitor(graph),
1204 assembler_(codegen->GetAssembler()),
1205 codegen_(codegen) {}
1206
1207#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001208 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001209
1210#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1211
1212enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001213 // Using a base helps identify when we hit such breakpoints.
1214 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001215#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1216 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1217#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1218};
1219
1220#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1221 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001222 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001223 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1224 } \
1225 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1226 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1227 locations->SetOut(Location::Any()); \
1228 }
1229 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1230#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1231
1232#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001233#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001234
Alexandre Rames67555f72014-11-18 10:55:16 +00001235void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001236 DCHECK_EQ(instr->InputCount(), 2U);
1237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1238 Primitive::Type type = instr->GetResultType();
1239 switch (type) {
1240 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001241 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001242 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001243 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001244 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001245 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001246
1247 case Primitive::kPrimFloat:
1248 case Primitive::kPrimDouble:
1249 locations->SetInAt(0, Location::RequiresFpuRegister());
1250 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001251 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001252 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001253
Alexandre Rames5319def2014-10-23 10:03:10 +01001254 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001255 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001256 }
1257}
1258
Alexandre Rames09a99962015-04-15 11:47:56 +01001259void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1260 LocationSummary* locations =
1261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1262 locations->SetInAt(0, Location::RequiresRegister());
1263 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1264 locations->SetOut(Location::RequiresFpuRegister());
1265 } else {
1266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1267 }
1268}
1269
1270void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1271 const FieldInfo& field_info) {
1272 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001273 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001274 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001275
1276 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1277 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1278
1279 if (field_info.IsVolatile()) {
1280 if (use_acquire_release) {
1281 // NB: LoadAcquire will record the pc info if needed.
1282 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1283 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001284 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001285 codegen_->MaybeRecordImplicitNullCheck(instruction);
1286 // For IRIW sequential consistency kLoadAny is not sufficient.
1287 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1288 }
1289 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001290 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001291 codegen_->MaybeRecordImplicitNullCheck(instruction);
1292 }
Roland Levillain4d027112015-07-01 15:41:14 +01001293
1294 if (field_type == Primitive::kPrimNot) {
1295 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1296 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001297}
1298
1299void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1300 LocationSummary* locations =
1301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1302 locations->SetInAt(0, Location::RequiresRegister());
1303 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1304 locations->SetInAt(1, Location::RequiresFpuRegister());
1305 } else {
1306 locations->SetInAt(1, Location::RequiresRegister());
1307 }
1308}
1309
1310void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001311 const FieldInfo& field_info,
1312 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001313 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001314 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001315
1316 Register obj = InputRegisterAt(instruction, 0);
1317 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001318 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001319 Offset offset = field_info.GetFieldOffset();
1320 Primitive::Type field_type = field_info.GetFieldType();
1321 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1322
Roland Levillain4d027112015-07-01 15:41:14 +01001323 {
1324 // We use a block to end the scratch scope before the write barrier, thus
1325 // freeing the temporary registers so they can be used in `MarkGCCard`.
1326 UseScratchRegisterScope temps(GetVIXLAssembler());
1327
1328 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1329 DCHECK(value.IsW());
1330 Register temp = temps.AcquireW();
1331 __ Mov(temp, value.W());
1332 GetAssembler()->PoisonHeapReference(temp.W());
1333 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001334 }
Roland Levillain4d027112015-07-01 15:41:14 +01001335
1336 if (field_info.IsVolatile()) {
1337 if (use_acquire_release) {
1338 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1339 codegen_->MaybeRecordImplicitNullCheck(instruction);
1340 } else {
1341 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1342 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1343 codegen_->MaybeRecordImplicitNullCheck(instruction);
1344 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1345 }
1346 } else {
1347 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1348 codegen_->MaybeRecordImplicitNullCheck(instruction);
1349 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001350 }
1351
1352 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001353 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001354 }
1355}
1356
Alexandre Rames67555f72014-11-18 10:55:16 +00001357void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001358 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001359
1360 switch (type) {
1361 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001362 case Primitive::kPrimLong: {
1363 Register dst = OutputRegister(instr);
1364 Register lhs = InputRegisterAt(instr, 0);
1365 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001366 if (instr->IsAdd()) {
1367 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001368 } else if (instr->IsAnd()) {
1369 __ And(dst, lhs, rhs);
1370 } else if (instr->IsOr()) {
1371 __ Orr(dst, lhs, rhs);
1372 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001373 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001374 } else {
1375 DCHECK(instr->IsXor());
1376 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 }
1378 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001379 }
1380 case Primitive::kPrimFloat:
1381 case Primitive::kPrimDouble: {
1382 FPRegister dst = OutputFPRegister(instr);
1383 FPRegister lhs = InputFPRegisterAt(instr, 0);
1384 FPRegister rhs = InputFPRegisterAt(instr, 1);
1385 if (instr->IsAdd()) {
1386 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001387 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001388 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001389 } else {
1390 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001391 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001392 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001393 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001394 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001395 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001396 }
1397}
1398
Serban Constantinescu02164b32014-11-13 14:05:07 +00001399void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1400 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1401
1402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1403 Primitive::Type type = instr->GetResultType();
1404 switch (type) {
1405 case Primitive::kPrimInt:
1406 case Primitive::kPrimLong: {
1407 locations->SetInAt(0, Location::RequiresRegister());
1408 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1409 locations->SetOut(Location::RequiresRegister());
1410 break;
1411 }
1412 default:
1413 LOG(FATAL) << "Unexpected shift type " << type;
1414 }
1415}
1416
1417void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1418 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1419
1420 Primitive::Type type = instr->GetType();
1421 switch (type) {
1422 case Primitive::kPrimInt:
1423 case Primitive::kPrimLong: {
1424 Register dst = OutputRegister(instr);
1425 Register lhs = InputRegisterAt(instr, 0);
1426 Operand rhs = InputOperandAt(instr, 1);
1427 if (rhs.IsImmediate()) {
1428 uint32_t shift_value = (type == Primitive::kPrimInt)
1429 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1430 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1431 if (instr->IsShl()) {
1432 __ Lsl(dst, lhs, shift_value);
1433 } else if (instr->IsShr()) {
1434 __ Asr(dst, lhs, shift_value);
1435 } else {
1436 __ Lsr(dst, lhs, shift_value);
1437 }
1438 } else {
1439 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1440
1441 if (instr->IsShl()) {
1442 __ Lsl(dst, lhs, rhs_reg);
1443 } else if (instr->IsShr()) {
1444 __ Asr(dst, lhs, rhs_reg);
1445 } else {
1446 __ Lsr(dst, lhs, rhs_reg);
1447 }
1448 }
1449 break;
1450 }
1451 default:
1452 LOG(FATAL) << "Unexpected shift operation type " << type;
1453 }
1454}
1455
Alexandre Rames5319def2014-10-23 10:03:10 +01001456void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001457 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001458}
1459
1460void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001461 HandleBinaryOp(instruction);
1462}
1463
1464void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1465 HandleBinaryOp(instruction);
1466}
1467
1468void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1469 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001470}
1471
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001472void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1473 LocationSummary* locations =
1474 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1475 locations->SetInAt(0, Location::RequiresRegister());
1476 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001477 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1478 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1479 } else {
1480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1481 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001482}
1483
1484void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1485 LocationSummary* locations = instruction->GetLocations();
1486 Primitive::Type type = instruction->GetType();
1487 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001488 Location index = locations->InAt(1);
1489 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001490 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001491 MacroAssembler* masm = GetVIXLAssembler();
1492 UseScratchRegisterScope temps(masm);
1493 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001494
1495 if (index.IsConstant()) {
1496 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001497 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001498 } else {
1499 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001500 __ Add(temp, obj, offset);
1501 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001502 }
1503
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001505 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001506
1507 if (type == Primitive::kPrimNot) {
1508 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1509 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001510}
1511
Alexandre Rames5319def2014-10-23 10:03:10 +01001512void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1514 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001515 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001516}
1517
1518void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001519 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001520 __ Ldr(OutputRegister(instruction),
1521 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001522 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001523}
1524
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001526 if (instruction->NeedsTypeCheck()) {
1527 LocationSummary* locations =
1528 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001529 InvokeRuntimeCallingConvention calling_convention;
1530 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1531 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1532 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1533 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001534 LocationSummary* locations =
1535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001536 locations->SetInAt(0, Location::RequiresRegister());
1537 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001538 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1539 locations->SetInAt(2, Location::RequiresFpuRegister());
1540 } else {
1541 locations->SetInAt(2, Location::RequiresRegister());
1542 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001543 }
1544}
1545
1546void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1547 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001548 LocationSummary* locations = instruction->GetLocations();
1549 bool needs_runtime_call = locations->WillCall();
1550
1551 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001552 // Note: if heap poisoning is enabled, pAputObject takes cares
1553 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001554 codegen_->InvokeRuntime(
1555 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001556 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001558 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001559 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001560 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001561 Location index = locations->InAt(1);
1562 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001563 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001564 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001565 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001566 {
1567 // We use a block to end the scratch scope before the write barrier, thus
1568 // freeing the temporary registers so they can be used in `MarkGCCard`.
1569 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001570
Roland Levillain4d027112015-07-01 15:41:14 +01001571 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1572 DCHECK(value.IsW());
1573 Register temp = temps.AcquireW();
1574 __ Mov(temp, value.W());
1575 GetAssembler()->PoisonHeapReference(temp.W());
1576 source = temp;
1577 }
1578
Alexandre Rames97833a02015-04-16 15:07:12 +01001579 if (index.IsConstant()) {
1580 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1581 destination = HeapOperand(obj, offset);
1582 } else {
1583 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001584 __ Add(temp, obj, offset);
1585 destination = HeapOperand(temp,
1586 XRegisterFrom(index),
1587 LSL,
1588 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001589 }
1590
Roland Levillain4d027112015-07-01 15:41:14 +01001591 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001592 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001594 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001595 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001596 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001597 }
1598}
1599
Alexandre Rames67555f72014-11-18 10:55:16 +00001600void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1601 LocationSummary* locations =
1602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1603 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001604 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001605 if (instruction->HasUses()) {
1606 locations->SetOut(Location::SameAsFirstInput());
1607 }
1608}
1609
1610void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001611 LocationSummary* locations = instruction->GetLocations();
1612 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1613 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001614 codegen_->AddSlowPath(slow_path);
1615
1616 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1617 __ B(slow_path->GetEntryLabel(), hs);
1618}
1619
1620void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1621 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1622 instruction, LocationSummary::kCallOnSlowPath);
1623 locations->SetInAt(0, Location::RequiresRegister());
1624 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001625 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001626}
1627
1628void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001629 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001630 Register obj = InputRegisterAt(instruction, 0);;
1631 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001632 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001633
Alexandre Rames3e69f162014-12-10 10:36:50 +00001634 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1635 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001636 codegen_->AddSlowPath(slow_path);
1637
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001638 // Avoid null check if we know obj is not null.
1639 if (instruction->MustDoNullCheck()) {
1640 __ Cbz(obj, slow_path->GetExitLabel());
1641 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001642 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001643 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001644 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001645 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001646 // The checkcast succeeds if the classes are equal (fast path).
1647 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001648 __ B(ne, slow_path->GetEntryLabel());
1649 __ Bind(slow_path->GetExitLabel());
1650}
1651
1652void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1653 LocationSummary* locations =
1654 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1655 locations->SetInAt(0, Location::RequiresRegister());
1656 if (check->HasUses()) {
1657 locations->SetOut(Location::SameAsFirstInput());
1658 }
1659}
1660
1661void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1662 // We assume the class is not null.
1663 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1664 check->GetLoadClass(), check, check->GetDexPc(), true);
1665 codegen_->AddSlowPath(slow_path);
1666 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1667}
1668
Roland Levillain7f63c522015-07-13 15:54:55 +00001669static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1670 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1671 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1672}
1673
Serban Constantinescu02164b32014-11-13 14:05:07 +00001674void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001675 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001676 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1677 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001678 switch (in_type) {
1679 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001680 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001681 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1683 break;
1684 }
1685 case Primitive::kPrimFloat:
1686 case Primitive::kPrimDouble: {
1687 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001688 locations->SetInAt(1,
1689 IsFloatingPointZeroConstant(compare->InputAt(1))
1690 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1691 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001692 locations->SetOut(Location::RequiresRegister());
1693 break;
1694 }
1695 default:
1696 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1697 }
1698}
1699
1700void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1701 Primitive::Type in_type = compare->InputAt(0)->GetType();
1702
1703 // 0 if: left == right
1704 // 1 if: left > right
1705 // -1 if: left < right
1706 switch (in_type) {
1707 case Primitive::kPrimLong: {
1708 Register result = OutputRegister(compare);
1709 Register left = InputRegisterAt(compare, 0);
1710 Operand right = InputOperandAt(compare, 1);
1711
1712 __ Cmp(left, right);
1713 __ Cset(result, ne);
1714 __ Cneg(result, result, lt);
1715 break;
1716 }
1717 case Primitive::kPrimFloat:
1718 case Primitive::kPrimDouble: {
1719 Register result = OutputRegister(compare);
1720 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001721 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001722 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1723 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001724 __ Fcmp(left, 0.0);
1725 } else {
1726 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1727 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001728 if (compare->IsGtBias()) {
1729 __ Cset(result, ne);
1730 } else {
1731 __ Csetm(result, ne);
1732 }
1733 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001734 break;
1735 }
1736 default:
1737 LOG(FATAL) << "Unimplemented compare type " << in_type;
1738 }
1739}
1740
1741void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1742 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001743
1744 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1745 locations->SetInAt(0, Location::RequiresFpuRegister());
1746 locations->SetInAt(1,
1747 IsFloatingPointZeroConstant(instruction->InputAt(1))
1748 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1749 : Location::RequiresFpuRegister());
1750 } else {
1751 // Integer cases.
1752 locations->SetInAt(0, Location::RequiresRegister());
1753 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1754 }
1755
Alexandre Rames5319def2014-10-23 10:03:10 +01001756 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001757 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001758 }
1759}
1760
1761void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1762 if (!instruction->NeedsMaterialization()) {
1763 return;
1764 }
1765
1766 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001767 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001768 IfCondition if_cond = instruction->GetCondition();
1769 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001770
Roland Levillain7f63c522015-07-13 15:54:55 +00001771 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1772 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1773 if (locations->InAt(1).IsConstant()) {
1774 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1775 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1776 __ Fcmp(lhs, 0.0);
1777 } else {
1778 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1779 }
1780 __ Cset(res, arm64_cond);
1781 if (instruction->IsFPConditionTrueIfNaN()) {
1782 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1783 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1784 } else if (instruction->IsFPConditionFalseIfNaN()) {
1785 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1786 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1787 }
1788 } else {
1789 // Integer cases.
1790 Register lhs = InputRegisterAt(instruction, 0);
1791 Operand rhs = InputOperandAt(instruction, 1);
1792 __ Cmp(lhs, rhs);
1793 __ Cset(res, arm64_cond);
1794 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001795}
1796
1797#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1798 M(Equal) \
1799 M(NotEqual) \
1800 M(LessThan) \
1801 M(LessThanOrEqual) \
1802 M(GreaterThan) \
1803 M(GreaterThanOrEqual)
1804#define DEFINE_CONDITION_VISITORS(Name) \
1805void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1806void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1807FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001808#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001809#undef FOR_EACH_CONDITION_INSTRUCTION
1810
Zheng Xuc6667102015-05-15 16:08:45 +08001811void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1812 DCHECK(instruction->IsDiv() || instruction->IsRem());
1813
1814 LocationSummary* locations = instruction->GetLocations();
1815 Location second = locations->InAt(1);
1816 DCHECK(second.IsConstant());
1817
1818 Register out = OutputRegister(instruction);
1819 Register dividend = InputRegisterAt(instruction, 0);
1820 int64_t imm = Int64FromConstant(second.GetConstant());
1821 DCHECK(imm == 1 || imm == -1);
1822
1823 if (instruction->IsRem()) {
1824 __ Mov(out, 0);
1825 } else {
1826 if (imm == 1) {
1827 __ Mov(out, dividend);
1828 } else {
1829 __ Neg(out, dividend);
1830 }
1831 }
1832}
1833
1834void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1835 DCHECK(instruction->IsDiv() || instruction->IsRem());
1836
1837 LocationSummary* locations = instruction->GetLocations();
1838 Location second = locations->InAt(1);
1839 DCHECK(second.IsConstant());
1840
1841 Register out = OutputRegister(instruction);
1842 Register dividend = InputRegisterAt(instruction, 0);
1843 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001844 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001845 DCHECK(IsPowerOfTwo(abs_imm));
1846 int ctz_imm = CTZ(abs_imm);
1847
1848 UseScratchRegisterScope temps(GetVIXLAssembler());
1849 Register temp = temps.AcquireSameSizeAs(out);
1850
1851 if (instruction->IsDiv()) {
1852 __ Add(temp, dividend, abs_imm - 1);
1853 __ Cmp(dividend, 0);
1854 __ Csel(out, temp, dividend, lt);
1855 if (imm > 0) {
1856 __ Asr(out, out, ctz_imm);
1857 } else {
1858 __ Neg(out, Operand(out, ASR, ctz_imm));
1859 }
1860 } else {
1861 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1862 __ Asr(temp, dividend, bits - 1);
1863 __ Lsr(temp, temp, bits - ctz_imm);
1864 __ Add(out, dividend, temp);
1865 __ And(out, out, abs_imm - 1);
1866 __ Sub(out, out, temp);
1867 }
1868}
1869
1870void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1871 DCHECK(instruction->IsDiv() || instruction->IsRem());
1872
1873 LocationSummary* locations = instruction->GetLocations();
1874 Location second = locations->InAt(1);
1875 DCHECK(second.IsConstant());
1876
1877 Register out = OutputRegister(instruction);
1878 Register dividend = InputRegisterAt(instruction, 0);
1879 int64_t imm = Int64FromConstant(second.GetConstant());
1880
1881 Primitive::Type type = instruction->GetResultType();
1882 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1883
1884 int64_t magic;
1885 int shift;
1886 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1887
1888 UseScratchRegisterScope temps(GetVIXLAssembler());
1889 Register temp = temps.AcquireSameSizeAs(out);
1890
1891 // temp = get_high(dividend * magic)
1892 __ Mov(temp, magic);
1893 if (type == Primitive::kPrimLong) {
1894 __ Smulh(temp, dividend, temp);
1895 } else {
1896 __ Smull(temp.X(), dividend, temp);
1897 __ Lsr(temp.X(), temp.X(), 32);
1898 }
1899
1900 if (imm > 0 && magic < 0) {
1901 __ Add(temp, temp, dividend);
1902 } else if (imm < 0 && magic > 0) {
1903 __ Sub(temp, temp, dividend);
1904 }
1905
1906 if (shift != 0) {
1907 __ Asr(temp, temp, shift);
1908 }
1909
1910 if (instruction->IsDiv()) {
1911 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1912 } else {
1913 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1914 // TODO: Strength reduction for msub.
1915 Register temp_imm = temps.AcquireSameSizeAs(out);
1916 __ Mov(temp_imm, imm);
1917 __ Msub(out, temp, temp_imm, dividend);
1918 }
1919}
1920
1921void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1922 DCHECK(instruction->IsDiv() || instruction->IsRem());
1923 Primitive::Type type = instruction->GetResultType();
1924 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1925
1926 LocationSummary* locations = instruction->GetLocations();
1927 Register out = OutputRegister(instruction);
1928 Location second = locations->InAt(1);
1929
1930 if (second.IsConstant()) {
1931 int64_t imm = Int64FromConstant(second.GetConstant());
1932
1933 if (imm == 0) {
1934 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1935 } else if (imm == 1 || imm == -1) {
1936 DivRemOneOrMinusOne(instruction);
1937 } else if (IsPowerOfTwo(std::abs(imm))) {
1938 DivRemByPowerOfTwo(instruction);
1939 } else {
1940 DCHECK(imm <= -2 || imm >= 2);
1941 GenerateDivRemWithAnyConstant(instruction);
1942 }
1943 } else {
1944 Register dividend = InputRegisterAt(instruction, 0);
1945 Register divisor = InputRegisterAt(instruction, 1);
1946 if (instruction->IsDiv()) {
1947 __ Sdiv(out, dividend, divisor);
1948 } else {
1949 UseScratchRegisterScope temps(GetVIXLAssembler());
1950 Register temp = temps.AcquireSameSizeAs(out);
1951 __ Sdiv(temp, dividend, divisor);
1952 __ Msub(out, temp, divisor, dividend);
1953 }
1954 }
1955}
1956
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001957void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1958 LocationSummary* locations =
1959 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1960 switch (div->GetResultType()) {
1961 case Primitive::kPrimInt:
1962 case Primitive::kPrimLong:
1963 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001964 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1966 break;
1967
1968 case Primitive::kPrimFloat:
1969 case Primitive::kPrimDouble:
1970 locations->SetInAt(0, Location::RequiresFpuRegister());
1971 locations->SetInAt(1, Location::RequiresFpuRegister());
1972 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1973 break;
1974
1975 default:
1976 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1977 }
1978}
1979
1980void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1981 Primitive::Type type = div->GetResultType();
1982 switch (type) {
1983 case Primitive::kPrimInt:
1984 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001985 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001986 break;
1987
1988 case Primitive::kPrimFloat:
1989 case Primitive::kPrimDouble:
1990 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1991 break;
1992
1993 default:
1994 LOG(FATAL) << "Unexpected div type " << type;
1995 }
1996}
1997
Alexandre Rames67555f72014-11-18 10:55:16 +00001998void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2001 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2002 if (instruction->HasUses()) {
2003 locations->SetOut(Location::SameAsFirstInput());
2004 }
2005}
2006
2007void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2008 SlowPathCodeARM64* slow_path =
2009 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2010 codegen_->AddSlowPath(slow_path);
2011 Location value = instruction->GetLocations()->InAt(0);
2012
Alexandre Rames3e69f162014-12-10 10:36:50 +00002013 Primitive::Type type = instruction->GetType();
2014
2015 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
2016 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
2017 return;
2018 }
2019
Alexandre Rames67555f72014-11-18 10:55:16 +00002020 if (value.IsConstant()) {
2021 int64_t divisor = Int64ConstantFrom(value);
2022 if (divisor == 0) {
2023 __ B(slow_path->GetEntryLabel());
2024 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002025 // A division by a non-null constant is valid. We don't need to perform
2026 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002027 }
2028 } else {
2029 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2030 }
2031}
2032
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002033void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2036 locations->SetOut(Location::ConstantLocation(constant));
2037}
2038
2039void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2040 UNUSED(constant);
2041 // Will be generated at use site.
2042}
2043
Alexandre Rames5319def2014-10-23 10:03:10 +01002044void LocationsBuilderARM64::VisitExit(HExit* exit) {
2045 exit->SetLocations(nullptr);
2046}
2047
2048void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002049 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002050}
2051
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002052void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2053 LocationSummary* locations =
2054 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2055 locations->SetOut(Location::ConstantLocation(constant));
2056}
2057
2058void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2059 UNUSED(constant);
2060 // Will be generated at use site.
2061}
2062
David Brazdilfc6a86a2015-06-26 10:33:45 +00002063void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002064 DCHECK(!successor->IsExitBlock());
2065 HBasicBlock* block = got->GetBlock();
2066 HInstruction* previous = got->GetPrevious();
2067 HLoopInformation* info = block->GetLoopInformation();
2068
David Brazdil46e2a392015-03-16 17:31:52 +00002069 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002070 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2071 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2072 return;
2073 }
2074 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2075 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2076 }
2077 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002078 __ B(codegen_->GetLabelOf(successor));
2079 }
2080}
2081
David Brazdilfc6a86a2015-06-26 10:33:45 +00002082void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2083 got->SetLocations(nullptr);
2084}
2085
2086void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2087 HandleGoto(got, got->GetSuccessor());
2088}
2089
2090void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2091 try_boundary->SetLocations(nullptr);
2092}
2093
2094void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2095 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2096 if (!successor->IsExitBlock()) {
2097 HandleGoto(try_boundary, successor);
2098 }
2099}
2100
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002101void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2102 vixl::Label* true_target,
2103 vixl::Label* false_target,
2104 vixl::Label* always_true_target) {
2105 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002106 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002107
Serban Constantinescu02164b32014-11-13 14:05:07 +00002108 if (cond->IsIntConstant()) {
2109 int32_t cond_value = cond->AsIntConstant()->GetValue();
2110 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002111 if (always_true_target != nullptr) {
2112 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002113 }
2114 return;
2115 } else {
2116 DCHECK_EQ(cond_value, 0);
2117 }
2118 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002119 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002120 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002121 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002122 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002123 } else {
2124 // The condition instruction has not been materialized, use its inputs as
2125 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002126 Primitive::Type type =
2127 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2128
2129 if (Primitive::IsFloatingPointType(type)) {
2130 // FP compares don't like null false_targets.
2131 if (false_target == nullptr) {
2132 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002133 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002134 FPRegister lhs = InputFPRegisterAt(condition, 0);
2135 if (condition->GetLocations()->InAt(1).IsConstant()) {
2136 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2137 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2138 __ Fcmp(lhs, 0.0);
2139 } else {
2140 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2141 }
2142 if (condition->IsFPConditionTrueIfNaN()) {
2143 __ B(vs, true_target); // VS for unordered.
2144 } else if (condition->IsFPConditionFalseIfNaN()) {
2145 __ B(vs, false_target); // VS for unordered.
2146 }
2147 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002148 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002149 // Integer cases.
2150 Register lhs = InputRegisterAt(condition, 0);
2151 Operand rhs = InputOperandAt(condition, 1);
2152 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2153 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2154 switch (arm64_cond) {
2155 case eq:
2156 __ Cbz(lhs, true_target);
2157 break;
2158 case ne:
2159 __ Cbnz(lhs, true_target);
2160 break;
2161 case lt:
2162 // Test the sign bit and branch accordingly.
2163 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2164 break;
2165 case ge:
2166 // Test the sign bit and branch accordingly.
2167 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2168 break;
2169 default:
2170 // Without the `static_cast` the compiler throws an error for
2171 // `-Werror=sign-promo`.
2172 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2173 }
2174 } else {
2175 __ Cmp(lhs, rhs);
2176 __ B(arm64_cond, true_target);
2177 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002178 }
2179 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002180 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002181 __ B(false_target);
2182 }
2183}
2184
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002185void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2186 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2187 HInstruction* cond = if_instr->InputAt(0);
2188 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2189 locations->SetInAt(0, Location::RequiresRegister());
2190 }
2191}
2192
2193void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2194 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2195 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2196 vixl::Label* always_true_target = true_target;
2197 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2198 if_instr->IfTrueSuccessor())) {
2199 always_true_target = nullptr;
2200 }
2201 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2202 if_instr->IfFalseSuccessor())) {
2203 false_target = nullptr;
2204 }
2205 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2206}
2207
2208void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2209 LocationSummary* locations = new (GetGraph()->GetArena())
2210 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2211 HInstruction* cond = deoptimize->InputAt(0);
2212 DCHECK(cond->IsCondition());
2213 if (cond->AsCondition()->NeedsMaterialization()) {
2214 locations->SetInAt(0, Location::RequiresRegister());
2215 }
2216}
2217
2218void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2219 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2220 DeoptimizationSlowPathARM64(deoptimize);
2221 codegen_->AddSlowPath(slow_path);
2222 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2223 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2224}
2225
Alexandre Rames5319def2014-10-23 10:03:10 +01002226void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002227 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002228}
2229
2230void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002231 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002232}
2233
2234void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002235 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002236}
2237
2238void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002239 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002240}
2241
Alexandre Rames67555f72014-11-18 10:55:16 +00002242void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2243 LocationSummary::CallKind call_kind =
2244 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2245 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2246 locations->SetInAt(0, Location::RequiresRegister());
2247 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002248 // The output does overlap inputs.
2249 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002250}
2251
2252void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2253 LocationSummary* locations = instruction->GetLocations();
2254 Register obj = InputRegisterAt(instruction, 0);;
2255 Register cls = InputRegisterAt(instruction, 1);;
2256 Register out = OutputRegister(instruction);
2257
2258 vixl::Label done;
2259
2260 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002261 // Avoid null check if we know `obj` is not null.
2262 if (instruction->MustDoNullCheck()) {
2263 __ Mov(out, 0);
2264 __ Cbz(obj, &done);
2265 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002266
2267 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002268 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002269 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002270 __ Cmp(out, cls);
2271 if (instruction->IsClassFinal()) {
2272 // Classes must be equal for the instanceof to succeed.
2273 __ Cset(out, eq);
2274 } else {
2275 // If the classes are not equal, we go into a slow path.
2276 DCHECK(locations->OnlyCallsOnSlowPath());
2277 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002278 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2279 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002280 codegen_->AddSlowPath(slow_path);
2281 __ B(ne, slow_path->GetEntryLabel());
2282 __ Mov(out, 1);
2283 __ Bind(slow_path->GetExitLabel());
2284 }
2285
2286 __ Bind(&done);
2287}
2288
Alexandre Rames5319def2014-10-23 10:03:10 +01002289void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2291 locations->SetOut(Location::ConstantLocation(constant));
2292}
2293
2294void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2295 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002296 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002297}
2298
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002299void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2301 locations->SetOut(Location::ConstantLocation(constant));
2302}
2303
2304void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2305 // Will be generated at use site.
2306 UNUSED(constant);
2307}
2308
Alexandre Rames5319def2014-10-23 10:03:10 +01002309void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002310 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002311 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002312}
2313
Alexandre Rames67555f72014-11-18 10:55:16 +00002314void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2315 HandleInvoke(invoke);
2316}
2317
2318void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2319 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002320 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2321 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2322 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002323 Location receiver = invoke->GetLocations()->InAt(0);
2324 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002325 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002326
2327 // The register ip1 is required to be used for the hidden argument in
2328 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002329 MacroAssembler* masm = GetVIXLAssembler();
2330 UseScratchRegisterScope scratch_scope(masm);
2331 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002332 scratch_scope.Exclude(ip1);
2333 __ Mov(ip1, invoke->GetDexMethodIndex());
2334
2335 // temp = object->GetClass();
2336 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002337 __ Ldr(temp.W(), StackOperandFrom(receiver));
2338 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002339 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002340 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002341 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002342 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002343 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002344 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002345 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002346 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002347 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002348 // lr();
2349 __ Blr(lr);
2350 DCHECK(!codegen_->IsLeafMethod());
2351 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2352}
2353
2354void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
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
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002363void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002364 // When we do not run baseline, explicit clinit checks triggered by static
2365 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2366 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002367
Andreas Gampe878d58c2015-01-15 23:24:00 -08002368 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2369 if (intrinsic.TryDispatch(invoke)) {
2370 return;
2371 }
2372
Alexandre Rames67555f72014-11-18 10:55:16 +00002373 HandleInvoke(invoke);
2374}
2375
Andreas Gampe878d58c2015-01-15 23:24:00 -08002376static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2377 if (invoke->GetLocations()->Intrinsified()) {
2378 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2379 intrinsic.Dispatch(invoke);
2380 return true;
2381 }
2382 return false;
2383}
2384
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002385void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002386 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002387 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002388
2389 // TODO: Implement all kinds of calls:
2390 // 1) boot -> boot
2391 // 2) app -> boot
2392 // 3) app -> app
2393 //
2394 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2395
Jeff Hao848f70a2014-01-15 13:49:50 -08002396 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002397 Register reg = XRegisterFrom(temp);
Jeff Hao848f70a2014-01-15 13:49:50 -08002398 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002399 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002400 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002401 __ Ldr(lr, MemOperand(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002402 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002403 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002404 __ Blr(lr);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002405 } else if (invoke->IsRecursive()) {
2406 __ Bl(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002407 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002408 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002409 Register reg = XRegisterFrom(temp);
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002410 Register method_reg;
2411 if (current_method.IsRegister()) {
2412 method_reg = XRegisterFrom(current_method);
2413 } else {
2414 DCHECK(invoke->GetLocations()->Intrinsified());
2415 DCHECK(!current_method.IsValid());
2416 method_reg = reg;
2417 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2418 }
2419
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002420 // temp = current_method->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002421 __ Ldr(reg.W(), MemOperand(method_reg.X(),
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002422 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2423 // temp = temp[index_in_cache];
2424 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2425 // lr = temp->entry_point_from_quick_compiled_code_;
2426 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2427 kArm64WordSize).Int32Value()));
2428 // lr();
2429 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002430 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002431
Andreas Gampe878d58c2015-01-15 23:24:00 -08002432 DCHECK(!IsLeafMethod());
2433}
2434
2435void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002436 // When we do not run baseline, explicit clinit checks triggered by static
2437 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2438 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002439
Andreas Gampe878d58c2015-01-15 23:24:00 -08002440 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2441 return;
2442 }
2443
Alexandre Ramesd921d642015-04-16 15:07:16 +01002444 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002445 LocationSummary* locations = invoke->GetLocations();
2446 codegen_->GenerateStaticOrDirectCall(
2447 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002448 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002449}
2450
2451void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002452 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2453 return;
2454 }
2455
Alexandre Rames5319def2014-10-23 10:03:10 +01002456 LocationSummary* locations = invoke->GetLocations();
2457 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002458 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2459 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2460 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002461 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002462 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002463
Alexandre Ramesd921d642015-04-16 15:07:16 +01002464 BlockPoolsScope block_pools(GetVIXLAssembler());
2465
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002466 DCHECK(receiver.IsRegister());
2467 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002468 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002469 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002471 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002472 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002473 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002474 // lr();
2475 __ Blr(lr);
2476 DCHECK(!codegen_->IsLeafMethod());
2477 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2478}
2479
Alexandre Rames67555f72014-11-18 10:55:16 +00002480void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2481 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2482 : LocationSummary::kNoCall;
2483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002484 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002485 locations->SetOut(Location::RequiresRegister());
2486}
2487
2488void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2489 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002490 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002491 if (cls->IsReferrersClass()) {
2492 DCHECK(!cls->CanCallRuntime());
2493 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002494 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002495 } else {
2496 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002497 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002498 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002499 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002500
2501 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2502 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2503 codegen_->AddSlowPath(slow_path);
2504 __ Cbz(out, slow_path->GetEntryLabel());
2505 if (cls->MustGenerateClinitCheck()) {
2506 GenerateClassInitializationCheck(slow_path, out);
2507 } else {
2508 __ Bind(slow_path->GetExitLabel());
2509 }
2510 }
2511}
2512
David Brazdilcb1c0552015-08-04 16:22:25 +01002513static MemOperand GetExceptionTlsAddress() {
2514 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2515}
2516
Alexandre Rames67555f72014-11-18 10:55:16 +00002517void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2518 LocationSummary* locations =
2519 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2520 locations->SetOut(Location::RequiresRegister());
2521}
2522
2523void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002524 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2525}
2526
2527void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2528 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2529}
2530
2531void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2532 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002533}
2534
Alexandre Rames5319def2014-10-23 10:03:10 +01002535void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2536 load->SetLocations(nullptr);
2537}
2538
2539void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2540 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002541 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002542}
2543
Alexandre Rames67555f72014-11-18 10:55:16 +00002544void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2545 LocationSummary* locations =
2546 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002547 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002548 locations->SetOut(Location::RequiresRegister());
2549}
2550
2551void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2552 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2553 codegen_->AddSlowPath(slow_path);
2554
2555 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002556 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002557 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002558 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002559 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002560 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002561 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002562 __ Cbz(out, slow_path->GetEntryLabel());
2563 __ Bind(slow_path->GetExitLabel());
2564}
2565
Alexandre Rames5319def2014-10-23 10:03:10 +01002566void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2567 local->SetLocations(nullptr);
2568}
2569
2570void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2571 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2572}
2573
2574void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2576 locations->SetOut(Location::ConstantLocation(constant));
2577}
2578
2579void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2580 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002581 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002582}
2583
Alexandre Rames67555f72014-11-18 10:55:16 +00002584void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2585 LocationSummary* locations =
2586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2587 InvokeRuntimeCallingConvention calling_convention;
2588 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2589}
2590
2591void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2592 codegen_->InvokeRuntime(instruction->IsEnter()
2593 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2594 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002595 instruction->GetDexPc(),
2596 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002597 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002598}
2599
Alexandre Rames42d641b2014-10-27 14:00:51 +00002600void LocationsBuilderARM64::VisitMul(HMul* mul) {
2601 LocationSummary* locations =
2602 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2603 switch (mul->GetResultType()) {
2604 case Primitive::kPrimInt:
2605 case Primitive::kPrimLong:
2606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002608 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002609 break;
2610
2611 case Primitive::kPrimFloat:
2612 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002613 locations->SetInAt(0, Location::RequiresFpuRegister());
2614 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002615 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002616 break;
2617
2618 default:
2619 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2620 }
2621}
2622
2623void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2624 switch (mul->GetResultType()) {
2625 case Primitive::kPrimInt:
2626 case Primitive::kPrimLong:
2627 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2628 break;
2629
2630 case Primitive::kPrimFloat:
2631 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002632 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002633 break;
2634
2635 default:
2636 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2637 }
2638}
2639
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002640void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2641 LocationSummary* locations =
2642 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2643 switch (neg->GetResultType()) {
2644 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002645 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002646 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002648 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002649
2650 case Primitive::kPrimFloat:
2651 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002652 locations->SetInAt(0, Location::RequiresFpuRegister());
2653 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002654 break;
2655
2656 default:
2657 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2658 }
2659}
2660
2661void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2662 switch (neg->GetResultType()) {
2663 case Primitive::kPrimInt:
2664 case Primitive::kPrimLong:
2665 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2666 break;
2667
2668 case Primitive::kPrimFloat:
2669 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002670 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002671 break;
2672
2673 default:
2674 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2675 }
2676}
2677
2678void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2679 LocationSummary* locations =
2680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2681 InvokeRuntimeCallingConvention calling_convention;
2682 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002683 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002684 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002685 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002686 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002687 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002688}
2689
2690void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2691 LocationSummary* locations = instruction->GetLocations();
2692 InvokeRuntimeCallingConvention calling_convention;
2693 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2694 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002695 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002696 // Note: if heap poisoning is enabled, the entry point takes cares
2697 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002698 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002699 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2700 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002701 instruction->GetDexPc(),
2702 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002703 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002704}
2705
Alexandre Rames5319def2014-10-23 10:03:10 +01002706void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2707 LocationSummary* locations =
2708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2709 InvokeRuntimeCallingConvention calling_convention;
2710 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002711 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002712 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002713 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002714}
2715
2716void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2717 LocationSummary* locations = instruction->GetLocations();
2718 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2719 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002720 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002721 // Note: if heap poisoning is enabled, the entry point takes cares
2722 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002723 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002724 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2725 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002726 instruction->GetDexPc(),
2727 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002728 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002729}
2730
2731void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2732 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002733 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002734 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002735}
2736
2737void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002738 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002739 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002740 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002741 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002742 break;
2743
2744 default:
2745 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2746 }
2747}
2748
David Brazdil66d126e2015-04-03 16:02:44 +01002749void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2750 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2751 locations->SetInAt(0, Location::RequiresRegister());
2752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2753}
2754
2755void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002756 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2757}
2758
Alexandre Rames5319def2014-10-23 10:03:10 +01002759void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2762 locations->SetInAt(0, Location::RequiresRegister());
2763 if (instruction->HasUses()) {
2764 locations->SetOut(Location::SameAsFirstInput());
2765 }
2766}
2767
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002768void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002769 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2770 return;
2771 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002772
Alexandre Ramesd921d642015-04-16 15:07:16 +01002773 BlockPoolsScope block_pools(GetVIXLAssembler());
2774 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002775 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2776 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2777}
2778
2779void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002780 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2781 codegen_->AddSlowPath(slow_path);
2782
2783 LocationSummary* locations = instruction->GetLocations();
2784 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002785
2786 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002787}
2788
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002789void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2790 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2791 GenerateImplicitNullCheck(instruction);
2792 } else {
2793 GenerateExplicitNullCheck(instruction);
2794 }
2795}
2796
Alexandre Rames67555f72014-11-18 10:55:16 +00002797void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2798 HandleBinaryOp(instruction);
2799}
2800
2801void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2802 HandleBinaryOp(instruction);
2803}
2804
Alexandre Rames3e69f162014-12-10 10:36:50 +00002805void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2806 LOG(FATAL) << "Unreachable";
2807}
2808
2809void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2810 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2811}
2812
Alexandre Rames5319def2014-10-23 10:03:10 +01002813void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2815 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2816 if (location.IsStackSlot()) {
2817 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2818 } else if (location.IsDoubleStackSlot()) {
2819 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2820 }
2821 locations->SetOut(location);
2822}
2823
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002824void InstructionCodeGeneratorARM64::VisitParameterValue(
2825 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002826 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002827}
2828
2829void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2830 LocationSummary* locations =
2831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002832 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002833}
2834
2835void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2836 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2837 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002838}
2839
2840void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2842 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2843 locations->SetInAt(i, Location::Any());
2844 }
2845 locations->SetOut(Location::Any());
2846}
2847
2848void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002849 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002850 LOG(FATAL) << "Unreachable";
2851}
2852
Serban Constantinescu02164b32014-11-13 14:05:07 +00002853void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002854 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002855 LocationSummary::CallKind call_kind =
2856 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2858
2859 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002860 case Primitive::kPrimInt:
2861 case Primitive::kPrimLong:
2862 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002863 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2865 break;
2866
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002867 case Primitive::kPrimFloat:
2868 case Primitive::kPrimDouble: {
2869 InvokeRuntimeCallingConvention calling_convention;
2870 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2871 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2872 locations->SetOut(calling_convention.GetReturnLocation(type));
2873
2874 break;
2875 }
2876
Serban Constantinescu02164b32014-11-13 14:05:07 +00002877 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002878 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002879 }
2880}
2881
2882void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2883 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002884
Serban Constantinescu02164b32014-11-13 14:05:07 +00002885 switch (type) {
2886 case Primitive::kPrimInt:
2887 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002888 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002889 break;
2890 }
2891
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002892 case Primitive::kPrimFloat:
2893 case Primitive::kPrimDouble: {
2894 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2895 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002896 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002897 break;
2898 }
2899
Serban Constantinescu02164b32014-11-13 14:05:07 +00002900 default:
2901 LOG(FATAL) << "Unexpected rem type " << type;
2902 }
2903}
2904
Calin Juravle27df7582015-04-17 19:12:31 +01002905void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2906 memory_barrier->SetLocations(nullptr);
2907}
2908
2909void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2910 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2911}
2912
Alexandre Rames5319def2014-10-23 10:03:10 +01002913void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2915 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002916 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002917}
2918
2919void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002920 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002921 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002922}
2923
2924void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2925 instruction->SetLocations(nullptr);
2926}
2927
2928void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002929 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002930 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002931}
2932
Serban Constantinescu02164b32014-11-13 14:05:07 +00002933void LocationsBuilderARM64::VisitShl(HShl* shl) {
2934 HandleShift(shl);
2935}
2936
2937void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2938 HandleShift(shl);
2939}
2940
2941void LocationsBuilderARM64::VisitShr(HShr* shr) {
2942 HandleShift(shr);
2943}
2944
2945void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2946 HandleShift(shr);
2947}
2948
Alexandre Rames5319def2014-10-23 10:03:10 +01002949void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2950 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2951 Primitive::Type field_type = store->InputAt(1)->GetType();
2952 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002953 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002954 case Primitive::kPrimBoolean:
2955 case Primitive::kPrimByte:
2956 case Primitive::kPrimChar:
2957 case Primitive::kPrimShort:
2958 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002959 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002960 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2961 break;
2962
2963 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002964 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002965 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2966 break;
2967
2968 default:
2969 LOG(FATAL) << "Unimplemented local type " << field_type;
2970 }
2971}
2972
2973void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002974 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002975}
2976
2977void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002978 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002979}
2980
2981void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002982 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002983}
2984
Alexandre Rames67555f72014-11-18 10:55:16 +00002985void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002986 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002987}
2988
2989void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002990 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002991}
2992
2993void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002994 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002995}
2996
Alexandre Rames67555f72014-11-18 10:55:16 +00002997void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002998 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002999}
3000
3001void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3003}
3004
3005void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003006 HBasicBlock* block = instruction->GetBlock();
3007 if (block->GetLoopInformation() != nullptr) {
3008 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3009 // The back edge will generate the suspend check.
3010 return;
3011 }
3012 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3013 // The goto will generate the suspend check.
3014 return;
3015 }
3016 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003017}
3018
3019void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3020 temp->SetLocations(nullptr);
3021}
3022
3023void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3024 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003025 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003026}
3027
Alexandre Rames67555f72014-11-18 10:55:16 +00003028void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3029 LocationSummary* locations =
3030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3031 InvokeRuntimeCallingConvention calling_convention;
3032 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3033}
3034
3035void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3036 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003037 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003038 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003039}
3040
3041void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3042 LocationSummary* locations =
3043 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3044 Primitive::Type input_type = conversion->GetInputType();
3045 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003046 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003047 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3048 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3049 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3050 }
3051
Alexandre Rames542361f2015-01-29 16:57:31 +00003052 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003053 locations->SetInAt(0, Location::RequiresFpuRegister());
3054 } else {
3055 locations->SetInAt(0, Location::RequiresRegister());
3056 }
3057
Alexandre Rames542361f2015-01-29 16:57:31 +00003058 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003059 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3060 } else {
3061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3062 }
3063}
3064
3065void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3066 Primitive::Type result_type = conversion->GetResultType();
3067 Primitive::Type input_type = conversion->GetInputType();
3068
3069 DCHECK_NE(input_type, result_type);
3070
Alexandre Rames542361f2015-01-29 16:57:31 +00003071 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003072 int result_size = Primitive::ComponentSize(result_type);
3073 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003074 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003075 Register output = OutputRegister(conversion);
3076 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003077 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3078 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3079 } else if ((result_type == Primitive::kPrimChar) ||
3080 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3081 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003082 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003083 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003084 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003085 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003086 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003087 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003088 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3089 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003090 } else if (Primitive::IsFloatingPointType(result_type) &&
3091 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003092 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3093 } else {
3094 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3095 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003096 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003097}
Alexandre Rames67555f72014-11-18 10:55:16 +00003098
Serban Constantinescu02164b32014-11-13 14:05:07 +00003099void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3100 HandleShift(ushr);
3101}
3102
3103void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3104 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003105}
3106
3107void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3108 HandleBinaryOp(instruction);
3109}
3110
3111void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3112 HandleBinaryOp(instruction);
3113}
3114
Calin Juravleb1498f62015-02-16 13:13:29 +00003115void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3116 // Nothing to do, this should be removed during prepare for register allocator.
3117 UNUSED(instruction);
3118 LOG(FATAL) << "Unreachable";
3119}
3120
3121void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3122 // Nothing to do, this should be removed during prepare for register allocator.
3123 UNUSED(instruction);
3124 LOG(FATAL) << "Unreachable";
3125}
3126
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003127void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3128 DCHECK(codegen_->IsBaseline());
3129 LocationSummary* locations =
3130 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3131 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3132}
3133
3134void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3135 DCHECK(codegen_->IsBaseline());
3136 // Will be generated at use site.
3137}
3138
Alexandre Rames67555f72014-11-18 10:55:16 +00003139#undef __
3140#undef QUICK_ENTRY_POINT
3141
Alexandre Rames5319def2014-10-23 10:03:10 +01003142} // namespace arm64
3143} // namespace art