blob: d93ef1b7608e604022d3d8600392d7266fc9e592 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080022#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010080 }
Roland Levillain7f63c522015-07-13 15:54:55 +000081 LOG(FATAL) << "Unreachable";
82 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010083}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010095 } else if (return_type == Primitive::kPrimVoid) {
96 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Zheng Xuda403092015-04-24 17:35:39 +0800109// Calculate memory accessing operand for save/restore live registers.
110static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
111 RegisterSet* register_set,
112 int64_t spill_offset,
113 bool is_save) {
114 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
115 codegen->GetNumberOfCoreRegisters(),
116 register_set->GetFloatingPointRegisters(),
117 codegen->GetNumberOfFloatingPointRegisters()));
118
119 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
120 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000193 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
194 Location index_location,
195 Location length_location)
196 : instruction_(instruction),
197 index_location_(index_location),
198 length_location_(length_location) {}
199
Alexandre Rames5319def2014-10-23 10:03:10 +0100200
Alexandre Rames67555f72014-11-18 10:55:16 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000202 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100203 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100208 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames8158f282015-08-07 10:26:17 +0100215 bool IsFatal() const OVERRIDE { return true; }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 HBoundsCheck* const instruction_;
221 const Location index_location_;
222 const Location length_location_;
223
Alexandre Rames5319def2014-10-23 10:03:10 +0100224 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
225};
226
Alexandre Rames67555f72014-11-18 10:55:16 +0000227class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
228 public:
229 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
233 __ Bind(GetEntryLabel());
234 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000235 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800236 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000237 }
238
Alexandre Rames8158f282015-08-07 10:26:17 +0100239 bool IsFatal() const OVERRIDE { return true; }
240
Alexandre Rames9931f312015-06-19 14:47:01 +0100241 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
242
Alexandre Rames67555f72014-11-18 10:55:16 +0000243 private:
244 HDivZeroCheck* const instruction_;
245 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
246};
247
248class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
249 public:
250 LoadClassSlowPathARM64(HLoadClass* cls,
251 HInstruction* at,
252 uint32_t dex_pc,
253 bool do_clinit)
254 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
255 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
256 }
257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 LocationSummary* locations = at_->GetLocations();
260 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
261
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000264
265 InvokeRuntimeCallingConvention calling_convention;
266 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
268 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800270 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100271 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100273 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000275
276 // Move the class to the desired location.
277 Location out = locations->Out();
278 if (out.IsValid()) {
279 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
280 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000281 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000282 }
283
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 __ B(GetExitLabel());
286 }
287
Alexandre Rames9931f312015-06-19 14:47:01 +0100288 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 private:
291 // The class this slow path will load.
292 HLoadClass* const cls_;
293
294 // The instruction where this slow path is happening.
295 // (Might be the load class or an initialization check).
296 HInstruction* const at_;
297
298 // The dex PC of `at_`.
299 const uint32_t dex_pc_;
300
301 // Whether to initialize the class.
302 const bool do_clinit_;
303
304 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
305};
306
307class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
308 public:
309 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
310
311 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
312 LocationSummary* locations = instruction_->GetLocations();
313 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
314 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
315
316 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000318
319 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800320 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000322 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100323 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000325 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000326
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000327 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328 __ B(GetExitLabel());
329 }
330
Alexandre Rames9931f312015-06-19 14:47:01 +0100331 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 private:
334 HLoadString* const instruction_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
337};
338
Alexandre Rames5319def2014-10-23 10:03:10 +0100339class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
341 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
342
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100345 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000347 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800348 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100349 }
350
Alexandre Rames8158f282015-08-07 10:26:17 +0100351 bool IsFatal() const OVERRIDE { return true; }
352
Alexandre Rames9931f312015-06-19 14:47:01 +0100353 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
354
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 private:
356 HNullCheck* const instruction_;
357
358 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
359};
360
361class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
362 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100363 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100364 : instruction_(instruction), successor_(successor) {}
365
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
367 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100368 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000369 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000370 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000371 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800372 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000373 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 if (successor_ == nullptr) {
375 __ B(GetReturnLabel());
376 } else {
377 __ B(arm64_codegen->GetLabelOf(successor_));
378 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100379 }
380
381 vixl::Label* GetReturnLabel() {
382 DCHECK(successor_ == nullptr);
383 return &return_label_;
384 }
385
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100386 HBasicBlock* GetSuccessor() const {
387 return successor_;
388 }
389
Alexandre Rames9931f312015-06-19 14:47:01 +0100390 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
391
Alexandre Rames5319def2014-10-23 10:03:10 +0100392 private:
393 HSuspendCheck* const instruction_;
394 // If not null, the block to branch to after the suspend check.
395 HBasicBlock* const successor_;
396
397 // If `successor_` is null, the label to branch to after the suspend check.
398 vixl::Label return_label_;
399
400 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
401};
402
Alexandre Rames67555f72014-11-18 10:55:16 +0000403class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
404 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000405 TypeCheckSlowPathARM64(HInstruction* instruction,
406 Location class_to_check,
407 Location object_class,
408 uint32_t dex_pc)
409 : instruction_(instruction),
410 class_to_check_(class_to_check),
411 object_class_(object_class),
412 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 LocationSummary* locations = instruction_->GetLocations();
416 DCHECK(instruction_->IsCheckCast()
417 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
418 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
419
Alexandre Rames67555f72014-11-18 10:55:16 +0000420 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000421 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000422
423 // We're moving two locations to locations that could overlap, so we need a parallel
424 // move resolver.
425 InvokeRuntimeCallingConvention calling_convention;
426 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100427 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
428 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000429
430 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000431 arm64_codegen->InvokeRuntime(
432 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000433 Primitive::Type ret_type = instruction_->GetType();
434 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
435 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800436 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
437 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438 } else {
439 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000440 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800441 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442 }
443
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000444 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000445 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000446 }
447
Alexandre Rames9931f312015-06-19 14:47:01 +0100448 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
449
Alexandre Rames67555f72014-11-18 10:55:16 +0000450 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451 HInstruction* const instruction_;
452 const Location class_to_check_;
453 const Location object_class_;
454 uint32_t dex_pc_;
455
Alexandre Rames67555f72014-11-18 10:55:16 +0000456 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
457};
458
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700459class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
460 public:
461 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
462 : instruction_(instruction) {}
463
464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 __ Bind(GetEntryLabel());
466 SaveLiveRegisters(codegen, instruction_->GetLocations());
467 DCHECK(instruction_->IsDeoptimize());
468 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
469 uint32_t dex_pc = deoptimize->GetDexPc();
470 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
471 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
472 }
473
Alexandre Rames9931f312015-06-19 14:47:01 +0100474 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
475
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700476 private:
477 HInstruction* const instruction_;
478 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
479};
480
Alexandre Rames5319def2014-10-23 10:03:10 +0100481#undef __
482
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100483Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 Location next_location;
485 if (type == Primitive::kPrimVoid) {
486 LOG(FATAL) << "Unreachable type " << type;
487 }
488
Alexandre Rames542361f2015-01-29 16:57:31 +0000489 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100490 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
491 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000492 } else if (!Primitive::IsFloatingPointType(type) &&
493 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000494 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
495 } else {
496 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000497 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
498 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100499 }
500
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000501 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000502 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100503 return next_location;
504}
505
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100506Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100507 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100508}
509
Serban Constantinescu579885a2015-02-22 20:51:33 +0000510CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
511 const Arm64InstructionSetFeatures& isa_features,
512 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100513 : CodeGenerator(graph,
514 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000515 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000516 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000517 callee_saved_core_registers.list(),
518 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000519 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 block_labels_(nullptr),
521 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000522 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000523 move_resolver_(graph->GetArena(), this),
524 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000525 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000526 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000527}
Alexandre Rames5319def2014-10-23 10:03:10 +0100528
Alexandre Rames67555f72014-11-18 10:55:16 +0000529#undef __
530#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100531
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000532void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
533 // Ensure we emit the literal pool.
534 __ FinalizeCode();
535 CodeGenerator::Finalize(allocator);
536}
537
Zheng Xuad4450e2015-04-17 18:48:56 +0800538void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
539 // Note: There are 6 kinds of moves:
540 // 1. constant -> GPR/FPR (non-cycle)
541 // 2. constant -> stack (non-cycle)
542 // 3. GPR/FPR -> GPR/FPR
543 // 4. GPR/FPR -> stack
544 // 5. stack -> GPR/FPR
545 // 6. stack -> stack (non-cycle)
546 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
547 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
548 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
549 // dependency.
550 vixl_temps_.Open(GetVIXLAssembler());
551}
552
553void ParallelMoveResolverARM64::FinishEmitNativeCode() {
554 vixl_temps_.Close();
555}
556
557Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
558 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
559 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
560 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
561 Location scratch = GetScratchLocation(kind);
562 if (!scratch.Equals(Location::NoLocation())) {
563 return scratch;
564 }
565 // Allocate from VIXL temp registers.
566 if (kind == Location::kRegister) {
567 scratch = LocationFrom(vixl_temps_.AcquireX());
568 } else {
569 DCHECK(kind == Location::kFpuRegister);
570 scratch = LocationFrom(vixl_temps_.AcquireD());
571 }
572 AddScratchLocation(scratch);
573 return scratch;
574}
575
576void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
577 if (loc.IsRegister()) {
578 vixl_temps_.Release(XRegisterFrom(loc));
579 } else {
580 DCHECK(loc.IsFpuRegister());
581 vixl_temps_.Release(DRegisterFrom(loc));
582 }
583 RemoveScratchLocation(loc);
584}
585
Alexandre Rames3e69f162014-12-10 10:36:50 +0000586void ParallelMoveResolverARM64::EmitMove(size_t index) {
587 MoveOperands* move = moves_.Get(index);
588 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
589}
590
Alexandre Rames5319def2014-10-23 10:03:10 +0100591void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100592 MacroAssembler* masm = GetVIXLAssembler();
593 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000594 __ Bind(&frame_entry_label_);
595
Serban Constantinescu02164b32014-11-13 14:05:07 +0000596 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
597 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100598 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000599 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000600 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000601 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000602 __ Ldr(wzr, MemOperand(temp, 0));
603 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000604 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100605
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000606 if (!HasEmptyFrame()) {
607 int frame_size = GetFrameSize();
608 // Stack layout:
609 // sp[frame_size - 8] : lr.
610 // ... : other preserved core registers.
611 // ... : other preserved fp registers.
612 // ... : reserved frame space.
613 // sp[0] : current method.
614 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100615 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800616 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
617 frame_size - GetCoreSpillSize());
618 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
619 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000620 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100621}
622
623void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100624 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100625 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000626 if (!HasEmptyFrame()) {
627 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800628 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
629 frame_size - FrameEntrySpillSize());
630 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
631 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000632 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100633 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000634 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100635 __ Ret();
636 GetAssembler()->cfi().RestoreState();
637 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100638}
639
Zheng Xuda403092015-04-24 17:35:39 +0800640vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
641 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
642 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
643 core_spill_mask_);
644}
645
646vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
647 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
648 GetNumberOfFloatingPointRegisters()));
649 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
650 fpu_spill_mask_);
651}
652
Alexandre Rames5319def2014-10-23 10:03:10 +0100653void CodeGeneratorARM64::Bind(HBasicBlock* block) {
654 __ Bind(GetLabelOf(block));
655}
656
Alexandre Rames5319def2014-10-23 10:03:10 +0100657void CodeGeneratorARM64::Move(HInstruction* instruction,
658 Location location,
659 HInstruction* move_for) {
660 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100661 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000662 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100663
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100664 if (instruction->IsFakeString()) {
665 // The fake string is an alias for null.
666 DCHECK(IsBaseline());
667 instruction = locations->Out().GetConstant();
668 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
669 }
670
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100671 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700672 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100673 } else if (locations != nullptr && locations->Out().Equals(location)) {
674 return;
675 } else if (instruction->IsIntConstant()
676 || instruction->IsLongConstant()
677 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000678 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100679 if (location.IsRegister()) {
680 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000681 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100682 (instruction->IsLongConstant() && dst.Is64Bits()));
683 __ Mov(dst, value);
684 } else {
685 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000686 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000687 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
688 ? temps.AcquireW()
689 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100690 __ Mov(temp, value);
691 __ Str(temp, StackOperandFrom(location));
692 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000693 } else if (instruction->IsTemporary()) {
694 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000695 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100696 } else if (instruction->IsLoadLocal()) {
697 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000698 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000699 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000700 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000701 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100702 }
703
704 } else {
705 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000706 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100707 }
708}
709
Alexandre Rames5319def2014-10-23 10:03:10 +0100710Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
711 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000712
Alexandre Rames5319def2014-10-23 10:03:10 +0100713 switch (type) {
714 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000715 case Primitive::kPrimInt:
716 case Primitive::kPrimFloat:
717 return Location::StackSlot(GetStackSlot(load->GetLocal()));
718
719 case Primitive::kPrimLong:
720 case Primitive::kPrimDouble:
721 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
722
Alexandre Rames5319def2014-10-23 10:03:10 +0100723 case Primitive::kPrimBoolean:
724 case Primitive::kPrimByte:
725 case Primitive::kPrimChar:
726 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100727 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100728 LOG(FATAL) << "Unexpected type " << type;
729 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000730
Alexandre Rames5319def2014-10-23 10:03:10 +0100731 LOG(FATAL) << "Unreachable";
732 return Location::NoLocation();
733}
734
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100735void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000736 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100737 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000738 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100739 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100740 if (value_can_be_null) {
741 __ Cbz(value, &done);
742 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100743 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
744 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000745 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100746 if (value_can_be_null) {
747 __ Bind(&done);
748 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100749}
750
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000751void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
752 // Blocked core registers:
753 // lr : Runtime reserved.
754 // tr : Runtime reserved.
755 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
756 // ip1 : VIXL core temp.
757 // ip0 : VIXL core temp.
758 //
759 // Blocked fp registers:
760 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100761 CPURegList reserved_core_registers = vixl_reserved_core_registers;
762 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100763 while (!reserved_core_registers.IsEmpty()) {
764 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
765 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000766
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000767 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800768 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000769 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
770 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000771
772 if (is_baseline) {
773 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
774 while (!reserved_core_baseline_registers.IsEmpty()) {
775 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
776 }
777
778 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
779 while (!reserved_fp_baseline_registers.IsEmpty()) {
780 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
781 }
782 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100783}
784
785Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
786 if (type == Primitive::kPrimVoid) {
787 LOG(FATAL) << "Unreachable type " << type;
788 }
789
Alexandre Rames542361f2015-01-29 16:57:31 +0000790 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000791 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
792 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100793 return Location::FpuRegisterLocation(reg);
794 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000795 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
796 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100797 return Location::RegisterLocation(reg);
798 }
799}
800
Alexandre Rames3e69f162014-12-10 10:36:50 +0000801size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
802 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
803 __ Str(reg, MemOperand(sp, stack_index));
804 return kArm64WordSize;
805}
806
807size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
808 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
809 __ Ldr(reg, MemOperand(sp, stack_index));
810 return kArm64WordSize;
811}
812
813size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
814 FPRegister reg = FPRegister(reg_id, kDRegSize);
815 __ Str(reg, MemOperand(sp, stack_index));
816 return kArm64WordSize;
817}
818
819size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
820 FPRegister reg = FPRegister(reg_id, kDRegSize);
821 __ Ldr(reg, MemOperand(sp, stack_index));
822 return kArm64WordSize;
823}
824
Alexandre Rames5319def2014-10-23 10:03:10 +0100825void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100826 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100827}
828
829void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100830 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100831}
832
Alexandre Rames67555f72014-11-18 10:55:16 +0000833void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000834 if (constant->IsIntConstant()) {
835 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
836 } else if (constant->IsLongConstant()) {
837 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
838 } else if (constant->IsNullConstant()) {
839 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000840 } else if (constant->IsFloatConstant()) {
841 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
842 } else {
843 DCHECK(constant->IsDoubleConstant());
844 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
845 }
846}
847
Alexandre Rames3e69f162014-12-10 10:36:50 +0000848
849static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
850 DCHECK(constant.IsConstant());
851 HConstant* cst = constant.GetConstant();
852 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000853 // Null is mapped to a core W register, which we associate with kPrimInt.
854 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000855 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
856 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
857 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
858}
859
860void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000861 if (source.Equals(destination)) {
862 return;
863 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000864
865 // A valid move can always be inferred from the destination and source
866 // locations. When moving from and to a register, the argument type can be
867 // used to generate 32bit instead of 64bit moves. In debug mode we also
868 // checks the coherency of the locations and the type.
869 bool unspecified_type = (type == Primitive::kPrimVoid);
870
871 if (destination.IsRegister() || destination.IsFpuRegister()) {
872 if (unspecified_type) {
873 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
874 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000875 (src_cst != nullptr && (src_cst->IsIntConstant()
876 || src_cst->IsFloatConstant()
877 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000878 // For stack slots and 32bit constants, a 64bit type is appropriate.
879 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000880 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000881 // If the source is a double stack slot or a 64bit constant, a 64bit
882 // type is appropriate. Else the source is a register, and since the
883 // type has not been specified, we chose a 64bit type to force a 64bit
884 // move.
885 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000886 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000887 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000888 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
889 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 CPURegister dst = CPURegisterFrom(destination, type);
891 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
892 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
893 __ Ldr(dst, StackOperandFrom(source));
894 } else if (source.IsConstant()) {
895 DCHECK(CoherentConstantAndType(source, type));
896 MoveConstant(dst, source.GetConstant());
897 } else {
898 if (destination.IsRegister()) {
899 __ Mov(Register(dst), RegisterFrom(source, type));
900 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800901 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000902 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
903 }
904 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000905 } else { // The destination is not a register. It must be a stack slot.
906 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
907 if (source.IsRegister() || source.IsFpuRegister()) {
908 if (unspecified_type) {
909 if (source.IsRegister()) {
910 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
911 } else {
912 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
913 }
914 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000915 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
916 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000917 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
918 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100919 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000920 UseScratchRegisterScope temps(GetVIXLAssembler());
921 HConstant* src_cst = source.GetConstant();
922 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000923 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000924 temp = temps.AcquireW();
925 } else if (src_cst->IsLongConstant()) {
926 temp = temps.AcquireX();
927 } else if (src_cst->IsFloatConstant()) {
928 temp = temps.AcquireS();
929 } else {
930 DCHECK(src_cst->IsDoubleConstant());
931 temp = temps.AcquireD();
932 }
933 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000934 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000935 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000936 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000937 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000938 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000939 // There is generally less pressure on FP registers.
940 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000941 __ Ldr(temp, StackOperandFrom(source));
942 __ Str(temp, StackOperandFrom(destination));
943 }
944 }
945}
946
947void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000948 CPURegister dst,
949 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000950 switch (type) {
951 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000952 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000953 break;
954 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000955 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000956 break;
957 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000958 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000959 break;
960 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000961 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000962 break;
963 case Primitive::kPrimInt:
964 case Primitive::kPrimNot:
965 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000966 case Primitive::kPrimFloat:
967 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000968 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000969 __ Ldr(dst, src);
970 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000971 case Primitive::kPrimVoid:
972 LOG(FATAL) << "Unreachable type " << type;
973 }
974}
975
Calin Juravle77520bc2015-01-12 18:45:46 +0000976void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000977 CPURegister dst,
978 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100979 MacroAssembler* masm = GetVIXLAssembler();
980 BlockPoolsScope block_pools(masm);
981 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000982 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000983 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000984
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000985 DCHECK(!src.IsPreIndex());
986 DCHECK(!src.IsPostIndex());
987
988 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800989 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990 MemOperand base = MemOperand(temp_base);
991 switch (type) {
992 case Primitive::kPrimBoolean:
993 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000994 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000995 break;
996 case Primitive::kPrimByte:
997 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000998 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1000 break;
1001 case Primitive::kPrimChar:
1002 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001003 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001004 break;
1005 case Primitive::kPrimShort:
1006 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001007 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001008 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1009 break;
1010 case Primitive::kPrimInt:
1011 case Primitive::kPrimNot:
1012 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001013 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001014 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001015 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001016 break;
1017 case Primitive::kPrimFloat:
1018 case Primitive::kPrimDouble: {
1019 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001020 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001021
1022 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1023 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001024 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001025 __ Fmov(FPRegister(dst), temp);
1026 break;
1027 }
1028 case Primitive::kPrimVoid:
1029 LOG(FATAL) << "Unreachable type " << type;
1030 }
1031}
1032
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001033void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 CPURegister src,
1035 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001036 switch (type) {
1037 case Primitive::kPrimBoolean:
1038 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001039 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001040 break;
1041 case Primitive::kPrimChar:
1042 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001044 break;
1045 case Primitive::kPrimInt:
1046 case Primitive::kPrimNot:
1047 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001048 case Primitive::kPrimFloat:
1049 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001050 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001051 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001052 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001053 case Primitive::kPrimVoid:
1054 LOG(FATAL) << "Unreachable type " << type;
1055 }
1056}
1057
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001058void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1059 CPURegister src,
1060 const MemOperand& dst) {
1061 UseScratchRegisterScope temps(GetVIXLAssembler());
1062 Register temp_base = temps.AcquireX();
1063
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001064 DCHECK(!dst.IsPreIndex());
1065 DCHECK(!dst.IsPostIndex());
1066
1067 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001068 Operand op = OperandFromMemOperand(dst);
1069 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001070 MemOperand base = MemOperand(temp_base);
1071 switch (type) {
1072 case Primitive::kPrimBoolean:
1073 case Primitive::kPrimByte:
1074 __ Stlrb(Register(src), base);
1075 break;
1076 case Primitive::kPrimChar:
1077 case Primitive::kPrimShort:
1078 __ Stlrh(Register(src), base);
1079 break;
1080 case Primitive::kPrimInt:
1081 case Primitive::kPrimNot:
1082 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001083 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001084 __ Stlr(Register(src), base);
1085 break;
1086 case Primitive::kPrimFloat:
1087 case Primitive::kPrimDouble: {
1088 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001089 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001090
1091 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1092 __ Fmov(temp, FPRegister(src));
1093 __ Stlr(temp, base);
1094 break;
1095 }
1096 case Primitive::kPrimVoid:
1097 LOG(FATAL) << "Unreachable type " << type;
1098 }
1099}
1100
Alexandre Rames67555f72014-11-18 10:55:16 +00001101void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1102 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001103 uint32_t dex_pc,
1104 SlowPathCode* slow_path) {
Alexandre Rames8158f282015-08-07 10:26:17 +01001105 // Ensure that the call kind indication given to the register allocator is
1106 // coherent with the runtime call generated.
1107 if (slow_path == nullptr) {
1108 DCHECK(instruction->GetLocations()->WillCall());
1109 } else {
1110 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal());
1111 }
1112
Alexandre Ramesd921d642015-04-16 15:07:16 +01001113 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001114 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1115 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001116 RecordPcInfo(instruction, dex_pc, slow_path);
1117 DCHECK(instruction->IsSuspendCheck()
1118 || instruction->IsBoundsCheck()
1119 || instruction->IsNullCheck()
1120 || instruction->IsDivZeroCheck()
1121 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001122}
1123
1124void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1125 vixl::Register class_reg) {
1126 UseScratchRegisterScope temps(GetVIXLAssembler());
1127 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001128 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001129 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001130
Serban Constantinescu02164b32014-11-13 14:05:07 +00001131 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001132 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001133 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1134 __ Add(temp, class_reg, status_offset);
1135 __ Ldar(temp, HeapOperand(temp));
1136 __ Cmp(temp, mirror::Class::kStatusInitialized);
1137 __ B(lt, slow_path->GetEntryLabel());
1138 } else {
1139 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1140 __ Cmp(temp, mirror::Class::kStatusInitialized);
1141 __ B(lt, slow_path->GetEntryLabel());
1142 __ Dmb(InnerShareable, BarrierReads);
1143 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001144 __ Bind(slow_path->GetExitLabel());
1145}
Alexandre Rames5319def2014-10-23 10:03:10 +01001146
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001147void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1148 BarrierType type = BarrierAll;
1149
1150 switch (kind) {
1151 case MemBarrierKind::kAnyAny:
1152 case MemBarrierKind::kAnyStore: {
1153 type = BarrierAll;
1154 break;
1155 }
1156 case MemBarrierKind::kLoadAny: {
1157 type = BarrierReads;
1158 break;
1159 }
1160 case MemBarrierKind::kStoreStore: {
1161 type = BarrierWrites;
1162 break;
1163 }
1164 default:
1165 LOG(FATAL) << "Unexpected memory barrier " << kind;
1166 }
1167 __ Dmb(InnerShareable, type);
1168}
1169
Serban Constantinescu02164b32014-11-13 14:05:07 +00001170void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1171 HBasicBlock* successor) {
1172 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001173 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1174 if (slow_path == nullptr) {
1175 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1176 instruction->SetSlowPath(slow_path);
1177 codegen_->AddSlowPath(slow_path);
1178 if (successor != nullptr) {
1179 DCHECK(successor->IsLoopHeader());
1180 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1181 }
1182 } else {
1183 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1184 }
1185
Serban Constantinescu02164b32014-11-13 14:05:07 +00001186 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1187 Register temp = temps.AcquireW();
1188
1189 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1190 if (successor == nullptr) {
1191 __ Cbnz(temp, slow_path->GetEntryLabel());
1192 __ Bind(slow_path->GetReturnLabel());
1193 } else {
1194 __ Cbz(temp, codegen_->GetLabelOf(successor));
1195 __ B(slow_path->GetEntryLabel());
1196 // slow_path will return to GetLabelOf(successor).
1197 }
1198}
1199
Alexandre Rames5319def2014-10-23 10:03:10 +01001200InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1201 CodeGeneratorARM64* codegen)
1202 : HGraphVisitor(graph),
1203 assembler_(codegen->GetAssembler()),
1204 codegen_(codegen) {}
1205
1206#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001207 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001208
1209#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1210
1211enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001212 // Using a base helps identify when we hit such breakpoints.
1213 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001214#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1215 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1216#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1217};
1218
1219#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1220 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001221 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001222 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1223 } \
1224 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1226 locations->SetOut(Location::Any()); \
1227 }
1228 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1229#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1230
1231#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001232#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001233
Alexandre Rames67555f72014-11-18 10:55:16 +00001234void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001235 DCHECK_EQ(instr->InputCount(), 2U);
1236 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1237 Primitive::Type type = instr->GetResultType();
1238 switch (type) {
1239 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001240 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001241 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001242 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001244 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001245
1246 case Primitive::kPrimFloat:
1247 case Primitive::kPrimDouble:
1248 locations->SetInAt(0, Location::RequiresFpuRegister());
1249 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001250 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001251 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001252
Alexandre Rames5319def2014-10-23 10:03:10 +01001253 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001254 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001255 }
1256}
1257
Alexandre Rames09a99962015-04-15 11:47:56 +01001258void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1259 LocationSummary* locations =
1260 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1261 locations->SetInAt(0, Location::RequiresRegister());
1262 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1263 locations->SetOut(Location::RequiresFpuRegister());
1264 } else {
1265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1266 }
1267}
1268
1269void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1270 const FieldInfo& field_info) {
1271 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001272 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001273 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001274
1275 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1276 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1277
1278 if (field_info.IsVolatile()) {
1279 if (use_acquire_release) {
1280 // NB: LoadAcquire will record the pc info if needed.
1281 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1282 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001283 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001284 codegen_->MaybeRecordImplicitNullCheck(instruction);
1285 // For IRIW sequential consistency kLoadAny is not sufficient.
1286 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1287 }
1288 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001289 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001290 codegen_->MaybeRecordImplicitNullCheck(instruction);
1291 }
Roland Levillain4d027112015-07-01 15:41:14 +01001292
1293 if (field_type == Primitive::kPrimNot) {
1294 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1295 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001296}
1297
1298void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1299 LocationSummary* locations =
1300 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1301 locations->SetInAt(0, Location::RequiresRegister());
1302 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1303 locations->SetInAt(1, Location::RequiresFpuRegister());
1304 } else {
1305 locations->SetInAt(1, Location::RequiresRegister());
1306 }
1307}
1308
1309void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001310 const FieldInfo& field_info,
1311 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001312 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001313 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001314
1315 Register obj = InputRegisterAt(instruction, 0);
1316 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001317 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001318 Offset offset = field_info.GetFieldOffset();
1319 Primitive::Type field_type = field_info.GetFieldType();
1320 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1321
Roland Levillain4d027112015-07-01 15:41:14 +01001322 {
1323 // We use a block to end the scratch scope before the write barrier, thus
1324 // freeing the temporary registers so they can be used in `MarkGCCard`.
1325 UseScratchRegisterScope temps(GetVIXLAssembler());
1326
1327 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1328 DCHECK(value.IsW());
1329 Register temp = temps.AcquireW();
1330 __ Mov(temp, value.W());
1331 GetAssembler()->PoisonHeapReference(temp.W());
1332 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001333 }
Roland Levillain4d027112015-07-01 15:41:14 +01001334
1335 if (field_info.IsVolatile()) {
1336 if (use_acquire_release) {
1337 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1338 codegen_->MaybeRecordImplicitNullCheck(instruction);
1339 } else {
1340 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1341 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1342 codegen_->MaybeRecordImplicitNullCheck(instruction);
1343 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1344 }
1345 } else {
1346 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1347 codegen_->MaybeRecordImplicitNullCheck(instruction);
1348 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001349 }
1350
1351 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001352 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001353 }
1354}
1355
Alexandre Rames67555f72014-11-18 10:55:16 +00001356void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001357 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001358
1359 switch (type) {
1360 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001361 case Primitive::kPrimLong: {
1362 Register dst = OutputRegister(instr);
1363 Register lhs = InputRegisterAt(instr, 0);
1364 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001365 if (instr->IsAdd()) {
1366 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001367 } else if (instr->IsAnd()) {
1368 __ And(dst, lhs, rhs);
1369 } else if (instr->IsOr()) {
1370 __ Orr(dst, lhs, rhs);
1371 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001372 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001373 } else {
1374 DCHECK(instr->IsXor());
1375 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001376 }
1377 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001378 }
1379 case Primitive::kPrimFloat:
1380 case Primitive::kPrimDouble: {
1381 FPRegister dst = OutputFPRegister(instr);
1382 FPRegister lhs = InputFPRegisterAt(instr, 0);
1383 FPRegister rhs = InputFPRegisterAt(instr, 1);
1384 if (instr->IsAdd()) {
1385 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001386 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001387 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001388 } else {
1389 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001390 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001391 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001392 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001393 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001394 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001395 }
1396}
1397
Serban Constantinescu02164b32014-11-13 14:05:07 +00001398void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1399 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1400
1401 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1402 Primitive::Type type = instr->GetResultType();
1403 switch (type) {
1404 case Primitive::kPrimInt:
1405 case Primitive::kPrimLong: {
1406 locations->SetInAt(0, Location::RequiresRegister());
1407 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1408 locations->SetOut(Location::RequiresRegister());
1409 break;
1410 }
1411 default:
1412 LOG(FATAL) << "Unexpected shift type " << type;
1413 }
1414}
1415
1416void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1417 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1418
1419 Primitive::Type type = instr->GetType();
1420 switch (type) {
1421 case Primitive::kPrimInt:
1422 case Primitive::kPrimLong: {
1423 Register dst = OutputRegister(instr);
1424 Register lhs = InputRegisterAt(instr, 0);
1425 Operand rhs = InputOperandAt(instr, 1);
1426 if (rhs.IsImmediate()) {
1427 uint32_t shift_value = (type == Primitive::kPrimInt)
1428 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1429 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1430 if (instr->IsShl()) {
1431 __ Lsl(dst, lhs, shift_value);
1432 } else if (instr->IsShr()) {
1433 __ Asr(dst, lhs, shift_value);
1434 } else {
1435 __ Lsr(dst, lhs, shift_value);
1436 }
1437 } else {
1438 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1439
1440 if (instr->IsShl()) {
1441 __ Lsl(dst, lhs, rhs_reg);
1442 } else if (instr->IsShr()) {
1443 __ Asr(dst, lhs, rhs_reg);
1444 } else {
1445 __ Lsr(dst, lhs, rhs_reg);
1446 }
1447 }
1448 break;
1449 }
1450 default:
1451 LOG(FATAL) << "Unexpected shift operation type " << type;
1452 }
1453}
1454
Alexandre Rames5319def2014-10-23 10:03:10 +01001455void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001456 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001457}
1458
1459void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001460 HandleBinaryOp(instruction);
1461}
1462
1463void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1464 HandleBinaryOp(instruction);
1465}
1466
1467void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1468 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001469}
1470
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001471void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1472 LocationSummary* locations =
1473 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1474 locations->SetInAt(0, Location::RequiresRegister());
1475 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001476 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1477 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1478 } else {
1479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1480 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001481}
1482
1483void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1484 LocationSummary* locations = instruction->GetLocations();
1485 Primitive::Type type = instruction->GetType();
1486 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001487 Location index = locations->InAt(1);
1488 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001489 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001490 MacroAssembler* masm = GetVIXLAssembler();
1491 UseScratchRegisterScope temps(masm);
1492 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001493
1494 if (index.IsConstant()) {
1495 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001496 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001497 } else {
1498 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001499 __ Add(temp, obj, offset);
1500 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001501 }
1502
Alexandre Rames67555f72014-11-18 10:55:16 +00001503 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001504 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001505
1506 if (type == Primitive::kPrimNot) {
1507 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1508 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001509}
1510
Alexandre Rames5319def2014-10-23 10:03:10 +01001511void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1513 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001515}
1516
1517void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001518 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001519 __ Ldr(OutputRegister(instruction),
1520 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001521 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001522}
1523
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001524void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001525 if (instruction->NeedsTypeCheck()) {
1526 LocationSummary* locations =
1527 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001528 InvokeRuntimeCallingConvention calling_convention;
1529 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1530 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1531 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1532 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001533 LocationSummary* locations =
1534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001535 locations->SetInAt(0, Location::RequiresRegister());
1536 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001537 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1538 locations->SetInAt(2, Location::RequiresFpuRegister());
1539 } else {
1540 locations->SetInAt(2, Location::RequiresRegister());
1541 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001542 }
1543}
1544
1545void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1546 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001547 LocationSummary* locations = instruction->GetLocations();
1548 bool needs_runtime_call = locations->WillCall();
1549
1550 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001551 // Note: if heap poisoning is enabled, pAputObject takes cares
1552 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001553 codegen_->InvokeRuntime(
1554 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001555 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001558 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001559 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001560 Location index = locations->InAt(1);
1561 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001562 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001563 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001564 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001565 {
1566 // We use a block to end the scratch scope before the write barrier, thus
1567 // freeing the temporary registers so they can be used in `MarkGCCard`.
1568 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001569
Roland Levillain4d027112015-07-01 15:41:14 +01001570 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1571 DCHECK(value.IsW());
1572 Register temp = temps.AcquireW();
1573 __ Mov(temp, value.W());
1574 GetAssembler()->PoisonHeapReference(temp.W());
1575 source = temp;
1576 }
1577
Alexandre Rames97833a02015-04-16 15:07:12 +01001578 if (index.IsConstant()) {
1579 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1580 destination = HeapOperand(obj, offset);
1581 } else {
1582 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001583 __ Add(temp, obj, offset);
1584 destination = HeapOperand(temp,
1585 XRegisterFrom(index),
1586 LSL,
1587 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001588 }
1589
Roland Levillain4d027112015-07-01 15:41:14 +01001590 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001591 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001592 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001593 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001594 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001595 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001596 }
1597}
1598
Alexandre Rames67555f72014-11-18 10:55:16 +00001599void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1600 LocationSummary* locations =
1601 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1602 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001603 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001604 if (instruction->HasUses()) {
1605 locations->SetOut(Location::SameAsFirstInput());
1606 }
1607}
1608
1609void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001610 LocationSummary* locations = instruction->GetLocations();
1611 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1612 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001613 codegen_->AddSlowPath(slow_path);
1614
1615 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1616 __ B(slow_path->GetEntryLabel(), hs);
1617}
1618
1619void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1621 instruction, LocationSummary::kCallOnSlowPath);
1622 locations->SetInAt(0, Location::RequiresRegister());
1623 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001624 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001625}
1626
1627void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001628 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 Register obj = InputRegisterAt(instruction, 0);;
1630 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001631 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001632
Alexandre Rames3e69f162014-12-10 10:36:50 +00001633 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1634 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001635 codegen_->AddSlowPath(slow_path);
1636
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001637 // Avoid null check if we know obj is not null.
1638 if (instruction->MustDoNullCheck()) {
1639 __ Cbz(obj, slow_path->GetExitLabel());
1640 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001641 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001642 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001643 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001644 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001645 // The checkcast succeeds if the classes are equal (fast path).
1646 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001647 __ B(ne, slow_path->GetEntryLabel());
1648 __ Bind(slow_path->GetExitLabel());
1649}
1650
1651void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1652 LocationSummary* locations =
1653 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1654 locations->SetInAt(0, Location::RequiresRegister());
1655 if (check->HasUses()) {
1656 locations->SetOut(Location::SameAsFirstInput());
1657 }
1658}
1659
1660void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1661 // We assume the class is not null.
1662 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1663 check->GetLoadClass(), check, check->GetDexPc(), true);
1664 codegen_->AddSlowPath(slow_path);
1665 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1666}
1667
Roland Levillain7f63c522015-07-13 15:54:55 +00001668static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1669 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1670 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1671}
1672
Serban Constantinescu02164b32014-11-13 14:05:07 +00001673void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001674 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001675 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1676 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001677 switch (in_type) {
1678 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001679 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001680 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1682 break;
1683 }
1684 case Primitive::kPrimFloat:
1685 case Primitive::kPrimDouble: {
1686 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001687 locations->SetInAt(1,
1688 IsFloatingPointZeroConstant(compare->InputAt(1))
1689 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1690 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001691 locations->SetOut(Location::RequiresRegister());
1692 break;
1693 }
1694 default:
1695 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1696 }
1697}
1698
1699void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1700 Primitive::Type in_type = compare->InputAt(0)->GetType();
1701
1702 // 0 if: left == right
1703 // 1 if: left > right
1704 // -1 if: left < right
1705 switch (in_type) {
1706 case Primitive::kPrimLong: {
1707 Register result = OutputRegister(compare);
1708 Register left = InputRegisterAt(compare, 0);
1709 Operand right = InputOperandAt(compare, 1);
1710
1711 __ Cmp(left, right);
1712 __ Cset(result, ne);
1713 __ Cneg(result, result, lt);
1714 break;
1715 }
1716 case Primitive::kPrimFloat:
1717 case Primitive::kPrimDouble: {
1718 Register result = OutputRegister(compare);
1719 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001720 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001721 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1722 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001723 __ Fcmp(left, 0.0);
1724 } else {
1725 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1726 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001727 if (compare->IsGtBias()) {
1728 __ Cset(result, ne);
1729 } else {
1730 __ Csetm(result, ne);
1731 }
1732 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001733 break;
1734 }
1735 default:
1736 LOG(FATAL) << "Unimplemented compare type " << in_type;
1737 }
1738}
1739
1740void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1741 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001742
1743 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1744 locations->SetInAt(0, Location::RequiresFpuRegister());
1745 locations->SetInAt(1,
1746 IsFloatingPointZeroConstant(instruction->InputAt(1))
1747 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1748 : Location::RequiresFpuRegister());
1749 } else {
1750 // Integer cases.
1751 locations->SetInAt(0, Location::RequiresRegister());
1752 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1753 }
1754
Alexandre Rames5319def2014-10-23 10:03:10 +01001755 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001757 }
1758}
1759
1760void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1761 if (!instruction->NeedsMaterialization()) {
1762 return;
1763 }
1764
1765 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001766 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001767 IfCondition if_cond = instruction->GetCondition();
1768 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001769
Roland Levillain7f63c522015-07-13 15:54:55 +00001770 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1771 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1772 if (locations->InAt(1).IsConstant()) {
1773 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1774 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1775 __ Fcmp(lhs, 0.0);
1776 } else {
1777 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1778 }
1779 __ Cset(res, arm64_cond);
1780 if (instruction->IsFPConditionTrueIfNaN()) {
1781 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1782 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1783 } else if (instruction->IsFPConditionFalseIfNaN()) {
1784 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1785 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1786 }
1787 } else {
1788 // Integer cases.
1789 Register lhs = InputRegisterAt(instruction, 0);
1790 Operand rhs = InputOperandAt(instruction, 1);
1791 __ Cmp(lhs, rhs);
1792 __ Cset(res, arm64_cond);
1793 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001794}
1795
1796#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1797 M(Equal) \
1798 M(NotEqual) \
1799 M(LessThan) \
1800 M(LessThanOrEqual) \
1801 M(GreaterThan) \
1802 M(GreaterThanOrEqual)
1803#define DEFINE_CONDITION_VISITORS(Name) \
1804void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1805void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1806FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001807#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001808#undef FOR_EACH_CONDITION_INSTRUCTION
1809
Zheng Xuc6667102015-05-15 16:08:45 +08001810void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1811 DCHECK(instruction->IsDiv() || instruction->IsRem());
1812
1813 LocationSummary* locations = instruction->GetLocations();
1814 Location second = locations->InAt(1);
1815 DCHECK(second.IsConstant());
1816
1817 Register out = OutputRegister(instruction);
1818 Register dividend = InputRegisterAt(instruction, 0);
1819 int64_t imm = Int64FromConstant(second.GetConstant());
1820 DCHECK(imm == 1 || imm == -1);
1821
1822 if (instruction->IsRem()) {
1823 __ Mov(out, 0);
1824 } else {
1825 if (imm == 1) {
1826 __ Mov(out, dividend);
1827 } else {
1828 __ Neg(out, dividend);
1829 }
1830 }
1831}
1832
1833void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1834 DCHECK(instruction->IsDiv() || instruction->IsRem());
1835
1836 LocationSummary* locations = instruction->GetLocations();
1837 Location second = locations->InAt(1);
1838 DCHECK(second.IsConstant());
1839
1840 Register out = OutputRegister(instruction);
1841 Register dividend = InputRegisterAt(instruction, 0);
1842 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001843 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001844 DCHECK(IsPowerOfTwo(abs_imm));
1845 int ctz_imm = CTZ(abs_imm);
1846
1847 UseScratchRegisterScope temps(GetVIXLAssembler());
1848 Register temp = temps.AcquireSameSizeAs(out);
1849
1850 if (instruction->IsDiv()) {
1851 __ Add(temp, dividend, abs_imm - 1);
1852 __ Cmp(dividend, 0);
1853 __ Csel(out, temp, dividend, lt);
1854 if (imm > 0) {
1855 __ Asr(out, out, ctz_imm);
1856 } else {
1857 __ Neg(out, Operand(out, ASR, ctz_imm));
1858 }
1859 } else {
1860 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1861 __ Asr(temp, dividend, bits - 1);
1862 __ Lsr(temp, temp, bits - ctz_imm);
1863 __ Add(out, dividend, temp);
1864 __ And(out, out, abs_imm - 1);
1865 __ Sub(out, out, temp);
1866 }
1867}
1868
1869void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1870 DCHECK(instruction->IsDiv() || instruction->IsRem());
1871
1872 LocationSummary* locations = instruction->GetLocations();
1873 Location second = locations->InAt(1);
1874 DCHECK(second.IsConstant());
1875
1876 Register out = OutputRegister(instruction);
1877 Register dividend = InputRegisterAt(instruction, 0);
1878 int64_t imm = Int64FromConstant(second.GetConstant());
1879
1880 Primitive::Type type = instruction->GetResultType();
1881 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1882
1883 int64_t magic;
1884 int shift;
1885 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1886
1887 UseScratchRegisterScope temps(GetVIXLAssembler());
1888 Register temp = temps.AcquireSameSizeAs(out);
1889
1890 // temp = get_high(dividend * magic)
1891 __ Mov(temp, magic);
1892 if (type == Primitive::kPrimLong) {
1893 __ Smulh(temp, dividend, temp);
1894 } else {
1895 __ Smull(temp.X(), dividend, temp);
1896 __ Lsr(temp.X(), temp.X(), 32);
1897 }
1898
1899 if (imm > 0 && magic < 0) {
1900 __ Add(temp, temp, dividend);
1901 } else if (imm < 0 && magic > 0) {
1902 __ Sub(temp, temp, dividend);
1903 }
1904
1905 if (shift != 0) {
1906 __ Asr(temp, temp, shift);
1907 }
1908
1909 if (instruction->IsDiv()) {
1910 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1911 } else {
1912 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1913 // TODO: Strength reduction for msub.
1914 Register temp_imm = temps.AcquireSameSizeAs(out);
1915 __ Mov(temp_imm, imm);
1916 __ Msub(out, temp, temp_imm, dividend);
1917 }
1918}
1919
1920void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1921 DCHECK(instruction->IsDiv() || instruction->IsRem());
1922 Primitive::Type type = instruction->GetResultType();
1923 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1924
1925 LocationSummary* locations = instruction->GetLocations();
1926 Register out = OutputRegister(instruction);
1927 Location second = locations->InAt(1);
1928
1929 if (second.IsConstant()) {
1930 int64_t imm = Int64FromConstant(second.GetConstant());
1931
1932 if (imm == 0) {
1933 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1934 } else if (imm == 1 || imm == -1) {
1935 DivRemOneOrMinusOne(instruction);
1936 } else if (IsPowerOfTwo(std::abs(imm))) {
1937 DivRemByPowerOfTwo(instruction);
1938 } else {
1939 DCHECK(imm <= -2 || imm >= 2);
1940 GenerateDivRemWithAnyConstant(instruction);
1941 }
1942 } else {
1943 Register dividend = InputRegisterAt(instruction, 0);
1944 Register divisor = InputRegisterAt(instruction, 1);
1945 if (instruction->IsDiv()) {
1946 __ Sdiv(out, dividend, divisor);
1947 } else {
1948 UseScratchRegisterScope temps(GetVIXLAssembler());
1949 Register temp = temps.AcquireSameSizeAs(out);
1950 __ Sdiv(temp, dividend, divisor);
1951 __ Msub(out, temp, divisor, dividend);
1952 }
1953 }
1954}
1955
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001956void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1957 LocationSummary* locations =
1958 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1959 switch (div->GetResultType()) {
1960 case Primitive::kPrimInt:
1961 case Primitive::kPrimLong:
1962 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001963 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1965 break;
1966
1967 case Primitive::kPrimFloat:
1968 case Primitive::kPrimDouble:
1969 locations->SetInAt(0, Location::RequiresFpuRegister());
1970 locations->SetInAt(1, Location::RequiresFpuRegister());
1971 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1972 break;
1973
1974 default:
1975 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1976 }
1977}
1978
1979void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1980 Primitive::Type type = div->GetResultType();
1981 switch (type) {
1982 case Primitive::kPrimInt:
1983 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001984 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001985 break;
1986
1987 case Primitive::kPrimFloat:
1988 case Primitive::kPrimDouble:
1989 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1990 break;
1991
1992 default:
1993 LOG(FATAL) << "Unexpected div type " << type;
1994 }
1995}
1996
Alexandre Rames67555f72014-11-18 10:55:16 +00001997void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1998 LocationSummary* locations =
1999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2000 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2001 if (instruction->HasUses()) {
2002 locations->SetOut(Location::SameAsFirstInput());
2003 }
2004}
2005
2006void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2007 SlowPathCodeARM64* slow_path =
2008 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2009 codegen_->AddSlowPath(slow_path);
2010 Location value = instruction->GetLocations()->InAt(0);
2011
Alexandre Rames3e69f162014-12-10 10:36:50 +00002012 Primitive::Type type = instruction->GetType();
2013
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002014 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2015 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002016 return;
2017 }
2018
Alexandre Rames67555f72014-11-18 10:55:16 +00002019 if (value.IsConstant()) {
2020 int64_t divisor = Int64ConstantFrom(value);
2021 if (divisor == 0) {
2022 __ B(slow_path->GetEntryLabel());
2023 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002024 // A division by a non-null constant is valid. We don't need to perform
2025 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002026 }
2027 } else {
2028 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2029 }
2030}
2031
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002032void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2033 LocationSummary* locations =
2034 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2035 locations->SetOut(Location::ConstantLocation(constant));
2036}
2037
2038void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2039 UNUSED(constant);
2040 // Will be generated at use site.
2041}
2042
Alexandre Rames5319def2014-10-23 10:03:10 +01002043void LocationsBuilderARM64::VisitExit(HExit* exit) {
2044 exit->SetLocations(nullptr);
2045}
2046
2047void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002048 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002049}
2050
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002051void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2052 LocationSummary* locations =
2053 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2054 locations->SetOut(Location::ConstantLocation(constant));
2055}
2056
2057void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2058 UNUSED(constant);
2059 // Will be generated at use site.
2060}
2061
David Brazdilfc6a86a2015-06-26 10:33:45 +00002062void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002063 DCHECK(!successor->IsExitBlock());
2064 HBasicBlock* block = got->GetBlock();
2065 HInstruction* previous = got->GetPrevious();
2066 HLoopInformation* info = block->GetLoopInformation();
2067
David Brazdil46e2a392015-03-16 17:31:52 +00002068 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002069 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2070 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2071 return;
2072 }
2073 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2074 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2075 }
2076 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002077 __ B(codegen_->GetLabelOf(successor));
2078 }
2079}
2080
David Brazdilfc6a86a2015-06-26 10:33:45 +00002081void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2082 got->SetLocations(nullptr);
2083}
2084
2085void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2086 HandleGoto(got, got->GetSuccessor());
2087}
2088
2089void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2090 try_boundary->SetLocations(nullptr);
2091}
2092
2093void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2094 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2095 if (!successor->IsExitBlock()) {
2096 HandleGoto(try_boundary, successor);
2097 }
2098}
2099
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002100void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2101 vixl::Label* true_target,
2102 vixl::Label* false_target,
2103 vixl::Label* always_true_target) {
2104 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002105 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002106
Serban Constantinescu02164b32014-11-13 14:05:07 +00002107 if (cond->IsIntConstant()) {
2108 int32_t cond_value = cond->AsIntConstant()->GetValue();
2109 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002110 if (always_true_target != nullptr) {
2111 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002112 }
2113 return;
2114 } else {
2115 DCHECK_EQ(cond_value, 0);
2116 }
2117 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002118 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002119 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002120 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002121 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002122 } else {
2123 // The condition instruction has not been materialized, use its inputs as
2124 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002125 Primitive::Type type =
2126 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2127
2128 if (Primitive::IsFloatingPointType(type)) {
2129 // FP compares don't like null false_targets.
2130 if (false_target == nullptr) {
2131 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002132 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002133 FPRegister lhs = InputFPRegisterAt(condition, 0);
2134 if (condition->GetLocations()->InAt(1).IsConstant()) {
2135 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2136 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2137 __ Fcmp(lhs, 0.0);
2138 } else {
2139 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2140 }
2141 if (condition->IsFPConditionTrueIfNaN()) {
2142 __ B(vs, true_target); // VS for unordered.
2143 } else if (condition->IsFPConditionFalseIfNaN()) {
2144 __ B(vs, false_target); // VS for unordered.
2145 }
2146 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002147 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002148 // Integer cases.
2149 Register lhs = InputRegisterAt(condition, 0);
2150 Operand rhs = InputOperandAt(condition, 1);
2151 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2152 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2153 switch (arm64_cond) {
2154 case eq:
2155 __ Cbz(lhs, true_target);
2156 break;
2157 case ne:
2158 __ Cbnz(lhs, true_target);
2159 break;
2160 case lt:
2161 // Test the sign bit and branch accordingly.
2162 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2163 break;
2164 case ge:
2165 // Test the sign bit and branch accordingly.
2166 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2167 break;
2168 default:
2169 // Without the `static_cast` the compiler throws an error for
2170 // `-Werror=sign-promo`.
2171 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2172 }
2173 } else {
2174 __ Cmp(lhs, rhs);
2175 __ B(arm64_cond, true_target);
2176 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002177 }
2178 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002179 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002180 __ B(false_target);
2181 }
2182}
2183
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002184void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2185 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2186 HInstruction* cond = if_instr->InputAt(0);
2187 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2188 locations->SetInAt(0, Location::RequiresRegister());
2189 }
2190}
2191
2192void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2193 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2194 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2195 vixl::Label* always_true_target = true_target;
2196 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2197 if_instr->IfTrueSuccessor())) {
2198 always_true_target = nullptr;
2199 }
2200 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2201 if_instr->IfFalseSuccessor())) {
2202 false_target = nullptr;
2203 }
2204 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2205}
2206
2207void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2208 LocationSummary* locations = new (GetGraph()->GetArena())
2209 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2210 HInstruction* cond = deoptimize->InputAt(0);
2211 DCHECK(cond->IsCondition());
2212 if (cond->AsCondition()->NeedsMaterialization()) {
2213 locations->SetInAt(0, Location::RequiresRegister());
2214 }
2215}
2216
2217void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2218 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2219 DeoptimizationSlowPathARM64(deoptimize);
2220 codegen_->AddSlowPath(slow_path);
2221 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2222 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2223}
2224
Alexandre Rames5319def2014-10-23 10:03:10 +01002225void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002226 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002227}
2228
2229void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002230 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002231}
2232
2233void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002234 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002235}
2236
2237void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002238 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002239}
2240
Alexandre Rames67555f72014-11-18 10:55:16 +00002241void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2242 LocationSummary::CallKind call_kind =
2243 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002247 // The output does overlap inputs.
2248 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002249}
2250
2251void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2252 LocationSummary* locations = instruction->GetLocations();
2253 Register obj = InputRegisterAt(instruction, 0);;
2254 Register cls = InputRegisterAt(instruction, 1);;
2255 Register out = OutputRegister(instruction);
2256
2257 vixl::Label done;
2258
2259 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002260 // Avoid null check if we know `obj` is not null.
2261 if (instruction->MustDoNullCheck()) {
2262 __ Mov(out, 0);
2263 __ Cbz(obj, &done);
2264 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002265
2266 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002267 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002268 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002269 __ Cmp(out, cls);
2270 if (instruction->IsClassFinal()) {
2271 // Classes must be equal for the instanceof to succeed.
2272 __ Cset(out, eq);
2273 } else {
2274 // If the classes are not equal, we go into a slow path.
2275 DCHECK(locations->OnlyCallsOnSlowPath());
2276 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002277 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2278 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002279 codegen_->AddSlowPath(slow_path);
2280 __ B(ne, slow_path->GetEntryLabel());
2281 __ Mov(out, 1);
2282 __ Bind(slow_path->GetExitLabel());
2283 }
2284
2285 __ Bind(&done);
2286}
2287
Alexandre Rames5319def2014-10-23 10:03:10 +01002288void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2290 locations->SetOut(Location::ConstantLocation(constant));
2291}
2292
2293void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2294 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002295 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002296}
2297
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002298void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2299 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2300 locations->SetOut(Location::ConstantLocation(constant));
2301}
2302
2303void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2304 // Will be generated at use site.
2305 UNUSED(constant);
2306}
2307
Alexandre Rames5319def2014-10-23 10:03:10 +01002308void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002309 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002310 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002311}
2312
Alexandre Rames67555f72014-11-18 10:55:16 +00002313void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2314 HandleInvoke(invoke);
2315}
2316
2317void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2318 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002319 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2320 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2321 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002322 Location receiver = invoke->GetLocations()->InAt(0);
2323 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002324 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002325
2326 // The register ip1 is required to be used for the hidden argument in
2327 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002328 MacroAssembler* masm = GetVIXLAssembler();
2329 UseScratchRegisterScope scratch_scope(masm);
2330 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002331 scratch_scope.Exclude(ip1);
2332 __ Mov(ip1, invoke->GetDexMethodIndex());
2333
2334 // temp = object->GetClass();
2335 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002336 __ Ldr(temp.W(), StackOperandFrom(receiver));
2337 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002338 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002339 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002340 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002341 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002342 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002343 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002344 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002345 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002346 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002347 // lr();
2348 __ Blr(lr);
2349 DCHECK(!codegen_->IsLeafMethod());
2350 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2351}
2352
2353void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002354 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2355 if (intrinsic.TryDispatch(invoke)) {
2356 return;
2357 }
2358
Alexandre Rames67555f72014-11-18 10:55:16 +00002359 HandleInvoke(invoke);
2360}
2361
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002362void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002363 // When we do not run baseline, explicit clinit checks triggered by static
2364 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2365 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002366
Andreas Gampe878d58c2015-01-15 23:24:00 -08002367 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2368 if (intrinsic.TryDispatch(invoke)) {
2369 return;
2370 }
2371
Alexandre Rames67555f72014-11-18 10:55:16 +00002372 HandleInvoke(invoke);
2373}
2374
Andreas Gampe878d58c2015-01-15 23:24:00 -08002375static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2376 if (invoke->GetLocations()->Intrinsified()) {
2377 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2378 intrinsic.Dispatch(invoke);
2379 return true;
2380 }
2381 return false;
2382}
2383
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002384void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002385 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002386 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002387
2388 // TODO: Implement all kinds of calls:
2389 // 1) boot -> boot
2390 // 2) app -> boot
2391 // 3) app -> app
2392 //
2393 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2394
Jeff Hao848f70a2014-01-15 13:49:50 -08002395 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002396 Register reg = XRegisterFrom(temp);
Jeff Hao848f70a2014-01-15 13:49:50 -08002397 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002398 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002399 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002400 __ Ldr(lr, MemOperand(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002401 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002402 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002403 __ Blr(lr);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002404 } else if (invoke->IsRecursive()) {
2405 __ Bl(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002406 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002407 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002408 Register reg = XRegisterFrom(temp);
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002409 Register method_reg;
2410 if (current_method.IsRegister()) {
2411 method_reg = XRegisterFrom(current_method);
2412 } else {
2413 DCHECK(invoke->GetLocations()->Intrinsified());
2414 DCHECK(!current_method.IsValid());
2415 method_reg = reg;
2416 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2417 }
2418
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002419 // temp = current_method->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002420 __ Ldr(reg.W(), MemOperand(method_reg.X(),
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002421 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2422 // temp = temp[index_in_cache];
2423 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2424 // lr = temp->entry_point_from_quick_compiled_code_;
2425 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2426 kArm64WordSize).Int32Value()));
2427 // lr();
2428 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002429 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002430
Andreas Gampe878d58c2015-01-15 23:24:00 -08002431 DCHECK(!IsLeafMethod());
2432}
2433
2434void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002435 // When we do not run baseline, explicit clinit checks triggered by static
2436 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2437 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002438
Andreas Gampe878d58c2015-01-15 23:24:00 -08002439 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2440 return;
2441 }
2442
Alexandre Ramesd921d642015-04-16 15:07:16 +01002443 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002444 LocationSummary* locations = invoke->GetLocations();
2445 codegen_->GenerateStaticOrDirectCall(
2446 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002447 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002448}
2449
2450void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002451 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2452 return;
2453 }
2454
Alexandre Rames5319def2014-10-23 10:03:10 +01002455 LocationSummary* locations = invoke->GetLocations();
2456 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002457 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2458 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2459 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002460 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002461 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002462
Alexandre Ramesd921d642015-04-16 15:07:16 +01002463 BlockPoolsScope block_pools(GetVIXLAssembler());
2464
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002465 DCHECK(receiver.IsRegister());
2466 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002467 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002468 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002469 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002470 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002471 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002472 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002473 // lr();
2474 __ Blr(lr);
2475 DCHECK(!codegen_->IsLeafMethod());
2476 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2477}
2478
Alexandre Rames67555f72014-11-18 10:55:16 +00002479void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2480 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2481 : LocationSummary::kNoCall;
2482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002483 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002484 locations->SetOut(Location::RequiresRegister());
2485}
2486
2487void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2488 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002489 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002490 if (cls->IsReferrersClass()) {
2491 DCHECK(!cls->CanCallRuntime());
2492 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002493 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002494 } else {
2495 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002496 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002497 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002498 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002499
2500 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2501 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2502 codegen_->AddSlowPath(slow_path);
2503 __ Cbz(out, slow_path->GetEntryLabel());
2504 if (cls->MustGenerateClinitCheck()) {
2505 GenerateClassInitializationCheck(slow_path, out);
2506 } else {
2507 __ Bind(slow_path->GetExitLabel());
2508 }
2509 }
2510}
2511
David Brazdilcb1c0552015-08-04 16:22:25 +01002512static MemOperand GetExceptionTlsAddress() {
2513 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2514}
2515
Alexandre Rames67555f72014-11-18 10:55:16 +00002516void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2517 LocationSummary* locations =
2518 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2519 locations->SetOut(Location::RequiresRegister());
2520}
2521
2522void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002523 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2524}
2525
2526void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2527 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2528}
2529
2530void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2531 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002532}
2533
Alexandre Rames5319def2014-10-23 10:03:10 +01002534void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2535 load->SetLocations(nullptr);
2536}
2537
2538void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2539 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002540 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002541}
2542
Alexandre Rames67555f72014-11-18 10:55:16 +00002543void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2544 LocationSummary* locations =
2545 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002546 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002547 locations->SetOut(Location::RequiresRegister());
2548}
2549
2550void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2551 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2552 codegen_->AddSlowPath(slow_path);
2553
2554 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002555 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002556 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002557 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002558 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002559 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002560 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002561 __ Cbz(out, slow_path->GetEntryLabel());
2562 __ Bind(slow_path->GetExitLabel());
2563}
2564
Alexandre Rames5319def2014-10-23 10:03:10 +01002565void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2566 local->SetLocations(nullptr);
2567}
2568
2569void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2570 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2571}
2572
2573void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2574 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2575 locations->SetOut(Location::ConstantLocation(constant));
2576}
2577
2578void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2579 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002580 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002581}
2582
Alexandre Rames67555f72014-11-18 10:55:16 +00002583void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2584 LocationSummary* locations =
2585 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2586 InvokeRuntimeCallingConvention calling_convention;
2587 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2588}
2589
2590void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2591 codegen_->InvokeRuntime(instruction->IsEnter()
2592 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2593 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002594 instruction->GetDexPc(),
2595 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002596 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002597}
2598
Alexandre Rames42d641b2014-10-27 14:00:51 +00002599void LocationsBuilderARM64::VisitMul(HMul* mul) {
2600 LocationSummary* locations =
2601 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2602 switch (mul->GetResultType()) {
2603 case Primitive::kPrimInt:
2604 case Primitive::kPrimLong:
2605 locations->SetInAt(0, Location::RequiresRegister());
2606 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002608 break;
2609
2610 case Primitive::kPrimFloat:
2611 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002612 locations->SetInAt(0, Location::RequiresFpuRegister());
2613 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002614 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002615 break;
2616
2617 default:
2618 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2619 }
2620}
2621
2622void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2623 switch (mul->GetResultType()) {
2624 case Primitive::kPrimInt:
2625 case Primitive::kPrimLong:
2626 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2627 break;
2628
2629 case Primitive::kPrimFloat:
2630 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002631 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002632 break;
2633
2634 default:
2635 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2636 }
2637}
2638
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002639void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2640 LocationSummary* locations =
2641 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2642 switch (neg->GetResultType()) {
2643 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002644 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002645 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002647 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002648
2649 case Primitive::kPrimFloat:
2650 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002651 locations->SetInAt(0, Location::RequiresFpuRegister());
2652 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002653 break;
2654
2655 default:
2656 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2657 }
2658}
2659
2660void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2661 switch (neg->GetResultType()) {
2662 case Primitive::kPrimInt:
2663 case Primitive::kPrimLong:
2664 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2665 break;
2666
2667 case Primitive::kPrimFloat:
2668 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002669 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002670 break;
2671
2672 default:
2673 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2674 }
2675}
2676
2677void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2678 LocationSummary* locations =
2679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2680 InvokeRuntimeCallingConvention calling_convention;
2681 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002682 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002683 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002684 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002685 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002686 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002687}
2688
2689void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2690 LocationSummary* locations = instruction->GetLocations();
2691 InvokeRuntimeCallingConvention calling_convention;
2692 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2693 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002694 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002695 // Note: if heap poisoning is enabled, the entry point takes cares
2696 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002697 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002698 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2699 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002700 instruction->GetDexPc(),
2701 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002702 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002703}
2704
Alexandre Rames5319def2014-10-23 10:03:10 +01002705void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2706 LocationSummary* locations =
2707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2708 InvokeRuntimeCallingConvention calling_convention;
2709 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002710 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002711 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002712 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002713}
2714
2715void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2716 LocationSummary* locations = instruction->GetLocations();
2717 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2718 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002719 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002720 // Note: if heap poisoning is enabled, the entry point takes cares
2721 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002722 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002723 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2724 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002725 instruction->GetDexPc(),
2726 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002727 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002728}
2729
2730void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002732 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002734}
2735
2736void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002737 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002738 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002739 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002740 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002741 break;
2742
2743 default:
2744 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2745 }
2746}
2747
David Brazdil66d126e2015-04-03 16:02:44 +01002748void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2750 locations->SetInAt(0, Location::RequiresRegister());
2751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2752}
2753
2754void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002755 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2756}
2757
Alexandre Rames5319def2014-10-23 10:03:10 +01002758void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2761 locations->SetInAt(0, Location::RequiresRegister());
2762 if (instruction->HasUses()) {
2763 locations->SetOut(Location::SameAsFirstInput());
2764 }
2765}
2766
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002767void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002768 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2769 return;
2770 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002771
Alexandre Ramesd921d642015-04-16 15:07:16 +01002772 BlockPoolsScope block_pools(GetVIXLAssembler());
2773 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002774 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2775 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2776}
2777
2778void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002779 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2780 codegen_->AddSlowPath(slow_path);
2781
2782 LocationSummary* locations = instruction->GetLocations();
2783 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002784
2785 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002786}
2787
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002788void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2789 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2790 GenerateImplicitNullCheck(instruction);
2791 } else {
2792 GenerateExplicitNullCheck(instruction);
2793 }
2794}
2795
Alexandre Rames67555f72014-11-18 10:55:16 +00002796void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2797 HandleBinaryOp(instruction);
2798}
2799
2800void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2801 HandleBinaryOp(instruction);
2802}
2803
Alexandre Rames3e69f162014-12-10 10:36:50 +00002804void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2805 LOG(FATAL) << "Unreachable";
2806}
2807
2808void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2809 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2810}
2811
Alexandre Rames5319def2014-10-23 10:03:10 +01002812void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2814 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2815 if (location.IsStackSlot()) {
2816 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2817 } else if (location.IsDoubleStackSlot()) {
2818 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2819 }
2820 locations->SetOut(location);
2821}
2822
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002823void InstructionCodeGeneratorARM64::VisitParameterValue(
2824 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002825 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002826}
2827
2828void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2829 LocationSummary* locations =
2830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002831 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002832}
2833
2834void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2835 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2836 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002837}
2838
2839void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2841 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2842 locations->SetInAt(i, Location::Any());
2843 }
2844 locations->SetOut(Location::Any());
2845}
2846
2847void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002848 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002849 LOG(FATAL) << "Unreachable";
2850}
2851
Serban Constantinescu02164b32014-11-13 14:05:07 +00002852void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002853 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002854 LocationSummary::CallKind call_kind =
2855 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2857
2858 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002859 case Primitive::kPrimInt:
2860 case Primitive::kPrimLong:
2861 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002862 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002863 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2864 break;
2865
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002866 case Primitive::kPrimFloat:
2867 case Primitive::kPrimDouble: {
2868 InvokeRuntimeCallingConvention calling_convention;
2869 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2870 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2871 locations->SetOut(calling_convention.GetReturnLocation(type));
2872
2873 break;
2874 }
2875
Serban Constantinescu02164b32014-11-13 14:05:07 +00002876 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002877 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002878 }
2879}
2880
2881void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2882 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002883
Serban Constantinescu02164b32014-11-13 14:05:07 +00002884 switch (type) {
2885 case Primitive::kPrimInt:
2886 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002887 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002888 break;
2889 }
2890
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002891 case Primitive::kPrimFloat:
2892 case Primitive::kPrimDouble: {
2893 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2894 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002895 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002896 break;
2897 }
2898
Serban Constantinescu02164b32014-11-13 14:05:07 +00002899 default:
2900 LOG(FATAL) << "Unexpected rem type " << type;
2901 }
2902}
2903
Calin Juravle27df7582015-04-17 19:12:31 +01002904void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2905 memory_barrier->SetLocations(nullptr);
2906}
2907
2908void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2909 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2910}
2911
Alexandre Rames5319def2014-10-23 10:03:10 +01002912void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2913 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2914 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002915 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002916}
2917
2918void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002919 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002920 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002921}
2922
2923void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2924 instruction->SetLocations(nullptr);
2925}
2926
2927void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002928 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002929 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002930}
2931
Serban Constantinescu02164b32014-11-13 14:05:07 +00002932void LocationsBuilderARM64::VisitShl(HShl* shl) {
2933 HandleShift(shl);
2934}
2935
2936void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2937 HandleShift(shl);
2938}
2939
2940void LocationsBuilderARM64::VisitShr(HShr* shr) {
2941 HandleShift(shr);
2942}
2943
2944void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2945 HandleShift(shr);
2946}
2947
Alexandre Rames5319def2014-10-23 10:03:10 +01002948void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2949 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2950 Primitive::Type field_type = store->InputAt(1)->GetType();
2951 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002952 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002953 case Primitive::kPrimBoolean:
2954 case Primitive::kPrimByte:
2955 case Primitive::kPrimChar:
2956 case Primitive::kPrimShort:
2957 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002958 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002959 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2960 break;
2961
2962 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002963 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002964 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2965 break;
2966
2967 default:
2968 LOG(FATAL) << "Unimplemented local type " << field_type;
2969 }
2970}
2971
2972void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002973 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002974}
2975
2976void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002977 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002978}
2979
2980void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002981 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002982}
2983
Alexandre Rames67555f72014-11-18 10:55:16 +00002984void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002985 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002986}
2987
2988void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002989 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002990}
2991
2992void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002993 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002994}
2995
Alexandre Rames67555f72014-11-18 10:55:16 +00002996void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002997 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002998}
2999
3000void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3002}
3003
3004void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003005 HBasicBlock* block = instruction->GetBlock();
3006 if (block->GetLoopInformation() != nullptr) {
3007 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3008 // The back edge will generate the suspend check.
3009 return;
3010 }
3011 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3012 // The goto will generate the suspend check.
3013 return;
3014 }
3015 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003016}
3017
3018void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3019 temp->SetLocations(nullptr);
3020}
3021
3022void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3023 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003024 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003025}
3026
Alexandre Rames67555f72014-11-18 10:55:16 +00003027void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3028 LocationSummary* locations =
3029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3030 InvokeRuntimeCallingConvention calling_convention;
3031 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3032}
3033
3034void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3035 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003036 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003037 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003038}
3039
3040void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3041 LocationSummary* locations =
3042 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3043 Primitive::Type input_type = conversion->GetInputType();
3044 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003045 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003046 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3047 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3048 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3049 }
3050
Alexandre Rames542361f2015-01-29 16:57:31 +00003051 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003052 locations->SetInAt(0, Location::RequiresFpuRegister());
3053 } else {
3054 locations->SetInAt(0, Location::RequiresRegister());
3055 }
3056
Alexandre Rames542361f2015-01-29 16:57:31 +00003057 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003058 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3059 } else {
3060 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3061 }
3062}
3063
3064void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3065 Primitive::Type result_type = conversion->GetResultType();
3066 Primitive::Type input_type = conversion->GetInputType();
3067
3068 DCHECK_NE(input_type, result_type);
3069
Alexandre Rames542361f2015-01-29 16:57:31 +00003070 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003071 int result_size = Primitive::ComponentSize(result_type);
3072 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003073 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003074 Register output = OutputRegister(conversion);
3075 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003076 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3077 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3078 } else if ((result_type == Primitive::kPrimChar) ||
3079 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3080 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003081 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003082 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003083 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003084 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003085 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003086 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003087 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3088 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003089 } else if (Primitive::IsFloatingPointType(result_type) &&
3090 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003091 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3092 } else {
3093 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3094 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003095 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003096}
Alexandre Rames67555f72014-11-18 10:55:16 +00003097
Serban Constantinescu02164b32014-11-13 14:05:07 +00003098void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3099 HandleShift(ushr);
3100}
3101
3102void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3103 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003104}
3105
3106void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3107 HandleBinaryOp(instruction);
3108}
3109
3110void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3111 HandleBinaryOp(instruction);
3112}
3113
Calin Juravleb1498f62015-02-16 13:13:29 +00003114void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3115 // Nothing to do, this should be removed during prepare for register allocator.
3116 UNUSED(instruction);
3117 LOG(FATAL) << "Unreachable";
3118}
3119
3120void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3121 // Nothing to do, this should be removed during prepare for register allocator.
3122 UNUSED(instruction);
3123 LOG(FATAL) << "Unreachable";
3124}
3125
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003126void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3127 DCHECK(codegen_->IsBaseline());
3128 LocationSummary* locations =
3129 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3130 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3131}
3132
3133void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3134 DCHECK(codegen_->IsBaseline());
3135 // Will be generated at use site.
3136}
3137
Alexandre Rames67555f72014-11-18 10:55:16 +00003138#undef __
3139#undef QUICK_ENTRY_POINT
3140
Alexandre Rames5319def2014-10-23 10:03:10 +01003141} // namespace arm64
3142} // namespace art