blob: 2f607f70a3218d82eee43019cf72ee0cbb920bd9 [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;
80 default:
81 LOG(FATAL) << "Unknown if condition";
82 }
83 return nv; // Unreachable.
84}
85
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000087 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010096 } else if (return_type == Primitive::kPrimVoid) {
97 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000098 } else {
99 return LocationFrom(w0);
100 }
101}
102
Alexandre Rames5319def2014-10-23 10:03:10 +0100103Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000104 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105}
106
Alexandre Rames67555f72014-11-18 10:55:16 +0000107#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
108#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100109
Zheng Xuda403092015-04-24 17:35:39 +0800110// Calculate memory accessing operand for save/restore live registers.
111static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
112 RegisterSet* register_set,
113 int64_t spill_offset,
114 bool is_save) {
115 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
116 codegen->GetNumberOfCoreRegisters(),
117 register_set->GetFloatingPointRegisters(),
118 codegen->GetNumberOfFloatingPointRegisters()));
119
120 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
121 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
122 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
123 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
124
125 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
126 UseScratchRegisterScope temps(masm);
127
128 Register base = masm->StackPointer();
129 int64_t core_spill_size = core_list.TotalSizeInBytes();
130 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
131 int64_t reg_size = kXRegSizeInBytes;
132 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
133 uint32_t ls_access_size = WhichPowerOf2(reg_size);
134 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
135 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
136 // If the offset does not fit in the instruction's immediate field, use an alternate register
137 // to compute the base address(float point registers spill base address).
138 Register new_base = temps.AcquireSameSizeAs(base);
139 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
140 base = new_base;
141 spill_offset = -core_spill_size;
142 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
143 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
144 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
145 }
146
147 if (is_save) {
148 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
149 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
150 } else {
151 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
152 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
153 }
154}
155
156void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
157 RegisterSet* register_set = locations->GetLiveRegisters();
158 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
159 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
160 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
161 // If the register holds an object, update the stack mask.
162 if (locations->RegisterContainsObject(i)) {
163 locations->SetStackBit(stack_offset / kVRegSize);
164 }
165 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
166 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
167 saved_core_stack_offsets_[i] = stack_offset;
168 stack_offset += kXRegSizeInBytes;
169 }
170 }
171
172 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
173 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
174 register_set->ContainsFloatingPointRegister(i)) {
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_fpu_stack_offsets_[i] = stack_offset;
178 stack_offset += kDRegSizeInBytes;
179 }
180 }
181
182 SaveRestoreLiveRegistersHelper(codegen, register_set,
183 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
184}
185
186void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 SaveRestoreLiveRegistersHelper(codegen, register_set,
189 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
190}
191
Alexandre Rames5319def2014-10-23 10:03:10 +0100192class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
193 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000194 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
195 Location index_location,
196 Location length_location)
197 : instruction_(instruction),
198 index_location_(index_location),
199 length_location_(length_location) {}
200
Alexandre Rames5319def2014-10-23 10:03:10 +0100201
Alexandre Rames67555f72014-11-18 10:55:16 +0000202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000203 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100204 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000205 // We're moving two locations to locations that could overlap, so we need a parallel
206 // move resolver.
207 InvokeRuntimeCallingConvention calling_convention;
208 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100209 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
210 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000211 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000212 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800213 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 }
215
216 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000217 HBoundsCheck* const instruction_;
218 const Location index_location_;
219 const Location length_location_;
220
Alexandre Rames5319def2014-10-23 10:03:10 +0100221 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
222};
223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
225 public:
226 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
227
228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
229 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
230 __ Bind(GetEntryLabel());
231 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000232 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800233 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000234 }
235
236 private:
237 HDivZeroCheck* const instruction_;
238 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
239};
240
241class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
242 public:
243 LoadClassSlowPathARM64(HLoadClass* cls,
244 HInstruction* at,
245 uint32_t dex_pc,
246 bool do_clinit)
247 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
248 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
249 }
250
251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
252 LocationSummary* locations = at_->GetLocations();
253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
254
255 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 InvokeRuntimeCallingConvention calling_convention;
259 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
261 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000262 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800263 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800265 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000268
269 // Move the class to the desired location.
270 Location out = locations->Out();
271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
273 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000274 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 }
276
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000278 __ B(GetExitLabel());
279 }
280
281 private:
282 // The class this slow path will load.
283 HLoadClass* const cls_;
284
285 // The instruction where this slow path is happening.
286 // (Might be the load class or an initialization check).
287 HInstruction* const at_;
288
289 // The dex PC of `at_`.
290 const uint32_t dex_pc_;
291
292 // Whether to initialize the class.
293 const bool do_clinit_;
294
295 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
296};
297
298class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
299 public:
300 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
301
302 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
303 LocationSummary* locations = instruction_->GetLocations();
304 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
305 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
306
307 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000308 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000309
310 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800311 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000313 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100314 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000316 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000318 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 __ B(GetExitLabel());
320 }
321
322 private:
323 HLoadString* const instruction_;
324
325 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
326};
327
Alexandre Rames5319def2014-10-23 10:03:10 +0100328class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
329 public:
330 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
331
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
333 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100334 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000335 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000336 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800337 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100338 }
339
340 private:
341 HNullCheck* const instruction_;
342
343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
344};
345
346class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
347 public:
348 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
349 HBasicBlock* successor)
350 : instruction_(instruction), successor_(successor) {}
351
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
353 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100354 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000355 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000357 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800358 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 if (successor_ == nullptr) {
361 __ B(GetReturnLabel());
362 } else {
363 __ B(arm64_codegen->GetLabelOf(successor_));
364 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100365 }
366
367 vixl::Label* GetReturnLabel() {
368 DCHECK(successor_ == nullptr);
369 return &return_label_;
370 }
371
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100372 HBasicBlock* GetSuccessor() const {
373 return successor_;
374 }
375
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 private:
377 HSuspendCheck* const instruction_;
378 // If not null, the block to branch to after the suspend check.
379 HBasicBlock* const successor_;
380
381 // If `successor_` is null, the label to branch to after the suspend check.
382 vixl::Label return_label_;
383
384 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
385};
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
388 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000389 TypeCheckSlowPathARM64(HInstruction* instruction,
390 Location class_to_check,
391 Location object_class,
392 uint32_t dex_pc)
393 : instruction_(instruction),
394 class_to_check_(class_to_check),
395 object_class_(object_class),
396 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000399 LocationSummary* locations = instruction_->GetLocations();
400 DCHECK(instruction_->IsCheckCast()
401 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
402 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
403
Alexandre Rames67555f72014-11-18 10:55:16 +0000404 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000405 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000406
407 // We're moving two locations to locations that could overlap, so we need a parallel
408 // move resolver.
409 InvokeRuntimeCallingConvention calling_convention;
410 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100411 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
412 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000413
414 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000415 arm64_codegen->InvokeRuntime(
416 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000417 Primitive::Type ret_type = instruction_->GetType();
418 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
419 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800420 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
421 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000422 } else {
423 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000424 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800425 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000426 }
427
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000428 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000429 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000430 }
431
432 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000433 HInstruction* const instruction_;
434 const Location class_to_check_;
435 const Location object_class_;
436 uint32_t dex_pc_;
437
Alexandre Rames67555f72014-11-18 10:55:16 +0000438 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
439};
440
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700441class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
442 public:
443 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
444 : instruction_(instruction) {}
445
446 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
447 __ Bind(GetEntryLabel());
448 SaveLiveRegisters(codegen, instruction_->GetLocations());
449 DCHECK(instruction_->IsDeoptimize());
450 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
451 uint32_t dex_pc = deoptimize->GetDexPc();
452 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
453 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
454 }
455
456 private:
457 HInstruction* const instruction_;
458 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
459};
460
Alexandre Rames5319def2014-10-23 10:03:10 +0100461#undef __
462
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100463Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100464 Location next_location;
465 if (type == Primitive::kPrimVoid) {
466 LOG(FATAL) << "Unreachable type " << type;
467 }
468
Alexandre Rames542361f2015-01-29 16:57:31 +0000469 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100470 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
471 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000472 } else if (!Primitive::IsFloatingPointType(type) &&
473 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000474 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
475 } else {
476 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000477 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
478 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100479 }
480
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000481 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000482 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 return next_location;
484}
485
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100486Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
487 return LocationFrom(x0);
488}
489
Serban Constantinescu579885a2015-02-22 20:51:33 +0000490CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
491 const Arm64InstructionSetFeatures& isa_features,
492 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100493 : CodeGenerator(graph,
494 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000495 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000496 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000497 callee_saved_core_registers.list(),
498 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000499 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100500 block_labels_(nullptr),
501 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000502 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000503 move_resolver_(graph->GetArena(), this),
504 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000505 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000506 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000507}
Alexandre Rames5319def2014-10-23 10:03:10 +0100508
Alexandre Rames67555f72014-11-18 10:55:16 +0000509#undef __
510#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100511
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000512void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
513 // Ensure we emit the literal pool.
514 __ FinalizeCode();
515 CodeGenerator::Finalize(allocator);
516}
517
Zheng Xuad4450e2015-04-17 18:48:56 +0800518void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
519 // Note: There are 6 kinds of moves:
520 // 1. constant -> GPR/FPR (non-cycle)
521 // 2. constant -> stack (non-cycle)
522 // 3. GPR/FPR -> GPR/FPR
523 // 4. GPR/FPR -> stack
524 // 5. stack -> GPR/FPR
525 // 6. stack -> stack (non-cycle)
526 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
527 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
528 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
529 // dependency.
530 vixl_temps_.Open(GetVIXLAssembler());
531}
532
533void ParallelMoveResolverARM64::FinishEmitNativeCode() {
534 vixl_temps_.Close();
535}
536
537Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
538 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
539 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
540 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
541 Location scratch = GetScratchLocation(kind);
542 if (!scratch.Equals(Location::NoLocation())) {
543 return scratch;
544 }
545 // Allocate from VIXL temp registers.
546 if (kind == Location::kRegister) {
547 scratch = LocationFrom(vixl_temps_.AcquireX());
548 } else {
549 DCHECK(kind == Location::kFpuRegister);
550 scratch = LocationFrom(vixl_temps_.AcquireD());
551 }
552 AddScratchLocation(scratch);
553 return scratch;
554}
555
556void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
557 if (loc.IsRegister()) {
558 vixl_temps_.Release(XRegisterFrom(loc));
559 } else {
560 DCHECK(loc.IsFpuRegister());
561 vixl_temps_.Release(DRegisterFrom(loc));
562 }
563 RemoveScratchLocation(loc);
564}
565
Alexandre Rames3e69f162014-12-10 10:36:50 +0000566void ParallelMoveResolverARM64::EmitMove(size_t index) {
567 MoveOperands* move = moves_.Get(index);
568 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
569}
570
Alexandre Rames5319def2014-10-23 10:03:10 +0100571void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100572 MacroAssembler* masm = GetVIXLAssembler();
573 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000574 __ Bind(&frame_entry_label_);
575
Serban Constantinescu02164b32014-11-13 14:05:07 +0000576 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
577 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100578 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000579 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000580 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000581 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000582 __ Ldr(wzr, MemOperand(temp, 0));
583 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000584 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100585
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000586 if (!HasEmptyFrame()) {
587 int frame_size = GetFrameSize();
588 // Stack layout:
589 // sp[frame_size - 8] : lr.
590 // ... : other preserved core registers.
591 // ... : other preserved fp registers.
592 // ... : reserved frame space.
593 // sp[0] : current method.
594 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100595 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800596 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
597 frame_size - GetCoreSpillSize());
598 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
599 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000600 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100601}
602
603void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100604 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100605 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000606 if (!HasEmptyFrame()) {
607 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800608 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
609 frame_size - FrameEntrySpillSize());
610 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
611 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000612 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100613 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000614 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100615 __ Ret();
616 GetAssembler()->cfi().RestoreState();
617 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100618}
619
Zheng Xuda403092015-04-24 17:35:39 +0800620vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
621 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
622 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
623 core_spill_mask_);
624}
625
626vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
627 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
628 GetNumberOfFloatingPointRegisters()));
629 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
630 fpu_spill_mask_);
631}
632
Alexandre Rames5319def2014-10-23 10:03:10 +0100633void CodeGeneratorARM64::Bind(HBasicBlock* block) {
634 __ Bind(GetLabelOf(block));
635}
636
Alexandre Rames5319def2014-10-23 10:03:10 +0100637void CodeGeneratorARM64::Move(HInstruction* instruction,
638 Location location,
639 HInstruction* move_for) {
640 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100641 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000642 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100643
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100644 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700645 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100646 } else if (locations != nullptr && locations->Out().Equals(location)) {
647 return;
648 } else if (instruction->IsIntConstant()
649 || instruction->IsLongConstant()
650 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000651 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100652 if (location.IsRegister()) {
653 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000654 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100655 (instruction->IsLongConstant() && dst.Is64Bits()));
656 __ Mov(dst, value);
657 } else {
658 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000659 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000660 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
661 ? temps.AcquireW()
662 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100663 __ Mov(temp, value);
664 __ Str(temp, StackOperandFrom(location));
665 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000666 } else if (instruction->IsTemporary()) {
667 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000668 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100669 } else if (instruction->IsLoadLocal()) {
670 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000671 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000672 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000673 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000674 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100675 }
676
677 } else {
678 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000679 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100680 }
681}
682
Alexandre Rames5319def2014-10-23 10:03:10 +0100683Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
684 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000685
Alexandre Rames5319def2014-10-23 10:03:10 +0100686 switch (type) {
687 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000688 case Primitive::kPrimInt:
689 case Primitive::kPrimFloat:
690 return Location::StackSlot(GetStackSlot(load->GetLocal()));
691
692 case Primitive::kPrimLong:
693 case Primitive::kPrimDouble:
694 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
695
Alexandre Rames5319def2014-10-23 10:03:10 +0100696 case Primitive::kPrimBoolean:
697 case Primitive::kPrimByte:
698 case Primitive::kPrimChar:
699 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100700 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100701 LOG(FATAL) << "Unexpected type " << type;
702 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000703
Alexandre Rames5319def2014-10-23 10:03:10 +0100704 LOG(FATAL) << "Unreachable";
705 return Location::NoLocation();
706}
707
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100708void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000709 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100710 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000711 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100712 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100713 if (value_can_be_null) {
714 __ Cbz(value, &done);
715 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100716 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
717 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000718 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100719 if (value_can_be_null) {
720 __ Bind(&done);
721 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100722}
723
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000724void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
725 // Blocked core registers:
726 // lr : Runtime reserved.
727 // tr : Runtime reserved.
728 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
729 // ip1 : VIXL core temp.
730 // ip0 : VIXL core temp.
731 //
732 // Blocked fp registers:
733 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100734 CPURegList reserved_core_registers = vixl_reserved_core_registers;
735 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100736 while (!reserved_core_registers.IsEmpty()) {
737 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
738 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000739
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000740 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800741 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000742 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
743 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000744
745 if (is_baseline) {
746 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
747 while (!reserved_core_baseline_registers.IsEmpty()) {
748 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
749 }
750
751 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
752 while (!reserved_fp_baseline_registers.IsEmpty()) {
753 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
754 }
755 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100756}
757
758Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
759 if (type == Primitive::kPrimVoid) {
760 LOG(FATAL) << "Unreachable type " << type;
761 }
762
Alexandre Rames542361f2015-01-29 16:57:31 +0000763 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000764 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
765 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100766 return Location::FpuRegisterLocation(reg);
767 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000768 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
769 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100770 return Location::RegisterLocation(reg);
771 }
772}
773
Alexandre Rames3e69f162014-12-10 10:36:50 +0000774size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
775 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
776 __ Str(reg, MemOperand(sp, stack_index));
777 return kArm64WordSize;
778}
779
780size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
781 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
782 __ Ldr(reg, MemOperand(sp, stack_index));
783 return kArm64WordSize;
784}
785
786size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
787 FPRegister reg = FPRegister(reg_id, kDRegSize);
788 __ Str(reg, MemOperand(sp, stack_index));
789 return kArm64WordSize;
790}
791
792size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
793 FPRegister reg = FPRegister(reg_id, kDRegSize);
794 __ Ldr(reg, MemOperand(sp, stack_index));
795 return kArm64WordSize;
796}
797
Alexandre Rames5319def2014-10-23 10:03:10 +0100798void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100799 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100800}
801
802void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100803 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100804}
805
Alexandre Rames67555f72014-11-18 10:55:16 +0000806void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000807 if (constant->IsIntConstant()) {
808 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
809 } else if (constant->IsLongConstant()) {
810 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
811 } else if (constant->IsNullConstant()) {
812 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000813 } else if (constant->IsFloatConstant()) {
814 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
815 } else {
816 DCHECK(constant->IsDoubleConstant());
817 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
818 }
819}
820
Alexandre Rames3e69f162014-12-10 10:36:50 +0000821
822static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
823 DCHECK(constant.IsConstant());
824 HConstant* cst = constant.GetConstant();
825 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000826 // Null is mapped to a core W register, which we associate with kPrimInt.
827 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000828 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
829 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
830 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
831}
832
833void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000834 if (source.Equals(destination)) {
835 return;
836 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000837
838 // A valid move can always be inferred from the destination and source
839 // locations. When moving from and to a register, the argument type can be
840 // used to generate 32bit instead of 64bit moves. In debug mode we also
841 // checks the coherency of the locations and the type.
842 bool unspecified_type = (type == Primitive::kPrimVoid);
843
844 if (destination.IsRegister() || destination.IsFpuRegister()) {
845 if (unspecified_type) {
846 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
847 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000848 (src_cst != nullptr && (src_cst->IsIntConstant()
849 || src_cst->IsFloatConstant()
850 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000851 // For stack slots and 32bit constants, a 64bit type is appropriate.
852 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000853 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000854 // If the source is a double stack slot or a 64bit constant, a 64bit
855 // type is appropriate. Else the source is a register, and since the
856 // type has not been specified, we chose a 64bit type to force a 64bit
857 // move.
858 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000859 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000860 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000861 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
862 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000863 CPURegister dst = CPURegisterFrom(destination, type);
864 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
865 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
866 __ Ldr(dst, StackOperandFrom(source));
867 } else if (source.IsConstant()) {
868 DCHECK(CoherentConstantAndType(source, type));
869 MoveConstant(dst, source.GetConstant());
870 } else {
871 if (destination.IsRegister()) {
872 __ Mov(Register(dst), RegisterFrom(source, type));
873 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800874 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000875 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
876 }
877 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000878 } else { // The destination is not a register. It must be a stack slot.
879 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
880 if (source.IsRegister() || source.IsFpuRegister()) {
881 if (unspecified_type) {
882 if (source.IsRegister()) {
883 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
884 } else {
885 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
886 }
887 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000888 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
889 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
891 } else if (source.IsConstant()) {
892 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
893 UseScratchRegisterScope temps(GetVIXLAssembler());
894 HConstant* src_cst = source.GetConstant();
895 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000896 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000897 temp = temps.AcquireW();
898 } else if (src_cst->IsLongConstant()) {
899 temp = temps.AcquireX();
900 } else if (src_cst->IsFloatConstant()) {
901 temp = temps.AcquireS();
902 } else {
903 DCHECK(src_cst->IsDoubleConstant());
904 temp = temps.AcquireD();
905 }
906 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000907 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000908 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000909 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000910 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000911 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000912 // There is generally less pressure on FP registers.
913 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000914 __ Ldr(temp, StackOperandFrom(source));
915 __ Str(temp, StackOperandFrom(destination));
916 }
917 }
918}
919
920void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000921 CPURegister dst,
922 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000923 switch (type) {
924 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000925 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000926 break;
927 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000928 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000929 break;
930 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000931 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000932 break;
933 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000934 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000935 break;
936 case Primitive::kPrimInt:
937 case Primitive::kPrimNot:
938 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000939 case Primitive::kPrimFloat:
940 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000941 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000942 __ Ldr(dst, src);
943 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000944 case Primitive::kPrimVoid:
945 LOG(FATAL) << "Unreachable type " << type;
946 }
947}
948
Calin Juravle77520bc2015-01-12 18:45:46 +0000949void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000950 CPURegister dst,
951 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100952 MacroAssembler* masm = GetVIXLAssembler();
953 BlockPoolsScope block_pools(masm);
954 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000955 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000956 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000957
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000958 DCHECK(!src.IsPreIndex());
959 DCHECK(!src.IsPostIndex());
960
961 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800962 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000963 MemOperand base = MemOperand(temp_base);
964 switch (type) {
965 case Primitive::kPrimBoolean:
966 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000967 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000968 break;
969 case Primitive::kPrimByte:
970 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000971 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000972 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
973 break;
974 case Primitive::kPrimChar:
975 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000976 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000977 break;
978 case Primitive::kPrimShort:
979 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000980 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000981 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
982 break;
983 case Primitive::kPrimInt:
984 case Primitive::kPrimNot:
985 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000986 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000987 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000988 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000989 break;
990 case Primitive::kPrimFloat:
991 case Primitive::kPrimDouble: {
992 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000993 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994
995 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
996 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000997 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000998 __ Fmov(FPRegister(dst), temp);
999 break;
1000 }
1001 case Primitive::kPrimVoid:
1002 LOG(FATAL) << "Unreachable type " << type;
1003 }
1004}
1005
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001006void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001007 CPURegister src,
1008 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001009 switch (type) {
1010 case Primitive::kPrimBoolean:
1011 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001012 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001013 break;
1014 case Primitive::kPrimChar:
1015 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001016 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001017 break;
1018 case Primitive::kPrimInt:
1019 case Primitive::kPrimNot:
1020 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001021 case Primitive::kPrimFloat:
1022 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001023 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001024 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001025 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001026 case Primitive::kPrimVoid:
1027 LOG(FATAL) << "Unreachable type " << type;
1028 }
1029}
1030
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001031void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1032 CPURegister src,
1033 const MemOperand& dst) {
1034 UseScratchRegisterScope temps(GetVIXLAssembler());
1035 Register temp_base = temps.AcquireX();
1036
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001037 DCHECK(!dst.IsPreIndex());
1038 DCHECK(!dst.IsPostIndex());
1039
1040 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001041 Operand op = OperandFromMemOperand(dst);
1042 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 MemOperand base = MemOperand(temp_base);
1044 switch (type) {
1045 case Primitive::kPrimBoolean:
1046 case Primitive::kPrimByte:
1047 __ Stlrb(Register(src), base);
1048 break;
1049 case Primitive::kPrimChar:
1050 case Primitive::kPrimShort:
1051 __ Stlrh(Register(src), base);
1052 break;
1053 case Primitive::kPrimInt:
1054 case Primitive::kPrimNot:
1055 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001056 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001057 __ Stlr(Register(src), base);
1058 break;
1059 case Primitive::kPrimFloat:
1060 case Primitive::kPrimDouble: {
1061 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001062 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001063
1064 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1065 __ Fmov(temp, FPRegister(src));
1066 __ Stlr(temp, base);
1067 break;
1068 }
1069 case Primitive::kPrimVoid:
1070 LOG(FATAL) << "Unreachable type " << type;
1071 }
1072}
1073
Alexandre Rames67555f72014-11-18 10:55:16 +00001074void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001075 DCHECK(RequiresCurrentMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001076 CHECK(current_method.IsX());
Alexandre Rames67555f72014-11-18 10:55:16 +00001077 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
1078}
1079
1080void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1081 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001082 uint32_t dex_pc,
1083 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001084 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001085 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1086 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001087 RecordPcInfo(instruction, dex_pc, slow_path);
1088 DCHECK(instruction->IsSuspendCheck()
1089 || instruction->IsBoundsCheck()
1090 || instruction->IsNullCheck()
1091 || instruction->IsDivZeroCheck()
1092 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001093}
1094
1095void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1096 vixl::Register class_reg) {
1097 UseScratchRegisterScope temps(GetVIXLAssembler());
1098 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001099 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001100 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001101
Serban Constantinescu02164b32014-11-13 14:05:07 +00001102 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001103 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001104 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1105 __ Add(temp, class_reg, status_offset);
1106 __ Ldar(temp, HeapOperand(temp));
1107 __ Cmp(temp, mirror::Class::kStatusInitialized);
1108 __ B(lt, slow_path->GetEntryLabel());
1109 } else {
1110 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1111 __ Cmp(temp, mirror::Class::kStatusInitialized);
1112 __ B(lt, slow_path->GetEntryLabel());
1113 __ Dmb(InnerShareable, BarrierReads);
1114 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001115 __ Bind(slow_path->GetExitLabel());
1116}
Alexandre Rames5319def2014-10-23 10:03:10 +01001117
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001118void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1119 BarrierType type = BarrierAll;
1120
1121 switch (kind) {
1122 case MemBarrierKind::kAnyAny:
1123 case MemBarrierKind::kAnyStore: {
1124 type = BarrierAll;
1125 break;
1126 }
1127 case MemBarrierKind::kLoadAny: {
1128 type = BarrierReads;
1129 break;
1130 }
1131 case MemBarrierKind::kStoreStore: {
1132 type = BarrierWrites;
1133 break;
1134 }
1135 default:
1136 LOG(FATAL) << "Unexpected memory barrier " << kind;
1137 }
1138 __ Dmb(InnerShareable, type);
1139}
1140
Serban Constantinescu02164b32014-11-13 14:05:07 +00001141void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1142 HBasicBlock* successor) {
1143 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001144 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1145 if (slow_path == nullptr) {
1146 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1147 instruction->SetSlowPath(slow_path);
1148 codegen_->AddSlowPath(slow_path);
1149 if (successor != nullptr) {
1150 DCHECK(successor->IsLoopHeader());
1151 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1152 }
1153 } else {
1154 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1155 }
1156
Serban Constantinescu02164b32014-11-13 14:05:07 +00001157 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1158 Register temp = temps.AcquireW();
1159
1160 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1161 if (successor == nullptr) {
1162 __ Cbnz(temp, slow_path->GetEntryLabel());
1163 __ Bind(slow_path->GetReturnLabel());
1164 } else {
1165 __ Cbz(temp, codegen_->GetLabelOf(successor));
1166 __ B(slow_path->GetEntryLabel());
1167 // slow_path will return to GetLabelOf(successor).
1168 }
1169}
1170
Alexandre Rames5319def2014-10-23 10:03:10 +01001171InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1172 CodeGeneratorARM64* codegen)
1173 : HGraphVisitor(graph),
1174 assembler_(codegen->GetAssembler()),
1175 codegen_(codegen) {}
1176
1177#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001178 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001179
1180#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1181
1182enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001183 // Using a base helps identify when we hit such breakpoints.
1184 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001185#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1186 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1187#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1188};
1189
1190#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1191 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001192 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001193 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1194 } \
1195 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1197 locations->SetOut(Location::Any()); \
1198 }
1199 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1200#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1201
1202#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001203#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001204
Alexandre Rames67555f72014-11-18 10:55:16 +00001205void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001206 DCHECK_EQ(instr->InputCount(), 2U);
1207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1208 Primitive::Type type = instr->GetResultType();
1209 switch (type) {
1210 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001211 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001212 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001213 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001215 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001216
1217 case Primitive::kPrimFloat:
1218 case Primitive::kPrimDouble:
1219 locations->SetInAt(0, Location::RequiresFpuRegister());
1220 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001221 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001222 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001223
Alexandre Rames5319def2014-10-23 10:03:10 +01001224 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001225 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001226 }
1227}
1228
Alexandre Rames09a99962015-04-15 11:47:56 +01001229void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1230 LocationSummary* locations =
1231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1232 locations->SetInAt(0, Location::RequiresRegister());
1233 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1234 locations->SetOut(Location::RequiresFpuRegister());
1235 } else {
1236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1237 }
1238}
1239
1240void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1241 const FieldInfo& field_info) {
1242 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001243 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001244
1245 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1246 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1247
1248 if (field_info.IsVolatile()) {
1249 if (use_acquire_release) {
1250 // NB: LoadAcquire will record the pc info if needed.
1251 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1252 } else {
1253 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1254 codegen_->MaybeRecordImplicitNullCheck(instruction);
1255 // For IRIW sequential consistency kLoadAny is not sufficient.
1256 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1257 }
1258 } else {
1259 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1260 codegen_->MaybeRecordImplicitNullCheck(instruction);
1261 }
1262}
1263
1264void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1265 LocationSummary* locations =
1266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1267 locations->SetInAt(0, Location::RequiresRegister());
1268 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1269 locations->SetInAt(1, Location::RequiresFpuRegister());
1270 } else {
1271 locations->SetInAt(1, Location::RequiresRegister());
1272 }
1273}
1274
1275void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001276 const FieldInfo& field_info,
1277 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001278 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001279 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001280
1281 Register obj = InputRegisterAt(instruction, 0);
1282 CPURegister value = InputCPURegisterAt(instruction, 1);
1283 Offset offset = field_info.GetFieldOffset();
1284 Primitive::Type field_type = field_info.GetFieldType();
1285 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1286
1287 if (field_info.IsVolatile()) {
1288 if (use_acquire_release) {
1289 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1290 codegen_->MaybeRecordImplicitNullCheck(instruction);
1291 } else {
1292 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1293 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1294 codegen_->MaybeRecordImplicitNullCheck(instruction);
1295 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1296 }
1297 } else {
1298 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1299 codegen_->MaybeRecordImplicitNullCheck(instruction);
1300 }
1301
1302 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001303 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001304 }
1305}
1306
Alexandre Rames67555f72014-11-18 10:55:16 +00001307void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001308 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001309
1310 switch (type) {
1311 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001312 case Primitive::kPrimLong: {
1313 Register dst = OutputRegister(instr);
1314 Register lhs = InputRegisterAt(instr, 0);
1315 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001316 if (instr->IsAdd()) {
1317 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 } else if (instr->IsAnd()) {
1319 __ And(dst, lhs, rhs);
1320 } else if (instr->IsOr()) {
1321 __ Orr(dst, lhs, rhs);
1322 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001323 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001324 } else {
1325 DCHECK(instr->IsXor());
1326 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001327 }
1328 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001329 }
1330 case Primitive::kPrimFloat:
1331 case Primitive::kPrimDouble: {
1332 FPRegister dst = OutputFPRegister(instr);
1333 FPRegister lhs = InputFPRegisterAt(instr, 0);
1334 FPRegister rhs = InputFPRegisterAt(instr, 1);
1335 if (instr->IsAdd()) {
1336 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001337 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001338 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001339 } else {
1340 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001341 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001342 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001343 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001344 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001345 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001346 }
1347}
1348
Serban Constantinescu02164b32014-11-13 14:05:07 +00001349void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1350 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1351
1352 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1353 Primitive::Type type = instr->GetResultType();
1354 switch (type) {
1355 case Primitive::kPrimInt:
1356 case Primitive::kPrimLong: {
1357 locations->SetInAt(0, Location::RequiresRegister());
1358 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1359 locations->SetOut(Location::RequiresRegister());
1360 break;
1361 }
1362 default:
1363 LOG(FATAL) << "Unexpected shift type " << type;
1364 }
1365}
1366
1367void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1368 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1369
1370 Primitive::Type type = instr->GetType();
1371 switch (type) {
1372 case Primitive::kPrimInt:
1373 case Primitive::kPrimLong: {
1374 Register dst = OutputRegister(instr);
1375 Register lhs = InputRegisterAt(instr, 0);
1376 Operand rhs = InputOperandAt(instr, 1);
1377 if (rhs.IsImmediate()) {
1378 uint32_t shift_value = (type == Primitive::kPrimInt)
1379 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1380 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1381 if (instr->IsShl()) {
1382 __ Lsl(dst, lhs, shift_value);
1383 } else if (instr->IsShr()) {
1384 __ Asr(dst, lhs, shift_value);
1385 } else {
1386 __ Lsr(dst, lhs, shift_value);
1387 }
1388 } else {
1389 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1390
1391 if (instr->IsShl()) {
1392 __ Lsl(dst, lhs, rhs_reg);
1393 } else if (instr->IsShr()) {
1394 __ Asr(dst, lhs, rhs_reg);
1395 } else {
1396 __ Lsr(dst, lhs, rhs_reg);
1397 }
1398 }
1399 break;
1400 }
1401 default:
1402 LOG(FATAL) << "Unexpected shift operation type " << type;
1403 }
1404}
1405
Alexandre Rames5319def2014-10-23 10:03:10 +01001406void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001407 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001408}
1409
1410void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001411 HandleBinaryOp(instruction);
1412}
1413
1414void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1415 HandleBinaryOp(instruction);
1416}
1417
1418void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1419 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001420}
1421
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001422void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1423 LocationSummary* locations =
1424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1425 locations->SetInAt(0, Location::RequiresRegister());
1426 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001427 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1428 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1429 } else {
1430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1431 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001432}
1433
1434void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1435 LocationSummary* locations = instruction->GetLocations();
1436 Primitive::Type type = instruction->GetType();
1437 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001438 Location index = locations->InAt(1);
1439 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001440 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001441 MacroAssembler* masm = GetVIXLAssembler();
1442 UseScratchRegisterScope temps(masm);
1443 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001444
1445 if (index.IsConstant()) {
1446 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001447 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001448 } else {
1449 Register temp = temps.AcquireSameSizeAs(obj);
1450 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1451 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001452 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001453 }
1454
Alexandre Rames67555f72014-11-18 10:55:16 +00001455 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001456 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001457}
1458
Alexandre Rames5319def2014-10-23 10:03:10 +01001459void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1461 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001463}
1464
1465void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001466 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001467 __ Ldr(OutputRegister(instruction),
1468 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001469 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001470}
1471
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001472void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001473 if (instruction->NeedsTypeCheck()) {
1474 LocationSummary* locations =
1475 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001476 InvokeRuntimeCallingConvention calling_convention;
1477 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1478 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1479 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1480 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001481 LocationSummary* locations =
1482 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001483 locations->SetInAt(0, Location::RequiresRegister());
1484 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001485 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1486 locations->SetInAt(2, Location::RequiresFpuRegister());
1487 } else {
1488 locations->SetInAt(2, Location::RequiresRegister());
1489 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001490 }
1491}
1492
1493void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1494 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001495 LocationSummary* locations = instruction->GetLocations();
1496 bool needs_runtime_call = locations->WillCall();
1497
1498 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001499 codegen_->InvokeRuntime(
1500 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001501 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001502 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001503 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001505 Location index = locations->InAt(1);
1506 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001507 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001508 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001509 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001510 {
1511 // We use a block to end the scratch scope before the write barrier, thus
1512 // freeing the temporary registers so they can be used in `MarkGCCard`.
1513 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001514
Alexandre Rames97833a02015-04-16 15:07:12 +01001515 if (index.IsConstant()) {
1516 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1517 destination = HeapOperand(obj, offset);
1518 } else {
1519 Register temp = temps.AcquireSameSizeAs(obj);
1520 Register index_reg = InputRegisterAt(instruction, 1);
1521 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1522 destination = HeapOperand(temp, offset);
1523 }
1524
1525 codegen_->Store(value_type, value, destination);
1526 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001527 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001528 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001529 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001530 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001531 }
1532}
1533
Alexandre Rames67555f72014-11-18 10:55:16 +00001534void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1535 LocationSummary* locations =
1536 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1537 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001538 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001539 if (instruction->HasUses()) {
1540 locations->SetOut(Location::SameAsFirstInput());
1541 }
1542}
1543
1544void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001545 LocationSummary* locations = instruction->GetLocations();
1546 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1547 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001548 codegen_->AddSlowPath(slow_path);
1549
1550 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1551 __ B(slow_path->GetEntryLabel(), hs);
1552}
1553
1554void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1555 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1556 instruction, LocationSummary::kCallOnSlowPath);
1557 locations->SetInAt(0, Location::RequiresRegister());
1558 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001559 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001560}
1561
1562void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001563 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001564 Register obj = InputRegisterAt(instruction, 0);;
1565 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001566 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001567
Alexandre Rames3e69f162014-12-10 10:36:50 +00001568 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1569 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001570 codegen_->AddSlowPath(slow_path);
1571
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001572 // Avoid null check if we know obj is not null.
1573 if (instruction->MustDoNullCheck()) {
1574 __ Cbz(obj, slow_path->GetExitLabel());
1575 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001576 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001577 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1578 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001579 __ B(ne, slow_path->GetEntryLabel());
1580 __ Bind(slow_path->GetExitLabel());
1581}
1582
1583void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1584 LocationSummary* locations =
1585 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1586 locations->SetInAt(0, Location::RequiresRegister());
1587 if (check->HasUses()) {
1588 locations->SetOut(Location::SameAsFirstInput());
1589 }
1590}
1591
1592void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1593 // We assume the class is not null.
1594 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1595 check->GetLoadClass(), check, check->GetDexPc(), true);
1596 codegen_->AddSlowPath(slow_path);
1597 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1598}
1599
Serban Constantinescu02164b32014-11-13 14:05:07 +00001600void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001601 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001602 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1603 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001604 switch (in_type) {
1605 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001606 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001607 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001608 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1609 break;
1610 }
1611 case Primitive::kPrimFloat:
1612 case Primitive::kPrimDouble: {
1613 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001614 HInstruction* right = compare->InputAt(1);
1615 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1616 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1617 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1618 } else {
1619 locations->SetInAt(1, Location::RequiresFpuRegister());
1620 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001621 locations->SetOut(Location::RequiresRegister());
1622 break;
1623 }
1624 default:
1625 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1626 }
1627}
1628
1629void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1630 Primitive::Type in_type = compare->InputAt(0)->GetType();
1631
1632 // 0 if: left == right
1633 // 1 if: left > right
1634 // -1 if: left < right
1635 switch (in_type) {
1636 case Primitive::kPrimLong: {
1637 Register result = OutputRegister(compare);
1638 Register left = InputRegisterAt(compare, 0);
1639 Operand right = InputOperandAt(compare, 1);
1640
1641 __ Cmp(left, right);
1642 __ Cset(result, ne);
1643 __ Cneg(result, result, lt);
1644 break;
1645 }
1646 case Primitive::kPrimFloat:
1647 case Primitive::kPrimDouble: {
1648 Register result = OutputRegister(compare);
1649 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001650 if (compare->GetLocations()->InAt(1).IsConstant()) {
1651 if (kIsDebugBuild) {
1652 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1653 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1654 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1655 }
1656 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1657 __ Fcmp(left, 0.0);
1658 } else {
1659 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1660 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001661 if (compare->IsGtBias()) {
1662 __ Cset(result, ne);
1663 } else {
1664 __ Csetm(result, ne);
1665 }
1666 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001667 break;
1668 }
1669 default:
1670 LOG(FATAL) << "Unimplemented compare type " << in_type;
1671 }
1672}
1673
1674void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1675 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1676 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001677 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001678 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001680 }
1681}
1682
1683void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1684 if (!instruction->NeedsMaterialization()) {
1685 return;
1686 }
1687
1688 LocationSummary* locations = instruction->GetLocations();
1689 Register lhs = InputRegisterAt(instruction, 0);
1690 Operand rhs = InputOperandAt(instruction, 1);
1691 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1692 Condition cond = ARM64Condition(instruction->GetCondition());
1693
1694 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001695 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001696}
1697
1698#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1699 M(Equal) \
1700 M(NotEqual) \
1701 M(LessThan) \
1702 M(LessThanOrEqual) \
1703 M(GreaterThan) \
1704 M(GreaterThanOrEqual)
1705#define DEFINE_CONDITION_VISITORS(Name) \
1706void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1707void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1708FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001709#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001710#undef FOR_EACH_CONDITION_INSTRUCTION
1711
Zheng Xuc6667102015-05-15 16:08:45 +08001712void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1713 DCHECK(instruction->IsDiv() || instruction->IsRem());
1714
1715 LocationSummary* locations = instruction->GetLocations();
1716 Location second = locations->InAt(1);
1717 DCHECK(second.IsConstant());
1718
1719 Register out = OutputRegister(instruction);
1720 Register dividend = InputRegisterAt(instruction, 0);
1721 int64_t imm = Int64FromConstant(second.GetConstant());
1722 DCHECK(imm == 1 || imm == -1);
1723
1724 if (instruction->IsRem()) {
1725 __ Mov(out, 0);
1726 } else {
1727 if (imm == 1) {
1728 __ Mov(out, dividend);
1729 } else {
1730 __ Neg(out, dividend);
1731 }
1732 }
1733}
1734
1735void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1736 DCHECK(instruction->IsDiv() || instruction->IsRem());
1737
1738 LocationSummary* locations = instruction->GetLocations();
1739 Location second = locations->InAt(1);
1740 DCHECK(second.IsConstant());
1741
1742 Register out = OutputRegister(instruction);
1743 Register dividend = InputRegisterAt(instruction, 0);
1744 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001745 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001746 DCHECK(IsPowerOfTwo(abs_imm));
1747 int ctz_imm = CTZ(abs_imm);
1748
1749 UseScratchRegisterScope temps(GetVIXLAssembler());
1750 Register temp = temps.AcquireSameSizeAs(out);
1751
1752 if (instruction->IsDiv()) {
1753 __ Add(temp, dividend, abs_imm - 1);
1754 __ Cmp(dividend, 0);
1755 __ Csel(out, temp, dividend, lt);
1756 if (imm > 0) {
1757 __ Asr(out, out, ctz_imm);
1758 } else {
1759 __ Neg(out, Operand(out, ASR, ctz_imm));
1760 }
1761 } else {
1762 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1763 __ Asr(temp, dividend, bits - 1);
1764 __ Lsr(temp, temp, bits - ctz_imm);
1765 __ Add(out, dividend, temp);
1766 __ And(out, out, abs_imm - 1);
1767 __ Sub(out, out, temp);
1768 }
1769}
1770
1771void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1772 DCHECK(instruction->IsDiv() || instruction->IsRem());
1773
1774 LocationSummary* locations = instruction->GetLocations();
1775 Location second = locations->InAt(1);
1776 DCHECK(second.IsConstant());
1777
1778 Register out = OutputRegister(instruction);
1779 Register dividend = InputRegisterAt(instruction, 0);
1780 int64_t imm = Int64FromConstant(second.GetConstant());
1781
1782 Primitive::Type type = instruction->GetResultType();
1783 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1784
1785 int64_t magic;
1786 int shift;
1787 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1788
1789 UseScratchRegisterScope temps(GetVIXLAssembler());
1790 Register temp = temps.AcquireSameSizeAs(out);
1791
1792 // temp = get_high(dividend * magic)
1793 __ Mov(temp, magic);
1794 if (type == Primitive::kPrimLong) {
1795 __ Smulh(temp, dividend, temp);
1796 } else {
1797 __ Smull(temp.X(), dividend, temp);
1798 __ Lsr(temp.X(), temp.X(), 32);
1799 }
1800
1801 if (imm > 0 && magic < 0) {
1802 __ Add(temp, temp, dividend);
1803 } else if (imm < 0 && magic > 0) {
1804 __ Sub(temp, temp, dividend);
1805 }
1806
1807 if (shift != 0) {
1808 __ Asr(temp, temp, shift);
1809 }
1810
1811 if (instruction->IsDiv()) {
1812 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1813 } else {
1814 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1815 // TODO: Strength reduction for msub.
1816 Register temp_imm = temps.AcquireSameSizeAs(out);
1817 __ Mov(temp_imm, imm);
1818 __ Msub(out, temp, temp_imm, dividend);
1819 }
1820}
1821
1822void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1823 DCHECK(instruction->IsDiv() || instruction->IsRem());
1824 Primitive::Type type = instruction->GetResultType();
1825 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1826
1827 LocationSummary* locations = instruction->GetLocations();
1828 Register out = OutputRegister(instruction);
1829 Location second = locations->InAt(1);
1830
1831 if (second.IsConstant()) {
1832 int64_t imm = Int64FromConstant(second.GetConstant());
1833
1834 if (imm == 0) {
1835 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1836 } else if (imm == 1 || imm == -1) {
1837 DivRemOneOrMinusOne(instruction);
1838 } else if (IsPowerOfTwo(std::abs(imm))) {
1839 DivRemByPowerOfTwo(instruction);
1840 } else {
1841 DCHECK(imm <= -2 || imm >= 2);
1842 GenerateDivRemWithAnyConstant(instruction);
1843 }
1844 } else {
1845 Register dividend = InputRegisterAt(instruction, 0);
1846 Register divisor = InputRegisterAt(instruction, 1);
1847 if (instruction->IsDiv()) {
1848 __ Sdiv(out, dividend, divisor);
1849 } else {
1850 UseScratchRegisterScope temps(GetVIXLAssembler());
1851 Register temp = temps.AcquireSameSizeAs(out);
1852 __ Sdiv(temp, dividend, divisor);
1853 __ Msub(out, temp, divisor, dividend);
1854 }
1855 }
1856}
1857
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001858void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1861 switch (div->GetResultType()) {
1862 case Primitive::kPrimInt:
1863 case Primitive::kPrimLong:
1864 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001865 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001866 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1867 break;
1868
1869 case Primitive::kPrimFloat:
1870 case Primitive::kPrimDouble:
1871 locations->SetInAt(0, Location::RequiresFpuRegister());
1872 locations->SetInAt(1, Location::RequiresFpuRegister());
1873 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1874 break;
1875
1876 default:
1877 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1878 }
1879}
1880
1881void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1882 Primitive::Type type = div->GetResultType();
1883 switch (type) {
1884 case Primitive::kPrimInt:
1885 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001886 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001887 break;
1888
1889 case Primitive::kPrimFloat:
1890 case Primitive::kPrimDouble:
1891 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unexpected div type " << type;
1896 }
1897}
1898
Alexandre Rames67555f72014-11-18 10:55:16 +00001899void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1902 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1903 if (instruction->HasUses()) {
1904 locations->SetOut(Location::SameAsFirstInput());
1905 }
1906}
1907
1908void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1909 SlowPathCodeARM64* slow_path =
1910 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1911 codegen_->AddSlowPath(slow_path);
1912 Location value = instruction->GetLocations()->InAt(0);
1913
Alexandre Rames3e69f162014-12-10 10:36:50 +00001914 Primitive::Type type = instruction->GetType();
1915
1916 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1917 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1918 return;
1919 }
1920
Alexandre Rames67555f72014-11-18 10:55:16 +00001921 if (value.IsConstant()) {
1922 int64_t divisor = Int64ConstantFrom(value);
1923 if (divisor == 0) {
1924 __ B(slow_path->GetEntryLabel());
1925 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001926 // A division by a non-null constant is valid. We don't need to perform
1927 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001928 }
1929 } else {
1930 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1931 }
1932}
1933
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001934void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1935 LocationSummary* locations =
1936 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1937 locations->SetOut(Location::ConstantLocation(constant));
1938}
1939
1940void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1941 UNUSED(constant);
1942 // Will be generated at use site.
1943}
1944
Alexandre Rames5319def2014-10-23 10:03:10 +01001945void LocationsBuilderARM64::VisitExit(HExit* exit) {
1946 exit->SetLocations(nullptr);
1947}
1948
1949void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001950 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001951}
1952
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001953void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1956 locations->SetOut(Location::ConstantLocation(constant));
1957}
1958
1959void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1960 UNUSED(constant);
1961 // Will be generated at use site.
1962}
1963
Alexandre Rames5319def2014-10-23 10:03:10 +01001964void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1965 got->SetLocations(nullptr);
1966}
1967
1968void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1969 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001970 DCHECK(!successor->IsExitBlock());
1971 HBasicBlock* block = got->GetBlock();
1972 HInstruction* previous = got->GetPrevious();
1973 HLoopInformation* info = block->GetLoopInformation();
1974
David Brazdil46e2a392015-03-16 17:31:52 +00001975 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001976 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1977 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1978 return;
1979 }
1980 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1981 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1982 }
1983 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001984 __ B(codegen_->GetLabelOf(successor));
1985 }
1986}
1987
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001988void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1989 vixl::Label* true_target,
1990 vixl::Label* false_target,
1991 vixl::Label* always_true_target) {
1992 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001993 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001994
Serban Constantinescu02164b32014-11-13 14:05:07 +00001995 if (cond->IsIntConstant()) {
1996 int32_t cond_value = cond->AsIntConstant()->GetValue();
1997 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001998 if (always_true_target != nullptr) {
1999 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002000 }
2001 return;
2002 } else {
2003 DCHECK_EQ(cond_value, 0);
2004 }
2005 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002006 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002007 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002008 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002009 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002010 } else {
2011 // The condition instruction has not been materialized, use its inputs as
2012 // the comparison and its condition as the branch condition.
2013 Register lhs = InputRegisterAt(condition, 0);
2014 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002015 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00002016 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2017 switch (arm64_cond) {
2018 case eq:
2019 __ Cbz(lhs, true_target);
2020 break;
2021 case ne:
2022 __ Cbnz(lhs, true_target);
2023 break;
2024 case lt:
2025 // Test the sign bit and branch accordingly.
2026 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2027 break;
2028 case ge:
2029 // Test the sign bit and branch accordingly.
2030 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2031 break;
2032 default:
2033 // Without the `static_cast` the compiler throws an error for
2034 // `-Werror=sign-promo`.
2035 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002036 }
2037 } else {
2038 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002039 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002040 }
2041 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002042 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002043 __ B(false_target);
2044 }
2045}
2046
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002047void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2049 HInstruction* cond = if_instr->InputAt(0);
2050 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2051 locations->SetInAt(0, Location::RequiresRegister());
2052 }
2053}
2054
2055void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2056 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2057 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2058 vixl::Label* always_true_target = true_target;
2059 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2060 if_instr->IfTrueSuccessor())) {
2061 always_true_target = nullptr;
2062 }
2063 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2064 if_instr->IfFalseSuccessor())) {
2065 false_target = nullptr;
2066 }
2067 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2068}
2069
2070void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2071 LocationSummary* locations = new (GetGraph()->GetArena())
2072 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2073 HInstruction* cond = deoptimize->InputAt(0);
2074 DCHECK(cond->IsCondition());
2075 if (cond->AsCondition()->NeedsMaterialization()) {
2076 locations->SetInAt(0, Location::RequiresRegister());
2077 }
2078}
2079
2080void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2081 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2082 DeoptimizationSlowPathARM64(deoptimize);
2083 codegen_->AddSlowPath(slow_path);
2084 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2085 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2086}
2087
Alexandre Rames5319def2014-10-23 10:03:10 +01002088void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002089 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002090}
2091
2092void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002093 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002094}
2095
2096void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002097 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002098}
2099
2100void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002101 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002102}
2103
Alexandre Rames67555f72014-11-18 10:55:16 +00002104void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2105 LocationSummary::CallKind call_kind =
2106 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2108 locations->SetInAt(0, Location::RequiresRegister());
2109 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002110 // The output does overlap inputs.
2111 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002112}
2113
2114void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2115 LocationSummary* locations = instruction->GetLocations();
2116 Register obj = InputRegisterAt(instruction, 0);;
2117 Register cls = InputRegisterAt(instruction, 1);;
2118 Register out = OutputRegister(instruction);
2119
2120 vixl::Label done;
2121
2122 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002123 // Avoid null check if we know `obj` is not null.
2124 if (instruction->MustDoNullCheck()) {
2125 __ Mov(out, 0);
2126 __ Cbz(obj, &done);
2127 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002128
2129 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002130 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002131 __ Cmp(out, cls);
2132 if (instruction->IsClassFinal()) {
2133 // Classes must be equal for the instanceof to succeed.
2134 __ Cset(out, eq);
2135 } else {
2136 // If the classes are not equal, we go into a slow path.
2137 DCHECK(locations->OnlyCallsOnSlowPath());
2138 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002139 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2140 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002141 codegen_->AddSlowPath(slow_path);
2142 __ B(ne, slow_path->GetEntryLabel());
2143 __ Mov(out, 1);
2144 __ Bind(slow_path->GetExitLabel());
2145 }
2146
2147 __ Bind(&done);
2148}
2149
Alexandre Rames5319def2014-10-23 10:03:10 +01002150void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2152 locations->SetOut(Location::ConstantLocation(constant));
2153}
2154
2155void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2156 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002157 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002158}
2159
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002160void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2161 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2162 locations->SetOut(Location::ConstantLocation(constant));
2163}
2164
2165void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2166 // Will be generated at use site.
2167 UNUSED(constant);
2168}
2169
Alexandre Rames5319def2014-10-23 10:03:10 +01002170void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002171 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002172 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002173}
2174
Alexandre Rames67555f72014-11-18 10:55:16 +00002175void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2176 HandleInvoke(invoke);
2177}
2178
2179void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2180 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002181 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2182 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2183 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002184 Location receiver = invoke->GetLocations()->InAt(0);
2185 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002186 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002187
2188 // The register ip1 is required to be used for the hidden argument in
2189 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002190 MacroAssembler* masm = GetVIXLAssembler();
2191 UseScratchRegisterScope scratch_scope(masm);
2192 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002193 scratch_scope.Exclude(ip1);
2194 __ Mov(ip1, invoke->GetDexMethodIndex());
2195
2196 // temp = object->GetClass();
2197 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002198 __ Ldr(temp.W(), StackOperandFrom(receiver));
2199 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002200 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002201 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002202 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002203 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00002204 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002205 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002206 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002207 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002208 // lr();
2209 __ Blr(lr);
2210 DCHECK(!codegen_->IsLeafMethod());
2211 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2212}
2213
2214void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002215 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2216 if (intrinsic.TryDispatch(invoke)) {
2217 return;
2218 }
2219
Alexandre Rames67555f72014-11-18 10:55:16 +00002220 HandleInvoke(invoke);
2221}
2222
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002223void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002224 // When we do not run baseline, explicit clinit checks triggered by static
2225 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2226 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002227
Andreas Gampe878d58c2015-01-15 23:24:00 -08002228 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2229 if (intrinsic.TryDispatch(invoke)) {
2230 return;
2231 }
2232
Alexandre Rames67555f72014-11-18 10:55:16 +00002233 HandleInvoke(invoke);
2234}
2235
Andreas Gampe878d58c2015-01-15 23:24:00 -08002236static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2237 if (invoke->GetLocations()->Intrinsified()) {
2238 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2239 intrinsic.Dispatch(invoke);
2240 return true;
2241 }
2242 return false;
2243}
2244
2245void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
2246 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
2247 DCHECK(temp.Is(kArtMethodRegister));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002248 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002249
2250 // TODO: Implement all kinds of calls:
2251 // 1) boot -> boot
2252 // 2) app -> boot
2253 // 3) app -> app
2254 //
2255 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2256
Jeff Hao848f70a2014-01-15 13:49:50 -08002257 if (invoke->IsStringInit()) {
2258 // temp = thread->string_init_entrypoint
Mathieu Chartiere401d142015-04-22 13:56:20 -07002259 __ Ldr(temp.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002260 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002261 __ Ldr(lr, MemOperand(
2262 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002263 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002264 __ Blr(lr);
2265 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08002266 // temp = method;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002267 LoadCurrentMethod(temp.X());
Jeff Hao848f70a2014-01-15 13:49:50 -08002268 if (!invoke->IsRecursive()) {
2269 // temp = temp->dex_cache_resolved_methods_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002270 __ Ldr(temp.W(), MemOperand(temp.X(),
2271 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002272 // temp = temp[index_in_cache];
Mathieu Chartiere401d142015-04-22 13:56:20 -07002273 __ Ldr(temp.X(), MemOperand(temp, index_in_cache));
Jeff Hao848f70a2014-01-15 13:49:50 -08002274 // lr = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002275 __ Ldr(lr, MemOperand(temp.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2276 kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002277 // lr();
2278 __ Blr(lr);
2279 } else {
2280 __ Bl(&frame_entry_label_);
2281 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002282 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002283
Andreas Gampe878d58c2015-01-15 23:24:00 -08002284 DCHECK(!IsLeafMethod());
2285}
2286
2287void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002288 // When we do not run baseline, explicit clinit checks triggered by static
2289 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2290 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002291
Andreas Gampe878d58c2015-01-15 23:24:00 -08002292 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2293 return;
2294 }
2295
Alexandre Ramesd921d642015-04-16 15:07:16 +01002296 BlockPoolsScope block_pools(GetVIXLAssembler());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002297 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002298 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002300}
2301
2302void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002303 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2304 return;
2305 }
2306
Alexandre Rames5319def2014-10-23 10:03:10 +01002307 LocationSummary* locations = invoke->GetLocations();
2308 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002309 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2310 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2311 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002312 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002313 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002314
Alexandre Ramesd921d642015-04-16 15:07:16 +01002315 BlockPoolsScope block_pools(GetVIXLAssembler());
2316
Alexandre Rames5319def2014-10-23 10:03:10 +01002317 // temp = object->GetClass();
2318 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002319 __ Ldr(temp.W(), MemOperand(sp, receiver.GetStackIndex()));
2320 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002321 } else {
2322 DCHECK(receiver.IsRegister());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002323 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002324 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002325 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002326 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002327 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002328 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002329 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002330 // lr();
2331 __ Blr(lr);
2332 DCHECK(!codegen_->IsLeafMethod());
2333 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2334}
2335
Alexandre Rames67555f72014-11-18 10:55:16 +00002336void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2337 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2338 : LocationSummary::kNoCall;
2339 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002340 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002341 locations->SetOut(Location::RequiresRegister());
2342}
2343
2344void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2345 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002346 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002347 if (cls->IsReferrersClass()) {
2348 DCHECK(!cls->CanCallRuntime());
2349 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002350 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002351 } else {
2352 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002353 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002354 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002355
2356 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2357 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2358 codegen_->AddSlowPath(slow_path);
2359 __ Cbz(out, slow_path->GetEntryLabel());
2360 if (cls->MustGenerateClinitCheck()) {
2361 GenerateClassInitializationCheck(slow_path, out);
2362 } else {
2363 __ Bind(slow_path->GetExitLabel());
2364 }
2365 }
2366}
2367
2368void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2369 LocationSummary* locations =
2370 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2371 locations->SetOut(Location::RequiresRegister());
2372}
2373
2374void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2375 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2376 __ Ldr(OutputRegister(instruction), exception);
2377 __ Str(wzr, exception);
2378}
2379
Alexandre Rames5319def2014-10-23 10:03:10 +01002380void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2381 load->SetLocations(nullptr);
2382}
2383
2384void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2385 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002386 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002387}
2388
Alexandre Rames67555f72014-11-18 10:55:16 +00002389void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2390 LocationSummary* locations =
2391 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002392 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002393 locations->SetOut(Location::RequiresRegister());
2394}
2395
2396void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2397 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2398 codegen_->AddSlowPath(slow_path);
2399
2400 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002401 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002402 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002403 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002404 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002405 __ Cbz(out, slow_path->GetEntryLabel());
2406 __ Bind(slow_path->GetExitLabel());
2407}
2408
Alexandre Rames5319def2014-10-23 10:03:10 +01002409void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2410 local->SetLocations(nullptr);
2411}
2412
2413void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2414 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2415}
2416
2417void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2418 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2419 locations->SetOut(Location::ConstantLocation(constant));
2420}
2421
2422void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2423 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002424 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002425}
2426
Alexandre Rames67555f72014-11-18 10:55:16 +00002427void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2428 LocationSummary* locations =
2429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2430 InvokeRuntimeCallingConvention calling_convention;
2431 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2432}
2433
2434void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2435 codegen_->InvokeRuntime(instruction->IsEnter()
2436 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2437 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002438 instruction->GetDexPc(),
2439 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002440 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002441}
2442
Alexandre Rames42d641b2014-10-27 14:00:51 +00002443void LocationsBuilderARM64::VisitMul(HMul* mul) {
2444 LocationSummary* locations =
2445 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2446 switch (mul->GetResultType()) {
2447 case Primitive::kPrimInt:
2448 case Primitive::kPrimLong:
2449 locations->SetInAt(0, Location::RequiresRegister());
2450 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002451 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002452 break;
2453
2454 case Primitive::kPrimFloat:
2455 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002456 locations->SetInAt(0, Location::RequiresFpuRegister());
2457 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002458 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002459 break;
2460
2461 default:
2462 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2463 }
2464}
2465
2466void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2467 switch (mul->GetResultType()) {
2468 case Primitive::kPrimInt:
2469 case Primitive::kPrimLong:
2470 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2471 break;
2472
2473 case Primitive::kPrimFloat:
2474 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002475 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002476 break;
2477
2478 default:
2479 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2480 }
2481}
2482
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002483void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2484 LocationSummary* locations =
2485 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2486 switch (neg->GetResultType()) {
2487 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002488 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002489 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002491 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002492
2493 case Primitive::kPrimFloat:
2494 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002495 locations->SetInAt(0, Location::RequiresFpuRegister());
2496 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2501 }
2502}
2503
2504void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2505 switch (neg->GetResultType()) {
2506 case Primitive::kPrimInt:
2507 case Primitive::kPrimLong:
2508 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2509 break;
2510
2511 case Primitive::kPrimFloat:
2512 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002513 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002514 break;
2515
2516 default:
2517 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2518 }
2519}
2520
2521void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2522 LocationSummary* locations =
2523 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2524 InvokeRuntimeCallingConvention calling_convention;
2525 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002526 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002527 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002528 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2529 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002530 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002531}
2532
2533void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2534 LocationSummary* locations = instruction->GetLocations();
2535 InvokeRuntimeCallingConvention calling_convention;
2536 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2537 DCHECK(type_index.Is(w0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002538 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimLong);
2539 DCHECK(current_method.Is(x2));
2540 codegen_->LoadCurrentMethod(current_method.X());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002541 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002542 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002543 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2544 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002545 instruction->GetDexPc(),
2546 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002547 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002548}
2549
Alexandre Rames5319def2014-10-23 10:03:10 +01002550void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2551 LocationSummary* locations =
2552 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2553 InvokeRuntimeCallingConvention calling_convention;
2554 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2555 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2556 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002557 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002558}
2559
2560void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2561 LocationSummary* locations = instruction->GetLocations();
2562 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2563 DCHECK(type_index.Is(w0));
2564 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2565 DCHECK(current_method.Is(w1));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002566 codegen_->LoadCurrentMethod(current_method.X());
Alexandre Rames5319def2014-10-23 10:03:10 +01002567 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002568 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002569 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2570 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002571 instruction->GetDexPc(),
2572 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002573 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002574}
2575
2576void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002578 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002580}
2581
2582void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002583 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002584 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002585 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002586 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002587 break;
2588
2589 default:
2590 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2591 }
2592}
2593
David Brazdil66d126e2015-04-03 16:02:44 +01002594void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2596 locations->SetInAt(0, Location::RequiresRegister());
2597 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2598}
2599
2600void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002601 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2602}
2603
Alexandre Rames5319def2014-10-23 10:03:10 +01002604void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2605 LocationSummary* locations =
2606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2607 locations->SetInAt(0, Location::RequiresRegister());
2608 if (instruction->HasUses()) {
2609 locations->SetOut(Location::SameAsFirstInput());
2610 }
2611}
2612
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002613void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002614 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2615 return;
2616 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002617
Alexandre Ramesd921d642015-04-16 15:07:16 +01002618 BlockPoolsScope block_pools(GetVIXLAssembler());
2619 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002620 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2621 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2622}
2623
2624void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002625 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2626 codegen_->AddSlowPath(slow_path);
2627
2628 LocationSummary* locations = instruction->GetLocations();
2629 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002630
2631 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002632}
2633
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002634void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2635 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2636 GenerateImplicitNullCheck(instruction);
2637 } else {
2638 GenerateExplicitNullCheck(instruction);
2639 }
2640}
2641
Alexandre Rames67555f72014-11-18 10:55:16 +00002642void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2643 HandleBinaryOp(instruction);
2644}
2645
2646void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2647 HandleBinaryOp(instruction);
2648}
2649
Alexandre Rames3e69f162014-12-10 10:36:50 +00002650void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2651 LOG(FATAL) << "Unreachable";
2652}
2653
2654void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2655 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2656}
2657
Alexandre Rames5319def2014-10-23 10:03:10 +01002658void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2659 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2660 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2661 if (location.IsStackSlot()) {
2662 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2663 } else if (location.IsDoubleStackSlot()) {
2664 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2665 }
2666 locations->SetOut(location);
2667}
2668
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002669void InstructionCodeGeneratorARM64::VisitParameterValue(
2670 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002671 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002672}
2673
2674void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2675 LocationSummary* locations =
2676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2677 locations->SetOut(LocationFrom(x0));
2678}
2679
2680void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2681 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2682 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002683}
2684
2685void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2687 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2688 locations->SetInAt(i, Location::Any());
2689 }
2690 locations->SetOut(Location::Any());
2691}
2692
2693void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002694 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002695 LOG(FATAL) << "Unreachable";
2696}
2697
Serban Constantinescu02164b32014-11-13 14:05:07 +00002698void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002699 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002700 LocationSummary::CallKind call_kind =
2701 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002702 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2703
2704 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002705 case Primitive::kPrimInt:
2706 case Primitive::kPrimLong:
2707 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002708 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2710 break;
2711
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002712 case Primitive::kPrimFloat:
2713 case Primitive::kPrimDouble: {
2714 InvokeRuntimeCallingConvention calling_convention;
2715 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2716 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2717 locations->SetOut(calling_convention.GetReturnLocation(type));
2718
2719 break;
2720 }
2721
Serban Constantinescu02164b32014-11-13 14:05:07 +00002722 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002723 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002724 }
2725}
2726
2727void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2728 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002729
Serban Constantinescu02164b32014-11-13 14:05:07 +00002730 switch (type) {
2731 case Primitive::kPrimInt:
2732 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002733 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002734 break;
2735 }
2736
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002737 case Primitive::kPrimFloat:
2738 case Primitive::kPrimDouble: {
2739 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2740 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002741 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002742 break;
2743 }
2744
Serban Constantinescu02164b32014-11-13 14:05:07 +00002745 default:
2746 LOG(FATAL) << "Unexpected rem type " << type;
2747 }
2748}
2749
Calin Juravle27df7582015-04-17 19:12:31 +01002750void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2751 memory_barrier->SetLocations(nullptr);
2752}
2753
2754void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2755 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2756}
2757
Alexandre Rames5319def2014-10-23 10:03:10 +01002758void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2759 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2760 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002761 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002762}
2763
2764void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002765 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002766 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002767}
2768
2769void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2770 instruction->SetLocations(nullptr);
2771}
2772
2773void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002774 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002775 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002776}
2777
Serban Constantinescu02164b32014-11-13 14:05:07 +00002778void LocationsBuilderARM64::VisitShl(HShl* shl) {
2779 HandleShift(shl);
2780}
2781
2782void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2783 HandleShift(shl);
2784}
2785
2786void LocationsBuilderARM64::VisitShr(HShr* shr) {
2787 HandleShift(shr);
2788}
2789
2790void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2791 HandleShift(shr);
2792}
2793
Alexandre Rames5319def2014-10-23 10:03:10 +01002794void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2795 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2796 Primitive::Type field_type = store->InputAt(1)->GetType();
2797 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002798 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002799 case Primitive::kPrimBoolean:
2800 case Primitive::kPrimByte:
2801 case Primitive::kPrimChar:
2802 case Primitive::kPrimShort:
2803 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002804 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002805 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2806 break;
2807
2808 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002809 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002810 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2811 break;
2812
2813 default:
2814 LOG(FATAL) << "Unimplemented local type " << field_type;
2815 }
2816}
2817
2818void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002819 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002820}
2821
2822void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002823 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002824}
2825
2826void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002827 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002828}
2829
Alexandre Rames67555f72014-11-18 10:55:16 +00002830void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002831 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002832}
2833
2834void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002835 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002836}
2837
2838void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002839 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002840}
2841
Alexandre Rames67555f72014-11-18 10:55:16 +00002842void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002843 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002844}
2845
2846void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2848}
2849
2850void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002851 HBasicBlock* block = instruction->GetBlock();
2852 if (block->GetLoopInformation() != nullptr) {
2853 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2854 // The back edge will generate the suspend check.
2855 return;
2856 }
2857 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2858 // The goto will generate the suspend check.
2859 return;
2860 }
2861 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002862}
2863
2864void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2865 temp->SetLocations(nullptr);
2866}
2867
2868void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2869 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002870 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002871}
2872
Alexandre Rames67555f72014-11-18 10:55:16 +00002873void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2874 LocationSummary* locations =
2875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2876 InvokeRuntimeCallingConvention calling_convention;
2877 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2878}
2879
2880void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2881 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002882 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002883 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002884}
2885
2886void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2887 LocationSummary* locations =
2888 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2889 Primitive::Type input_type = conversion->GetInputType();
2890 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002891 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002892 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2893 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2894 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2895 }
2896
Alexandre Rames542361f2015-01-29 16:57:31 +00002897 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002898 locations->SetInAt(0, Location::RequiresFpuRegister());
2899 } else {
2900 locations->SetInAt(0, Location::RequiresRegister());
2901 }
2902
Alexandre Rames542361f2015-01-29 16:57:31 +00002903 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002904 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2905 } else {
2906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2907 }
2908}
2909
2910void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2911 Primitive::Type result_type = conversion->GetResultType();
2912 Primitive::Type input_type = conversion->GetInputType();
2913
2914 DCHECK_NE(input_type, result_type);
2915
Alexandre Rames542361f2015-01-29 16:57:31 +00002916 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002917 int result_size = Primitive::ComponentSize(result_type);
2918 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002919 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002920 Register output = OutputRegister(conversion);
2921 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002922 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2923 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2924 } else if ((result_type == Primitive::kPrimChar) ||
2925 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2926 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002927 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002928 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002929 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002930 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002931 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002932 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002933 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2934 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002935 } else if (Primitive::IsFloatingPointType(result_type) &&
2936 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002937 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2938 } else {
2939 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2940 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002941 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002942}
Alexandre Rames67555f72014-11-18 10:55:16 +00002943
Serban Constantinescu02164b32014-11-13 14:05:07 +00002944void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2945 HandleShift(ushr);
2946}
2947
2948void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2949 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002950}
2951
2952void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2953 HandleBinaryOp(instruction);
2954}
2955
2956void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2957 HandleBinaryOp(instruction);
2958}
2959
Calin Juravleb1498f62015-02-16 13:13:29 +00002960void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2961 // Nothing to do, this should be removed during prepare for register allocator.
2962 UNUSED(instruction);
2963 LOG(FATAL) << "Unreachable";
2964}
2965
2966void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2967 // Nothing to do, this should be removed during prepare for register allocator.
2968 UNUSED(instruction);
2969 LOG(FATAL) << "Unreachable";
2970}
2971
Alexandre Rames67555f72014-11-18 10:55:16 +00002972#undef __
2973#undef QUICK_ENTRY_POINT
2974
Alexandre Rames5319def2014-10-23 10:03:10 +01002975} // namespace arm64
2976} // namespace art