blob: e5b6df54d9d792dad268efe08fd13b98b10f52b3 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080022#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010080 }
Roland Levillain7f63c522015-07-13 15:54:55 +000081 LOG(FATAL) << "Unreachable";
82 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010083}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010095 } else if (return_type == Primitive::kPrimVoid) {
96 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Zheng Xuda403092015-04-24 17:35:39 +0800109// Calculate memory accessing operand for save/restore live registers.
110static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
111 RegisterSet* register_set,
112 int64_t spill_offset,
113 bool is_save) {
114 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
115 codegen->GetNumberOfCoreRegisters(),
116 register_set->GetFloatingPointRegisters(),
117 codegen->GetNumberOfFloatingPointRegisters()));
118
119 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
120 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000193 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
194 Location index_location,
195 Location length_location)
196 : instruction_(instruction),
197 index_location_(index_location),
198 length_location_(length_location) {}
199
Alexandre Rames5319def2014-10-23 10:03:10 +0100200
Alexandre Rames67555f72014-11-18 10:55:16 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000202 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100203 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100208 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames8158f282015-08-07 10:26:17 +0100215 bool IsFatal() const OVERRIDE { return true; }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 HBoundsCheck* const instruction_;
221 const Location index_location_;
222 const Location length_location_;
223
Alexandre Rames5319def2014-10-23 10:03:10 +0100224 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
225};
226
Alexandre Rames67555f72014-11-18 10:55:16 +0000227class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
228 public:
229 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
233 __ Bind(GetEntryLabel());
234 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000235 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800236 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000237 }
238
Alexandre Rames8158f282015-08-07 10:26:17 +0100239 bool IsFatal() const OVERRIDE { return true; }
240
Alexandre Rames9931f312015-06-19 14:47:01 +0100241 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
242
Alexandre Rames67555f72014-11-18 10:55:16 +0000243 private:
244 HDivZeroCheck* const instruction_;
245 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
246};
247
248class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
249 public:
250 LoadClassSlowPathARM64(HLoadClass* cls,
251 HInstruction* at,
252 uint32_t dex_pc,
253 bool do_clinit)
254 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
255 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
256 }
257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 LocationSummary* locations = at_->GetLocations();
260 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
261
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000264
265 InvokeRuntimeCallingConvention calling_convention;
266 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
268 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800270 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100271 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100273 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000275
276 // Move the class to the desired location.
277 Location out = locations->Out();
278 if (out.IsValid()) {
279 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
280 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000281 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000282 }
283
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 __ B(GetExitLabel());
286 }
287
Alexandre Rames9931f312015-06-19 14:47:01 +0100288 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 private:
291 // The class this slow path will load.
292 HLoadClass* const cls_;
293
294 // The instruction where this slow path is happening.
295 // (Might be the load class or an initialization check).
296 HInstruction* const at_;
297
298 // The dex PC of `at_`.
299 const uint32_t dex_pc_;
300
301 // Whether to initialize the class.
302 const bool do_clinit_;
303
304 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
305};
306
307class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
308 public:
309 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
310
311 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
312 LocationSummary* locations = instruction_->GetLocations();
313 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
314 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
315
316 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000318
319 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800320 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000322 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100323 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000325 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000326
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000327 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328 __ B(GetExitLabel());
329 }
330
Alexandre Rames9931f312015-06-19 14:47:01 +0100331 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 private:
334 HLoadString* const instruction_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
337};
338
Alexandre Rames5319def2014-10-23 10:03:10 +0100339class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
341 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
342
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100345 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000347 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800348 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100349 }
350
Alexandre Rames8158f282015-08-07 10:26:17 +0100351 bool IsFatal() const OVERRIDE { return true; }
352
Alexandre Rames9931f312015-06-19 14:47:01 +0100353 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
354
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 private:
356 HNullCheck* const instruction_;
357
358 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
359};
360
361class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
362 public:
363 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
364 HBasicBlock* successor)
365 : instruction_(instruction), successor_(successor) {}
366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
368 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100369 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000370 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000372 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800373 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000374 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 if (successor_ == nullptr) {
376 __ B(GetReturnLabel());
377 } else {
378 __ B(arm64_codegen->GetLabelOf(successor_));
379 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 }
381
382 vixl::Label* GetReturnLabel() {
383 DCHECK(successor_ == nullptr);
384 return &return_label_;
385 }
386
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100387 HBasicBlock* GetSuccessor() const {
388 return successor_;
389 }
390
Alexandre Rames9931f312015-06-19 14:47:01 +0100391 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
392
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 private:
394 HSuspendCheck* const instruction_;
395 // If not null, the block to branch to after the suspend check.
396 HBasicBlock* const successor_;
397
398 // If `successor_` is null, the label to branch to after the suspend check.
399 vixl::Label return_label_;
400
401 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
402};
403
Alexandre Rames67555f72014-11-18 10:55:16 +0000404class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
405 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000406 TypeCheckSlowPathARM64(HInstruction* instruction,
407 Location class_to_check,
408 Location object_class,
409 uint32_t dex_pc)
410 : instruction_(instruction),
411 class_to_check_(class_to_check),
412 object_class_(object_class),
413 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000416 LocationSummary* locations = instruction_->GetLocations();
417 DCHECK(instruction_->IsCheckCast()
418 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
419 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
420
Alexandre Rames67555f72014-11-18 10:55:16 +0000421 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000422 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
424 // We're moving two locations to locations that could overlap, so we need a parallel
425 // move resolver.
426 InvokeRuntimeCallingConvention calling_convention;
427 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100428 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
429 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430
431 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000432 arm64_codegen->InvokeRuntime(
433 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 Primitive::Type ret_type = instruction_->GetType();
435 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
436 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800437 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
438 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000439 } else {
440 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000441 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800442 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443 }
444
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000445 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000446 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 }
448
Alexandre Rames9931f312015-06-19 14:47:01 +0100449 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
450
Alexandre Rames67555f72014-11-18 10:55:16 +0000451 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452 HInstruction* const instruction_;
453 const Location class_to_check_;
454 const Location object_class_;
455 uint32_t dex_pc_;
456
Alexandre Rames67555f72014-11-18 10:55:16 +0000457 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
458};
459
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700460class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
462 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
463 : instruction_(instruction) {}
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 __ Bind(GetEntryLabel());
467 SaveLiveRegisters(codegen, instruction_->GetLocations());
468 DCHECK(instruction_->IsDeoptimize());
469 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
470 uint32_t dex_pc = deoptimize->GetDexPc();
471 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
472 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
473 }
474
Alexandre Rames9931f312015-06-19 14:47:01 +0100475 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700477 private:
478 HInstruction* const instruction_;
479 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
480};
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482#undef __
483
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100484Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 Location next_location;
486 if (type == Primitive::kPrimVoid) {
487 LOG(FATAL) << "Unreachable type " << type;
488 }
489
Alexandre Rames542361f2015-01-29 16:57:31 +0000490 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100491 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
492 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000493 } else if (!Primitive::IsFloatingPointType(type) &&
494 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000495 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
496 } else {
497 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000498 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
499 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100500 }
501
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000502 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000503 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 return next_location;
505}
506
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100507Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100508 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100509}
510
Serban Constantinescu579885a2015-02-22 20:51:33 +0000511CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
512 const Arm64InstructionSetFeatures& isa_features,
513 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100514 : CodeGenerator(graph,
515 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000516 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000517 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000518 callee_saved_core_registers.list(),
519 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000520 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100521 block_labels_(nullptr),
522 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000523 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000524 move_resolver_(graph->GetArena(), this),
525 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000526 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000527 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000528}
Alexandre Rames5319def2014-10-23 10:03:10 +0100529
Alexandre Rames67555f72014-11-18 10:55:16 +0000530#undef __
531#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100532
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000533void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
534 // Ensure we emit the literal pool.
535 __ FinalizeCode();
536 CodeGenerator::Finalize(allocator);
537}
538
Zheng Xuad4450e2015-04-17 18:48:56 +0800539void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
540 // Note: There are 6 kinds of moves:
541 // 1. constant -> GPR/FPR (non-cycle)
542 // 2. constant -> stack (non-cycle)
543 // 3. GPR/FPR -> GPR/FPR
544 // 4. GPR/FPR -> stack
545 // 5. stack -> GPR/FPR
546 // 6. stack -> stack (non-cycle)
547 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
548 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
549 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
550 // dependency.
551 vixl_temps_.Open(GetVIXLAssembler());
552}
553
554void ParallelMoveResolverARM64::FinishEmitNativeCode() {
555 vixl_temps_.Close();
556}
557
558Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
559 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
560 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
561 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
562 Location scratch = GetScratchLocation(kind);
563 if (!scratch.Equals(Location::NoLocation())) {
564 return scratch;
565 }
566 // Allocate from VIXL temp registers.
567 if (kind == Location::kRegister) {
568 scratch = LocationFrom(vixl_temps_.AcquireX());
569 } else {
570 DCHECK(kind == Location::kFpuRegister);
571 scratch = LocationFrom(vixl_temps_.AcquireD());
572 }
573 AddScratchLocation(scratch);
574 return scratch;
575}
576
577void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
578 if (loc.IsRegister()) {
579 vixl_temps_.Release(XRegisterFrom(loc));
580 } else {
581 DCHECK(loc.IsFpuRegister());
582 vixl_temps_.Release(DRegisterFrom(loc));
583 }
584 RemoveScratchLocation(loc);
585}
586
Alexandre Rames3e69f162014-12-10 10:36:50 +0000587void ParallelMoveResolverARM64::EmitMove(size_t index) {
588 MoveOperands* move = moves_.Get(index);
589 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
590}
591
Alexandre Rames5319def2014-10-23 10:03:10 +0100592void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100593 MacroAssembler* masm = GetVIXLAssembler();
594 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000595 __ Bind(&frame_entry_label_);
596
Serban Constantinescu02164b32014-11-13 14:05:07 +0000597 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
598 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100599 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000600 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000601 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000602 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000603 __ Ldr(wzr, MemOperand(temp, 0));
604 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000605 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100606
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000607 if (!HasEmptyFrame()) {
608 int frame_size = GetFrameSize();
609 // Stack layout:
610 // sp[frame_size - 8] : lr.
611 // ... : other preserved core registers.
612 // ... : other preserved fp registers.
613 // ... : reserved frame space.
614 // sp[0] : current method.
615 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100616 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800617 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
618 frame_size - GetCoreSpillSize());
619 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
620 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000621 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100622}
623
624void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100625 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100626 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000627 if (!HasEmptyFrame()) {
628 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800629 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
630 frame_size - FrameEntrySpillSize());
631 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
632 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000633 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100634 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000635 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100636 __ Ret();
637 GetAssembler()->cfi().RestoreState();
638 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100639}
640
Zheng Xuda403092015-04-24 17:35:39 +0800641vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
642 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
643 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
644 core_spill_mask_);
645}
646
647vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
648 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
649 GetNumberOfFloatingPointRegisters()));
650 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
651 fpu_spill_mask_);
652}
653
Alexandre Rames5319def2014-10-23 10:03:10 +0100654void CodeGeneratorARM64::Bind(HBasicBlock* block) {
655 __ Bind(GetLabelOf(block));
656}
657
Alexandre Rames5319def2014-10-23 10:03:10 +0100658void CodeGeneratorARM64::Move(HInstruction* instruction,
659 Location location,
660 HInstruction* move_for) {
661 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100662 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000663 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100664
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100665 if (instruction->IsFakeString()) {
666 // The fake string is an alias for null.
667 DCHECK(IsBaseline());
668 instruction = locations->Out().GetConstant();
669 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
670 }
671
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100672 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700673 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100674 } else if (locations != nullptr && locations->Out().Equals(location)) {
675 return;
676 } else if (instruction->IsIntConstant()
677 || instruction->IsLongConstant()
678 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000679 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100680 if (location.IsRegister()) {
681 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000682 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100683 (instruction->IsLongConstant() && dst.Is64Bits()));
684 __ Mov(dst, value);
685 } else {
686 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000687 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000688 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
689 ? temps.AcquireW()
690 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 __ Mov(temp, value);
692 __ Str(temp, StackOperandFrom(location));
693 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000694 } else if (instruction->IsTemporary()) {
695 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000696 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100697 } else if (instruction->IsLoadLocal()) {
698 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000699 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000700 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000701 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000702 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100703 }
704
705 } else {
706 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000707 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100708 }
709}
710
Alexandre Rames5319def2014-10-23 10:03:10 +0100711Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
712 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000713
Alexandre Rames5319def2014-10-23 10:03:10 +0100714 switch (type) {
715 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000716 case Primitive::kPrimInt:
717 case Primitive::kPrimFloat:
718 return Location::StackSlot(GetStackSlot(load->GetLocal()));
719
720 case Primitive::kPrimLong:
721 case Primitive::kPrimDouble:
722 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
723
Alexandre Rames5319def2014-10-23 10:03:10 +0100724 case Primitive::kPrimBoolean:
725 case Primitive::kPrimByte:
726 case Primitive::kPrimChar:
727 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100728 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100729 LOG(FATAL) << "Unexpected type " << type;
730 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000731
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 LOG(FATAL) << "Unreachable";
733 return Location::NoLocation();
734}
735
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100736void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000737 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100738 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000739 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100740 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100741 if (value_can_be_null) {
742 __ Cbz(value, &done);
743 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100744 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
745 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000746 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100747 if (value_can_be_null) {
748 __ Bind(&done);
749 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100750}
751
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000752void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
753 // Blocked core registers:
754 // lr : Runtime reserved.
755 // tr : Runtime reserved.
756 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
757 // ip1 : VIXL core temp.
758 // ip0 : VIXL core temp.
759 //
760 // Blocked fp registers:
761 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100762 CPURegList reserved_core_registers = vixl_reserved_core_registers;
763 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100764 while (!reserved_core_registers.IsEmpty()) {
765 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
766 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000767
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000768 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800769 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000770 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
771 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000772
773 if (is_baseline) {
774 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
775 while (!reserved_core_baseline_registers.IsEmpty()) {
776 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
777 }
778
779 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
780 while (!reserved_fp_baseline_registers.IsEmpty()) {
781 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
782 }
783 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100784}
785
786Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
787 if (type == Primitive::kPrimVoid) {
788 LOG(FATAL) << "Unreachable type " << type;
789 }
790
Alexandre Rames542361f2015-01-29 16:57:31 +0000791 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000792 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
793 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100794 return Location::FpuRegisterLocation(reg);
795 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000796 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
797 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100798 return Location::RegisterLocation(reg);
799 }
800}
801
Alexandre Rames3e69f162014-12-10 10:36:50 +0000802size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
803 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
804 __ Str(reg, MemOperand(sp, stack_index));
805 return kArm64WordSize;
806}
807
808size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
809 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
810 __ Ldr(reg, MemOperand(sp, stack_index));
811 return kArm64WordSize;
812}
813
814size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
815 FPRegister reg = FPRegister(reg_id, kDRegSize);
816 __ Str(reg, MemOperand(sp, stack_index));
817 return kArm64WordSize;
818}
819
820size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
821 FPRegister reg = FPRegister(reg_id, kDRegSize);
822 __ Ldr(reg, MemOperand(sp, stack_index));
823 return kArm64WordSize;
824}
825
Alexandre Rames5319def2014-10-23 10:03:10 +0100826void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100827 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100828}
829
830void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100831 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100832}
833
Alexandre Rames67555f72014-11-18 10:55:16 +0000834void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000835 if (constant->IsIntConstant()) {
836 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
837 } else if (constant->IsLongConstant()) {
838 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
839 } else if (constant->IsNullConstant()) {
840 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000841 } else if (constant->IsFloatConstant()) {
842 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
843 } else {
844 DCHECK(constant->IsDoubleConstant());
845 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
846 }
847}
848
Alexandre Rames3e69f162014-12-10 10:36:50 +0000849
850static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
851 DCHECK(constant.IsConstant());
852 HConstant* cst = constant.GetConstant();
853 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000854 // Null is mapped to a core W register, which we associate with kPrimInt.
855 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000856 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
857 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
858 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
859}
860
861void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000862 if (source.Equals(destination)) {
863 return;
864 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000865
866 // A valid move can always be inferred from the destination and source
867 // locations. When moving from and to a register, the argument type can be
868 // used to generate 32bit instead of 64bit moves. In debug mode we also
869 // checks the coherency of the locations and the type.
870 bool unspecified_type = (type == Primitive::kPrimVoid);
871
872 if (destination.IsRegister() || destination.IsFpuRegister()) {
873 if (unspecified_type) {
874 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
875 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000876 (src_cst != nullptr && (src_cst->IsIntConstant()
877 || src_cst->IsFloatConstant()
878 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000879 // For stack slots and 32bit constants, a 64bit type is appropriate.
880 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000881 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000882 // If the source is a double stack slot or a 64bit constant, a 64bit
883 // type is appropriate. Else the source is a register, and since the
884 // type has not been specified, we chose a 64bit type to force a 64bit
885 // move.
886 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000887 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000888 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000889 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
890 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000891 CPURegister dst = CPURegisterFrom(destination, type);
892 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
893 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
894 __ Ldr(dst, StackOperandFrom(source));
895 } else if (source.IsConstant()) {
896 DCHECK(CoherentConstantAndType(source, type));
897 MoveConstant(dst, source.GetConstant());
898 } else {
899 if (destination.IsRegister()) {
900 __ Mov(Register(dst), RegisterFrom(source, type));
901 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800902 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000903 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
904 }
905 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000906 } else { // The destination is not a register. It must be a stack slot.
907 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
908 if (source.IsRegister() || source.IsFpuRegister()) {
909 if (unspecified_type) {
910 if (source.IsRegister()) {
911 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
912 } else {
913 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
914 }
915 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000916 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
917 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000918 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
919 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100920 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000921 UseScratchRegisterScope temps(GetVIXLAssembler());
922 HConstant* src_cst = source.GetConstant();
923 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000924 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000925 temp = temps.AcquireW();
926 } else if (src_cst->IsLongConstant()) {
927 temp = temps.AcquireX();
928 } else if (src_cst->IsFloatConstant()) {
929 temp = temps.AcquireS();
930 } else {
931 DCHECK(src_cst->IsDoubleConstant());
932 temp = temps.AcquireD();
933 }
934 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000935 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000936 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000937 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000938 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000939 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000940 // There is generally less pressure on FP registers.
941 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000942 __ Ldr(temp, StackOperandFrom(source));
943 __ Str(temp, StackOperandFrom(destination));
944 }
945 }
946}
947
948void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000949 CPURegister dst,
950 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000951 switch (type) {
952 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000953 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000954 break;
955 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000956 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000957 break;
958 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000959 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000960 break;
961 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000962 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000963 break;
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot:
966 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000967 case Primitive::kPrimFloat:
968 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000969 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000970 __ Ldr(dst, src);
971 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000972 case Primitive::kPrimVoid:
973 LOG(FATAL) << "Unreachable type " << type;
974 }
975}
976
Calin Juravle77520bc2015-01-12 18:45:46 +0000977void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000978 CPURegister dst,
979 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100980 MacroAssembler* masm = GetVIXLAssembler();
981 BlockPoolsScope block_pools(masm);
982 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000983 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000984 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000985
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000986 DCHECK(!src.IsPreIndex());
987 DCHECK(!src.IsPostIndex());
988
989 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800990 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000991 MemOperand base = MemOperand(temp_base);
992 switch (type) {
993 case Primitive::kPrimBoolean:
994 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000995 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000996 break;
997 case Primitive::kPrimByte:
998 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000999 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001000 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1001 break;
1002 case Primitive::kPrimChar:
1003 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001004 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001005 break;
1006 case Primitive::kPrimShort:
1007 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001008 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1010 break;
1011 case Primitive::kPrimInt:
1012 case Primitive::kPrimNot:
1013 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001014 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001015 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001016 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001017 break;
1018 case Primitive::kPrimFloat:
1019 case Primitive::kPrimDouble: {
1020 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001021 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001022
1023 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1024 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001025 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001026 __ Fmov(FPRegister(dst), temp);
1027 break;
1028 }
1029 case Primitive::kPrimVoid:
1030 LOG(FATAL) << "Unreachable type " << type;
1031 }
1032}
1033
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001034void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001035 CPURegister src,
1036 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001037 switch (type) {
1038 case Primitive::kPrimBoolean:
1039 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001040 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001041 break;
1042 case Primitive::kPrimChar:
1043 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001044 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001045 break;
1046 case Primitive::kPrimInt:
1047 case Primitive::kPrimNot:
1048 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001049 case Primitive::kPrimFloat:
1050 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001051 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001053 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001054 case Primitive::kPrimVoid:
1055 LOG(FATAL) << "Unreachable type " << type;
1056 }
1057}
1058
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001059void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1060 CPURegister src,
1061 const MemOperand& dst) {
1062 UseScratchRegisterScope temps(GetVIXLAssembler());
1063 Register temp_base = temps.AcquireX();
1064
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001065 DCHECK(!dst.IsPreIndex());
1066 DCHECK(!dst.IsPostIndex());
1067
1068 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001069 Operand op = OperandFromMemOperand(dst);
1070 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001071 MemOperand base = MemOperand(temp_base);
1072 switch (type) {
1073 case Primitive::kPrimBoolean:
1074 case Primitive::kPrimByte:
1075 __ Stlrb(Register(src), base);
1076 break;
1077 case Primitive::kPrimChar:
1078 case Primitive::kPrimShort:
1079 __ Stlrh(Register(src), base);
1080 break;
1081 case Primitive::kPrimInt:
1082 case Primitive::kPrimNot:
1083 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001084 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001085 __ Stlr(Register(src), base);
1086 break;
1087 case Primitive::kPrimFloat:
1088 case Primitive::kPrimDouble: {
1089 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001090 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001091
1092 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1093 __ Fmov(temp, FPRegister(src));
1094 __ Stlr(temp, base);
1095 break;
1096 }
1097 case Primitive::kPrimVoid:
1098 LOG(FATAL) << "Unreachable type " << type;
1099 }
1100}
1101
Alexandre Rames67555f72014-11-18 10:55:16 +00001102void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1103 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001104 uint32_t dex_pc,
1105 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001106 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001107 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001108 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1109 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001110 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001111}
1112
1113void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1114 vixl::Register class_reg) {
1115 UseScratchRegisterScope temps(GetVIXLAssembler());
1116 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001117 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001118 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001119
Serban Constantinescu02164b32014-11-13 14:05:07 +00001120 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001121 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001122 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1123 __ Add(temp, class_reg, status_offset);
1124 __ Ldar(temp, HeapOperand(temp));
1125 __ Cmp(temp, mirror::Class::kStatusInitialized);
1126 __ B(lt, slow_path->GetEntryLabel());
1127 } else {
1128 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1129 __ Cmp(temp, mirror::Class::kStatusInitialized);
1130 __ B(lt, slow_path->GetEntryLabel());
1131 __ Dmb(InnerShareable, BarrierReads);
1132 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001133 __ Bind(slow_path->GetExitLabel());
1134}
Alexandre Rames5319def2014-10-23 10:03:10 +01001135
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001136void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1137 BarrierType type = BarrierAll;
1138
1139 switch (kind) {
1140 case MemBarrierKind::kAnyAny:
1141 case MemBarrierKind::kAnyStore: {
1142 type = BarrierAll;
1143 break;
1144 }
1145 case MemBarrierKind::kLoadAny: {
1146 type = BarrierReads;
1147 break;
1148 }
1149 case MemBarrierKind::kStoreStore: {
1150 type = BarrierWrites;
1151 break;
1152 }
1153 default:
1154 LOG(FATAL) << "Unexpected memory barrier " << kind;
1155 }
1156 __ Dmb(InnerShareable, type);
1157}
1158
Serban Constantinescu02164b32014-11-13 14:05:07 +00001159void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1160 HBasicBlock* successor) {
1161 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001162 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1163 if (slow_path == nullptr) {
1164 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1165 instruction->SetSlowPath(slow_path);
1166 codegen_->AddSlowPath(slow_path);
1167 if (successor != nullptr) {
1168 DCHECK(successor->IsLoopHeader());
1169 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1170 }
1171 } else {
1172 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1173 }
1174
Serban Constantinescu02164b32014-11-13 14:05:07 +00001175 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1176 Register temp = temps.AcquireW();
1177
1178 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1179 if (successor == nullptr) {
1180 __ Cbnz(temp, slow_path->GetEntryLabel());
1181 __ Bind(slow_path->GetReturnLabel());
1182 } else {
1183 __ Cbz(temp, codegen_->GetLabelOf(successor));
1184 __ B(slow_path->GetEntryLabel());
1185 // slow_path will return to GetLabelOf(successor).
1186 }
1187}
1188
Alexandre Rames5319def2014-10-23 10:03:10 +01001189InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1190 CodeGeneratorARM64* codegen)
1191 : HGraphVisitor(graph),
1192 assembler_(codegen->GetAssembler()),
1193 codegen_(codegen) {}
1194
1195#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001196 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001197
1198#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1199
1200enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001201 // Using a base helps identify when we hit such breakpoints.
1202 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001203#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1204 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1205#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1206};
1207
1208#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1209 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001210 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001211 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1212 } \
1213 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1214 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1215 locations->SetOut(Location::Any()); \
1216 }
1217 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1218#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1219
1220#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001221#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001222
Alexandre Rames67555f72014-11-18 10:55:16 +00001223void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001224 DCHECK_EQ(instr->InputCount(), 2U);
1225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1226 Primitive::Type type = instr->GetResultType();
1227 switch (type) {
1228 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001229 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001230 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001231 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001233 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001234
1235 case Primitive::kPrimFloat:
1236 case Primitive::kPrimDouble:
1237 locations->SetInAt(0, Location::RequiresFpuRegister());
1238 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001239 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001240 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001241
Alexandre Rames5319def2014-10-23 10:03:10 +01001242 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001243 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001244 }
1245}
1246
Alexandre Rames09a99962015-04-15 11:47:56 +01001247void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1248 LocationSummary* locations =
1249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1250 locations->SetInAt(0, Location::RequiresRegister());
1251 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1252 locations->SetOut(Location::RequiresFpuRegister());
1253 } else {
1254 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1255 }
1256}
1257
1258void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1259 const FieldInfo& field_info) {
1260 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001261 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001262 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001263
1264 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1265 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1266
1267 if (field_info.IsVolatile()) {
1268 if (use_acquire_release) {
1269 // NB: LoadAcquire will record the pc info if needed.
1270 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1271 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001272 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001273 codegen_->MaybeRecordImplicitNullCheck(instruction);
1274 // For IRIW sequential consistency kLoadAny is not sufficient.
1275 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1276 }
1277 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001278 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001279 codegen_->MaybeRecordImplicitNullCheck(instruction);
1280 }
Roland Levillain4d027112015-07-01 15:41:14 +01001281
1282 if (field_type == Primitive::kPrimNot) {
1283 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1284 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001285}
1286
1287void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1288 LocationSummary* locations =
1289 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1290 locations->SetInAt(0, Location::RequiresRegister());
1291 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1292 locations->SetInAt(1, Location::RequiresFpuRegister());
1293 } else {
1294 locations->SetInAt(1, Location::RequiresRegister());
1295 }
1296}
1297
1298void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001299 const FieldInfo& field_info,
1300 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001301 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001302 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001303
1304 Register obj = InputRegisterAt(instruction, 0);
1305 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001306 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001307 Offset offset = field_info.GetFieldOffset();
1308 Primitive::Type field_type = field_info.GetFieldType();
1309 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1310
Roland Levillain4d027112015-07-01 15:41:14 +01001311 {
1312 // We use a block to end the scratch scope before the write barrier, thus
1313 // freeing the temporary registers so they can be used in `MarkGCCard`.
1314 UseScratchRegisterScope temps(GetVIXLAssembler());
1315
1316 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1317 DCHECK(value.IsW());
1318 Register temp = temps.AcquireW();
1319 __ Mov(temp, value.W());
1320 GetAssembler()->PoisonHeapReference(temp.W());
1321 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001322 }
Roland Levillain4d027112015-07-01 15:41:14 +01001323
1324 if (field_info.IsVolatile()) {
1325 if (use_acquire_release) {
1326 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1327 codegen_->MaybeRecordImplicitNullCheck(instruction);
1328 } else {
1329 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1330 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1331 codegen_->MaybeRecordImplicitNullCheck(instruction);
1332 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1333 }
1334 } else {
1335 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1336 codegen_->MaybeRecordImplicitNullCheck(instruction);
1337 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001338 }
1339
1340 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001341 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001342 }
1343}
1344
Alexandre Rames67555f72014-11-18 10:55:16 +00001345void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001346 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001347
1348 switch (type) {
1349 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001350 case Primitive::kPrimLong: {
1351 Register dst = OutputRegister(instr);
1352 Register lhs = InputRegisterAt(instr, 0);
1353 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 if (instr->IsAdd()) {
1355 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001356 } else if (instr->IsAnd()) {
1357 __ And(dst, lhs, rhs);
1358 } else if (instr->IsOr()) {
1359 __ Orr(dst, lhs, rhs);
1360 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001361 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001362 } else {
1363 DCHECK(instr->IsXor());
1364 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001365 }
1366 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001367 }
1368 case Primitive::kPrimFloat:
1369 case Primitive::kPrimDouble: {
1370 FPRegister dst = OutputFPRegister(instr);
1371 FPRegister lhs = InputFPRegisterAt(instr, 0);
1372 FPRegister rhs = InputFPRegisterAt(instr, 1);
1373 if (instr->IsAdd()) {
1374 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001375 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001376 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001377 } else {
1378 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001379 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001381 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001382 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001383 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001384 }
1385}
1386
Serban Constantinescu02164b32014-11-13 14:05:07 +00001387void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1388 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1389
1390 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1391 Primitive::Type type = instr->GetResultType();
1392 switch (type) {
1393 case Primitive::kPrimInt:
1394 case Primitive::kPrimLong: {
1395 locations->SetInAt(0, Location::RequiresRegister());
1396 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1397 locations->SetOut(Location::RequiresRegister());
1398 break;
1399 }
1400 default:
1401 LOG(FATAL) << "Unexpected shift type " << type;
1402 }
1403}
1404
1405void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1406 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1407
1408 Primitive::Type type = instr->GetType();
1409 switch (type) {
1410 case Primitive::kPrimInt:
1411 case Primitive::kPrimLong: {
1412 Register dst = OutputRegister(instr);
1413 Register lhs = InputRegisterAt(instr, 0);
1414 Operand rhs = InputOperandAt(instr, 1);
1415 if (rhs.IsImmediate()) {
1416 uint32_t shift_value = (type == Primitive::kPrimInt)
1417 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1418 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1419 if (instr->IsShl()) {
1420 __ Lsl(dst, lhs, shift_value);
1421 } else if (instr->IsShr()) {
1422 __ Asr(dst, lhs, shift_value);
1423 } else {
1424 __ Lsr(dst, lhs, shift_value);
1425 }
1426 } else {
1427 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1428
1429 if (instr->IsShl()) {
1430 __ Lsl(dst, lhs, rhs_reg);
1431 } else if (instr->IsShr()) {
1432 __ Asr(dst, lhs, rhs_reg);
1433 } else {
1434 __ Lsr(dst, lhs, rhs_reg);
1435 }
1436 }
1437 break;
1438 }
1439 default:
1440 LOG(FATAL) << "Unexpected shift operation type " << type;
1441 }
1442}
1443
Alexandre Rames5319def2014-10-23 10:03:10 +01001444void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001445 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001446}
1447
1448void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001449 HandleBinaryOp(instruction);
1450}
1451
1452void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1453 HandleBinaryOp(instruction);
1454}
1455
1456void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1457 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001458}
1459
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001460void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1461 LocationSummary* locations =
1462 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1463 locations->SetInAt(0, Location::RequiresRegister());
1464 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001465 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1466 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1467 } else {
1468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1469 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001470}
1471
1472void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1473 LocationSummary* locations = instruction->GetLocations();
1474 Primitive::Type type = instruction->GetType();
1475 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001476 Location index = locations->InAt(1);
1477 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001478 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001479 MacroAssembler* masm = GetVIXLAssembler();
1480 UseScratchRegisterScope temps(masm);
1481 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001482
1483 if (index.IsConstant()) {
1484 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001485 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001486 } else {
1487 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001488 __ Add(temp, obj, offset);
1489 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001490 }
1491
Alexandre Rames67555f72014-11-18 10:55:16 +00001492 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001493 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001494
1495 if (type == Primitive::kPrimNot) {
1496 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1497 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001498}
1499
Alexandre Rames5319def2014-10-23 10:03:10 +01001500void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1502 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001504}
1505
1506void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001507 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001508 __ Ldr(OutputRegister(instruction),
1509 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001510 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001511}
1512
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001513void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001514 if (instruction->NeedsTypeCheck()) {
1515 LocationSummary* locations =
1516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001517 InvokeRuntimeCallingConvention calling_convention;
1518 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1519 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1520 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1521 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001522 LocationSummary* locations =
1523 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001524 locations->SetInAt(0, Location::RequiresRegister());
1525 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001526 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1527 locations->SetInAt(2, Location::RequiresFpuRegister());
1528 } else {
1529 locations->SetInAt(2, Location::RequiresRegister());
1530 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001531 }
1532}
1533
1534void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1535 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001536 LocationSummary* locations = instruction->GetLocations();
1537 bool needs_runtime_call = locations->WillCall();
1538
1539 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001540 // Note: if heap poisoning is enabled, pAputObject takes cares
1541 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001542 codegen_->InvokeRuntime(
1543 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001544 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001545 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001546 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001547 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001548 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001549 Location index = locations->InAt(1);
1550 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001551 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001552 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001553 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001554 {
1555 // We use a block to end the scratch scope before the write barrier, thus
1556 // freeing the temporary registers so they can be used in `MarkGCCard`.
1557 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001558
Roland Levillain4d027112015-07-01 15:41:14 +01001559 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1560 DCHECK(value.IsW());
1561 Register temp = temps.AcquireW();
1562 __ Mov(temp, value.W());
1563 GetAssembler()->PoisonHeapReference(temp.W());
1564 source = temp;
1565 }
1566
Alexandre Rames97833a02015-04-16 15:07:12 +01001567 if (index.IsConstant()) {
1568 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1569 destination = HeapOperand(obj, offset);
1570 } else {
1571 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001572 __ Add(temp, obj, offset);
1573 destination = HeapOperand(temp,
1574 XRegisterFrom(index),
1575 LSL,
1576 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001577 }
1578
Roland Levillain4d027112015-07-01 15:41:14 +01001579 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001580 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001581 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001582 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001583 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001584 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001585 }
1586}
1587
Alexandre Rames67555f72014-11-18 10:55:16 +00001588void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1589 LocationSummary* locations =
1590 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1591 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001592 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001593 if (instruction->HasUses()) {
1594 locations->SetOut(Location::SameAsFirstInput());
1595 }
1596}
1597
1598void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001599 LocationSummary* locations = instruction->GetLocations();
1600 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1601 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001602 codegen_->AddSlowPath(slow_path);
1603
1604 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1605 __ B(slow_path->GetEntryLabel(), hs);
1606}
1607
1608void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1610 instruction, LocationSummary::kCallOnSlowPath);
1611 locations->SetInAt(0, Location::RequiresRegister());
1612 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001613 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001614}
1615
1616void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001617 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001618 Register obj = InputRegisterAt(instruction, 0);;
1619 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001620 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001621
Alexandre Rames3e69f162014-12-10 10:36:50 +00001622 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1623 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001624 codegen_->AddSlowPath(slow_path);
1625
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001626 // Avoid null check if we know obj is not null.
1627 if (instruction->MustDoNullCheck()) {
1628 __ Cbz(obj, slow_path->GetExitLabel());
1629 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001630 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001631 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001632 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001633 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001634 // The checkcast succeeds if the classes are equal (fast path).
1635 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001636 __ B(ne, slow_path->GetEntryLabel());
1637 __ Bind(slow_path->GetExitLabel());
1638}
1639
1640void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1641 LocationSummary* locations =
1642 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1643 locations->SetInAt(0, Location::RequiresRegister());
1644 if (check->HasUses()) {
1645 locations->SetOut(Location::SameAsFirstInput());
1646 }
1647}
1648
1649void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1650 // We assume the class is not null.
1651 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1652 check->GetLoadClass(), check, check->GetDexPc(), true);
1653 codegen_->AddSlowPath(slow_path);
1654 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1655}
1656
Roland Levillain7f63c522015-07-13 15:54:55 +00001657static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1658 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1659 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1660}
1661
Serban Constantinescu02164b32014-11-13 14:05:07 +00001662void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001663 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001664 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1665 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001666 switch (in_type) {
1667 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001668 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001669 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001670 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1671 break;
1672 }
1673 case Primitive::kPrimFloat:
1674 case Primitive::kPrimDouble: {
1675 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001676 locations->SetInAt(1,
1677 IsFloatingPointZeroConstant(compare->InputAt(1))
1678 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1679 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001680 locations->SetOut(Location::RequiresRegister());
1681 break;
1682 }
1683 default:
1684 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1685 }
1686}
1687
1688void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1689 Primitive::Type in_type = compare->InputAt(0)->GetType();
1690
1691 // 0 if: left == right
1692 // 1 if: left > right
1693 // -1 if: left < right
1694 switch (in_type) {
1695 case Primitive::kPrimLong: {
1696 Register result = OutputRegister(compare);
1697 Register left = InputRegisterAt(compare, 0);
1698 Operand right = InputOperandAt(compare, 1);
1699
1700 __ Cmp(left, right);
1701 __ Cset(result, ne);
1702 __ Cneg(result, result, lt);
1703 break;
1704 }
1705 case Primitive::kPrimFloat:
1706 case Primitive::kPrimDouble: {
1707 Register result = OutputRegister(compare);
1708 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001709 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001710 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1711 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001712 __ Fcmp(left, 0.0);
1713 } else {
1714 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1715 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001716 if (compare->IsGtBias()) {
1717 __ Cset(result, ne);
1718 } else {
1719 __ Csetm(result, ne);
1720 }
1721 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001722 break;
1723 }
1724 default:
1725 LOG(FATAL) << "Unimplemented compare type " << in_type;
1726 }
1727}
1728
1729void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1730 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001731
1732 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1733 locations->SetInAt(0, Location::RequiresFpuRegister());
1734 locations->SetInAt(1,
1735 IsFloatingPointZeroConstant(instruction->InputAt(1))
1736 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1737 : Location::RequiresFpuRegister());
1738 } else {
1739 // Integer cases.
1740 locations->SetInAt(0, Location::RequiresRegister());
1741 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1742 }
1743
Alexandre Rames5319def2014-10-23 10:03:10 +01001744 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001745 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001746 }
1747}
1748
1749void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1750 if (!instruction->NeedsMaterialization()) {
1751 return;
1752 }
1753
1754 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001755 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001756 IfCondition if_cond = instruction->GetCondition();
1757 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001758
Roland Levillain7f63c522015-07-13 15:54:55 +00001759 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1760 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1761 if (locations->InAt(1).IsConstant()) {
1762 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1763 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1764 __ Fcmp(lhs, 0.0);
1765 } else {
1766 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1767 }
1768 __ Cset(res, arm64_cond);
1769 if (instruction->IsFPConditionTrueIfNaN()) {
1770 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1771 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1772 } else if (instruction->IsFPConditionFalseIfNaN()) {
1773 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1774 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1775 }
1776 } else {
1777 // Integer cases.
1778 Register lhs = InputRegisterAt(instruction, 0);
1779 Operand rhs = InputOperandAt(instruction, 1);
1780 __ Cmp(lhs, rhs);
1781 __ Cset(res, arm64_cond);
1782 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001783}
1784
1785#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1786 M(Equal) \
1787 M(NotEqual) \
1788 M(LessThan) \
1789 M(LessThanOrEqual) \
1790 M(GreaterThan) \
1791 M(GreaterThanOrEqual)
1792#define DEFINE_CONDITION_VISITORS(Name) \
1793void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1794void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1795FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001796#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001797#undef FOR_EACH_CONDITION_INSTRUCTION
1798
Zheng Xuc6667102015-05-15 16:08:45 +08001799void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1800 DCHECK(instruction->IsDiv() || instruction->IsRem());
1801
1802 LocationSummary* locations = instruction->GetLocations();
1803 Location second = locations->InAt(1);
1804 DCHECK(second.IsConstant());
1805
1806 Register out = OutputRegister(instruction);
1807 Register dividend = InputRegisterAt(instruction, 0);
1808 int64_t imm = Int64FromConstant(second.GetConstant());
1809 DCHECK(imm == 1 || imm == -1);
1810
1811 if (instruction->IsRem()) {
1812 __ Mov(out, 0);
1813 } else {
1814 if (imm == 1) {
1815 __ Mov(out, dividend);
1816 } else {
1817 __ Neg(out, dividend);
1818 }
1819 }
1820}
1821
1822void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1823 DCHECK(instruction->IsDiv() || instruction->IsRem());
1824
1825 LocationSummary* locations = instruction->GetLocations();
1826 Location second = locations->InAt(1);
1827 DCHECK(second.IsConstant());
1828
1829 Register out = OutputRegister(instruction);
1830 Register dividend = InputRegisterAt(instruction, 0);
1831 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001832 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001833 DCHECK(IsPowerOfTwo(abs_imm));
1834 int ctz_imm = CTZ(abs_imm);
1835
1836 UseScratchRegisterScope temps(GetVIXLAssembler());
1837 Register temp = temps.AcquireSameSizeAs(out);
1838
1839 if (instruction->IsDiv()) {
1840 __ Add(temp, dividend, abs_imm - 1);
1841 __ Cmp(dividend, 0);
1842 __ Csel(out, temp, dividend, lt);
1843 if (imm > 0) {
1844 __ Asr(out, out, ctz_imm);
1845 } else {
1846 __ Neg(out, Operand(out, ASR, ctz_imm));
1847 }
1848 } else {
1849 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1850 __ Asr(temp, dividend, bits - 1);
1851 __ Lsr(temp, temp, bits - ctz_imm);
1852 __ Add(out, dividend, temp);
1853 __ And(out, out, abs_imm - 1);
1854 __ Sub(out, out, temp);
1855 }
1856}
1857
1858void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1859 DCHECK(instruction->IsDiv() || instruction->IsRem());
1860
1861 LocationSummary* locations = instruction->GetLocations();
1862 Location second = locations->InAt(1);
1863 DCHECK(second.IsConstant());
1864
1865 Register out = OutputRegister(instruction);
1866 Register dividend = InputRegisterAt(instruction, 0);
1867 int64_t imm = Int64FromConstant(second.GetConstant());
1868
1869 Primitive::Type type = instruction->GetResultType();
1870 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1871
1872 int64_t magic;
1873 int shift;
1874 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1875
1876 UseScratchRegisterScope temps(GetVIXLAssembler());
1877 Register temp = temps.AcquireSameSizeAs(out);
1878
1879 // temp = get_high(dividend * magic)
1880 __ Mov(temp, magic);
1881 if (type == Primitive::kPrimLong) {
1882 __ Smulh(temp, dividend, temp);
1883 } else {
1884 __ Smull(temp.X(), dividend, temp);
1885 __ Lsr(temp.X(), temp.X(), 32);
1886 }
1887
1888 if (imm > 0 && magic < 0) {
1889 __ Add(temp, temp, dividend);
1890 } else if (imm < 0 && magic > 0) {
1891 __ Sub(temp, temp, dividend);
1892 }
1893
1894 if (shift != 0) {
1895 __ Asr(temp, temp, shift);
1896 }
1897
1898 if (instruction->IsDiv()) {
1899 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1900 } else {
1901 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1902 // TODO: Strength reduction for msub.
1903 Register temp_imm = temps.AcquireSameSizeAs(out);
1904 __ Mov(temp_imm, imm);
1905 __ Msub(out, temp, temp_imm, dividend);
1906 }
1907}
1908
1909void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1910 DCHECK(instruction->IsDiv() || instruction->IsRem());
1911 Primitive::Type type = instruction->GetResultType();
1912 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1913
1914 LocationSummary* locations = instruction->GetLocations();
1915 Register out = OutputRegister(instruction);
1916 Location second = locations->InAt(1);
1917
1918 if (second.IsConstant()) {
1919 int64_t imm = Int64FromConstant(second.GetConstant());
1920
1921 if (imm == 0) {
1922 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1923 } else if (imm == 1 || imm == -1) {
1924 DivRemOneOrMinusOne(instruction);
1925 } else if (IsPowerOfTwo(std::abs(imm))) {
1926 DivRemByPowerOfTwo(instruction);
1927 } else {
1928 DCHECK(imm <= -2 || imm >= 2);
1929 GenerateDivRemWithAnyConstant(instruction);
1930 }
1931 } else {
1932 Register dividend = InputRegisterAt(instruction, 0);
1933 Register divisor = InputRegisterAt(instruction, 1);
1934 if (instruction->IsDiv()) {
1935 __ Sdiv(out, dividend, divisor);
1936 } else {
1937 UseScratchRegisterScope temps(GetVIXLAssembler());
1938 Register temp = temps.AcquireSameSizeAs(out);
1939 __ Sdiv(temp, dividend, divisor);
1940 __ Msub(out, temp, divisor, dividend);
1941 }
1942 }
1943}
1944
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001945void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1946 LocationSummary* locations =
1947 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1948 switch (div->GetResultType()) {
1949 case Primitive::kPrimInt:
1950 case Primitive::kPrimLong:
1951 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001952 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1954 break;
1955
1956 case Primitive::kPrimFloat:
1957 case Primitive::kPrimDouble:
1958 locations->SetInAt(0, Location::RequiresFpuRegister());
1959 locations->SetInAt(1, Location::RequiresFpuRegister());
1960 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1961 break;
1962
1963 default:
1964 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1965 }
1966}
1967
1968void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1969 Primitive::Type type = div->GetResultType();
1970 switch (type) {
1971 case Primitive::kPrimInt:
1972 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001973 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001974 break;
1975
1976 case Primitive::kPrimFloat:
1977 case Primitive::kPrimDouble:
1978 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1979 break;
1980
1981 default:
1982 LOG(FATAL) << "Unexpected div type " << type;
1983 }
1984}
1985
Alexandre Rames67555f72014-11-18 10:55:16 +00001986void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1987 LocationSummary* locations =
1988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1989 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1990 if (instruction->HasUses()) {
1991 locations->SetOut(Location::SameAsFirstInput());
1992 }
1993}
1994
1995void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1996 SlowPathCodeARM64* slow_path =
1997 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1998 codegen_->AddSlowPath(slow_path);
1999 Location value = instruction->GetLocations()->InAt(0);
2000
Alexandre Rames3e69f162014-12-10 10:36:50 +00002001 Primitive::Type type = instruction->GetType();
2002
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002003 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2004 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002005 return;
2006 }
2007
Alexandre Rames67555f72014-11-18 10:55:16 +00002008 if (value.IsConstant()) {
2009 int64_t divisor = Int64ConstantFrom(value);
2010 if (divisor == 0) {
2011 __ B(slow_path->GetEntryLabel());
2012 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002013 // A division by a non-null constant is valid. We don't need to perform
2014 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002015 }
2016 } else {
2017 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2018 }
2019}
2020
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002021void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2024 locations->SetOut(Location::ConstantLocation(constant));
2025}
2026
2027void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2028 UNUSED(constant);
2029 // Will be generated at use site.
2030}
2031
Alexandre Rames5319def2014-10-23 10:03:10 +01002032void LocationsBuilderARM64::VisitExit(HExit* exit) {
2033 exit->SetLocations(nullptr);
2034}
2035
2036void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002037 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002038}
2039
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002040void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2041 LocationSummary* locations =
2042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2043 locations->SetOut(Location::ConstantLocation(constant));
2044}
2045
2046void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2047 UNUSED(constant);
2048 // Will be generated at use site.
2049}
2050
David Brazdilfc6a86a2015-06-26 10:33:45 +00002051void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002052 DCHECK(!successor->IsExitBlock());
2053 HBasicBlock* block = got->GetBlock();
2054 HInstruction* previous = got->GetPrevious();
2055 HLoopInformation* info = block->GetLoopInformation();
2056
David Brazdil46e2a392015-03-16 17:31:52 +00002057 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002058 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2059 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2060 return;
2061 }
2062 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2063 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2064 }
2065 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002066 __ B(codegen_->GetLabelOf(successor));
2067 }
2068}
2069
David Brazdilfc6a86a2015-06-26 10:33:45 +00002070void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2071 got->SetLocations(nullptr);
2072}
2073
2074void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2075 HandleGoto(got, got->GetSuccessor());
2076}
2077
2078void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2079 try_boundary->SetLocations(nullptr);
2080}
2081
2082void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2083 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2084 if (!successor->IsExitBlock()) {
2085 HandleGoto(try_boundary, successor);
2086 }
2087}
2088
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002089void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2090 vixl::Label* true_target,
2091 vixl::Label* false_target,
2092 vixl::Label* always_true_target) {
2093 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002094 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002095
Serban Constantinescu02164b32014-11-13 14:05:07 +00002096 if (cond->IsIntConstant()) {
2097 int32_t cond_value = cond->AsIntConstant()->GetValue();
2098 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002099 if (always_true_target != nullptr) {
2100 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002101 }
2102 return;
2103 } else {
2104 DCHECK_EQ(cond_value, 0);
2105 }
2106 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002107 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002108 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002109 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002110 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002111 } else {
2112 // The condition instruction has not been materialized, use its inputs as
2113 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002114 Primitive::Type type =
2115 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2116
2117 if (Primitive::IsFloatingPointType(type)) {
2118 // FP compares don't like null false_targets.
2119 if (false_target == nullptr) {
2120 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002121 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002122 FPRegister lhs = InputFPRegisterAt(condition, 0);
2123 if (condition->GetLocations()->InAt(1).IsConstant()) {
2124 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2125 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2126 __ Fcmp(lhs, 0.0);
2127 } else {
2128 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2129 }
2130 if (condition->IsFPConditionTrueIfNaN()) {
2131 __ B(vs, true_target); // VS for unordered.
2132 } else if (condition->IsFPConditionFalseIfNaN()) {
2133 __ B(vs, false_target); // VS for unordered.
2134 }
2135 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002136 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002137 // Integer cases.
2138 Register lhs = InputRegisterAt(condition, 0);
2139 Operand rhs = InputOperandAt(condition, 1);
2140 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2141 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2142 switch (arm64_cond) {
2143 case eq:
2144 __ Cbz(lhs, true_target);
2145 break;
2146 case ne:
2147 __ Cbnz(lhs, true_target);
2148 break;
2149 case lt:
2150 // Test the sign bit and branch accordingly.
2151 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2152 break;
2153 case ge:
2154 // Test the sign bit and branch accordingly.
2155 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2156 break;
2157 default:
2158 // Without the `static_cast` the compiler throws an error for
2159 // `-Werror=sign-promo`.
2160 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2161 }
2162 } else {
2163 __ Cmp(lhs, rhs);
2164 __ B(arm64_cond, true_target);
2165 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002166 }
2167 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002168 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002169 __ B(false_target);
2170 }
2171}
2172
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002173void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2175 HInstruction* cond = if_instr->InputAt(0);
2176 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2177 locations->SetInAt(0, Location::RequiresRegister());
2178 }
2179}
2180
2181void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2182 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2183 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2184 vixl::Label* always_true_target = true_target;
2185 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2186 if_instr->IfTrueSuccessor())) {
2187 always_true_target = nullptr;
2188 }
2189 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2190 if_instr->IfFalseSuccessor())) {
2191 false_target = nullptr;
2192 }
2193 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2194}
2195
2196void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2197 LocationSummary* locations = new (GetGraph()->GetArena())
2198 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2199 HInstruction* cond = deoptimize->InputAt(0);
2200 DCHECK(cond->IsCondition());
2201 if (cond->AsCondition()->NeedsMaterialization()) {
2202 locations->SetInAt(0, Location::RequiresRegister());
2203 }
2204}
2205
2206void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2207 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2208 DeoptimizationSlowPathARM64(deoptimize);
2209 codegen_->AddSlowPath(slow_path);
2210 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2211 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2212}
2213
Alexandre Rames5319def2014-10-23 10:03:10 +01002214void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002215 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002216}
2217
2218void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002219 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002220}
2221
2222void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002223 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002224}
2225
2226void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002227 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002228}
2229
Alexandre Rames67555f72014-11-18 10:55:16 +00002230void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2231 LocationSummary::CallKind call_kind =
2232 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2234 locations->SetInAt(0, Location::RequiresRegister());
2235 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002236 // The output does overlap inputs.
2237 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002238}
2239
2240void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2241 LocationSummary* locations = instruction->GetLocations();
2242 Register obj = InputRegisterAt(instruction, 0);;
2243 Register cls = InputRegisterAt(instruction, 1);;
2244 Register out = OutputRegister(instruction);
2245
2246 vixl::Label done;
2247
2248 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002249 // Avoid null check if we know `obj` is not null.
2250 if (instruction->MustDoNullCheck()) {
2251 __ Mov(out, 0);
2252 __ Cbz(obj, &done);
2253 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002254
2255 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002256 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002257 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002258 __ Cmp(out, cls);
2259 if (instruction->IsClassFinal()) {
2260 // Classes must be equal for the instanceof to succeed.
2261 __ Cset(out, eq);
2262 } else {
2263 // If the classes are not equal, we go into a slow path.
2264 DCHECK(locations->OnlyCallsOnSlowPath());
2265 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002266 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2267 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002268 codegen_->AddSlowPath(slow_path);
2269 __ B(ne, slow_path->GetEntryLabel());
2270 __ Mov(out, 1);
2271 __ Bind(slow_path->GetExitLabel());
2272 }
2273
2274 __ Bind(&done);
2275}
2276
Alexandre Rames5319def2014-10-23 10:03:10 +01002277void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2279 locations->SetOut(Location::ConstantLocation(constant));
2280}
2281
2282void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2283 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002284 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002285}
2286
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002287void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2289 locations->SetOut(Location::ConstantLocation(constant));
2290}
2291
2292void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2293 // Will be generated at use site.
2294 UNUSED(constant);
2295}
2296
Alexandre Rames5319def2014-10-23 10:03:10 +01002297void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002298 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002299 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002300}
2301
Alexandre Rames67555f72014-11-18 10:55:16 +00002302void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2303 HandleInvoke(invoke);
2304}
2305
2306void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2307 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002308 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2309 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2310 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002311 Location receiver = invoke->GetLocations()->InAt(0);
2312 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002313 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002314
2315 // The register ip1 is required to be used for the hidden argument in
2316 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002317 MacroAssembler* masm = GetVIXLAssembler();
2318 UseScratchRegisterScope scratch_scope(masm);
2319 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002320 scratch_scope.Exclude(ip1);
2321 __ Mov(ip1, invoke->GetDexMethodIndex());
2322
2323 // temp = object->GetClass();
2324 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002325 __ Ldr(temp.W(), StackOperandFrom(receiver));
2326 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002327 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002328 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002329 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002330 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002331 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002332 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002334 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002335 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002336 // lr();
2337 __ Blr(lr);
2338 DCHECK(!codegen_->IsLeafMethod());
2339 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2340}
2341
2342void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002343 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2344 if (intrinsic.TryDispatch(invoke)) {
2345 return;
2346 }
2347
Alexandre Rames67555f72014-11-18 10:55:16 +00002348 HandleInvoke(invoke);
2349}
2350
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002351void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002352 // When we do not run baseline, explicit clinit checks triggered by static
2353 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2354 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002355
Andreas Gampe878d58c2015-01-15 23:24:00 -08002356 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2357 if (intrinsic.TryDispatch(invoke)) {
2358 return;
2359 }
2360
Alexandre Rames67555f72014-11-18 10:55:16 +00002361 HandleInvoke(invoke);
2362}
2363
Andreas Gampe878d58c2015-01-15 23:24:00 -08002364static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2365 if (invoke->GetLocations()->Intrinsified()) {
2366 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2367 intrinsic.Dispatch(invoke);
2368 return true;
2369 }
2370 return false;
2371}
2372
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002373void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002374 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002375 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002376
2377 // TODO: Implement all kinds of calls:
2378 // 1) boot -> boot
2379 // 2) app -> boot
2380 // 3) app -> app
2381 //
2382 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2383
Jeff Hao848f70a2014-01-15 13:49:50 -08002384 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002385 Register reg = XRegisterFrom(temp);
Jeff Hao848f70a2014-01-15 13:49:50 -08002386 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002387 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002388 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002389 __ Ldr(lr, MemOperand(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002390 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002391 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002392 __ Blr(lr);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002393 } else if (invoke->IsRecursive()) {
2394 __ Bl(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002395 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002396 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002397 Register reg = XRegisterFrom(temp);
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002398 Register method_reg;
2399 if (current_method.IsRegister()) {
2400 method_reg = XRegisterFrom(current_method);
2401 } else {
2402 DCHECK(invoke->GetLocations()->Intrinsified());
2403 DCHECK(!current_method.IsValid());
2404 method_reg = reg;
2405 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2406 }
2407
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002408 // temp = current_method->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002409 __ Ldr(reg.W(), MemOperand(method_reg.X(),
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002410 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2411 // temp = temp[index_in_cache];
2412 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2413 // lr = temp->entry_point_from_quick_compiled_code_;
2414 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2415 kArm64WordSize).Int32Value()));
2416 // lr();
2417 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002418 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002419
Andreas Gampe878d58c2015-01-15 23:24:00 -08002420 DCHECK(!IsLeafMethod());
2421}
2422
2423void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002424 // When we do not run baseline, explicit clinit checks triggered by static
2425 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2426 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002427
Andreas Gampe878d58c2015-01-15 23:24:00 -08002428 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2429 return;
2430 }
2431
Alexandre Ramesd921d642015-04-16 15:07:16 +01002432 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002433 LocationSummary* locations = invoke->GetLocations();
2434 codegen_->GenerateStaticOrDirectCall(
2435 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002436 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002437}
2438
2439void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002440 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2441 return;
2442 }
2443
Alexandre Rames5319def2014-10-23 10:03:10 +01002444 LocationSummary* locations = invoke->GetLocations();
2445 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002446 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2447 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2448 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002449 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002450 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002451
Alexandre Ramesd921d642015-04-16 15:07:16 +01002452 BlockPoolsScope block_pools(GetVIXLAssembler());
2453
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002454 DCHECK(receiver.IsRegister());
2455 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002456 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002457 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002458 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002459 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002460 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002461 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002462 // lr();
2463 __ Blr(lr);
2464 DCHECK(!codegen_->IsLeafMethod());
2465 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2466}
2467
Alexandre Rames67555f72014-11-18 10:55:16 +00002468void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2469 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2470 : LocationSummary::kNoCall;
2471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002472 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002473 locations->SetOut(Location::RequiresRegister());
2474}
2475
2476void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2477 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002478 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002479 if (cls->IsReferrersClass()) {
2480 DCHECK(!cls->CanCallRuntime());
2481 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002482 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002483 } else {
2484 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002485 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002486 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002487 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002488
2489 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2490 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2491 codegen_->AddSlowPath(slow_path);
2492 __ Cbz(out, slow_path->GetEntryLabel());
2493 if (cls->MustGenerateClinitCheck()) {
2494 GenerateClassInitializationCheck(slow_path, out);
2495 } else {
2496 __ Bind(slow_path->GetExitLabel());
2497 }
2498 }
2499}
2500
David Brazdilcb1c0552015-08-04 16:22:25 +01002501static MemOperand GetExceptionTlsAddress() {
2502 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2503}
2504
Alexandre Rames67555f72014-11-18 10:55:16 +00002505void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2506 LocationSummary* locations =
2507 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2508 locations->SetOut(Location::RequiresRegister());
2509}
2510
2511void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002512 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2513}
2514
2515void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2516 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2517}
2518
2519void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2520 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002521}
2522
Alexandre Rames5319def2014-10-23 10:03:10 +01002523void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2524 load->SetLocations(nullptr);
2525}
2526
2527void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2528 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002529 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002530}
2531
Alexandre Rames67555f72014-11-18 10:55:16 +00002532void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2533 LocationSummary* locations =
2534 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002535 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002536 locations->SetOut(Location::RequiresRegister());
2537}
2538
2539void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2540 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2541 codegen_->AddSlowPath(slow_path);
2542
2543 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002544 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002545 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002546 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002547 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002548 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002549 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002550 __ Cbz(out, slow_path->GetEntryLabel());
2551 __ Bind(slow_path->GetExitLabel());
2552}
2553
Alexandre Rames5319def2014-10-23 10:03:10 +01002554void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2555 local->SetLocations(nullptr);
2556}
2557
2558void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2559 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2560}
2561
2562void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2564 locations->SetOut(Location::ConstantLocation(constant));
2565}
2566
2567void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2568 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002569 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002570}
2571
Alexandre Rames67555f72014-11-18 10:55:16 +00002572void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2573 LocationSummary* locations =
2574 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2575 InvokeRuntimeCallingConvention calling_convention;
2576 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2577}
2578
2579void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2580 codegen_->InvokeRuntime(instruction->IsEnter()
2581 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2582 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002583 instruction->GetDexPc(),
2584 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002585 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002586}
2587
Alexandre Rames42d641b2014-10-27 14:00:51 +00002588void LocationsBuilderARM64::VisitMul(HMul* mul) {
2589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2591 switch (mul->GetResultType()) {
2592 case Primitive::kPrimInt:
2593 case Primitive::kPrimLong:
2594 locations->SetInAt(0, Location::RequiresRegister());
2595 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002597 break;
2598
2599 case Primitive::kPrimFloat:
2600 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002601 locations->SetInAt(0, Location::RequiresFpuRegister());
2602 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002603 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002604 break;
2605
2606 default:
2607 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2608 }
2609}
2610
2611void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2612 switch (mul->GetResultType()) {
2613 case Primitive::kPrimInt:
2614 case Primitive::kPrimLong:
2615 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2616 break;
2617
2618 case Primitive::kPrimFloat:
2619 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002620 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002621 break;
2622
2623 default:
2624 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2625 }
2626}
2627
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002628void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2629 LocationSummary* locations =
2630 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2631 switch (neg->GetResultType()) {
2632 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002633 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002634 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002635 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002636 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002637
2638 case Primitive::kPrimFloat:
2639 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002640 locations->SetInAt(0, Location::RequiresFpuRegister());
2641 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002642 break;
2643
2644 default:
2645 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2646 }
2647}
2648
2649void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2650 switch (neg->GetResultType()) {
2651 case Primitive::kPrimInt:
2652 case Primitive::kPrimLong:
2653 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2654 break;
2655
2656 case Primitive::kPrimFloat:
2657 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002658 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002659 break;
2660
2661 default:
2662 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2663 }
2664}
2665
2666void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2667 LocationSummary* locations =
2668 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2669 InvokeRuntimeCallingConvention calling_convention;
2670 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002671 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002672 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002673 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002674 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002675 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002676}
2677
2678void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2679 LocationSummary* locations = instruction->GetLocations();
2680 InvokeRuntimeCallingConvention calling_convention;
2681 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2682 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002683 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002684 // Note: if heap poisoning is enabled, the entry point takes cares
2685 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002686 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002687 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2688 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002689 instruction->GetDexPc(),
2690 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002691 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002692}
2693
Alexandre Rames5319def2014-10-23 10:03:10 +01002694void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2697 InvokeRuntimeCallingConvention calling_convention;
2698 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002699 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002700 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002701 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002702}
2703
2704void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2705 LocationSummary* locations = instruction->GetLocations();
2706 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2707 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002708 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002709 // Note: if heap poisoning is enabled, the entry point takes cares
2710 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002711 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002712 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2713 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002714 instruction->GetDexPc(),
2715 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002716 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002717}
2718
2719void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002721 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002723}
2724
2725void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002726 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002727 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002728 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002729 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002730 break;
2731
2732 default:
2733 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2734 }
2735}
2736
David Brazdil66d126e2015-04-03 16:02:44 +01002737void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2739 locations->SetInAt(0, Location::RequiresRegister());
2740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2741}
2742
2743void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002744 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2745}
2746
Alexandre Rames5319def2014-10-23 10:03:10 +01002747void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2748 LocationSummary* locations =
2749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2750 locations->SetInAt(0, Location::RequiresRegister());
2751 if (instruction->HasUses()) {
2752 locations->SetOut(Location::SameAsFirstInput());
2753 }
2754}
2755
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002756void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002757 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2758 return;
2759 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002760
Alexandre Ramesd921d642015-04-16 15:07:16 +01002761 BlockPoolsScope block_pools(GetVIXLAssembler());
2762 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002763 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2764 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2765}
2766
2767void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002768 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2769 codegen_->AddSlowPath(slow_path);
2770
2771 LocationSummary* locations = instruction->GetLocations();
2772 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002773
2774 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002775}
2776
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002777void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2778 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2779 GenerateImplicitNullCheck(instruction);
2780 } else {
2781 GenerateExplicitNullCheck(instruction);
2782 }
2783}
2784
Alexandre Rames67555f72014-11-18 10:55:16 +00002785void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2786 HandleBinaryOp(instruction);
2787}
2788
2789void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2790 HandleBinaryOp(instruction);
2791}
2792
Alexandre Rames3e69f162014-12-10 10:36:50 +00002793void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2794 LOG(FATAL) << "Unreachable";
2795}
2796
2797void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2798 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2799}
2800
Alexandre Rames5319def2014-10-23 10:03:10 +01002801void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2803 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2804 if (location.IsStackSlot()) {
2805 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2806 } else if (location.IsDoubleStackSlot()) {
2807 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2808 }
2809 locations->SetOut(location);
2810}
2811
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002812void InstructionCodeGeneratorARM64::VisitParameterValue(
2813 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002814 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002815}
2816
2817void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2818 LocationSummary* locations =
2819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002820 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002821}
2822
2823void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2824 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2825 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002826}
2827
2828void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2829 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2830 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2831 locations->SetInAt(i, Location::Any());
2832 }
2833 locations->SetOut(Location::Any());
2834}
2835
2836void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002837 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002838 LOG(FATAL) << "Unreachable";
2839}
2840
Serban Constantinescu02164b32014-11-13 14:05:07 +00002841void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002842 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002843 LocationSummary::CallKind call_kind =
2844 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002845 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2846
2847 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002848 case Primitive::kPrimInt:
2849 case Primitive::kPrimLong:
2850 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002851 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2853 break;
2854
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002855 case Primitive::kPrimFloat:
2856 case Primitive::kPrimDouble: {
2857 InvokeRuntimeCallingConvention calling_convention;
2858 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2859 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2860 locations->SetOut(calling_convention.GetReturnLocation(type));
2861
2862 break;
2863 }
2864
Serban Constantinescu02164b32014-11-13 14:05:07 +00002865 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002866 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002867 }
2868}
2869
2870void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2871 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002872
Serban Constantinescu02164b32014-11-13 14:05:07 +00002873 switch (type) {
2874 case Primitive::kPrimInt:
2875 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002876 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002877 break;
2878 }
2879
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002880 case Primitive::kPrimFloat:
2881 case Primitive::kPrimDouble: {
2882 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2883 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002884 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002885 break;
2886 }
2887
Serban Constantinescu02164b32014-11-13 14:05:07 +00002888 default:
2889 LOG(FATAL) << "Unexpected rem type " << type;
2890 }
2891}
2892
Calin Juravle27df7582015-04-17 19:12:31 +01002893void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2894 memory_barrier->SetLocations(nullptr);
2895}
2896
2897void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2898 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2899}
2900
Alexandre Rames5319def2014-10-23 10:03:10 +01002901void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2903 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002904 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002905}
2906
2907void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002908 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002909 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002910}
2911
2912void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2913 instruction->SetLocations(nullptr);
2914}
2915
2916void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002917 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002918 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002919}
2920
Serban Constantinescu02164b32014-11-13 14:05:07 +00002921void LocationsBuilderARM64::VisitShl(HShl* shl) {
2922 HandleShift(shl);
2923}
2924
2925void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2926 HandleShift(shl);
2927}
2928
2929void LocationsBuilderARM64::VisitShr(HShr* shr) {
2930 HandleShift(shr);
2931}
2932
2933void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2934 HandleShift(shr);
2935}
2936
Alexandre Rames5319def2014-10-23 10:03:10 +01002937void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2939 Primitive::Type field_type = store->InputAt(1)->GetType();
2940 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002941 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002942 case Primitive::kPrimBoolean:
2943 case Primitive::kPrimByte:
2944 case Primitive::kPrimChar:
2945 case Primitive::kPrimShort:
2946 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002947 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002948 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2949 break;
2950
2951 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002952 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002953 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2954 break;
2955
2956 default:
2957 LOG(FATAL) << "Unimplemented local type " << field_type;
2958 }
2959}
2960
2961void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002962 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002963}
2964
2965void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002966 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002967}
2968
2969void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002970 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002971}
2972
Alexandre Rames67555f72014-11-18 10:55:16 +00002973void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002974 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002975}
2976
2977void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002978 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002979}
2980
2981void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002982 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002983}
2984
Alexandre Rames67555f72014-11-18 10:55:16 +00002985void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002986 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002987}
2988
2989void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2991}
2992
2993void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002994 HBasicBlock* block = instruction->GetBlock();
2995 if (block->GetLoopInformation() != nullptr) {
2996 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2997 // The back edge will generate the suspend check.
2998 return;
2999 }
3000 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3001 // The goto will generate the suspend check.
3002 return;
3003 }
3004 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003005}
3006
3007void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3008 temp->SetLocations(nullptr);
3009}
3010
3011void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3012 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003013 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003014}
3015
Alexandre Rames67555f72014-11-18 10:55:16 +00003016void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3019 InvokeRuntimeCallingConvention calling_convention;
3020 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3021}
3022
3023void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3024 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003025 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003026 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003027}
3028
3029void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3030 LocationSummary* locations =
3031 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3032 Primitive::Type input_type = conversion->GetInputType();
3033 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003034 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003035 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3036 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3037 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3038 }
3039
Alexandre Rames542361f2015-01-29 16:57:31 +00003040 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003041 locations->SetInAt(0, Location::RequiresFpuRegister());
3042 } else {
3043 locations->SetInAt(0, Location::RequiresRegister());
3044 }
3045
Alexandre Rames542361f2015-01-29 16:57:31 +00003046 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003047 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3048 } else {
3049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3050 }
3051}
3052
3053void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3054 Primitive::Type result_type = conversion->GetResultType();
3055 Primitive::Type input_type = conversion->GetInputType();
3056
3057 DCHECK_NE(input_type, result_type);
3058
Alexandre Rames542361f2015-01-29 16:57:31 +00003059 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003060 int result_size = Primitive::ComponentSize(result_type);
3061 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003062 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003063 Register output = OutputRegister(conversion);
3064 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003065 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3066 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3067 } else if ((result_type == Primitive::kPrimChar) ||
3068 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3069 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003070 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003071 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003072 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003073 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003074 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003075 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003076 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3077 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003078 } else if (Primitive::IsFloatingPointType(result_type) &&
3079 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003080 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3081 } else {
3082 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3083 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003084 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003085}
Alexandre Rames67555f72014-11-18 10:55:16 +00003086
Serban Constantinescu02164b32014-11-13 14:05:07 +00003087void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3088 HandleShift(ushr);
3089}
3090
3091void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3092 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003093}
3094
3095void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3096 HandleBinaryOp(instruction);
3097}
3098
3099void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3100 HandleBinaryOp(instruction);
3101}
3102
Calin Juravleb1498f62015-02-16 13:13:29 +00003103void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3104 // Nothing to do, this should be removed during prepare for register allocator.
3105 UNUSED(instruction);
3106 LOG(FATAL) << "Unreachable";
3107}
3108
3109void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3110 // Nothing to do, this should be removed during prepare for register allocator.
3111 UNUSED(instruction);
3112 LOG(FATAL) << "Unreachable";
3113}
3114
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003115void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3116 DCHECK(codegen_->IsBaseline());
3117 LocationSummary* locations =
3118 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3119 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3120}
3121
3122void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3123 DCHECK(codegen_->IsBaseline());
3124 // Will be generated at use site.
3125}
3126
Alexandre Rames67555f72014-11-18 10:55:16 +00003127#undef __
3128#undef QUICK_ENTRY_POINT
3129
Alexandre Rames5319def2014-10-23 10:03:10 +01003130} // namespace arm64
3131} // namespace art