blob: 0a0902b0e9f5beda63acd3456375693a3f5c70a6 [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 +0100101Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000102 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100103}
104
Alexandre Rames67555f72014-11-18 10:55:16 +0000105#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100107
Alexandre Rames5319def2014-10-23 10:03:10 +0100108class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
109 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000110 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
111 Location index_location,
112 Location length_location)
113 : instruction_(instruction),
114 index_location_(index_location),
115 length_location_(length_location) {}
116
Alexandre Rames5319def2014-10-23 10:03:10 +0100117
Alexandre Rames67555f72014-11-18 10:55:16 +0000118 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000119 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100120 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000121 // We're moving two locations to locations that could overlap, so we need a parallel
122 // move resolver.
123 InvokeRuntimeCallingConvention calling_convention;
124 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100125 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
126 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000127 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000128 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800129 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100130 }
131
132 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000133 HBoundsCheck* const instruction_;
134 const Location index_location_;
135 const Location length_location_;
136
Alexandre Rames5319def2014-10-23 10:03:10 +0100137 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
138};
139
Alexandre Rames67555f72014-11-18 10:55:16 +0000140class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
141 public:
142 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
143
144 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
145 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
146 __ Bind(GetEntryLabel());
147 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000148 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800149 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000150 }
151
152 private:
153 HDivZeroCheck* const instruction_;
154 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
155};
156
157class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
158 public:
159 LoadClassSlowPathARM64(HLoadClass* cls,
160 HInstruction* at,
161 uint32_t dex_pc,
162 bool do_clinit)
163 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
164 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
165 }
166
167 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
168 LocationSummary* locations = at_->GetLocations();
169 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
170
171 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000173
174 InvokeRuntimeCallingConvention calling_convention;
175 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000176 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
177 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000178 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800179 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100180 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800181 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100182 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800183 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000184
185 // Move the class to the desired location.
186 Location out = locations->Out();
187 if (out.IsValid()) {
188 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
189 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000190 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000191 }
192
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000193 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000194 __ B(GetExitLabel());
195 }
196
197 private:
198 // The class this slow path will load.
199 HLoadClass* const cls_;
200
201 // The instruction where this slow path is happening.
202 // (Might be the load class or an initialization check).
203 HInstruction* const at_;
204
205 // The dex PC of `at_`.
206 const uint32_t dex_pc_;
207
208 // Whether to initialize the class.
209 const bool do_clinit_;
210
211 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
212};
213
214class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
215 public:
216 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
217
218 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
219 LocationSummary* locations = instruction_->GetLocations();
220 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
221 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
222
223 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000224 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000225
226 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800227 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000228 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000229 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100230 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000231 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000233
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000234 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000235 __ B(GetExitLabel());
236 }
237
238 private:
239 HLoadString* const instruction_;
240
241 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
242};
243
Alexandre Rames5319def2014-10-23 10:03:10 +0100244class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
245 public:
246 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
247
Alexandre Rames67555f72014-11-18 10:55:16 +0000248 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
249 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000252 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800253 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 }
255
256 private:
257 HNullCheck* const instruction_;
258
259 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
260};
261
262class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
263 public:
264 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
265 HBasicBlock* successor)
266 : instruction_(instruction), successor_(successor) {}
267
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100270 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000272 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000273 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000275 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 if (successor_ == nullptr) {
277 __ B(GetReturnLabel());
278 } else {
279 __ B(arm64_codegen->GetLabelOf(successor_));
280 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 }
282
283 vixl::Label* GetReturnLabel() {
284 DCHECK(successor_ == nullptr);
285 return &return_label_;
286 }
287
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100288 HBasicBlock* GetSuccessor() const {
289 return successor_;
290 }
291
Alexandre Rames5319def2014-10-23 10:03:10 +0100292 private:
293 HSuspendCheck* const instruction_;
294 // If not null, the block to branch to after the suspend check.
295 HBasicBlock* const successor_;
296
297 // If `successor_` is null, the label to branch to after the suspend check.
298 vixl::Label return_label_;
299
300 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
301};
302
Alexandre Rames67555f72014-11-18 10:55:16 +0000303class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
304 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000305 TypeCheckSlowPathARM64(HInstruction* instruction,
306 Location class_to_check,
307 Location object_class,
308 uint32_t dex_pc)
309 : instruction_(instruction),
310 class_to_check_(class_to_check),
311 object_class_(object_class),
312 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000313
314 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000315 LocationSummary* locations = instruction_->GetLocations();
316 DCHECK(instruction_->IsCheckCast()
317 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
318 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
319
Alexandre Rames67555f72014-11-18 10:55:16 +0000320 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000321 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000322
323 // We're moving two locations to locations that could overlap, so we need a parallel
324 // move resolver.
325 InvokeRuntimeCallingConvention calling_convention;
326 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100327 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
328 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000329
330 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000331 arm64_codegen->InvokeRuntime(
332 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000333 Primitive::Type ret_type = instruction_->GetType();
334 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
335 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800336 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
337 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000338 } else {
339 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000340 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800341 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000342 }
343
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000344 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000345 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 }
347
348 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000349 HInstruction* const instruction_;
350 const Location class_to_check_;
351 const Location object_class_;
352 uint32_t dex_pc_;
353
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
355};
356
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700357class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
358 public:
359 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
360 : instruction_(instruction) {}
361
362 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
363 __ Bind(GetEntryLabel());
364 SaveLiveRegisters(codegen, instruction_->GetLocations());
365 DCHECK(instruction_->IsDeoptimize());
366 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
367 uint32_t dex_pc = deoptimize->GetDexPc();
368 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
369 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
370 }
371
372 private:
373 HInstruction* const instruction_;
374 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
375};
376
Alexandre Rames5319def2014-10-23 10:03:10 +0100377#undef __
378
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100379Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 Location next_location;
381 if (type == Primitive::kPrimVoid) {
382 LOG(FATAL) << "Unreachable type " << type;
383 }
384
Alexandre Rames542361f2015-01-29 16:57:31 +0000385 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100386 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
387 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000388 } else if (!Primitive::IsFloatingPointType(type) &&
389 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000390 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
391 } else {
392 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000393 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
394 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100395 }
396
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000397 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000398 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100399 return next_location;
400}
401
Serban Constantinescu579885a2015-02-22 20:51:33 +0000402CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
403 const Arm64InstructionSetFeatures& isa_features,
404 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100405 : CodeGenerator(graph,
406 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000407 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000408 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000409 callee_saved_core_registers.list(),
410 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000411 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100412 block_labels_(nullptr),
413 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000414 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000415 move_resolver_(graph->GetArena(), this),
416 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000417 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000418 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000419}
Alexandre Rames5319def2014-10-23 10:03:10 +0100420
Alexandre Rames67555f72014-11-18 10:55:16 +0000421#undef __
422#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100423
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000424void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
425 // Ensure we emit the literal pool.
426 __ FinalizeCode();
427 CodeGenerator::Finalize(allocator);
428}
429
Zheng Xuad4450e2015-04-17 18:48:56 +0800430void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
431 // Note: There are 6 kinds of moves:
432 // 1. constant -> GPR/FPR (non-cycle)
433 // 2. constant -> stack (non-cycle)
434 // 3. GPR/FPR -> GPR/FPR
435 // 4. GPR/FPR -> stack
436 // 5. stack -> GPR/FPR
437 // 6. stack -> stack (non-cycle)
438 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
439 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
440 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
441 // dependency.
442 vixl_temps_.Open(GetVIXLAssembler());
443}
444
445void ParallelMoveResolverARM64::FinishEmitNativeCode() {
446 vixl_temps_.Close();
447}
448
449Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
450 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
451 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
452 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
453 Location scratch = GetScratchLocation(kind);
454 if (!scratch.Equals(Location::NoLocation())) {
455 return scratch;
456 }
457 // Allocate from VIXL temp registers.
458 if (kind == Location::kRegister) {
459 scratch = LocationFrom(vixl_temps_.AcquireX());
460 } else {
461 DCHECK(kind == Location::kFpuRegister);
462 scratch = LocationFrom(vixl_temps_.AcquireD());
463 }
464 AddScratchLocation(scratch);
465 return scratch;
466}
467
468void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
469 if (loc.IsRegister()) {
470 vixl_temps_.Release(XRegisterFrom(loc));
471 } else {
472 DCHECK(loc.IsFpuRegister());
473 vixl_temps_.Release(DRegisterFrom(loc));
474 }
475 RemoveScratchLocation(loc);
476}
477
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478void ParallelMoveResolverARM64::EmitMove(size_t index) {
479 MoveOperands* move = moves_.Get(index);
480 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
481}
482
Alexandre Rames5319def2014-10-23 10:03:10 +0100483void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100484 MacroAssembler* masm = GetVIXLAssembler();
485 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000486 __ Bind(&frame_entry_label_);
487
Serban Constantinescu02164b32014-11-13 14:05:07 +0000488 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
489 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100490 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000491 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000492 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000493 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000494 __ Ldr(wzr, MemOperand(temp, 0));
495 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000496 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100497
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000498 if (!HasEmptyFrame()) {
499 int frame_size = GetFrameSize();
500 // Stack layout:
501 // sp[frame_size - 8] : lr.
502 // ... : other preserved core registers.
503 // ... : other preserved fp registers.
504 // ... : reserved frame space.
505 // sp[0] : current method.
506 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100507 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800508 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
509 frame_size - GetCoreSpillSize());
510 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
511 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000512 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100513}
514
515void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100516 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100517 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000518 if (!HasEmptyFrame()) {
519 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800520 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
521 frame_size - FrameEntrySpillSize());
522 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
523 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000524 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100525 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000526 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100527 __ Ret();
528 GetAssembler()->cfi().RestoreState();
529 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100530}
531
532void CodeGeneratorARM64::Bind(HBasicBlock* block) {
533 __ Bind(GetLabelOf(block));
534}
535
Alexandre Rames5319def2014-10-23 10:03:10 +0100536void CodeGeneratorARM64::Move(HInstruction* instruction,
537 Location location,
538 HInstruction* move_for) {
539 LocationSummary* locations = instruction->GetLocations();
540 if (locations != nullptr && locations->Out().Equals(location)) {
541 return;
542 }
543
544 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000545 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100546
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000547 if (instruction->IsIntConstant()
548 || instruction->IsLongConstant()
549 || instruction->IsNullConstant()) {
550 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100551 if (location.IsRegister()) {
552 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000553 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100554 (instruction->IsLongConstant() && dst.Is64Bits()));
555 __ Mov(dst, value);
556 } else {
557 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000558 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000559 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
560 ? temps.AcquireW()
561 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100562 __ Mov(temp, value);
563 __ Str(temp, StackOperandFrom(location));
564 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000565 } else if (instruction->IsTemporary()) {
566 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000567 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100568 } else if (instruction->IsLoadLocal()) {
569 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000570 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000571 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000572 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000573 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100574 }
575
576 } else {
577 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000578 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100579 }
580}
581
Alexandre Rames5319def2014-10-23 10:03:10 +0100582Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
583 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000584
Alexandre Rames5319def2014-10-23 10:03:10 +0100585 switch (type) {
586 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000587 case Primitive::kPrimInt:
588 case Primitive::kPrimFloat:
589 return Location::StackSlot(GetStackSlot(load->GetLocal()));
590
591 case Primitive::kPrimLong:
592 case Primitive::kPrimDouble:
593 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
594
Alexandre Rames5319def2014-10-23 10:03:10 +0100595 case Primitive::kPrimBoolean:
596 case Primitive::kPrimByte:
597 case Primitive::kPrimChar:
598 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100599 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100600 LOG(FATAL) << "Unexpected type " << type;
601 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000602
Alexandre Rames5319def2014-10-23 10:03:10 +0100603 LOG(FATAL) << "Unreachable";
604 return Location::NoLocation();
605}
606
607void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000608 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100609 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000610 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100611 vixl::Label done;
612 __ Cbz(value, &done);
613 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
614 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000615 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100616 __ Bind(&done);
617}
618
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000619void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
620 // Blocked core registers:
621 // lr : Runtime reserved.
622 // tr : Runtime reserved.
623 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
624 // ip1 : VIXL core temp.
625 // ip0 : VIXL core temp.
626 //
627 // Blocked fp registers:
628 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100629 CPURegList reserved_core_registers = vixl_reserved_core_registers;
630 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100631 while (!reserved_core_registers.IsEmpty()) {
632 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
633 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000634
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000635 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800636 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000637 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
638 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000639
640 if (is_baseline) {
641 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
642 while (!reserved_core_baseline_registers.IsEmpty()) {
643 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
644 }
645
646 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
647 while (!reserved_fp_baseline_registers.IsEmpty()) {
648 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
649 }
650 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100651}
652
653Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
654 if (type == Primitive::kPrimVoid) {
655 LOG(FATAL) << "Unreachable type " << type;
656 }
657
Alexandre Rames542361f2015-01-29 16:57:31 +0000658 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000659 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
660 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100661 return Location::FpuRegisterLocation(reg);
662 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000663 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
664 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100665 return Location::RegisterLocation(reg);
666 }
667}
668
Alexandre Rames3e69f162014-12-10 10:36:50 +0000669size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
670 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
671 __ Str(reg, MemOperand(sp, stack_index));
672 return kArm64WordSize;
673}
674
675size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
676 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
677 __ Ldr(reg, MemOperand(sp, stack_index));
678 return kArm64WordSize;
679}
680
681size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
682 FPRegister reg = FPRegister(reg_id, kDRegSize);
683 __ Str(reg, MemOperand(sp, stack_index));
684 return kArm64WordSize;
685}
686
687size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
688 FPRegister reg = FPRegister(reg_id, kDRegSize);
689 __ Ldr(reg, MemOperand(sp, stack_index));
690 return kArm64WordSize;
691}
692
Alexandre Rames5319def2014-10-23 10:03:10 +0100693void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100694 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100695}
696
697void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100698 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100699}
700
Alexandre Rames67555f72014-11-18 10:55:16 +0000701void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000702 if (constant->IsIntConstant()) {
703 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
704 } else if (constant->IsLongConstant()) {
705 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
706 } else if (constant->IsNullConstant()) {
707 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000708 } else if (constant->IsFloatConstant()) {
709 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
710 } else {
711 DCHECK(constant->IsDoubleConstant());
712 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
713 }
714}
715
Alexandre Rames3e69f162014-12-10 10:36:50 +0000716
717static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
718 DCHECK(constant.IsConstant());
719 HConstant* cst = constant.GetConstant();
720 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000721 // Null is mapped to a core W register, which we associate with kPrimInt.
722 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000723 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
724 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
725 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
726}
727
728void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000729 if (source.Equals(destination)) {
730 return;
731 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000732
733 // A valid move can always be inferred from the destination and source
734 // locations. When moving from and to a register, the argument type can be
735 // used to generate 32bit instead of 64bit moves. In debug mode we also
736 // checks the coherency of the locations and the type.
737 bool unspecified_type = (type == Primitive::kPrimVoid);
738
739 if (destination.IsRegister() || destination.IsFpuRegister()) {
740 if (unspecified_type) {
741 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
742 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000743 (src_cst != nullptr && (src_cst->IsIntConstant()
744 || src_cst->IsFloatConstant()
745 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000746 // For stack slots and 32bit constants, a 64bit type is appropriate.
747 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000748 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000749 // If the source is a double stack slot or a 64bit constant, a 64bit
750 // type is appropriate. Else the source is a register, and since the
751 // type has not been specified, we chose a 64bit type to force a 64bit
752 // move.
753 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000754 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000755 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000756 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
757 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000758 CPURegister dst = CPURegisterFrom(destination, type);
759 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
760 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
761 __ Ldr(dst, StackOperandFrom(source));
762 } else if (source.IsConstant()) {
763 DCHECK(CoherentConstantAndType(source, type));
764 MoveConstant(dst, source.GetConstant());
765 } else {
766 if (destination.IsRegister()) {
767 __ Mov(Register(dst), RegisterFrom(source, type));
768 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800769 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000770 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
771 }
772 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000773 } else { // The destination is not a register. It must be a stack slot.
774 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
775 if (source.IsRegister() || source.IsFpuRegister()) {
776 if (unspecified_type) {
777 if (source.IsRegister()) {
778 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
779 } else {
780 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
781 }
782 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000783 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
784 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000785 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
786 } else if (source.IsConstant()) {
787 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
788 UseScratchRegisterScope temps(GetVIXLAssembler());
789 HConstant* src_cst = source.GetConstant();
790 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000791 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000792 temp = temps.AcquireW();
793 } else if (src_cst->IsLongConstant()) {
794 temp = temps.AcquireX();
795 } else if (src_cst->IsFloatConstant()) {
796 temp = temps.AcquireS();
797 } else {
798 DCHECK(src_cst->IsDoubleConstant());
799 temp = temps.AcquireD();
800 }
801 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000802 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000803 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000804 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000805 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000806 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000807 // There is generally less pressure on FP registers.
808 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000809 __ Ldr(temp, StackOperandFrom(source));
810 __ Str(temp, StackOperandFrom(destination));
811 }
812 }
813}
814
815void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000816 CPURegister dst,
817 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000818 switch (type) {
819 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000820 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000821 break;
822 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000823 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000824 break;
825 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000826 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000827 break;
828 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000829 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000830 break;
831 case Primitive::kPrimInt:
832 case Primitive::kPrimNot:
833 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000834 case Primitive::kPrimFloat:
835 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000836 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000837 __ Ldr(dst, src);
838 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000839 case Primitive::kPrimVoid:
840 LOG(FATAL) << "Unreachable type " << type;
841 }
842}
843
Calin Juravle77520bc2015-01-12 18:45:46 +0000844void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000845 CPURegister dst,
846 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100847 MacroAssembler* masm = GetVIXLAssembler();
848 BlockPoolsScope block_pools(masm);
849 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000850 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000851 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000852
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000853 DCHECK(!src.IsPreIndex());
854 DCHECK(!src.IsPostIndex());
855
856 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800857 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000858 MemOperand base = MemOperand(temp_base);
859 switch (type) {
860 case Primitive::kPrimBoolean:
861 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000862 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000863 break;
864 case Primitive::kPrimByte:
865 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000866 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000867 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
868 break;
869 case Primitive::kPrimChar:
870 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000871 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000872 break;
873 case Primitive::kPrimShort:
874 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000875 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000876 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
877 break;
878 case Primitive::kPrimInt:
879 case Primitive::kPrimNot:
880 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000881 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000882 __ Ldar(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::kPrimFloat:
886 case Primitive::kPrimDouble: {
887 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000888 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000889
890 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
891 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000892 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000893 __ Fmov(FPRegister(dst), temp);
894 break;
895 }
896 case Primitive::kPrimVoid:
897 LOG(FATAL) << "Unreachable type " << type;
898 }
899}
900
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000901void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000902 CPURegister src,
903 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000904 switch (type) {
905 case Primitive::kPrimBoolean:
906 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000907 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000908 break;
909 case Primitive::kPrimChar:
910 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000911 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000912 break;
913 case Primitive::kPrimInt:
914 case Primitive::kPrimNot:
915 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000916 case Primitive::kPrimFloat:
917 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000918 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000919 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000920 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000921 case Primitive::kPrimVoid:
922 LOG(FATAL) << "Unreachable type " << type;
923 }
924}
925
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000926void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
927 CPURegister src,
928 const MemOperand& dst) {
929 UseScratchRegisterScope temps(GetVIXLAssembler());
930 Register temp_base = temps.AcquireX();
931
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000932 DCHECK(!dst.IsPreIndex());
933 DCHECK(!dst.IsPostIndex());
934
935 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800936 Operand op = OperandFromMemOperand(dst);
937 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000938 MemOperand base = MemOperand(temp_base);
939 switch (type) {
940 case Primitive::kPrimBoolean:
941 case Primitive::kPrimByte:
942 __ Stlrb(Register(src), base);
943 break;
944 case Primitive::kPrimChar:
945 case Primitive::kPrimShort:
946 __ Stlrh(Register(src), base);
947 break;
948 case Primitive::kPrimInt:
949 case Primitive::kPrimNot:
950 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000951 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000952 __ Stlr(Register(src), base);
953 break;
954 case Primitive::kPrimFloat:
955 case Primitive::kPrimDouble: {
956 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000957 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000958
959 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
960 __ Fmov(temp, FPRegister(src));
961 __ Stlr(temp, base);
962 break;
963 }
964 case Primitive::kPrimVoid:
965 LOG(FATAL) << "Unreachable type " << type;
966 }
967}
968
Alexandre Rames67555f72014-11-18 10:55:16 +0000969void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000970 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000971 DCHECK(current_method.IsW());
972 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
973}
974
975void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
976 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000977 uint32_t dex_pc,
978 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100979 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +0000980 __ Ldr(lr, MemOperand(tr, entry_point_offset));
981 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +0100982 RecordPcInfo(instruction, dex_pc, slow_path);
983 DCHECK(instruction->IsSuspendCheck()
984 || instruction->IsBoundsCheck()
985 || instruction->IsNullCheck()
986 || instruction->IsDivZeroCheck()
987 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000988}
989
990void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
991 vixl::Register class_reg) {
992 UseScratchRegisterScope temps(GetVIXLAssembler());
993 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +0000995 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000996
Serban Constantinescu02164b32014-11-13 14:05:07 +0000997 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +0000998 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1000 __ Add(temp, class_reg, status_offset);
1001 __ Ldar(temp, HeapOperand(temp));
1002 __ Cmp(temp, mirror::Class::kStatusInitialized);
1003 __ B(lt, slow_path->GetEntryLabel());
1004 } else {
1005 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1006 __ Cmp(temp, mirror::Class::kStatusInitialized);
1007 __ B(lt, slow_path->GetEntryLabel());
1008 __ Dmb(InnerShareable, BarrierReads);
1009 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001010 __ Bind(slow_path->GetExitLabel());
1011}
Alexandre Rames5319def2014-10-23 10:03:10 +01001012
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001013void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1014 BarrierType type = BarrierAll;
1015
1016 switch (kind) {
1017 case MemBarrierKind::kAnyAny:
1018 case MemBarrierKind::kAnyStore: {
1019 type = BarrierAll;
1020 break;
1021 }
1022 case MemBarrierKind::kLoadAny: {
1023 type = BarrierReads;
1024 break;
1025 }
1026 case MemBarrierKind::kStoreStore: {
1027 type = BarrierWrites;
1028 break;
1029 }
1030 default:
1031 LOG(FATAL) << "Unexpected memory barrier " << kind;
1032 }
1033 __ Dmb(InnerShareable, type);
1034}
1035
Serban Constantinescu02164b32014-11-13 14:05:07 +00001036void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1037 HBasicBlock* successor) {
1038 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001039 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1040 if (slow_path == nullptr) {
1041 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1042 instruction->SetSlowPath(slow_path);
1043 codegen_->AddSlowPath(slow_path);
1044 if (successor != nullptr) {
1045 DCHECK(successor->IsLoopHeader());
1046 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1047 }
1048 } else {
1049 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1050 }
1051
Serban Constantinescu02164b32014-11-13 14:05:07 +00001052 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1053 Register temp = temps.AcquireW();
1054
1055 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1056 if (successor == nullptr) {
1057 __ Cbnz(temp, slow_path->GetEntryLabel());
1058 __ Bind(slow_path->GetReturnLabel());
1059 } else {
1060 __ Cbz(temp, codegen_->GetLabelOf(successor));
1061 __ B(slow_path->GetEntryLabel());
1062 // slow_path will return to GetLabelOf(successor).
1063 }
1064}
1065
Alexandre Rames5319def2014-10-23 10:03:10 +01001066InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1067 CodeGeneratorARM64* codegen)
1068 : HGraphVisitor(graph),
1069 assembler_(codegen->GetAssembler()),
1070 codegen_(codegen) {}
1071
1072#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001073 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001074
1075#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1076
1077enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001078 // Using a base helps identify when we hit such breakpoints.
1079 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001080#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1081 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1082#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1083};
1084
1085#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1086 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001087 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001088 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1089 } \
1090 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1092 locations->SetOut(Location::Any()); \
1093 }
1094 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1095#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1096
1097#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001098#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001099
Alexandre Rames67555f72014-11-18 10:55:16 +00001100void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 DCHECK_EQ(instr->InputCount(), 2U);
1102 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1103 Primitive::Type type = instr->GetResultType();
1104 switch (type) {
1105 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001106 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001108 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001110 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001111
1112 case Primitive::kPrimFloat:
1113 case Primitive::kPrimDouble:
1114 locations->SetInAt(0, Location::RequiresFpuRegister());
1115 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001116 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001117 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001118
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001120 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 }
1122}
1123
Alexandre Rames09a99962015-04-15 11:47:56 +01001124void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1127 locations->SetInAt(0, Location::RequiresRegister());
1128 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1129 locations->SetOut(Location::RequiresFpuRegister());
1130 } else {
1131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1132 }
1133}
1134
1135void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1136 const FieldInfo& field_info) {
1137 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001138 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001139
1140 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1141 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1142
1143 if (field_info.IsVolatile()) {
1144 if (use_acquire_release) {
1145 // NB: LoadAcquire will record the pc info if needed.
1146 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1147 } else {
1148 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1149 codegen_->MaybeRecordImplicitNullCheck(instruction);
1150 // For IRIW sequential consistency kLoadAny is not sufficient.
1151 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1152 }
1153 } else {
1154 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1155 codegen_->MaybeRecordImplicitNullCheck(instruction);
1156 }
1157}
1158
1159void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1160 LocationSummary* locations =
1161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1162 locations->SetInAt(0, Location::RequiresRegister());
1163 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1164 locations->SetInAt(1, Location::RequiresFpuRegister());
1165 } else {
1166 locations->SetInAt(1, Location::RequiresRegister());
1167 }
1168}
1169
1170void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
1171 const FieldInfo& field_info) {
1172 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001173 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001174
1175 Register obj = InputRegisterAt(instruction, 0);
1176 CPURegister value = InputCPURegisterAt(instruction, 1);
1177 Offset offset = field_info.GetFieldOffset();
1178 Primitive::Type field_type = field_info.GetFieldType();
1179 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1180
1181 if (field_info.IsVolatile()) {
1182 if (use_acquire_release) {
1183 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1184 codegen_->MaybeRecordImplicitNullCheck(instruction);
1185 } else {
1186 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1187 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1188 codegen_->MaybeRecordImplicitNullCheck(instruction);
1189 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1190 }
1191 } else {
1192 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1193 codegen_->MaybeRecordImplicitNullCheck(instruction);
1194 }
1195
1196 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
1197 codegen_->MarkGCCard(obj, Register(value));
1198 }
1199}
1200
Alexandre Rames67555f72014-11-18 10:55:16 +00001201void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001202 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001203
1204 switch (type) {
1205 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001206 case Primitive::kPrimLong: {
1207 Register dst = OutputRegister(instr);
1208 Register lhs = InputRegisterAt(instr, 0);
1209 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001210 if (instr->IsAdd()) {
1211 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001212 } else if (instr->IsAnd()) {
1213 __ And(dst, lhs, rhs);
1214 } else if (instr->IsOr()) {
1215 __ Orr(dst, lhs, rhs);
1216 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001217 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001218 } else {
1219 DCHECK(instr->IsXor());
1220 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001221 }
1222 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001223 }
1224 case Primitive::kPrimFloat:
1225 case Primitive::kPrimDouble: {
1226 FPRegister dst = OutputFPRegister(instr);
1227 FPRegister lhs = InputFPRegisterAt(instr, 0);
1228 FPRegister rhs = InputFPRegisterAt(instr, 1);
1229 if (instr->IsAdd()) {
1230 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001231 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001232 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001233 } else {
1234 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001235 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001236 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001237 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001238 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001239 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001240 }
1241}
1242
Serban Constantinescu02164b32014-11-13 14:05:07 +00001243void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1244 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1245
1246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1247 Primitive::Type type = instr->GetResultType();
1248 switch (type) {
1249 case Primitive::kPrimInt:
1250 case Primitive::kPrimLong: {
1251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1253 locations->SetOut(Location::RequiresRegister());
1254 break;
1255 }
1256 default:
1257 LOG(FATAL) << "Unexpected shift type " << type;
1258 }
1259}
1260
1261void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1262 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1263
1264 Primitive::Type type = instr->GetType();
1265 switch (type) {
1266 case Primitive::kPrimInt:
1267 case Primitive::kPrimLong: {
1268 Register dst = OutputRegister(instr);
1269 Register lhs = InputRegisterAt(instr, 0);
1270 Operand rhs = InputOperandAt(instr, 1);
1271 if (rhs.IsImmediate()) {
1272 uint32_t shift_value = (type == Primitive::kPrimInt)
1273 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1274 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1275 if (instr->IsShl()) {
1276 __ Lsl(dst, lhs, shift_value);
1277 } else if (instr->IsShr()) {
1278 __ Asr(dst, lhs, shift_value);
1279 } else {
1280 __ Lsr(dst, lhs, shift_value);
1281 }
1282 } else {
1283 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1284
1285 if (instr->IsShl()) {
1286 __ Lsl(dst, lhs, rhs_reg);
1287 } else if (instr->IsShr()) {
1288 __ Asr(dst, lhs, rhs_reg);
1289 } else {
1290 __ Lsr(dst, lhs, rhs_reg);
1291 }
1292 }
1293 break;
1294 }
1295 default:
1296 LOG(FATAL) << "Unexpected shift operation type " << type;
1297 }
1298}
1299
Alexandre Rames5319def2014-10-23 10:03:10 +01001300void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001301 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001302}
1303
1304void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001305 HandleBinaryOp(instruction);
1306}
1307
1308void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1309 HandleBinaryOp(instruction);
1310}
1311
1312void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1313 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001314}
1315
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1317 LocationSummary* locations =
1318 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1319 locations->SetInAt(0, Location::RequiresRegister());
1320 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001321 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1322 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1323 } else {
1324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1325 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001326}
1327
1328void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1329 LocationSummary* locations = instruction->GetLocations();
1330 Primitive::Type type = instruction->GetType();
1331 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001332 Location index = locations->InAt(1);
1333 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001334 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001335 MacroAssembler* masm = GetVIXLAssembler();
1336 UseScratchRegisterScope temps(masm);
1337 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001338
1339 if (index.IsConstant()) {
1340 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001341 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001342 } else {
1343 Register temp = temps.AcquireSameSizeAs(obj);
1344 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1345 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001346 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001347 }
1348
Alexandre Rames67555f72014-11-18 10:55:16 +00001349 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001350 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001351}
1352
Alexandre Rames5319def2014-10-23 10:03:10 +01001353void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1354 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1355 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001357}
1358
1359void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001360 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001361 __ Ldr(OutputRegister(instruction),
1362 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001363 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001364}
1365
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001366void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001367 if (instruction->NeedsTypeCheck()) {
1368 LocationSummary* locations =
1369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001370 InvokeRuntimeCallingConvention calling_convention;
1371 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1372 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1373 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1374 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001375 LocationSummary* locations =
1376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001377 locations->SetInAt(0, Location::RequiresRegister());
1378 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001379 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1380 locations->SetInAt(2, Location::RequiresFpuRegister());
1381 } else {
1382 locations->SetInAt(2, Location::RequiresRegister());
1383 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001384 }
1385}
1386
1387void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1388 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001389 LocationSummary* locations = instruction->GetLocations();
1390 bool needs_runtime_call = locations->WillCall();
1391
1392 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001393 codegen_->InvokeRuntime(
1394 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001395 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001396 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001397 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001398 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001399 Location index = locations->InAt(1);
1400 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001401 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001402 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001403 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001404 {
1405 // We use a block to end the scratch scope before the write barrier, thus
1406 // freeing the temporary registers so they can be used in `MarkGCCard`.
1407 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001408
Alexandre Rames97833a02015-04-16 15:07:12 +01001409 if (index.IsConstant()) {
1410 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1411 destination = HeapOperand(obj, offset);
1412 } else {
1413 Register temp = temps.AcquireSameSizeAs(obj);
1414 Register index_reg = InputRegisterAt(instruction, 1);
1415 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1416 destination = HeapOperand(temp, offset);
1417 }
1418
1419 codegen_->Store(value_type, value, destination);
1420 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001422 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
1423 codegen_->MarkGCCard(obj, value.W());
1424 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001425 }
1426}
1427
Alexandre Rames67555f72014-11-18 10:55:16 +00001428void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1429 LocationSummary* locations =
1430 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1431 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001432 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001433 if (instruction->HasUses()) {
1434 locations->SetOut(Location::SameAsFirstInput());
1435 }
1436}
1437
1438void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001439 LocationSummary* locations = instruction->GetLocations();
1440 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1441 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001442 codegen_->AddSlowPath(slow_path);
1443
1444 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1445 __ B(slow_path->GetEntryLabel(), hs);
1446}
1447
1448void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1450 instruction, LocationSummary::kCallOnSlowPath);
1451 locations->SetInAt(0, Location::RequiresRegister());
1452 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001454}
1455
1456void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001457 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001458 Register obj = InputRegisterAt(instruction, 0);;
1459 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001460 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001461
Alexandre Rames3e69f162014-12-10 10:36:50 +00001462 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1463 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001464 codegen_->AddSlowPath(slow_path);
1465
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001466 // Avoid null check if we know obj is not null.
1467 if (instruction->MustDoNullCheck()) {
1468 __ Cbz(obj, slow_path->GetExitLabel());
1469 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001470 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001471 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1472 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001473 __ B(ne, slow_path->GetEntryLabel());
1474 __ Bind(slow_path->GetExitLabel());
1475}
1476
1477void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1478 LocationSummary* locations =
1479 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1480 locations->SetInAt(0, Location::RequiresRegister());
1481 if (check->HasUses()) {
1482 locations->SetOut(Location::SameAsFirstInput());
1483 }
1484}
1485
1486void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1487 // We assume the class is not null.
1488 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1489 check->GetLoadClass(), check, check->GetDexPc(), true);
1490 codegen_->AddSlowPath(slow_path);
1491 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1492}
1493
Serban Constantinescu02164b32014-11-13 14:05:07 +00001494void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001495 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001496 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1497 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001498 switch (in_type) {
1499 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001500 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001501 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001502 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1503 break;
1504 }
1505 case Primitive::kPrimFloat:
1506 case Primitive::kPrimDouble: {
1507 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001508 HInstruction* right = compare->InputAt(1);
1509 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1510 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1511 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1512 } else {
1513 locations->SetInAt(1, Location::RequiresFpuRegister());
1514 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001515 locations->SetOut(Location::RequiresRegister());
1516 break;
1517 }
1518 default:
1519 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1520 }
1521}
1522
1523void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1524 Primitive::Type in_type = compare->InputAt(0)->GetType();
1525
1526 // 0 if: left == right
1527 // 1 if: left > right
1528 // -1 if: left < right
1529 switch (in_type) {
1530 case Primitive::kPrimLong: {
1531 Register result = OutputRegister(compare);
1532 Register left = InputRegisterAt(compare, 0);
1533 Operand right = InputOperandAt(compare, 1);
1534
1535 __ Cmp(left, right);
1536 __ Cset(result, ne);
1537 __ Cneg(result, result, lt);
1538 break;
1539 }
1540 case Primitive::kPrimFloat:
1541 case Primitive::kPrimDouble: {
1542 Register result = OutputRegister(compare);
1543 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001544 if (compare->GetLocations()->InAt(1).IsConstant()) {
1545 if (kIsDebugBuild) {
1546 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1547 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1548 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1549 }
1550 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1551 __ Fcmp(left, 0.0);
1552 } else {
1553 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1554 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001555 if (compare->IsGtBias()) {
1556 __ Cset(result, ne);
1557 } else {
1558 __ Csetm(result, ne);
1559 }
1560 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001561 break;
1562 }
1563 default:
1564 LOG(FATAL) << "Unimplemented compare type " << in_type;
1565 }
1566}
1567
1568void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1570 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001571 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001572 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001574 }
1575}
1576
1577void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1578 if (!instruction->NeedsMaterialization()) {
1579 return;
1580 }
1581
1582 LocationSummary* locations = instruction->GetLocations();
1583 Register lhs = InputRegisterAt(instruction, 0);
1584 Operand rhs = InputOperandAt(instruction, 1);
1585 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1586 Condition cond = ARM64Condition(instruction->GetCondition());
1587
1588 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001589 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001590}
1591
1592#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1593 M(Equal) \
1594 M(NotEqual) \
1595 M(LessThan) \
1596 M(LessThanOrEqual) \
1597 M(GreaterThan) \
1598 M(GreaterThanOrEqual)
1599#define DEFINE_CONDITION_VISITORS(Name) \
1600void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1601void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1602FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001603#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001604#undef FOR_EACH_CONDITION_INSTRUCTION
1605
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001606void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1607 LocationSummary* locations =
1608 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1609 switch (div->GetResultType()) {
1610 case Primitive::kPrimInt:
1611 case Primitive::kPrimLong:
1612 locations->SetInAt(0, Location::RequiresRegister());
1613 locations->SetInAt(1, Location::RequiresRegister());
1614 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1615 break;
1616
1617 case Primitive::kPrimFloat:
1618 case Primitive::kPrimDouble:
1619 locations->SetInAt(0, Location::RequiresFpuRegister());
1620 locations->SetInAt(1, Location::RequiresFpuRegister());
1621 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1622 break;
1623
1624 default:
1625 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1626 }
1627}
1628
1629void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1630 Primitive::Type type = div->GetResultType();
1631 switch (type) {
1632 case Primitive::kPrimInt:
1633 case Primitive::kPrimLong:
1634 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1635 break;
1636
1637 case Primitive::kPrimFloat:
1638 case Primitive::kPrimDouble:
1639 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1640 break;
1641
1642 default:
1643 LOG(FATAL) << "Unexpected div type " << type;
1644 }
1645}
1646
Alexandre Rames67555f72014-11-18 10:55:16 +00001647void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1648 LocationSummary* locations =
1649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1650 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1651 if (instruction->HasUses()) {
1652 locations->SetOut(Location::SameAsFirstInput());
1653 }
1654}
1655
1656void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1657 SlowPathCodeARM64* slow_path =
1658 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1659 codegen_->AddSlowPath(slow_path);
1660 Location value = instruction->GetLocations()->InAt(0);
1661
Alexandre Rames3e69f162014-12-10 10:36:50 +00001662 Primitive::Type type = instruction->GetType();
1663
1664 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1665 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1666 return;
1667 }
1668
Alexandre Rames67555f72014-11-18 10:55:16 +00001669 if (value.IsConstant()) {
1670 int64_t divisor = Int64ConstantFrom(value);
1671 if (divisor == 0) {
1672 __ B(slow_path->GetEntryLabel());
1673 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001674 // A division by a non-null constant is valid. We don't need to perform
1675 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001676 }
1677 } else {
1678 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1679 }
1680}
1681
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001682void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1683 LocationSummary* locations =
1684 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1685 locations->SetOut(Location::ConstantLocation(constant));
1686}
1687
1688void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1689 UNUSED(constant);
1690 // Will be generated at use site.
1691}
1692
Alexandre Rames5319def2014-10-23 10:03:10 +01001693void LocationsBuilderARM64::VisitExit(HExit* exit) {
1694 exit->SetLocations(nullptr);
1695}
1696
1697void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001698 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001699}
1700
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001701void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1702 LocationSummary* locations =
1703 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1704 locations->SetOut(Location::ConstantLocation(constant));
1705}
1706
1707void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1708 UNUSED(constant);
1709 // Will be generated at use site.
1710}
1711
Alexandre Rames5319def2014-10-23 10:03:10 +01001712void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1713 got->SetLocations(nullptr);
1714}
1715
1716void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1717 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001718 DCHECK(!successor->IsExitBlock());
1719 HBasicBlock* block = got->GetBlock();
1720 HInstruction* previous = got->GetPrevious();
1721 HLoopInformation* info = block->GetLoopInformation();
1722
David Brazdil46e2a392015-03-16 17:31:52 +00001723 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001724 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1725 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1726 return;
1727 }
1728 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1729 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1730 }
1731 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001732 __ B(codegen_->GetLabelOf(successor));
1733 }
1734}
1735
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001736void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1737 vixl::Label* true_target,
1738 vixl::Label* false_target,
1739 vixl::Label* always_true_target) {
1740 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001741 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001742
Serban Constantinescu02164b32014-11-13 14:05:07 +00001743 if (cond->IsIntConstant()) {
1744 int32_t cond_value = cond->AsIntConstant()->GetValue();
1745 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001746 if (always_true_target != nullptr) {
1747 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001748 }
1749 return;
1750 } else {
1751 DCHECK_EQ(cond_value, 0);
1752 }
1753 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001754 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001755 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001756 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001757 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001758 } else {
1759 // The condition instruction has not been materialized, use its inputs as
1760 // the comparison and its condition as the branch condition.
1761 Register lhs = InputRegisterAt(condition, 0);
1762 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001763 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00001764 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1765 switch (arm64_cond) {
1766 case eq:
1767 __ Cbz(lhs, true_target);
1768 break;
1769 case ne:
1770 __ Cbnz(lhs, true_target);
1771 break;
1772 case lt:
1773 // Test the sign bit and branch accordingly.
1774 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1775 break;
1776 case ge:
1777 // Test the sign bit and branch accordingly.
1778 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1779 break;
1780 default:
1781 // Without the `static_cast` the compiler throws an error for
1782 // `-Werror=sign-promo`.
1783 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001784 }
1785 } else {
1786 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001787 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001788 }
1789 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001790 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001791 __ B(false_target);
1792 }
1793}
1794
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001795void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1796 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1797 HInstruction* cond = if_instr->InputAt(0);
1798 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1799 locations->SetInAt(0, Location::RequiresRegister());
1800 }
1801}
1802
1803void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1804 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1805 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1806 vixl::Label* always_true_target = true_target;
1807 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1808 if_instr->IfTrueSuccessor())) {
1809 always_true_target = nullptr;
1810 }
1811 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1812 if_instr->IfFalseSuccessor())) {
1813 false_target = nullptr;
1814 }
1815 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1816}
1817
1818void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1819 LocationSummary* locations = new (GetGraph()->GetArena())
1820 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1821 HInstruction* cond = deoptimize->InputAt(0);
1822 DCHECK(cond->IsCondition());
1823 if (cond->AsCondition()->NeedsMaterialization()) {
1824 locations->SetInAt(0, Location::RequiresRegister());
1825 }
1826}
1827
1828void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1829 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
1830 DeoptimizationSlowPathARM64(deoptimize);
1831 codegen_->AddSlowPath(slow_path);
1832 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
1833 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1834}
1835
Alexandre Rames5319def2014-10-23 10:03:10 +01001836void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001837 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001838}
1839
1840void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001841 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001842}
1843
1844void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001845 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001846}
1847
1848void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001849 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001850}
1851
Alexandre Rames67555f72014-11-18 10:55:16 +00001852void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1853 LocationSummary::CallKind call_kind =
1854 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1856 locations->SetInAt(0, Location::RequiresRegister());
1857 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001858 // The output does overlap inputs.
1859 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001860}
1861
1862void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1863 LocationSummary* locations = instruction->GetLocations();
1864 Register obj = InputRegisterAt(instruction, 0);;
1865 Register cls = InputRegisterAt(instruction, 1);;
1866 Register out = OutputRegister(instruction);
1867
1868 vixl::Label done;
1869
1870 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001871 // Avoid null check if we know `obj` is not null.
1872 if (instruction->MustDoNullCheck()) {
1873 __ Mov(out, 0);
1874 __ Cbz(obj, &done);
1875 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001876
1877 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001878 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001879 __ Cmp(out, cls);
1880 if (instruction->IsClassFinal()) {
1881 // Classes must be equal for the instanceof to succeed.
1882 __ Cset(out, eq);
1883 } else {
1884 // If the classes are not equal, we go into a slow path.
1885 DCHECK(locations->OnlyCallsOnSlowPath());
1886 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001887 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1888 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001889 codegen_->AddSlowPath(slow_path);
1890 __ B(ne, slow_path->GetEntryLabel());
1891 __ Mov(out, 1);
1892 __ Bind(slow_path->GetExitLabel());
1893 }
1894
1895 __ Bind(&done);
1896}
1897
Alexandre Rames5319def2014-10-23 10:03:10 +01001898void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1900 locations->SetOut(Location::ConstantLocation(constant));
1901}
1902
1903void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1904 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001905 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001906}
1907
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001908void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1910 locations->SetOut(Location::ConstantLocation(constant));
1911}
1912
1913void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1914 // Will be generated at use site.
1915 UNUSED(constant);
1916}
1917
Alexandre Rames5319def2014-10-23 10:03:10 +01001918void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1919 LocationSummary* locations =
1920 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1921 locations->AddTemp(LocationFrom(x0));
1922
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001923 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001924 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001925 HInstruction* input = invoke->InputAt(i);
1926 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1927 }
1928
1929 Primitive::Type return_type = invoke->GetType();
1930 if (return_type != Primitive::kPrimVoid) {
1931 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1932 }
1933}
1934
Alexandre Rames67555f72014-11-18 10:55:16 +00001935void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1936 HandleInvoke(invoke);
1937}
1938
1939void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1940 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1941 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1942 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1943 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1944 Location receiver = invoke->GetLocations()->InAt(0);
1945 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001946 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001947
1948 // The register ip1 is required to be used for the hidden argument in
1949 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01001950 MacroAssembler* masm = GetVIXLAssembler();
1951 UseScratchRegisterScope scratch_scope(masm);
1952 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00001953 scratch_scope.Exclude(ip1);
1954 __ Mov(ip1, invoke->GetDexMethodIndex());
1955
1956 // temp = object->GetClass();
1957 if (receiver.IsStackSlot()) {
1958 __ Ldr(temp, StackOperandFrom(receiver));
1959 __ Ldr(temp, HeapOperand(temp, class_offset));
1960 } else {
1961 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1962 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001963 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001964 // temp = temp->GetImtEntryAt(method_offset);
1965 __ Ldr(temp, HeapOperand(temp, method_offset));
1966 // lr = temp->GetEntryPoint();
1967 __ Ldr(lr, HeapOperand(temp, entry_point));
1968 // lr();
1969 __ Blr(lr);
1970 DCHECK(!codegen_->IsLeafMethod());
1971 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1972}
1973
1974void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001975 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1976 if (intrinsic.TryDispatch(invoke)) {
1977 return;
1978 }
1979
Alexandre Rames67555f72014-11-18 10:55:16 +00001980 HandleInvoke(invoke);
1981}
1982
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001983void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001984 // When we do not run baseline, explicit clinit checks triggered by static
1985 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1986 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001987
Andreas Gampe878d58c2015-01-15 23:24:00 -08001988 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1989 if (intrinsic.TryDispatch(invoke)) {
1990 return;
1991 }
1992
Alexandre Rames67555f72014-11-18 10:55:16 +00001993 HandleInvoke(invoke);
1994}
1995
Andreas Gampe878d58c2015-01-15 23:24:00 -08001996static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1997 if (invoke->GetLocations()->Intrinsified()) {
1998 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1999 intrinsic.Dispatch(invoke);
2000 return true;
2001 }
2002 return false;
2003}
2004
2005void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
2006 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
2007 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01002008 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08002009 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01002010
2011 // TODO: Implement all kinds of calls:
2012 // 1) boot -> boot
2013 // 2) app -> boot
2014 // 3) app -> app
2015 //
2016 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2017
Jeff Hao848f70a2014-01-15 13:49:50 -08002018 if (invoke->IsStringInit()) {
2019 // temp = thread->string_init_entrypoint
2020 __ Ldr(temp, HeapOperand(tr, invoke->GetStringInitOffset()));
2021 // LR = temp->entry_point_from_quick_compiled_code_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002022 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2023 kArm64WordSize)));
Jeff Hao848f70a2014-01-15 13:49:50 -08002024 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002025 __ Blr(lr);
2026 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08002027 // temp = method;
2028 LoadCurrentMethod(temp);
2029 if (!invoke->IsRecursive()) {
2030 // temp = temp->dex_cache_resolved_methods_;
2031 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
2032 // temp = temp[index_in_cache];
2033 __ Ldr(temp, HeapOperand(temp, index_in_cache));
2034 // lr = temp->entry_point_from_quick_compiled_code_;
2035 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2036 kArm64WordSize)));
2037 // lr();
2038 __ Blr(lr);
2039 } else {
2040 __ Bl(&frame_entry_label_);
2041 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002042 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002043
Andreas Gampe878d58c2015-01-15 23:24:00 -08002044 DCHECK(!IsLeafMethod());
2045}
2046
2047void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002048 // When we do not run baseline, explicit clinit checks triggered by static
2049 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2050 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002051
Andreas Gampe878d58c2015-01-15 23:24:00 -08002052 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2053 return;
2054 }
2055
Alexandre Ramesd921d642015-04-16 15:07:16 +01002056 BlockPoolsScope block_pools(GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -08002057 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
2058 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002059 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002060}
2061
2062void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002063 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2064 return;
2065 }
2066
Alexandre Rames5319def2014-10-23 10:03:10 +01002067 LocationSummary* locations = invoke->GetLocations();
2068 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002069 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002070 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
2071 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
2072 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002073 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002074
Alexandre Ramesd921d642015-04-16 15:07:16 +01002075 BlockPoolsScope block_pools(GetVIXLAssembler());
2076
Alexandre Rames5319def2014-10-23 10:03:10 +01002077 // temp = object->GetClass();
2078 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002079 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
2080 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002081 } else {
2082 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002083 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002084 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002085 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002086 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002087 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002088 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002089 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002090 // lr();
2091 __ Blr(lr);
2092 DCHECK(!codegen_->IsLeafMethod());
2093 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2094}
2095
Alexandre Rames67555f72014-11-18 10:55:16 +00002096void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2097 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2098 : LocationSummary::kNoCall;
2099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2100 locations->SetOut(Location::RequiresRegister());
2101}
2102
2103void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2104 Register out = OutputRegister(cls);
2105 if (cls->IsReferrersClass()) {
2106 DCHECK(!cls->CanCallRuntime());
2107 DCHECK(!cls->MustGenerateClinitCheck());
2108 codegen_->LoadCurrentMethod(out);
2109 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2110 } else {
2111 DCHECK(cls->CanCallRuntime());
2112 codegen_->LoadCurrentMethod(out);
2113 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002114 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002115
2116 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2117 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2118 codegen_->AddSlowPath(slow_path);
2119 __ Cbz(out, slow_path->GetEntryLabel());
2120 if (cls->MustGenerateClinitCheck()) {
2121 GenerateClassInitializationCheck(slow_path, out);
2122 } else {
2123 __ Bind(slow_path->GetExitLabel());
2124 }
2125 }
2126}
2127
2128void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2129 LocationSummary* locations =
2130 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2131 locations->SetOut(Location::RequiresRegister());
2132}
2133
2134void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2135 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2136 __ Ldr(OutputRegister(instruction), exception);
2137 __ Str(wzr, exception);
2138}
2139
Alexandre Rames5319def2014-10-23 10:03:10 +01002140void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2141 load->SetLocations(nullptr);
2142}
2143
2144void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2145 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002146 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002147}
2148
Alexandre Rames67555f72014-11-18 10:55:16 +00002149void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2150 LocationSummary* locations =
2151 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2152 locations->SetOut(Location::RequiresRegister());
2153}
2154
2155void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2156 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2157 codegen_->AddSlowPath(slow_path);
2158
2159 Register out = OutputRegister(load);
2160 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002161 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2162 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002163 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002164 __ Cbz(out, slow_path->GetEntryLabel());
2165 __ Bind(slow_path->GetExitLabel());
2166}
2167
Alexandre Rames5319def2014-10-23 10:03:10 +01002168void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2169 local->SetLocations(nullptr);
2170}
2171
2172void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2173 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2174}
2175
2176void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2177 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2178 locations->SetOut(Location::ConstantLocation(constant));
2179}
2180
2181void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2182 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002183 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002184}
2185
Alexandre Rames67555f72014-11-18 10:55:16 +00002186void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2189 InvokeRuntimeCallingConvention calling_convention;
2190 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2191}
2192
2193void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2194 codegen_->InvokeRuntime(instruction->IsEnter()
2195 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2196 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002197 instruction->GetDexPc(),
2198 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002199 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002200}
2201
Alexandre Rames42d641b2014-10-27 14:00:51 +00002202void LocationsBuilderARM64::VisitMul(HMul* mul) {
2203 LocationSummary* locations =
2204 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2205 switch (mul->GetResultType()) {
2206 case Primitive::kPrimInt:
2207 case Primitive::kPrimLong:
2208 locations->SetInAt(0, Location::RequiresRegister());
2209 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002211 break;
2212
2213 case Primitive::kPrimFloat:
2214 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002215 locations->SetInAt(0, Location::RequiresFpuRegister());
2216 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002217 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002218 break;
2219
2220 default:
2221 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2222 }
2223}
2224
2225void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2226 switch (mul->GetResultType()) {
2227 case Primitive::kPrimInt:
2228 case Primitive::kPrimLong:
2229 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2230 break;
2231
2232 case Primitive::kPrimFloat:
2233 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002234 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002235 break;
2236
2237 default:
2238 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2239 }
2240}
2241
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002242void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2243 LocationSummary* locations =
2244 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2245 switch (neg->GetResultType()) {
2246 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002247 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002248 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002250 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002251
2252 case Primitive::kPrimFloat:
2253 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002254 locations->SetInAt(0, Location::RequiresFpuRegister());
2255 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002256 break;
2257
2258 default:
2259 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2260 }
2261}
2262
2263void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2264 switch (neg->GetResultType()) {
2265 case Primitive::kPrimInt:
2266 case Primitive::kPrimLong:
2267 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2268 break;
2269
2270 case Primitive::kPrimFloat:
2271 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002272 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002273 break;
2274
2275 default:
2276 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2277 }
2278}
2279
2280void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2281 LocationSummary* locations =
2282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2283 InvokeRuntimeCallingConvention calling_convention;
2284 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002285 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002286 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002287 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2288 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2289 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002290}
2291
2292void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2293 LocationSummary* locations = instruction->GetLocations();
2294 InvokeRuntimeCallingConvention calling_convention;
2295 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2296 DCHECK(type_index.Is(w0));
2297 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002298 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002299 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002300 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002301 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002302 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2303 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002304 instruction->GetDexPc(),
2305 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002306 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2307 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002308}
2309
Alexandre Rames5319def2014-10-23 10:03:10 +01002310void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2311 LocationSummary* locations =
2312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2313 InvokeRuntimeCallingConvention calling_convention;
2314 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2315 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2316 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002317 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002318}
2319
2320void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2321 LocationSummary* locations = instruction->GetLocations();
2322 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2323 DCHECK(type_index.Is(w0));
2324 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2325 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002326 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002327 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002328 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002329 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2330 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002331 instruction->GetDexPc(),
2332 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002333 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002334}
2335
2336void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2337 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002338 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002340}
2341
2342void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002343 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002344 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002345 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002346 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002347 break;
2348
2349 default:
2350 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2351 }
2352}
2353
David Brazdil66d126e2015-04-03 16:02:44 +01002354void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2355 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2356 locations->SetInAt(0, Location::RequiresRegister());
2357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2358}
2359
2360void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002361 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2362}
2363
Alexandre Rames5319def2014-10-23 10:03:10 +01002364void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2365 LocationSummary* locations =
2366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2367 locations->SetInAt(0, Location::RequiresRegister());
2368 if (instruction->HasUses()) {
2369 locations->SetOut(Location::SameAsFirstInput());
2370 }
2371}
2372
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002373void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002374 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2375 return;
2376 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002377
Alexandre Ramesd921d642015-04-16 15:07:16 +01002378 BlockPoolsScope block_pools(GetVIXLAssembler());
2379 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002380 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2381 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2382}
2383
2384void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002385 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2386 codegen_->AddSlowPath(slow_path);
2387
2388 LocationSummary* locations = instruction->GetLocations();
2389 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002390
2391 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002392}
2393
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002394void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2395 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2396 GenerateImplicitNullCheck(instruction);
2397 } else {
2398 GenerateExplicitNullCheck(instruction);
2399 }
2400}
2401
Alexandre Rames67555f72014-11-18 10:55:16 +00002402void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2403 HandleBinaryOp(instruction);
2404}
2405
2406void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2407 HandleBinaryOp(instruction);
2408}
2409
Alexandre Rames3e69f162014-12-10 10:36:50 +00002410void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2411 LOG(FATAL) << "Unreachable";
2412}
2413
2414void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2415 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2416}
2417
Alexandre Rames5319def2014-10-23 10:03:10 +01002418void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2420 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2421 if (location.IsStackSlot()) {
2422 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2423 } else if (location.IsDoubleStackSlot()) {
2424 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2425 }
2426 locations->SetOut(location);
2427}
2428
2429void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2430 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002431 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002432}
2433
2434void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2435 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2436 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2437 locations->SetInAt(i, Location::Any());
2438 }
2439 locations->SetOut(Location::Any());
2440}
2441
2442void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002443 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002444 LOG(FATAL) << "Unreachable";
2445}
2446
Serban Constantinescu02164b32014-11-13 14:05:07 +00002447void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002448 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002449 LocationSummary::CallKind call_kind =
2450 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2452
2453 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002454 case Primitive::kPrimInt:
2455 case Primitive::kPrimLong:
2456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetInAt(1, Location::RequiresRegister());
2458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2459 break;
2460
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002461 case Primitive::kPrimFloat:
2462 case Primitive::kPrimDouble: {
2463 InvokeRuntimeCallingConvention calling_convention;
2464 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2465 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2466 locations->SetOut(calling_convention.GetReturnLocation(type));
2467
2468 break;
2469 }
2470
Serban Constantinescu02164b32014-11-13 14:05:07 +00002471 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002472 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002473 }
2474}
2475
2476void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2477 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002478
Serban Constantinescu02164b32014-11-13 14:05:07 +00002479 switch (type) {
2480 case Primitive::kPrimInt:
2481 case Primitive::kPrimLong: {
2482 UseScratchRegisterScope temps(GetVIXLAssembler());
2483 Register dividend = InputRegisterAt(rem, 0);
2484 Register divisor = InputRegisterAt(rem, 1);
2485 Register output = OutputRegister(rem);
2486 Register temp = temps.AcquireSameSizeAs(output);
2487
2488 __ Sdiv(temp, dividend, divisor);
2489 __ Msub(output, temp, divisor, dividend);
2490 break;
2491 }
2492
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002493 case Primitive::kPrimFloat:
2494 case Primitive::kPrimDouble: {
2495 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2496 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002497 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002498 break;
2499 }
2500
Serban Constantinescu02164b32014-11-13 14:05:07 +00002501 default:
2502 LOG(FATAL) << "Unexpected rem type " << type;
2503 }
2504}
2505
Calin Juravle27df7582015-04-17 19:12:31 +01002506void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2507 memory_barrier->SetLocations(nullptr);
2508}
2509
2510void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2511 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2512}
2513
Alexandre Rames5319def2014-10-23 10:03:10 +01002514void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2516 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002517 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002518}
2519
2520void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002521 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002522 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002523}
2524
2525void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2526 instruction->SetLocations(nullptr);
2527}
2528
2529void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002530 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002531 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002532}
2533
Serban Constantinescu02164b32014-11-13 14:05:07 +00002534void LocationsBuilderARM64::VisitShl(HShl* shl) {
2535 HandleShift(shl);
2536}
2537
2538void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2539 HandleShift(shl);
2540}
2541
2542void LocationsBuilderARM64::VisitShr(HShr* shr) {
2543 HandleShift(shr);
2544}
2545
2546void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2547 HandleShift(shr);
2548}
2549
Alexandre Rames5319def2014-10-23 10:03:10 +01002550void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2552 Primitive::Type field_type = store->InputAt(1)->GetType();
2553 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002554 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002555 case Primitive::kPrimBoolean:
2556 case Primitive::kPrimByte:
2557 case Primitive::kPrimChar:
2558 case Primitive::kPrimShort:
2559 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002560 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002561 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2562 break;
2563
2564 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002565 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002566 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2567 break;
2568
2569 default:
2570 LOG(FATAL) << "Unimplemented local type " << field_type;
2571 }
2572}
2573
2574void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002575 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002576}
2577
2578void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002579 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002580}
2581
2582void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002583 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002584}
2585
Alexandre Rames67555f72014-11-18 10:55:16 +00002586void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002587 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002588}
2589
2590void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002591 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002592}
2593
2594void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002595 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002596}
2597
Alexandre Rames67555f72014-11-18 10:55:16 +00002598void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002599 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002600}
2601
2602void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2604}
2605
2606void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002607 HBasicBlock* block = instruction->GetBlock();
2608 if (block->GetLoopInformation() != nullptr) {
2609 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2610 // The back edge will generate the suspend check.
2611 return;
2612 }
2613 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2614 // The goto will generate the suspend check.
2615 return;
2616 }
2617 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002618}
2619
2620void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2621 temp->SetLocations(nullptr);
2622}
2623
2624void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2625 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002626 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002627}
2628
Alexandre Rames67555f72014-11-18 10:55:16 +00002629void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2630 LocationSummary* locations =
2631 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2632 InvokeRuntimeCallingConvention calling_convention;
2633 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2634}
2635
2636void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2637 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002638 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002639 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002640}
2641
2642void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2643 LocationSummary* locations =
2644 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2645 Primitive::Type input_type = conversion->GetInputType();
2646 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002647 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002648 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2649 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2650 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2651 }
2652
Alexandre Rames542361f2015-01-29 16:57:31 +00002653 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002654 locations->SetInAt(0, Location::RequiresFpuRegister());
2655 } else {
2656 locations->SetInAt(0, Location::RequiresRegister());
2657 }
2658
Alexandre Rames542361f2015-01-29 16:57:31 +00002659 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002660 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2661 } else {
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663 }
2664}
2665
2666void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2667 Primitive::Type result_type = conversion->GetResultType();
2668 Primitive::Type input_type = conversion->GetInputType();
2669
2670 DCHECK_NE(input_type, result_type);
2671
Alexandre Rames542361f2015-01-29 16:57:31 +00002672 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002673 int result_size = Primitive::ComponentSize(result_type);
2674 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002675 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002676 Register output = OutputRegister(conversion);
2677 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002678 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2679 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2680 } else if ((result_type == Primitive::kPrimChar) ||
2681 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2682 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002683 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002684 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002685 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002686 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002687 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002688 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002689 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2690 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002691 } else if (Primitive::IsFloatingPointType(result_type) &&
2692 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002693 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2694 } else {
2695 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2696 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002697 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002698}
Alexandre Rames67555f72014-11-18 10:55:16 +00002699
Serban Constantinescu02164b32014-11-13 14:05:07 +00002700void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2701 HandleShift(ushr);
2702}
2703
2704void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2705 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002706}
2707
2708void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2709 HandleBinaryOp(instruction);
2710}
2711
2712void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2713 HandleBinaryOp(instruction);
2714}
2715
Calin Juravleb1498f62015-02-16 13:13:29 +00002716void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2717 // Nothing to do, this should be removed during prepare for register allocator.
2718 UNUSED(instruction);
2719 LOG(FATAL) << "Unreachable";
2720}
2721
2722void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2723 // Nothing to do, this should be removed during prepare for register allocator.
2724 UNUSED(instruction);
2725 LOG(FATAL) << "Unreachable";
2726}
2727
Alexandre Rames67555f72014-11-18 10:55:16 +00002728#undef __
2729#undef QUICK_ENTRY_POINT
2730
Alexandre Rames5319def2014-10-23 10:03:10 +01002731} // namespace arm64
2732} // namespace art