blob: 8ee150b9999abe38cd9f30e54f856b27b89bd7cd [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"
Andreas Gampe878d58c2015-01-15 23:24:00 -080020#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010021#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080022#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080024#include "intrinsics.h"
25#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "mirror/array-inl.h"
27#include "mirror/art_method.h"
28#include "mirror/class.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000029#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "thread.h"
31#include "utils/arm64/assembler_arm64.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35
36using namespace vixl; // NOLINT(build/namespaces)
37
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
44namespace arm64 {
45
Andreas Gampe878d58c2015-01-15 23:24:00 -080046using helpers::CPURegisterFrom;
47using helpers::DRegisterFrom;
48using helpers::FPRegisterFrom;
49using helpers::HeapOperand;
50using helpers::HeapOperandFrom;
51using helpers::InputCPURegisterAt;
52using helpers::InputFPRegisterAt;
53using helpers::InputRegisterAt;
54using helpers::InputOperandAt;
55using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::LocationFrom;
57using helpers::OperandFromMemOperand;
58using helpers::OutputCPURegister;
59using helpers::OutputFPRegister;
60using helpers::OutputRegister;
61using helpers::RegisterFrom;
62using helpers::StackOperandFrom;
63using helpers::VIXLRegCodeFromART;
64using helpers::WRegisterFrom;
65using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000066using helpers::ARM64EncodableConstantOrRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067
Alexandre Rames5319def2014-10-23 10:03:10 +010068static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>);
69static constexpr int kCurrentMethodStackOffset = 0;
70
Alexandre Rames5319def2014-10-23 10:03:10 +010071inline Condition ARM64Condition(IfCondition cond) {
72 switch (cond) {
73 case kCondEQ: return eq;
74 case kCondNE: return ne;
75 case kCondLT: return lt;
76 case kCondLE: return le;
77 case kCondGT: return gt;
78 case kCondGE: return ge;
79 default:
80 LOG(FATAL) << "Unknown if condition";
81 }
82 return nv; // Unreachable.
83}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
86 DCHECK_NE(return_type, Primitive::kPrimVoid);
87 // 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);
96 } else {
97 return LocationFrom(w0);
98 }
99}
100
Alexandre Rames5319def2014-10-23 10:03:10 +0100101static const Register kRuntimeParameterCoreRegisters[] = { x0, x1, x2, x3, x4, x5, x6, x7 };
102static constexpr size_t kRuntimeParameterCoreRegistersLength =
103 arraysize(kRuntimeParameterCoreRegisters);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000104static const FPRegister kRuntimeParameterFpuRegisters[] = { d0, d1, d2, d3, d4, d5, d6, d7 };
105static constexpr size_t kRuntimeParameterFpuRegistersLength =
106 arraysize(kRuntimeParameterCoreRegisters);
Alexandre Rames5319def2014-10-23 10:03:10 +0100107
108class InvokeRuntimeCallingConvention : public CallingConvention<Register, FPRegister> {
109 public:
110 static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
111
112 InvokeRuntimeCallingConvention()
113 : CallingConvention(kRuntimeParameterCoreRegisters,
114 kRuntimeParameterCoreRegistersLength,
115 kRuntimeParameterFpuRegisters,
116 kRuntimeParameterFpuRegistersLength) {}
117
118 Location GetReturnLocation(Primitive::Type return_type);
119
120 private:
121 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
122};
123
124Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000125 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100126}
127
Alexandre Rames67555f72014-11-18 10:55:16 +0000128#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
129#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
132 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000133 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
134 Location index_location,
135 Location length_location)
136 : instruction_(instruction),
137 index_location_(index_location),
138 length_location_(length_location) {}
139
Alexandre Rames5319def2014-10-23 10:03:10 +0100140
Alexandre Rames67555f72014-11-18 10:55:16 +0000141 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000142 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100143 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000144 // We're moving two locations to locations that could overlap, so we need a parallel
145 // move resolver.
146 InvokeRuntimeCallingConvention calling_convention;
147 codegen->EmitParallelMoves(
148 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)),
149 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)));
150 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000151 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800152 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100153 }
154
155 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000156 HBoundsCheck* const instruction_;
157 const Location index_location_;
158 const Location length_location_;
159
Alexandre Rames5319def2014-10-23 10:03:10 +0100160 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
161};
162
Alexandre Rames67555f72014-11-18 10:55:16 +0000163class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
164 public:
165 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
166
167 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
168 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
169 __ Bind(GetEntryLabel());
170 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000171 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800172 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000173 }
174
175 private:
176 HDivZeroCheck* const instruction_;
177 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
178};
179
180class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
181 public:
182 LoadClassSlowPathARM64(HLoadClass* cls,
183 HInstruction* at,
184 uint32_t dex_pc,
185 bool do_clinit)
186 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
187 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
188 }
189
190 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
191 LocationSummary* locations = at_->GetLocations();
192 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
193
194 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000195 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000196
197 InvokeRuntimeCallingConvention calling_convention;
198 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
199 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
200 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
201 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000202 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800203 if (do_clinit_) {
204 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t, mirror::ArtMethod*>();
205 } else {
206 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t, mirror::ArtMethod*>();
207 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000208
209 // Move the class to the desired location.
210 Location out = locations->Out();
211 if (out.IsValid()) {
212 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
213 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000214 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000215 }
216
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000217 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000218 __ B(GetExitLabel());
219 }
220
221 private:
222 // The class this slow path will load.
223 HLoadClass* const cls_;
224
225 // The instruction where this slow path is happening.
226 // (Might be the load class or an initialization check).
227 HInstruction* const at_;
228
229 // The dex PC of `at_`.
230 const uint32_t dex_pc_;
231
232 // Whether to initialize the class.
233 const bool do_clinit_;
234
235 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
236};
237
238class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
239 public:
240 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
241
242 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
243 LocationSummary* locations = instruction_->GetLocations();
244 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
245 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
246
247 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000248 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000249
250 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800251 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
252 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000254 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000257 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000258
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000259 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 __ B(GetExitLabel());
261 }
262
263 private:
264 HLoadString* const instruction_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
267};
268
Alexandre Rames5319def2014-10-23 10:03:10 +0100269class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
270 public:
271 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
272
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100275 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000277 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800278 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 }
280
281 private:
282 HNullCheck* const instruction_;
283
284 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
285};
286
287class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
288 public:
289 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
290 HBasicBlock* successor)
291 : instruction_(instruction), successor_(successor) {}
292
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
294 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100295 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000296 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000298 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000300 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000301 if (successor_ == nullptr) {
302 __ B(GetReturnLabel());
303 } else {
304 __ B(arm64_codegen->GetLabelOf(successor_));
305 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100306 }
307
308 vixl::Label* GetReturnLabel() {
309 DCHECK(successor_ == nullptr);
310 return &return_label_;
311 }
312
Alexandre Rames5319def2014-10-23 10:03:10 +0100313 private:
314 HSuspendCheck* const instruction_;
315 // If not null, the block to branch to after the suspend check.
316 HBasicBlock* const successor_;
317
318 // If `successor_` is null, the label to branch to after the suspend check.
319 vixl::Label return_label_;
320
321 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
322};
323
Alexandre Rames67555f72014-11-18 10:55:16 +0000324class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
325 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000326 TypeCheckSlowPathARM64(HInstruction* instruction,
327 Location class_to_check,
328 Location object_class,
329 uint32_t dex_pc)
330 : instruction_(instruction),
331 class_to_check_(class_to_check),
332 object_class_(object_class),
333 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000334
335 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000336 LocationSummary* locations = instruction_->GetLocations();
337 DCHECK(instruction_->IsCheckCast()
338 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
339 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
340
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000342 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000343
344 // We're moving two locations to locations that could overlap, so we need a parallel
345 // move resolver.
346 InvokeRuntimeCallingConvention calling_convention;
347 codegen->EmitParallelMoves(
348 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)),
349 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)));
350
351 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000352 arm64_codegen->InvokeRuntime(
353 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000354 Primitive::Type ret_type = instruction_->GetType();
355 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
356 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800357 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
358 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000359 } else {
360 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000361 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800362 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000363 }
364
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000365 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000366 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 }
368
369 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000370 HInstruction* const instruction_;
371 const Location class_to_check_;
372 const Location object_class_;
373 uint32_t dex_pc_;
374
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
376};
377
Alexandre Rames5319def2014-10-23 10:03:10 +0100378#undef __
379
380Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
381 Location next_location;
382 if (type == Primitive::kPrimVoid) {
383 LOG(FATAL) << "Unreachable type " << type;
384 }
385
Alexandre Rames542361f2015-01-29 16:57:31 +0000386 if (Primitive::IsFloatingPointType(type) &&
387 (fp_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000388 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(fp_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000389 } else if (!Primitive::IsFloatingPointType(type) &&
390 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000391 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
392 } else {
393 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000394 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
395 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100396 }
397
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000398 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000399 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100400 return next_location;
401}
402
Serban Constantinescu579885a2015-02-22 20:51:33 +0000403CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
404 const Arm64InstructionSetFeatures& isa_features,
405 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100406 : CodeGenerator(graph,
407 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000408 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000409 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000410 callee_saved_core_registers.list(),
411 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000412 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 block_labels_(nullptr),
414 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000416 move_resolver_(graph->GetArena(), this),
417 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000418 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000419 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000420}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422#undef __
423#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100424
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000425void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
426 // Ensure we emit the literal pool.
427 __ FinalizeCode();
428 CodeGenerator::Finalize(allocator);
429}
430
Alexandre Rames3e69f162014-12-10 10:36:50 +0000431void ParallelMoveResolverARM64::EmitMove(size_t index) {
432 MoveOperands* move = moves_.Get(index);
433 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
434}
435
436void ParallelMoveResolverARM64::EmitSwap(size_t index) {
437 MoveOperands* move = moves_.Get(index);
438 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
439}
440
441void ParallelMoveResolverARM64::RestoreScratch(int reg) {
442 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
443}
444
445void ParallelMoveResolverARM64::SpillScratch(int reg) {
446 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
447}
448
Alexandre Rames5319def2014-10-23 10:03:10 +0100449void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000450 __ Bind(&frame_entry_label_);
451
Serban Constantinescu02164b32014-11-13 14:05:07 +0000452 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
453 if (do_overflow_check) {
454 UseScratchRegisterScope temps(GetVIXLAssembler());
455 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000456 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000457 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000458 __ Ldr(wzr, MemOperand(temp, 0));
459 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000460 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100461
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000462 if (!HasEmptyFrame()) {
463 int frame_size = GetFrameSize();
464 // Stack layout:
465 // sp[frame_size - 8] : lr.
466 // ... : other preserved core registers.
467 // ... : other preserved fp registers.
468 // ... : reserved frame space.
469 // sp[0] : current method.
470 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
471 __ PokeCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
472 __ PokeCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
473 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100474}
475
476void CodeGeneratorARM64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000477 if (!HasEmptyFrame()) {
478 int frame_size = GetFrameSize();
479 __ PeekCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
480 __ PeekCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
481 __ Drop(frame_size);
482 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100483}
484
485void CodeGeneratorARM64::Bind(HBasicBlock* block) {
486 __ Bind(GetLabelOf(block));
487}
488
Alexandre Rames5319def2014-10-23 10:03:10 +0100489void CodeGeneratorARM64::Move(HInstruction* instruction,
490 Location location,
491 HInstruction* move_for) {
492 LocationSummary* locations = instruction->GetLocations();
493 if (locations != nullptr && locations->Out().Equals(location)) {
494 return;
495 }
496
497 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000498 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100499
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000500 if (instruction->IsIntConstant()
501 || instruction->IsLongConstant()
502 || instruction->IsNullConstant()) {
503 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 if (location.IsRegister()) {
505 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000506 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100507 (instruction->IsLongConstant() && dst.Is64Bits()));
508 __ Mov(dst, value);
509 } else {
510 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000511 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000512 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
513 ? temps.AcquireW()
514 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 __ Mov(temp, value);
516 __ Str(temp, StackOperandFrom(location));
517 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000518 } else if (instruction->IsTemporary()) {
519 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000520 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100521 } else if (instruction->IsLoadLocal()) {
522 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000523 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000524 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000525 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000526 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100527 }
528
529 } else {
530 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000531 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100532 }
533}
534
Alexandre Rames5319def2014-10-23 10:03:10 +0100535Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
536 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000537
Alexandre Rames5319def2014-10-23 10:03:10 +0100538 switch (type) {
539 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000540 case Primitive::kPrimInt:
541 case Primitive::kPrimFloat:
542 return Location::StackSlot(GetStackSlot(load->GetLocal()));
543
544 case Primitive::kPrimLong:
545 case Primitive::kPrimDouble:
546 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
547
Alexandre Rames5319def2014-10-23 10:03:10 +0100548 case Primitive::kPrimBoolean:
549 case Primitive::kPrimByte:
550 case Primitive::kPrimChar:
551 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100552 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100553 LOG(FATAL) << "Unexpected type " << type;
554 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000555
Alexandre Rames5319def2014-10-23 10:03:10 +0100556 LOG(FATAL) << "Unreachable";
557 return Location::NoLocation();
558}
559
560void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000561 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100562 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000563 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100564 vixl::Label done;
565 __ Cbz(value, &done);
566 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
567 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000568 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100569 __ Bind(&done);
570}
571
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000572void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
573 // Blocked core registers:
574 // lr : Runtime reserved.
575 // tr : Runtime reserved.
576 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
577 // ip1 : VIXL core temp.
578 // ip0 : VIXL core temp.
579 //
580 // Blocked fp registers:
581 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100582 CPURegList reserved_core_registers = vixl_reserved_core_registers;
583 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100584 while (!reserved_core_registers.IsEmpty()) {
585 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
586 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000587
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000588 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800589 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000590 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
591 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000592
593 if (is_baseline) {
594 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
595 while (!reserved_core_baseline_registers.IsEmpty()) {
596 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
597 }
598
599 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
600 while (!reserved_fp_baseline_registers.IsEmpty()) {
601 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
602 }
603 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100604}
605
606Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
607 if (type == Primitive::kPrimVoid) {
608 LOG(FATAL) << "Unreachable type " << type;
609 }
610
Alexandre Rames542361f2015-01-29 16:57:31 +0000611 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000612 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
613 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100614 return Location::FpuRegisterLocation(reg);
615 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000616 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
617 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100618 return Location::RegisterLocation(reg);
619 }
620}
621
Alexandre Rames3e69f162014-12-10 10:36:50 +0000622size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
623 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
624 __ Str(reg, MemOperand(sp, stack_index));
625 return kArm64WordSize;
626}
627
628size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
629 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
630 __ Ldr(reg, MemOperand(sp, stack_index));
631 return kArm64WordSize;
632}
633
634size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
635 FPRegister reg = FPRegister(reg_id, kDRegSize);
636 __ Str(reg, MemOperand(sp, stack_index));
637 return kArm64WordSize;
638}
639
640size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
641 FPRegister reg = FPRegister(reg_id, kDRegSize);
642 __ Ldr(reg, MemOperand(sp, stack_index));
643 return kArm64WordSize;
644}
645
Alexandre Rames5319def2014-10-23 10:03:10 +0100646void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
647 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
648}
649
650void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
651 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
652}
653
Alexandre Rames67555f72014-11-18 10:55:16 +0000654void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000655 if (constant->IsIntConstant()) {
656 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
657 } else if (constant->IsLongConstant()) {
658 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
659 } else if (constant->IsNullConstant()) {
660 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000661 } else if (constant->IsFloatConstant()) {
662 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
663 } else {
664 DCHECK(constant->IsDoubleConstant());
665 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
666 }
667}
668
Alexandre Rames3e69f162014-12-10 10:36:50 +0000669
670static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
671 DCHECK(constant.IsConstant());
672 HConstant* cst = constant.GetConstant();
673 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000674 // Null is mapped to a core W register, which we associate with kPrimInt.
675 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000676 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
677 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
678 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
679}
680
681void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000682 if (source.Equals(destination)) {
683 return;
684 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000685
686 // A valid move can always be inferred from the destination and source
687 // locations. When moving from and to a register, the argument type can be
688 // used to generate 32bit instead of 64bit moves. In debug mode we also
689 // checks the coherency of the locations and the type.
690 bool unspecified_type = (type == Primitive::kPrimVoid);
691
692 if (destination.IsRegister() || destination.IsFpuRegister()) {
693 if (unspecified_type) {
694 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
695 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000696 (src_cst != nullptr && (src_cst->IsIntConstant()
697 || src_cst->IsFloatConstant()
698 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000699 // For stack slots and 32bit constants, a 64bit type is appropriate.
700 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000701 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000702 // If the source is a double stack slot or a 64bit constant, a 64bit
703 // type is appropriate. Else the source is a register, and since the
704 // type has not been specified, we chose a 64bit type to force a 64bit
705 // move.
706 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000707 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000708 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000709 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
710 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000711 CPURegister dst = CPURegisterFrom(destination, type);
712 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
713 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
714 __ Ldr(dst, StackOperandFrom(source));
715 } else if (source.IsConstant()) {
716 DCHECK(CoherentConstantAndType(source, type));
717 MoveConstant(dst, source.GetConstant());
718 } else {
719 if (destination.IsRegister()) {
720 __ Mov(Register(dst), RegisterFrom(source, type));
721 } else {
722 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
723 }
724 }
725
726 } else { // The destination is not a register. It must be a stack slot.
727 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
728 if (source.IsRegister() || source.IsFpuRegister()) {
729 if (unspecified_type) {
730 if (source.IsRegister()) {
731 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
732 } else {
733 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
734 }
735 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000736 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
737 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000738 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
739 } else if (source.IsConstant()) {
740 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
741 UseScratchRegisterScope temps(GetVIXLAssembler());
742 HConstant* src_cst = source.GetConstant();
743 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000744 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000745 temp = temps.AcquireW();
746 } else if (src_cst->IsLongConstant()) {
747 temp = temps.AcquireX();
748 } else if (src_cst->IsFloatConstant()) {
749 temp = temps.AcquireS();
750 } else {
751 DCHECK(src_cst->IsDoubleConstant());
752 temp = temps.AcquireD();
753 }
754 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000755 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000756 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000757 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000758 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000759 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000760 // There is generally less pressure on FP registers.
761 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000762 __ Ldr(temp, StackOperandFrom(source));
763 __ Str(temp, StackOperandFrom(destination));
764 }
765 }
766}
767
Alexandre Rames3e69f162014-12-10 10:36:50 +0000768void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
769 DCHECK(!loc1.IsConstant());
770 DCHECK(!loc2.IsConstant());
771
772 if (loc1.Equals(loc2)) {
773 return;
774 }
775
776 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
777
778 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
779 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
780 bool is_fp_reg1 = loc1.IsFpuRegister();
781 bool is_fp_reg2 = loc2.IsFpuRegister();
782
783 if (loc2.IsRegister() && loc1.IsRegister()) {
784 Register r1 = XRegisterFrom(loc1);
785 Register r2 = XRegisterFrom(loc2);
786 Register tmp = temps.AcquireSameSizeAs(r1);
787 __ Mov(tmp, r2);
788 __ Mov(r2, r1);
789 __ Mov(r1, tmp);
790 } else if (is_fp_reg2 && is_fp_reg1) {
791 FPRegister r1 = DRegisterFrom(loc1);
792 FPRegister r2 = DRegisterFrom(loc2);
793 FPRegister tmp = temps.AcquireSameSizeAs(r1);
794 __ Fmov(tmp, r2);
795 __ Fmov(r2, r1);
796 __ Fmov(r1, tmp);
797 } else if (is_slot1 != is_slot2) {
798 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
799 Location reg_loc = is_slot1 ? loc2 : loc1;
800 CPURegister reg, tmp;
801 if (reg_loc.IsFpuRegister()) {
802 reg = DRegisterFrom(reg_loc);
803 tmp = temps.AcquireD();
804 } else {
805 reg = XRegisterFrom(reg_loc);
806 tmp = temps.AcquireX();
807 }
808 __ Ldr(tmp, mem);
809 __ Str(reg, mem);
810 if (reg_loc.IsFpuRegister()) {
811 __ Fmov(FPRegister(reg), FPRegister(tmp));
812 } else {
813 __ Mov(Register(reg), Register(tmp));
814 }
815 } else if (is_slot1 && is_slot2) {
816 MemOperand mem1 = StackOperandFrom(loc1);
817 MemOperand mem2 = StackOperandFrom(loc2);
818 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
819 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
820 __ Ldr(tmp1, mem1);
821 __ Ldr(tmp2, mem2);
822 __ Str(tmp1, mem2);
823 __ Str(tmp2, mem1);
824 } else {
825 LOG(FATAL) << "Unimplemented";
826 }
827}
828
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000829void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000830 CPURegister dst,
831 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000832 switch (type) {
833 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000834 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000835 break;
836 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000837 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000838 break;
839 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000840 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000841 break;
842 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000843 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000844 break;
845 case Primitive::kPrimInt:
846 case Primitive::kPrimNot:
847 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000848 case Primitive::kPrimFloat:
849 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000850 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000851 __ Ldr(dst, src);
852 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000853 case Primitive::kPrimVoid:
854 LOG(FATAL) << "Unreachable type " << type;
855 }
856}
857
Calin Juravle77520bc2015-01-12 18:45:46 +0000858void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000859 CPURegister dst,
860 const MemOperand& src) {
861 UseScratchRegisterScope temps(GetVIXLAssembler());
862 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000863 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000864
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000865 DCHECK(!src.IsPreIndex());
866 DCHECK(!src.IsPostIndex());
867
868 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800869 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000870 MemOperand base = MemOperand(temp_base);
871 switch (type) {
872 case Primitive::kPrimBoolean:
873 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000874 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000875 break;
876 case Primitive::kPrimByte:
877 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000878 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000879 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
880 break;
881 case Primitive::kPrimChar:
882 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000883 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000884 break;
885 case Primitive::kPrimShort:
886 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000887 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000888 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
889 break;
890 case Primitive::kPrimInt:
891 case Primitive::kPrimNot:
892 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000893 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000894 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000895 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000896 break;
897 case Primitive::kPrimFloat:
898 case Primitive::kPrimDouble: {
899 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000900 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000901
902 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
903 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000904 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000905 __ Fmov(FPRegister(dst), temp);
906 break;
907 }
908 case Primitive::kPrimVoid:
909 LOG(FATAL) << "Unreachable type " << type;
910 }
911}
912
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000913void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000914 CPURegister src,
915 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000916 switch (type) {
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000919 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000920 break;
921 case Primitive::kPrimChar:
922 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000923 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000924 break;
925 case Primitive::kPrimInt:
926 case Primitive::kPrimNot:
927 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000928 case Primitive::kPrimFloat:
929 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000930 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000931 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000932 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000933 case Primitive::kPrimVoid:
934 LOG(FATAL) << "Unreachable type " << type;
935 }
936}
937
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000938void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
939 CPURegister src,
940 const MemOperand& dst) {
941 UseScratchRegisterScope temps(GetVIXLAssembler());
942 Register temp_base = temps.AcquireX();
943
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000944 DCHECK(!dst.IsPreIndex());
945 DCHECK(!dst.IsPostIndex());
946
947 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800948 Operand op = OperandFromMemOperand(dst);
949 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000950 MemOperand base = MemOperand(temp_base);
951 switch (type) {
952 case Primitive::kPrimBoolean:
953 case Primitive::kPrimByte:
954 __ Stlrb(Register(src), base);
955 break;
956 case Primitive::kPrimChar:
957 case Primitive::kPrimShort:
958 __ Stlrh(Register(src), base);
959 break;
960 case Primitive::kPrimInt:
961 case Primitive::kPrimNot:
962 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000963 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000964 __ Stlr(Register(src), base);
965 break;
966 case Primitive::kPrimFloat:
967 case Primitive::kPrimDouble: {
968 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000969 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000970
971 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
972 __ Fmov(temp, FPRegister(src));
973 __ Stlr(temp, base);
974 break;
975 }
976 case Primitive::kPrimVoid:
977 LOG(FATAL) << "Unreachable type " << type;
978 }
979}
980
Alexandre Rames67555f72014-11-18 10:55:16 +0000981void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000982 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000983 DCHECK(current_method.IsW());
984 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
985}
986
987void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
988 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000989 uint32_t dex_pc,
990 SlowPathCode* slow_path) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000991 __ Ldr(lr, MemOperand(tr, entry_point_offset));
992 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000993 if (instruction != nullptr) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000994 RecordPcInfo(instruction, dex_pc, slow_path);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000995 DCHECK(instruction->IsSuspendCheck()
996 || instruction->IsBoundsCheck()
997 || instruction->IsNullCheck()
998 || instruction->IsDivZeroCheck()
999 || !IsLeafMethod());
1000 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001001}
1002
1003void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1004 vixl::Register class_reg) {
1005 UseScratchRegisterScope temps(GetVIXLAssembler());
1006 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001007 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001008 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009
Serban Constantinescu02164b32014-11-13 14:05:07 +00001010 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001011 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001012 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1013 __ Add(temp, class_reg, status_offset);
1014 __ Ldar(temp, HeapOperand(temp));
1015 __ Cmp(temp, mirror::Class::kStatusInitialized);
1016 __ B(lt, slow_path->GetEntryLabel());
1017 } else {
1018 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1019 __ Cmp(temp, mirror::Class::kStatusInitialized);
1020 __ B(lt, slow_path->GetEntryLabel());
1021 __ Dmb(InnerShareable, BarrierReads);
1022 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001023 __ Bind(slow_path->GetExitLabel());
1024}
Alexandre Rames5319def2014-10-23 10:03:10 +01001025
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001026void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1027 BarrierType type = BarrierAll;
1028
1029 switch (kind) {
1030 case MemBarrierKind::kAnyAny:
1031 case MemBarrierKind::kAnyStore: {
1032 type = BarrierAll;
1033 break;
1034 }
1035 case MemBarrierKind::kLoadAny: {
1036 type = BarrierReads;
1037 break;
1038 }
1039 case MemBarrierKind::kStoreStore: {
1040 type = BarrierWrites;
1041 break;
1042 }
1043 default:
1044 LOG(FATAL) << "Unexpected memory barrier " << kind;
1045 }
1046 __ Dmb(InnerShareable, type);
1047}
1048
Serban Constantinescu02164b32014-11-13 14:05:07 +00001049void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1050 HBasicBlock* successor) {
1051 SuspendCheckSlowPathARM64* slow_path =
1052 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1053 codegen_->AddSlowPath(slow_path);
1054 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1055 Register temp = temps.AcquireW();
1056
1057 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1058 if (successor == nullptr) {
1059 __ Cbnz(temp, slow_path->GetEntryLabel());
1060 __ Bind(slow_path->GetReturnLabel());
1061 } else {
1062 __ Cbz(temp, codegen_->GetLabelOf(successor));
1063 __ B(slow_path->GetEntryLabel());
1064 // slow_path will return to GetLabelOf(successor).
1065 }
1066}
1067
Alexandre Rames5319def2014-10-23 10:03:10 +01001068InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1069 CodeGeneratorARM64* codegen)
1070 : HGraphVisitor(graph),
1071 assembler_(codegen->GetAssembler()),
1072 codegen_(codegen) {}
1073
1074#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001075 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001076
1077#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1078
1079enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001080 // Using a base helps identify when we hit such breakpoints.
1081 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001082#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1083 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1084#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1085};
1086
1087#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1088 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001089 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001090 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1091 } \
1092 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1093 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1094 locations->SetOut(Location::Any()); \
1095 }
1096 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1097#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1098
1099#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001100#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001101
Alexandre Rames67555f72014-11-18 10:55:16 +00001102void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001103 DCHECK_EQ(instr->InputCount(), 2U);
1104 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1105 Primitive::Type type = instr->GetResultType();
1106 switch (type) {
1107 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001108 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001109 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001110 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001112 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001113
1114 case Primitive::kPrimFloat:
1115 case Primitive::kPrimDouble:
1116 locations->SetInAt(0, Location::RequiresFpuRegister());
1117 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001118 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001120
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001122 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001123 }
1124}
1125
Alexandre Rames67555f72014-11-18 10:55:16 +00001126void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001127 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001128
1129 switch (type) {
1130 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 case Primitive::kPrimLong: {
1132 Register dst = OutputRegister(instr);
1133 Register lhs = InputRegisterAt(instr, 0);
1134 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001135 if (instr->IsAdd()) {
1136 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001137 } else if (instr->IsAnd()) {
1138 __ And(dst, lhs, rhs);
1139 } else if (instr->IsOr()) {
1140 __ Orr(dst, lhs, rhs);
1141 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001142 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001143 } else {
1144 DCHECK(instr->IsXor());
1145 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001146 }
1147 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001148 }
1149 case Primitive::kPrimFloat:
1150 case Primitive::kPrimDouble: {
1151 FPRegister dst = OutputFPRegister(instr);
1152 FPRegister lhs = InputFPRegisterAt(instr, 0);
1153 FPRegister rhs = InputFPRegisterAt(instr, 1);
1154 if (instr->IsAdd()) {
1155 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001156 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001157 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001158 } else {
1159 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001160 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001161 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001162 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001163 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001164 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001165 }
1166}
1167
Serban Constantinescu02164b32014-11-13 14:05:07 +00001168void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1169 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1170
1171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1172 Primitive::Type type = instr->GetResultType();
1173 switch (type) {
1174 case Primitive::kPrimInt:
1175 case Primitive::kPrimLong: {
1176 locations->SetInAt(0, Location::RequiresRegister());
1177 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1178 locations->SetOut(Location::RequiresRegister());
1179 break;
1180 }
1181 default:
1182 LOG(FATAL) << "Unexpected shift type " << type;
1183 }
1184}
1185
1186void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1187 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1188
1189 Primitive::Type type = instr->GetType();
1190 switch (type) {
1191 case Primitive::kPrimInt:
1192 case Primitive::kPrimLong: {
1193 Register dst = OutputRegister(instr);
1194 Register lhs = InputRegisterAt(instr, 0);
1195 Operand rhs = InputOperandAt(instr, 1);
1196 if (rhs.IsImmediate()) {
1197 uint32_t shift_value = (type == Primitive::kPrimInt)
1198 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1199 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1200 if (instr->IsShl()) {
1201 __ Lsl(dst, lhs, shift_value);
1202 } else if (instr->IsShr()) {
1203 __ Asr(dst, lhs, shift_value);
1204 } else {
1205 __ Lsr(dst, lhs, shift_value);
1206 }
1207 } else {
1208 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1209
1210 if (instr->IsShl()) {
1211 __ Lsl(dst, lhs, rhs_reg);
1212 } else if (instr->IsShr()) {
1213 __ Asr(dst, lhs, rhs_reg);
1214 } else {
1215 __ Lsr(dst, lhs, rhs_reg);
1216 }
1217 }
1218 break;
1219 }
1220 default:
1221 LOG(FATAL) << "Unexpected shift operation type " << type;
1222 }
1223}
1224
Alexandre Rames5319def2014-10-23 10:03:10 +01001225void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001226 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001227}
1228
1229void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001230 HandleBinaryOp(instruction);
1231}
1232
1233void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1234 HandleBinaryOp(instruction);
1235}
1236
1237void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1238 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001239}
1240
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001241void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1242 LocationSummary* locations =
1243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1244 locations->SetInAt(0, Location::RequiresRegister());
1245 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1246 locations->SetOut(Location::RequiresRegister());
1247}
1248
1249void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1250 LocationSummary* locations = instruction->GetLocations();
1251 Primitive::Type type = instruction->GetType();
1252 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001253 Location index = locations->InAt(1);
1254 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001255 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001256 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001257
1258 if (index.IsConstant()) {
1259 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001260 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001261 } else {
1262 Register temp = temps.AcquireSameSizeAs(obj);
1263 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1264 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001265 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001266 }
1267
Alexandre Rames67555f72014-11-18 10:55:16 +00001268 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001269 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001270}
1271
Alexandre Rames5319def2014-10-23 10:03:10 +01001272void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1273 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1274 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001276}
1277
1278void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1279 __ Ldr(OutputRegister(instruction),
1280 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001281 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001282}
1283
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001284void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1285 Primitive::Type value_type = instruction->GetComponentType();
1286 bool is_object = value_type == Primitive::kPrimNot;
1287 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1288 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1289 if (is_object) {
1290 InvokeRuntimeCallingConvention calling_convention;
1291 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1292 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1293 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1294 } else {
1295 locations->SetInAt(0, Location::RequiresRegister());
1296 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1297 locations->SetInAt(2, Location::RequiresRegister());
1298 }
1299}
1300
1301void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1302 Primitive::Type value_type = instruction->GetComponentType();
1303 if (value_type == Primitive::kPrimNot) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001304 codegen_->InvokeRuntime(
1305 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001306 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001307 } else {
1308 LocationSummary* locations = instruction->GetLocations();
1309 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001310 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001311 Location index = locations->InAt(1);
1312 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001313 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001314 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001315
1316 if (index.IsConstant()) {
1317 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001318 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001319 } else {
1320 Register temp = temps.AcquireSameSizeAs(obj);
1321 Register index_reg = InputRegisterAt(instruction, 1);
1322 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001323 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001324 }
1325
1326 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001327 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001328 }
1329}
1330
Alexandre Rames67555f72014-11-18 10:55:16 +00001331void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1332 LocationSummary* locations =
1333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1334 locations->SetInAt(0, Location::RequiresRegister());
1335 locations->SetInAt(1, Location::RequiresRegister());
1336 if (instruction->HasUses()) {
1337 locations->SetOut(Location::SameAsFirstInput());
1338 }
1339}
1340
1341void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001342 LocationSummary* locations = instruction->GetLocations();
1343 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1344 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001345 codegen_->AddSlowPath(slow_path);
1346
1347 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1348 __ B(slow_path->GetEntryLabel(), hs);
1349}
1350
1351void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1352 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1353 instruction, LocationSummary::kCallOnSlowPath);
1354 locations->SetInAt(0, Location::RequiresRegister());
1355 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001356 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001357}
1358
1359void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001360 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001361 Register obj = InputRegisterAt(instruction, 0);;
1362 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001363 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001364
Alexandre Rames3e69f162014-12-10 10:36:50 +00001365 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1366 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001367 codegen_->AddSlowPath(slow_path);
1368
1369 // TODO: avoid this check if we know obj is not null.
1370 __ Cbz(obj, slow_path->GetExitLabel());
1371 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001372 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1373 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001374 __ B(ne, slow_path->GetEntryLabel());
1375 __ Bind(slow_path->GetExitLabel());
1376}
1377
1378void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1379 LocationSummary* locations =
1380 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1381 locations->SetInAt(0, Location::RequiresRegister());
1382 if (check->HasUses()) {
1383 locations->SetOut(Location::SameAsFirstInput());
1384 }
1385}
1386
1387void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1388 // We assume the class is not null.
1389 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1390 check->GetLoadClass(), check, check->GetDexPc(), true);
1391 codegen_->AddSlowPath(slow_path);
1392 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1393}
1394
Serban Constantinescu02164b32014-11-13 14:05:07 +00001395void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001396 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001397 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1398 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001399 switch (in_type) {
1400 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001401 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001402 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1404 break;
1405 }
1406 case Primitive::kPrimFloat:
1407 case Primitive::kPrimDouble: {
1408 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001409 HInstruction* right = compare->InputAt(1);
1410 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1411 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1412 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1413 } else {
1414 locations->SetInAt(1, Location::RequiresFpuRegister());
1415 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001416 locations->SetOut(Location::RequiresRegister());
1417 break;
1418 }
1419 default:
1420 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1421 }
1422}
1423
1424void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1425 Primitive::Type in_type = compare->InputAt(0)->GetType();
1426
1427 // 0 if: left == right
1428 // 1 if: left > right
1429 // -1 if: left < right
1430 switch (in_type) {
1431 case Primitive::kPrimLong: {
1432 Register result = OutputRegister(compare);
1433 Register left = InputRegisterAt(compare, 0);
1434 Operand right = InputOperandAt(compare, 1);
1435
1436 __ Cmp(left, right);
1437 __ Cset(result, ne);
1438 __ Cneg(result, result, lt);
1439 break;
1440 }
1441 case Primitive::kPrimFloat:
1442 case Primitive::kPrimDouble: {
1443 Register result = OutputRegister(compare);
1444 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001445 if (compare->GetLocations()->InAt(1).IsConstant()) {
1446 if (kIsDebugBuild) {
1447 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1448 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1449 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1450 }
1451 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1452 __ Fcmp(left, 0.0);
1453 } else {
1454 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1455 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001456 if (compare->IsGtBias()) {
1457 __ Cset(result, ne);
1458 } else {
1459 __ Csetm(result, ne);
1460 }
1461 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001462 break;
1463 }
1464 default:
1465 LOG(FATAL) << "Unimplemented compare type " << in_type;
1466 }
1467}
1468
1469void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1470 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1471 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001472 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001473 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001475 }
1476}
1477
1478void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1479 if (!instruction->NeedsMaterialization()) {
1480 return;
1481 }
1482
1483 LocationSummary* locations = instruction->GetLocations();
1484 Register lhs = InputRegisterAt(instruction, 0);
1485 Operand rhs = InputOperandAt(instruction, 1);
1486 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1487 Condition cond = ARM64Condition(instruction->GetCondition());
1488
1489 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001490 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001491}
1492
1493#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1494 M(Equal) \
1495 M(NotEqual) \
1496 M(LessThan) \
1497 M(LessThanOrEqual) \
1498 M(GreaterThan) \
1499 M(GreaterThanOrEqual)
1500#define DEFINE_CONDITION_VISITORS(Name) \
1501void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1502void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1503FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001504#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001505#undef FOR_EACH_CONDITION_INSTRUCTION
1506
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001507void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1508 LocationSummary* locations =
1509 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1510 switch (div->GetResultType()) {
1511 case Primitive::kPrimInt:
1512 case Primitive::kPrimLong:
1513 locations->SetInAt(0, Location::RequiresRegister());
1514 locations->SetInAt(1, Location::RequiresRegister());
1515 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1516 break;
1517
1518 case Primitive::kPrimFloat:
1519 case Primitive::kPrimDouble:
1520 locations->SetInAt(0, Location::RequiresFpuRegister());
1521 locations->SetInAt(1, Location::RequiresFpuRegister());
1522 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1523 break;
1524
1525 default:
1526 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1527 }
1528}
1529
1530void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1531 Primitive::Type type = div->GetResultType();
1532 switch (type) {
1533 case Primitive::kPrimInt:
1534 case Primitive::kPrimLong:
1535 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1536 break;
1537
1538 case Primitive::kPrimFloat:
1539 case Primitive::kPrimDouble:
1540 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1541 break;
1542
1543 default:
1544 LOG(FATAL) << "Unexpected div type " << type;
1545 }
1546}
1547
Alexandre Rames67555f72014-11-18 10:55:16 +00001548void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1549 LocationSummary* locations =
1550 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1551 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1552 if (instruction->HasUses()) {
1553 locations->SetOut(Location::SameAsFirstInput());
1554 }
1555}
1556
1557void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1558 SlowPathCodeARM64* slow_path =
1559 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1560 codegen_->AddSlowPath(slow_path);
1561 Location value = instruction->GetLocations()->InAt(0);
1562
Alexandre Rames3e69f162014-12-10 10:36:50 +00001563 Primitive::Type type = instruction->GetType();
1564
1565 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1566 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1567 return;
1568 }
1569
Alexandre Rames67555f72014-11-18 10:55:16 +00001570 if (value.IsConstant()) {
1571 int64_t divisor = Int64ConstantFrom(value);
1572 if (divisor == 0) {
1573 __ B(slow_path->GetEntryLabel());
1574 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001575 // A division by a non-null constant is valid. We don't need to perform
1576 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001577 }
1578 } else {
1579 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1580 }
1581}
1582
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001583void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1584 LocationSummary* locations =
1585 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1586 locations->SetOut(Location::ConstantLocation(constant));
1587}
1588
1589void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1590 UNUSED(constant);
1591 // Will be generated at use site.
1592}
1593
Alexandre Rames5319def2014-10-23 10:03:10 +01001594void LocationsBuilderARM64::VisitExit(HExit* exit) {
1595 exit->SetLocations(nullptr);
1596}
1597
1598void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001599 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 if (kIsDebugBuild) {
1601 down_cast<Arm64Assembler*>(GetAssembler())->Comment("Unreachable");
Alexandre Rames67555f72014-11-18 10:55:16 +00001602 __ Brk(__LINE__); // TODO: Introduce special markers for such code locations.
Alexandre Rames5319def2014-10-23 10:03:10 +01001603 }
1604}
1605
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001606void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1607 LocationSummary* locations =
1608 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1609 locations->SetOut(Location::ConstantLocation(constant));
1610}
1611
1612void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1613 UNUSED(constant);
1614 // Will be generated at use site.
1615}
1616
Alexandre Rames5319def2014-10-23 10:03:10 +01001617void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1618 got->SetLocations(nullptr);
1619}
1620
1621void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1622 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001623 DCHECK(!successor->IsExitBlock());
1624 HBasicBlock* block = got->GetBlock();
1625 HInstruction* previous = got->GetPrevious();
1626 HLoopInformation* info = block->GetLoopInformation();
1627
1628 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
1629 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1630 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1631 return;
1632 }
1633 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1634 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1635 }
1636 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001637 __ B(codegen_->GetLabelOf(successor));
1638 }
1639}
1640
1641void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1643 HInstruction* cond = if_instr->InputAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001644 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 locations->SetInAt(0, Location::RequiresRegister());
1646 }
1647}
1648
1649void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1650 HInstruction* cond = if_instr->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001651 HCondition* condition = cond->AsCondition();
1652 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1653 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1654
Serban Constantinescu02164b32014-11-13 14:05:07 +00001655 if (cond->IsIntConstant()) {
1656 int32_t cond_value = cond->AsIntConstant()->GetValue();
1657 if (cond_value == 1) {
1658 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
1659 __ B(true_target);
1660 }
1661 return;
1662 } else {
1663 DCHECK_EQ(cond_value, 0);
1664 }
1665 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001666 // The condition instruction has been materialized, compare the output to 0.
1667 Location cond_val = if_instr->GetLocations()->InAt(0);
1668 DCHECK(cond_val.IsRegister());
1669 __ Cbnz(InputRegisterAt(if_instr, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001670 } else {
1671 // The condition instruction has not been materialized, use its inputs as
1672 // the comparison and its condition as the branch condition.
1673 Register lhs = InputRegisterAt(condition, 0);
1674 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001675 Condition arm64_cond = ARM64Condition(condition->GetCondition());
1676 if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1677 if (arm64_cond == eq) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001678 __ Cbz(lhs, true_target);
1679 } else {
1680 __ Cbnz(lhs, true_target);
1681 }
1682 } else {
1683 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001684 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001685 }
1686 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001687 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
1688 __ B(false_target);
1689 }
1690}
1691
1692void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001693 LocationSummary* locations =
1694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001695 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001697}
1698
1699void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001700 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00001701 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001702
1703 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001704 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001705 // NB: LoadAcquire will record the pc info if needed.
1706 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001707 } else {
1708 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001709 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001710 // For IRIW sequential consistency kLoadAny is not sufficient.
1711 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1712 }
1713 } else {
1714 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001715 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001716 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001717}
1718
1719void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001720 LocationSummary* locations =
1721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001722 locations->SetInAt(0, Location::RequiresRegister());
1723 locations->SetInAt(1, Location::RequiresRegister());
1724}
1725
1726void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001727 Register obj = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001728 CPURegister value = InputCPURegisterAt(instruction, 1);
1729 Offset offset = instruction->GetFieldOffset();
1730 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001731 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001732
1733 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001734 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001735 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001736 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001737 } else {
1738 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1739 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001741 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1742 }
1743 } else {
1744 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001745 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001746 }
1747
1748 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001749 codegen_->MarkGCCard(obj, Register(value));
Alexandre Rames5319def2014-10-23 10:03:10 +01001750 }
1751}
1752
Alexandre Rames67555f72014-11-18 10:55:16 +00001753void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1754 LocationSummary::CallKind call_kind =
1755 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1756 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1757 locations->SetInAt(0, Location::RequiresRegister());
1758 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001759 // The output does overlap inputs.
1760 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001761}
1762
1763void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1764 LocationSummary* locations = instruction->GetLocations();
1765 Register obj = InputRegisterAt(instruction, 0);;
1766 Register cls = InputRegisterAt(instruction, 1);;
1767 Register out = OutputRegister(instruction);
1768
1769 vixl::Label done;
1770
1771 // Return 0 if `obj` is null.
1772 // TODO: Avoid this check if we know `obj` is not null.
1773 __ Mov(out, 0);
1774 __ Cbz(obj, &done);
1775
1776 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001777 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001778 __ Cmp(out, cls);
1779 if (instruction->IsClassFinal()) {
1780 // Classes must be equal for the instanceof to succeed.
1781 __ Cset(out, eq);
1782 } else {
1783 // If the classes are not equal, we go into a slow path.
1784 DCHECK(locations->OnlyCallsOnSlowPath());
1785 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001786 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1787 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001788 codegen_->AddSlowPath(slow_path);
1789 __ B(ne, slow_path->GetEntryLabel());
1790 __ Mov(out, 1);
1791 __ Bind(slow_path->GetExitLabel());
1792 }
1793
1794 __ Bind(&done);
1795}
1796
Alexandre Rames5319def2014-10-23 10:03:10 +01001797void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1798 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1799 locations->SetOut(Location::ConstantLocation(constant));
1800}
1801
1802void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1803 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001804 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001805}
1806
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001807void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1809 locations->SetOut(Location::ConstantLocation(constant));
1810}
1811
1812void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1813 // Will be generated at use site.
1814 UNUSED(constant);
1815}
1816
Alexandre Rames5319def2014-10-23 10:03:10 +01001817void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1820 locations->AddTemp(LocationFrom(x0));
1821
1822 InvokeDexCallingConventionVisitor calling_convention_visitor;
1823 for (size_t i = 0; i < invoke->InputCount(); i++) {
1824 HInstruction* input = invoke->InputAt(i);
1825 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1826 }
1827
1828 Primitive::Type return_type = invoke->GetType();
1829 if (return_type != Primitive::kPrimVoid) {
1830 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1831 }
1832}
1833
Alexandre Rames67555f72014-11-18 10:55:16 +00001834void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1835 HandleInvoke(invoke);
1836}
1837
1838void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1839 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1840 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1841 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1842 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1843 Location receiver = invoke->GetLocations()->InAt(0);
1844 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001845 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001846
1847 // The register ip1 is required to be used for the hidden argument in
1848 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1849 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1850 scratch_scope.Exclude(ip1);
1851 __ Mov(ip1, invoke->GetDexMethodIndex());
1852
1853 // temp = object->GetClass();
1854 if (receiver.IsStackSlot()) {
1855 __ Ldr(temp, StackOperandFrom(receiver));
1856 __ Ldr(temp, HeapOperand(temp, class_offset));
1857 } else {
1858 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1859 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001860 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001861 // temp = temp->GetImtEntryAt(method_offset);
1862 __ Ldr(temp, HeapOperand(temp, method_offset));
1863 // lr = temp->GetEntryPoint();
1864 __ Ldr(lr, HeapOperand(temp, entry_point));
1865 // lr();
1866 __ Blr(lr);
1867 DCHECK(!codegen_->IsLeafMethod());
1868 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1869}
1870
1871void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001872 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1873 if (intrinsic.TryDispatch(invoke)) {
1874 return;
1875 }
1876
Alexandre Rames67555f72014-11-18 10:55:16 +00001877 HandleInvoke(invoke);
1878}
1879
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001880void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001881 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1882 if (intrinsic.TryDispatch(invoke)) {
1883 return;
1884 }
1885
Alexandre Rames67555f72014-11-18 10:55:16 +00001886 HandleInvoke(invoke);
1887}
1888
Andreas Gampe878d58c2015-01-15 23:24:00 -08001889static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1890 if (invoke->GetLocations()->Intrinsified()) {
1891 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1892 intrinsic.Dispatch(invoke);
1893 return true;
1894 }
1895 return false;
1896}
1897
1898void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1899 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1900 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001901 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001902 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001903
1904 // TODO: Implement all kinds of calls:
1905 // 1) boot -> boot
1906 // 2) app -> boot
1907 // 3) app -> app
1908 //
1909 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1910
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00001911 // temp = method;
1912 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001913 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001914 // temp = temp->dex_cache_resolved_methods_;
1915 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
1916 // temp = temp[index_in_cache];
1917 __ Ldr(temp, HeapOperand(temp, index_in_cache));
1918 // lr = temp->entry_point_from_quick_compiled_code_;
1919 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1920 kArm64WordSize)));
1921 // lr();
1922 __ Blr(lr);
1923 } else {
1924 __ Bl(&frame_entry_label_);
1925 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001926
Andreas Gampe878d58c2015-01-15 23:24:00 -08001927 DCHECK(!IsLeafMethod());
1928}
1929
1930void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1931 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1932 return;
1933 }
1934
1935 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1936 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001937 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01001938}
1939
1940void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001941 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1942 return;
1943 }
1944
Alexandre Rames5319def2014-10-23 10:03:10 +01001945 LocationSummary* locations = invoke->GetLocations();
1946 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001947 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01001948 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1949 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1950 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001951 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01001952
1953 // temp = object->GetClass();
1954 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001955 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
1956 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001957 } else {
1958 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001959 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001960 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001961 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01001962 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001963 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001964 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001965 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001966 // lr();
1967 __ Blr(lr);
1968 DCHECK(!codegen_->IsLeafMethod());
1969 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1970}
1971
Alexandre Rames67555f72014-11-18 10:55:16 +00001972void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
1973 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
1974 : LocationSummary::kNoCall;
1975 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
1976 locations->SetOut(Location::RequiresRegister());
1977}
1978
1979void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
1980 Register out = OutputRegister(cls);
1981 if (cls->IsReferrersClass()) {
1982 DCHECK(!cls->CanCallRuntime());
1983 DCHECK(!cls->MustGenerateClinitCheck());
1984 codegen_->LoadCurrentMethod(out);
1985 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
1986 } else {
1987 DCHECK(cls->CanCallRuntime());
1988 codegen_->LoadCurrentMethod(out);
1989 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001990 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00001991
1992 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1993 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
1994 codegen_->AddSlowPath(slow_path);
1995 __ Cbz(out, slow_path->GetEntryLabel());
1996 if (cls->MustGenerateClinitCheck()) {
1997 GenerateClassInitializationCheck(slow_path, out);
1998 } else {
1999 __ Bind(slow_path->GetExitLabel());
2000 }
2001 }
2002}
2003
2004void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2005 LocationSummary* locations =
2006 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2007 locations->SetOut(Location::RequiresRegister());
2008}
2009
2010void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2011 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2012 __ Ldr(OutputRegister(instruction), exception);
2013 __ Str(wzr, exception);
2014}
2015
Alexandre Rames5319def2014-10-23 10:03:10 +01002016void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2017 load->SetLocations(nullptr);
2018}
2019
2020void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2021 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002022 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002023}
2024
Alexandre Rames67555f72014-11-18 10:55:16 +00002025void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2026 LocationSummary* locations =
2027 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2028 locations->SetOut(Location::RequiresRegister());
2029}
2030
2031void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2032 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2033 codegen_->AddSlowPath(slow_path);
2034
2035 Register out = OutputRegister(load);
2036 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002037 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2038 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002039 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002040 __ Cbz(out, slow_path->GetEntryLabel());
2041 __ Bind(slow_path->GetExitLabel());
2042}
2043
Alexandre Rames5319def2014-10-23 10:03:10 +01002044void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2045 local->SetLocations(nullptr);
2046}
2047
2048void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2049 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2050}
2051
2052void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2053 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2054 locations->SetOut(Location::ConstantLocation(constant));
2055}
2056
2057void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2058 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002059 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002060}
2061
Alexandre Rames67555f72014-11-18 10:55:16 +00002062void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2063 LocationSummary* locations =
2064 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2065 InvokeRuntimeCallingConvention calling_convention;
2066 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2067}
2068
2069void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2070 codegen_->InvokeRuntime(instruction->IsEnter()
2071 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2072 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002073 instruction->GetDexPc(),
2074 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002075 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002076}
2077
Alexandre Rames42d641b2014-10-27 14:00:51 +00002078void LocationsBuilderARM64::VisitMul(HMul* mul) {
2079 LocationSummary* locations =
2080 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2081 switch (mul->GetResultType()) {
2082 case Primitive::kPrimInt:
2083 case Primitive::kPrimLong:
2084 locations->SetInAt(0, Location::RequiresRegister());
2085 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002087 break;
2088
2089 case Primitive::kPrimFloat:
2090 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002091 locations->SetInAt(0, Location::RequiresFpuRegister());
2092 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002093 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002094 break;
2095
2096 default:
2097 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2098 }
2099}
2100
2101void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2102 switch (mul->GetResultType()) {
2103 case Primitive::kPrimInt:
2104 case Primitive::kPrimLong:
2105 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2106 break;
2107
2108 case Primitive::kPrimFloat:
2109 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002110 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002111 break;
2112
2113 default:
2114 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2115 }
2116}
2117
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002118void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2119 LocationSummary* locations =
2120 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2121 switch (neg->GetResultType()) {
2122 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002123 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002124 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002126 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002127
2128 case Primitive::kPrimFloat:
2129 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002130 locations->SetInAt(0, Location::RequiresFpuRegister());
2131 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002132 break;
2133
2134 default:
2135 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2136 }
2137}
2138
2139void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2140 switch (neg->GetResultType()) {
2141 case Primitive::kPrimInt:
2142 case Primitive::kPrimLong:
2143 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2144 break;
2145
2146 case Primitive::kPrimFloat:
2147 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002148 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002149 break;
2150
2151 default:
2152 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2153 }
2154}
2155
2156void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2157 LocationSummary* locations =
2158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2159 InvokeRuntimeCallingConvention calling_convention;
2160 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002161 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002162 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002163 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2164 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2165 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002166}
2167
2168void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2169 LocationSummary* locations = instruction->GetLocations();
2170 InvokeRuntimeCallingConvention calling_convention;
2171 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2172 DCHECK(type_index.Is(w0));
2173 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002174 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002175 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002176 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002177 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002178 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2179 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002180 instruction->GetDexPc(),
2181 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002182 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2183 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002184}
2185
Alexandre Rames5319def2014-10-23 10:03:10 +01002186void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2189 InvokeRuntimeCallingConvention calling_convention;
2190 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2191 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2192 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002193 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002194}
2195
2196void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2197 LocationSummary* locations = instruction->GetLocations();
2198 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2199 DCHECK(type_index.Is(w0));
2200 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2201 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002202 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002203 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002204 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002205 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2206 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002207 instruction->GetDexPc(),
2208 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002209 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002210}
2211
2212void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002214 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002216}
2217
2218void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002219 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002220 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002221 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002222 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002223 break;
2224
2225 default:
2226 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2227 }
2228}
2229
2230void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2231 LocationSummary* locations =
2232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2233 locations->SetInAt(0, Location::RequiresRegister());
2234 if (instruction->HasUses()) {
2235 locations->SetOut(Location::SameAsFirstInput());
2236 }
2237}
2238
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002239void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002240 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2241 return;
2242 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002243 Location obj = instruction->GetLocations()->InAt(0);
2244
2245 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2246 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2247}
2248
2249void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002250 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2251 codegen_->AddSlowPath(slow_path);
2252
2253 LocationSummary* locations = instruction->GetLocations();
2254 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002255
2256 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002257}
2258
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002259void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2260 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2261 GenerateImplicitNullCheck(instruction);
2262 } else {
2263 GenerateExplicitNullCheck(instruction);
2264 }
2265}
2266
Alexandre Rames67555f72014-11-18 10:55:16 +00002267void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2268 HandleBinaryOp(instruction);
2269}
2270
2271void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2272 HandleBinaryOp(instruction);
2273}
2274
Alexandre Rames3e69f162014-12-10 10:36:50 +00002275void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2276 LOG(FATAL) << "Unreachable";
2277}
2278
2279void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2280 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2281}
2282
Alexandre Rames5319def2014-10-23 10:03:10 +01002283void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2285 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2286 if (location.IsStackSlot()) {
2287 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2288 } else if (location.IsDoubleStackSlot()) {
2289 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2290 }
2291 locations->SetOut(location);
2292}
2293
2294void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2295 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002296 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002297}
2298
2299void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2301 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2302 locations->SetInAt(i, Location::Any());
2303 }
2304 locations->SetOut(Location::Any());
2305}
2306
2307void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002308 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002309 LOG(FATAL) << "Unreachable";
2310}
2311
Serban Constantinescu02164b32014-11-13 14:05:07 +00002312void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002313 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002314 LocationSummary::CallKind call_kind =
2315 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2317
2318 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002319 case Primitive::kPrimInt:
2320 case Primitive::kPrimLong:
2321 locations->SetInAt(0, Location::RequiresRegister());
2322 locations->SetInAt(1, Location::RequiresRegister());
2323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2324 break;
2325
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002326 case Primitive::kPrimFloat:
2327 case Primitive::kPrimDouble: {
2328 InvokeRuntimeCallingConvention calling_convention;
2329 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2330 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2331 locations->SetOut(calling_convention.GetReturnLocation(type));
2332
2333 break;
2334 }
2335
Serban Constantinescu02164b32014-11-13 14:05:07 +00002336 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002337 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002338 }
2339}
2340
2341void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2342 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002343
Serban Constantinescu02164b32014-11-13 14:05:07 +00002344 switch (type) {
2345 case Primitive::kPrimInt:
2346 case Primitive::kPrimLong: {
2347 UseScratchRegisterScope temps(GetVIXLAssembler());
2348 Register dividend = InputRegisterAt(rem, 0);
2349 Register divisor = InputRegisterAt(rem, 1);
2350 Register output = OutputRegister(rem);
2351 Register temp = temps.AcquireSameSizeAs(output);
2352
2353 __ Sdiv(temp, dividend, divisor);
2354 __ Msub(output, temp, divisor, dividend);
2355 break;
2356 }
2357
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002358 case Primitive::kPrimFloat:
2359 case Primitive::kPrimDouble: {
2360 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2361 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002362 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002363 break;
2364 }
2365
Serban Constantinescu02164b32014-11-13 14:05:07 +00002366 default:
2367 LOG(FATAL) << "Unexpected rem type " << type;
2368 }
2369}
2370
Alexandre Rames5319def2014-10-23 10:03:10 +01002371void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2372 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2373 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002374 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002375}
2376
2377void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002378 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002379 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002380 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002381}
2382
2383void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2384 instruction->SetLocations(nullptr);
2385}
2386
2387void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002388 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002389 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002390 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002391}
2392
Serban Constantinescu02164b32014-11-13 14:05:07 +00002393void LocationsBuilderARM64::VisitShl(HShl* shl) {
2394 HandleShift(shl);
2395}
2396
2397void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2398 HandleShift(shl);
2399}
2400
2401void LocationsBuilderARM64::VisitShr(HShr* shr) {
2402 HandleShift(shr);
2403}
2404
2405void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2406 HandleShift(shr);
2407}
2408
Alexandre Rames5319def2014-10-23 10:03:10 +01002409void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2411 Primitive::Type field_type = store->InputAt(1)->GetType();
2412 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002413 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002414 case Primitive::kPrimBoolean:
2415 case Primitive::kPrimByte:
2416 case Primitive::kPrimChar:
2417 case Primitive::kPrimShort:
2418 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002419 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002420 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2421 break;
2422
2423 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002424 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002425 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2426 break;
2427
2428 default:
2429 LOG(FATAL) << "Unimplemented local type " << field_type;
2430 }
2431}
2432
2433void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002434 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002435}
2436
2437void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002438 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002439}
2440
2441void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002442 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002443}
2444
Alexandre Rames67555f72014-11-18 10:55:16 +00002445void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2446 LocationSummary* locations =
2447 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2448 locations->SetInAt(0, Location::RequiresRegister());
2449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2450}
2451
2452void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002453 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00002454 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002455
2456 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002457 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002458 // NB: LoadAcquire will record the pc info if needed.
2459 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002460 } else {
2461 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2462 // For IRIW sequential consistency kLoadAny is not sufficient.
2463 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2464 }
2465 } else {
2466 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2467 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002468}
2469
2470void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002471 LocationSummary* locations =
2472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2473 locations->SetInAt(0, Location::RequiresRegister());
2474 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01002475}
2476
Alexandre Rames67555f72014-11-18 10:55:16 +00002477void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002478 Register cls = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002479 CPURegister value = InputCPURegisterAt(instruction, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002480 Offset offset = instruction->GetFieldOffset();
Alexandre Rames67555f72014-11-18 10:55:16 +00002481 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00002482 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Alexandre Rames5319def2014-10-23 10:03:10 +01002483
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002484 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002485 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002486 codegen_->StoreRelease(field_type, value, HeapOperand(cls, offset));
2487 } else {
2488 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2489 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2490 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2491 }
2492 } else {
2493 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2494 }
2495
2496 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002497 codegen_->MarkGCCard(cls, Register(value));
2498 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002499}
2500
2501void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2502 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2503}
2504
2505void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002506 HBasicBlock* block = instruction->GetBlock();
2507 if (block->GetLoopInformation() != nullptr) {
2508 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2509 // The back edge will generate the suspend check.
2510 return;
2511 }
2512 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2513 // The goto will generate the suspend check.
2514 return;
2515 }
2516 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002517}
2518
2519void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2520 temp->SetLocations(nullptr);
2521}
2522
2523void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2524 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002525 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002526}
2527
Alexandre Rames67555f72014-11-18 10:55:16 +00002528void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2529 LocationSummary* locations =
2530 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2531 InvokeRuntimeCallingConvention calling_convention;
2532 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2533}
2534
2535void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2536 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002537 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002538 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002539}
2540
2541void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2542 LocationSummary* locations =
2543 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2544 Primitive::Type input_type = conversion->GetInputType();
2545 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002546 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002547 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2548 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2549 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2550 }
2551
Alexandre Rames542361f2015-01-29 16:57:31 +00002552 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002553 locations->SetInAt(0, Location::RequiresFpuRegister());
2554 } else {
2555 locations->SetInAt(0, Location::RequiresRegister());
2556 }
2557
Alexandre Rames542361f2015-01-29 16:57:31 +00002558 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2560 } else {
2561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2562 }
2563}
2564
2565void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2566 Primitive::Type result_type = conversion->GetResultType();
2567 Primitive::Type input_type = conversion->GetInputType();
2568
2569 DCHECK_NE(input_type, result_type);
2570
Alexandre Rames542361f2015-01-29 16:57:31 +00002571 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002572 int result_size = Primitive::ComponentSize(result_type);
2573 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002574 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002575 Register output = OutputRegister(conversion);
2576 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002577 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2578 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2579 } else if ((result_type == Primitive::kPrimChar) ||
2580 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2581 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002582 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002583 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002584 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002585 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002586 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002587 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002588 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2589 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002590 } else if (Primitive::IsFloatingPointType(result_type) &&
2591 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002592 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2593 } else {
2594 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2595 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002596 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002597}
Alexandre Rames67555f72014-11-18 10:55:16 +00002598
Serban Constantinescu02164b32014-11-13 14:05:07 +00002599void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2600 HandleShift(ushr);
2601}
2602
2603void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2604 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002605}
2606
2607void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2608 HandleBinaryOp(instruction);
2609}
2610
2611void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2612 HandleBinaryOp(instruction);
2613}
2614
Calin Juravleb1498f62015-02-16 13:13:29 +00002615void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2616 // Nothing to do, this should be removed during prepare for register allocator.
2617 UNUSED(instruction);
2618 LOG(FATAL) << "Unreachable";
2619}
2620
2621void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2622 // Nothing to do, this should be removed during prepare for register allocator.
2623 UNUSED(instruction);
2624 LOG(FATAL) << "Unreachable";
2625}
2626
Alexandre Rames67555f72014-11-18 10:55:16 +00002627#undef __
2628#undef QUICK_ENTRY_POINT
2629
Alexandre Rames5319def2014-10-23 10:03:10 +01002630} // namespace arm64
2631} // namespace art