blob: 9474d99555eedd15633f6d702548d48389c3160d [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
Alexandre Rames5319def2014-10-23 10:03:10 +0100288 private:
289 HSuspendCheck* const instruction_;
290 // If not null, the block to branch to after the suspend check.
291 HBasicBlock* const successor_;
292
293 // If `successor_` is null, the label to branch to after the suspend check.
294 vixl::Label return_label_;
295
296 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
297};
298
Alexandre Rames67555f72014-11-18 10:55:16 +0000299class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
300 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000301 TypeCheckSlowPathARM64(HInstruction* instruction,
302 Location class_to_check,
303 Location object_class,
304 uint32_t dex_pc)
305 : instruction_(instruction),
306 class_to_check_(class_to_check),
307 object_class_(object_class),
308 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000309
310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000311 LocationSummary* locations = instruction_->GetLocations();
312 DCHECK(instruction_->IsCheckCast()
313 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
314 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
315
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000318
319 // We're moving two locations to locations that could overlap, so we need a parallel
320 // move resolver.
321 InvokeRuntimeCallingConvention calling_convention;
322 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100323 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
324 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000325
326 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000327 arm64_codegen->InvokeRuntime(
328 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000329 Primitive::Type ret_type = instruction_->GetType();
330 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
331 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800332 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
333 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000334 } else {
335 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000336 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800337 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000338 }
339
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000340 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000341 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000342 }
343
344 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000345 HInstruction* const instruction_;
346 const Location class_to_check_;
347 const Location object_class_;
348 uint32_t dex_pc_;
349
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
351};
352
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700353class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
354 public:
355 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
356 : instruction_(instruction) {}
357
358 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
359 __ Bind(GetEntryLabel());
360 SaveLiveRegisters(codegen, instruction_->GetLocations());
361 DCHECK(instruction_->IsDeoptimize());
362 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
363 uint32_t dex_pc = deoptimize->GetDexPc();
364 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
365 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
366 }
367
368 private:
369 HInstruction* const instruction_;
370 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
371};
372
Alexandre Rames5319def2014-10-23 10:03:10 +0100373#undef __
374
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100375Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 Location next_location;
377 if (type == Primitive::kPrimVoid) {
378 LOG(FATAL) << "Unreachable type " << type;
379 }
380
Alexandre Rames542361f2015-01-29 16:57:31 +0000381 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100382 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
383 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000384 } else if (!Primitive::IsFloatingPointType(type) &&
385 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000386 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
387 } else {
388 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000389 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
390 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100391 }
392
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000393 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000394 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100395 return next_location;
396}
397
Serban Constantinescu579885a2015-02-22 20:51:33 +0000398CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
399 const Arm64InstructionSetFeatures& isa_features,
400 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100401 : CodeGenerator(graph,
402 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000403 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000404 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000405 callee_saved_core_registers.list(),
406 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000407 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 block_labels_(nullptr),
409 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000410 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000411 move_resolver_(graph->GetArena(), this),
412 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000413 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000414 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000415}
Alexandre Rames5319def2014-10-23 10:03:10 +0100416
Alexandre Rames67555f72014-11-18 10:55:16 +0000417#undef __
418#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100419
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000420void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
421 // Ensure we emit the literal pool.
422 __ FinalizeCode();
423 CodeGenerator::Finalize(allocator);
424}
425
Zheng Xuad4450e2015-04-17 18:48:56 +0800426void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
427 // Note: There are 6 kinds of moves:
428 // 1. constant -> GPR/FPR (non-cycle)
429 // 2. constant -> stack (non-cycle)
430 // 3. GPR/FPR -> GPR/FPR
431 // 4. GPR/FPR -> stack
432 // 5. stack -> GPR/FPR
433 // 6. stack -> stack (non-cycle)
434 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
435 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
436 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
437 // dependency.
438 vixl_temps_.Open(GetVIXLAssembler());
439}
440
441void ParallelMoveResolverARM64::FinishEmitNativeCode() {
442 vixl_temps_.Close();
443}
444
445Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
446 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
447 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
448 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
449 Location scratch = GetScratchLocation(kind);
450 if (!scratch.Equals(Location::NoLocation())) {
451 return scratch;
452 }
453 // Allocate from VIXL temp registers.
454 if (kind == Location::kRegister) {
455 scratch = LocationFrom(vixl_temps_.AcquireX());
456 } else {
457 DCHECK(kind == Location::kFpuRegister);
458 scratch = LocationFrom(vixl_temps_.AcquireD());
459 }
460 AddScratchLocation(scratch);
461 return scratch;
462}
463
464void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
465 if (loc.IsRegister()) {
466 vixl_temps_.Release(XRegisterFrom(loc));
467 } else {
468 DCHECK(loc.IsFpuRegister());
469 vixl_temps_.Release(DRegisterFrom(loc));
470 }
471 RemoveScratchLocation(loc);
472}
473
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474void ParallelMoveResolverARM64::EmitMove(size_t index) {
475 MoveOperands* move = moves_.Get(index);
476 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
477}
478
Alexandre Rames5319def2014-10-23 10:03:10 +0100479void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100480 MacroAssembler* masm = GetVIXLAssembler();
481 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000482 __ Bind(&frame_entry_label_);
483
Serban Constantinescu02164b32014-11-13 14:05:07 +0000484 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
485 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100486 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000487 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000488 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000489 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000490 __ Ldr(wzr, MemOperand(temp, 0));
491 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000492 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100493
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000494 if (!HasEmptyFrame()) {
495 int frame_size = GetFrameSize();
496 // Stack layout:
497 // sp[frame_size - 8] : lr.
498 // ... : other preserved core registers.
499 // ... : other preserved fp registers.
500 // ... : reserved frame space.
501 // sp[0] : current method.
502 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100503 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800504 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
505 frame_size - GetCoreSpillSize());
506 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
507 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000508 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100509}
510
511void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100512 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100513 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000514 if (!HasEmptyFrame()) {
515 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800516 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
517 frame_size - FrameEntrySpillSize());
518 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
519 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000520 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100521 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000522 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100523 __ Ret();
524 GetAssembler()->cfi().RestoreState();
525 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100526}
527
528void CodeGeneratorARM64::Bind(HBasicBlock* block) {
529 __ Bind(GetLabelOf(block));
530}
531
Alexandre Rames5319def2014-10-23 10:03:10 +0100532void CodeGeneratorARM64::Move(HInstruction* instruction,
533 Location location,
534 HInstruction* move_for) {
535 LocationSummary* locations = instruction->GetLocations();
536 if (locations != nullptr && locations->Out().Equals(location)) {
537 return;
538 }
539
540 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000541 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100542
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000543 if (instruction->IsIntConstant()
544 || instruction->IsLongConstant()
545 || instruction->IsNullConstant()) {
546 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100547 if (location.IsRegister()) {
548 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000549 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100550 (instruction->IsLongConstant() && dst.Is64Bits()));
551 __ Mov(dst, value);
552 } else {
553 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000554 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000555 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
556 ? temps.AcquireW()
557 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100558 __ Mov(temp, value);
559 __ Str(temp, StackOperandFrom(location));
560 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000561 } else if (instruction->IsTemporary()) {
562 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000563 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100564 } else if (instruction->IsLoadLocal()) {
565 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000566 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000567 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000568 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000569 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100570 }
571
572 } else {
573 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000574 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100575 }
576}
577
Alexandre Rames5319def2014-10-23 10:03:10 +0100578Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
579 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000580
Alexandre Rames5319def2014-10-23 10:03:10 +0100581 switch (type) {
582 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000583 case Primitive::kPrimInt:
584 case Primitive::kPrimFloat:
585 return Location::StackSlot(GetStackSlot(load->GetLocal()));
586
587 case Primitive::kPrimLong:
588 case Primitive::kPrimDouble:
589 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
590
Alexandre Rames5319def2014-10-23 10:03:10 +0100591 case Primitive::kPrimBoolean:
592 case Primitive::kPrimByte:
593 case Primitive::kPrimChar:
594 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100595 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100596 LOG(FATAL) << "Unexpected type " << type;
597 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000598
Alexandre Rames5319def2014-10-23 10:03:10 +0100599 LOG(FATAL) << "Unreachable";
600 return Location::NoLocation();
601}
602
603void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000604 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100605 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000606 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100607 vixl::Label done;
608 __ Cbz(value, &done);
609 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
610 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000611 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100612 __ Bind(&done);
613}
614
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000615void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
616 // Blocked core registers:
617 // lr : Runtime reserved.
618 // tr : Runtime reserved.
619 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
620 // ip1 : VIXL core temp.
621 // ip0 : VIXL core temp.
622 //
623 // Blocked fp registers:
624 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100625 CPURegList reserved_core_registers = vixl_reserved_core_registers;
626 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100627 while (!reserved_core_registers.IsEmpty()) {
628 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
629 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000630
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000631 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800632 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000633 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
634 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000635
636 if (is_baseline) {
637 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
638 while (!reserved_core_baseline_registers.IsEmpty()) {
639 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
640 }
641
642 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
643 while (!reserved_fp_baseline_registers.IsEmpty()) {
644 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
645 }
646 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100647}
648
649Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
650 if (type == Primitive::kPrimVoid) {
651 LOG(FATAL) << "Unreachable type " << type;
652 }
653
Alexandre Rames542361f2015-01-29 16:57:31 +0000654 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000655 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
656 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100657 return Location::FpuRegisterLocation(reg);
658 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000659 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
660 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100661 return Location::RegisterLocation(reg);
662 }
663}
664
Alexandre Rames3e69f162014-12-10 10:36:50 +0000665size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
666 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
667 __ Str(reg, MemOperand(sp, stack_index));
668 return kArm64WordSize;
669}
670
671size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
672 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
673 __ Ldr(reg, MemOperand(sp, stack_index));
674 return kArm64WordSize;
675}
676
677size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
678 FPRegister reg = FPRegister(reg_id, kDRegSize);
679 __ Str(reg, MemOperand(sp, stack_index));
680 return kArm64WordSize;
681}
682
683size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
684 FPRegister reg = FPRegister(reg_id, kDRegSize);
685 __ Ldr(reg, MemOperand(sp, stack_index));
686 return kArm64WordSize;
687}
688
Alexandre Rames5319def2014-10-23 10:03:10 +0100689void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
690 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
691}
692
693void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
694 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
695}
696
Alexandre Rames67555f72014-11-18 10:55:16 +0000697void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000698 if (constant->IsIntConstant()) {
699 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
700 } else if (constant->IsLongConstant()) {
701 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
702 } else if (constant->IsNullConstant()) {
703 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000704 } else if (constant->IsFloatConstant()) {
705 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
706 } else {
707 DCHECK(constant->IsDoubleConstant());
708 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
709 }
710}
711
Alexandre Rames3e69f162014-12-10 10:36:50 +0000712
713static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
714 DCHECK(constant.IsConstant());
715 HConstant* cst = constant.GetConstant();
716 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000717 // Null is mapped to a core W register, which we associate with kPrimInt.
718 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000719 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
720 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
721 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
722}
723
724void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000725 if (source.Equals(destination)) {
726 return;
727 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000728
729 // A valid move can always be inferred from the destination and source
730 // locations. When moving from and to a register, the argument type can be
731 // used to generate 32bit instead of 64bit moves. In debug mode we also
732 // checks the coherency of the locations and the type.
733 bool unspecified_type = (type == Primitive::kPrimVoid);
734
735 if (destination.IsRegister() || destination.IsFpuRegister()) {
736 if (unspecified_type) {
737 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
738 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000739 (src_cst != nullptr && (src_cst->IsIntConstant()
740 || src_cst->IsFloatConstant()
741 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000742 // For stack slots and 32bit constants, a 64bit type is appropriate.
743 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000744 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000745 // If the source is a double stack slot or a 64bit constant, a 64bit
746 // type is appropriate. Else the source is a register, and since the
747 // type has not been specified, we chose a 64bit type to force a 64bit
748 // move.
749 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000750 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000751 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000752 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
753 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000754 CPURegister dst = CPURegisterFrom(destination, type);
755 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
756 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
757 __ Ldr(dst, StackOperandFrom(source));
758 } else if (source.IsConstant()) {
759 DCHECK(CoherentConstantAndType(source, type));
760 MoveConstant(dst, source.GetConstant());
761 } else {
762 if (destination.IsRegister()) {
763 __ Mov(Register(dst), RegisterFrom(source, type));
764 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800765 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000766 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
767 }
768 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000769 } else { // The destination is not a register. It must be a stack slot.
770 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
771 if (source.IsRegister() || source.IsFpuRegister()) {
772 if (unspecified_type) {
773 if (source.IsRegister()) {
774 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
775 } else {
776 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
777 }
778 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000779 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
780 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000781 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
782 } else if (source.IsConstant()) {
783 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
784 UseScratchRegisterScope temps(GetVIXLAssembler());
785 HConstant* src_cst = source.GetConstant();
786 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000787 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000788 temp = temps.AcquireW();
789 } else if (src_cst->IsLongConstant()) {
790 temp = temps.AcquireX();
791 } else if (src_cst->IsFloatConstant()) {
792 temp = temps.AcquireS();
793 } else {
794 DCHECK(src_cst->IsDoubleConstant());
795 temp = temps.AcquireD();
796 }
797 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000798 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000799 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000800 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000801 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000802 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000803 // There is generally less pressure on FP registers.
804 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000805 __ Ldr(temp, StackOperandFrom(source));
806 __ Str(temp, StackOperandFrom(destination));
807 }
808 }
809}
810
811void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000812 CPURegister dst,
813 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000814 switch (type) {
815 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000816 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000817 break;
818 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000819 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000820 break;
821 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000822 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000823 break;
824 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000825 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000826 break;
827 case Primitive::kPrimInt:
828 case Primitive::kPrimNot:
829 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000830 case Primitive::kPrimFloat:
831 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000832 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000833 __ Ldr(dst, src);
834 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000835 case Primitive::kPrimVoid:
836 LOG(FATAL) << "Unreachable type " << type;
837 }
838}
839
Calin Juravle77520bc2015-01-12 18:45:46 +0000840void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000841 CPURegister dst,
842 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100843 MacroAssembler* masm = GetVIXLAssembler();
844 BlockPoolsScope block_pools(masm);
845 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000846 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000847 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000848
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000849 DCHECK(!src.IsPreIndex());
850 DCHECK(!src.IsPostIndex());
851
852 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800853 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000854 MemOperand base = MemOperand(temp_base);
855 switch (type) {
856 case Primitive::kPrimBoolean:
857 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000858 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000859 break;
860 case Primitive::kPrimByte:
861 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000862 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000863 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
864 break;
865 case Primitive::kPrimChar:
866 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000867 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000868 break;
869 case Primitive::kPrimShort:
870 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000871 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000872 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
873 break;
874 case Primitive::kPrimInt:
875 case Primitive::kPrimNot:
876 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000877 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000878 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000879 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000880 break;
881 case Primitive::kPrimFloat:
882 case Primitive::kPrimDouble: {
883 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000884 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000885
886 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
887 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000888 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000889 __ Fmov(FPRegister(dst), temp);
890 break;
891 }
892 case Primitive::kPrimVoid:
893 LOG(FATAL) << "Unreachable type " << type;
894 }
895}
896
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000897void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000898 CPURegister src,
899 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000900 switch (type) {
901 case Primitive::kPrimBoolean:
902 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000903 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000904 break;
905 case Primitive::kPrimChar:
906 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000907 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000908 break;
909 case Primitive::kPrimInt:
910 case Primitive::kPrimNot:
911 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000912 case Primitive::kPrimFloat:
913 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000914 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000915 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000916 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000917 case Primitive::kPrimVoid:
918 LOG(FATAL) << "Unreachable type " << type;
919 }
920}
921
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000922void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
923 CPURegister src,
924 const MemOperand& dst) {
925 UseScratchRegisterScope temps(GetVIXLAssembler());
926 Register temp_base = temps.AcquireX();
927
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000928 DCHECK(!dst.IsPreIndex());
929 DCHECK(!dst.IsPostIndex());
930
931 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800932 Operand op = OperandFromMemOperand(dst);
933 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000934 MemOperand base = MemOperand(temp_base);
935 switch (type) {
936 case Primitive::kPrimBoolean:
937 case Primitive::kPrimByte:
938 __ Stlrb(Register(src), base);
939 break;
940 case Primitive::kPrimChar:
941 case Primitive::kPrimShort:
942 __ Stlrh(Register(src), base);
943 break;
944 case Primitive::kPrimInt:
945 case Primitive::kPrimNot:
946 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000947 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000948 __ Stlr(Register(src), base);
949 break;
950 case Primitive::kPrimFloat:
951 case Primitive::kPrimDouble: {
952 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000953 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000954
955 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
956 __ Fmov(temp, FPRegister(src));
957 __ Stlr(temp, base);
958 break;
959 }
960 case Primitive::kPrimVoid:
961 LOG(FATAL) << "Unreachable type " << type;
962 }
963}
964
Alexandre Rames67555f72014-11-18 10:55:16 +0000965void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000966 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000967 DCHECK(current_method.IsW());
968 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
969}
970
971void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
972 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000973 uint32_t dex_pc,
974 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100975 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +0000976 __ Ldr(lr, MemOperand(tr, entry_point_offset));
977 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +0100978 RecordPcInfo(instruction, dex_pc, slow_path);
979 DCHECK(instruction->IsSuspendCheck()
980 || instruction->IsBoundsCheck()
981 || instruction->IsNullCheck()
982 || instruction->IsDivZeroCheck()
983 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000984}
985
986void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
987 vixl::Register class_reg) {
988 UseScratchRegisterScope temps(GetVIXLAssembler());
989 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +0000991 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000992
Serban Constantinescu02164b32014-11-13 14:05:07 +0000993 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +0000994 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000995 // TODO(vixl): Let the MacroAssembler handle MemOperand.
996 __ Add(temp, class_reg, status_offset);
997 __ Ldar(temp, HeapOperand(temp));
998 __ Cmp(temp, mirror::Class::kStatusInitialized);
999 __ B(lt, slow_path->GetEntryLabel());
1000 } else {
1001 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1002 __ Cmp(temp, mirror::Class::kStatusInitialized);
1003 __ B(lt, slow_path->GetEntryLabel());
1004 __ Dmb(InnerShareable, BarrierReads);
1005 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001006 __ Bind(slow_path->GetExitLabel());
1007}
Alexandre Rames5319def2014-10-23 10:03:10 +01001008
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1010 BarrierType type = BarrierAll;
1011
1012 switch (kind) {
1013 case MemBarrierKind::kAnyAny:
1014 case MemBarrierKind::kAnyStore: {
1015 type = BarrierAll;
1016 break;
1017 }
1018 case MemBarrierKind::kLoadAny: {
1019 type = BarrierReads;
1020 break;
1021 }
1022 case MemBarrierKind::kStoreStore: {
1023 type = BarrierWrites;
1024 break;
1025 }
1026 default:
1027 LOG(FATAL) << "Unexpected memory barrier " << kind;
1028 }
1029 __ Dmb(InnerShareable, type);
1030}
1031
Serban Constantinescu02164b32014-11-13 14:05:07 +00001032void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1033 HBasicBlock* successor) {
1034 SuspendCheckSlowPathARM64* slow_path =
1035 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1036 codegen_->AddSlowPath(slow_path);
1037 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1038 Register temp = temps.AcquireW();
1039
1040 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1041 if (successor == nullptr) {
1042 __ Cbnz(temp, slow_path->GetEntryLabel());
1043 __ Bind(slow_path->GetReturnLabel());
1044 } else {
1045 __ Cbz(temp, codegen_->GetLabelOf(successor));
1046 __ B(slow_path->GetEntryLabel());
1047 // slow_path will return to GetLabelOf(successor).
1048 }
1049}
1050
Alexandre Rames5319def2014-10-23 10:03:10 +01001051InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1052 CodeGeneratorARM64* codegen)
1053 : HGraphVisitor(graph),
1054 assembler_(codegen->GetAssembler()),
1055 codegen_(codegen) {}
1056
1057#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001058 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001059
1060#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1061
1062enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001063 // Using a base helps identify when we hit such breakpoints.
1064 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001065#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1066 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1067#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1068};
1069
1070#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1071 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001072 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001073 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1074 } \
1075 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1076 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1077 locations->SetOut(Location::Any()); \
1078 }
1079 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1080#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1081
1082#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001083#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001084
Alexandre Rames67555f72014-11-18 10:55:16 +00001085void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001086 DCHECK_EQ(instr->InputCount(), 2U);
1087 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1088 Primitive::Type type = instr->GetResultType();
1089 switch (type) {
1090 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001091 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001092 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001093 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001094 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001096
1097 case Primitive::kPrimFloat:
1098 case Primitive::kPrimDouble:
1099 locations->SetInAt(0, Location::RequiresFpuRegister());
1100 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001103
Alexandre Rames5319def2014-10-23 10:03:10 +01001104 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001105 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001106 }
1107}
1108
Alexandre Rames09a99962015-04-15 11:47:56 +01001109void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1110 LocationSummary* locations =
1111 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1112 locations->SetInAt(0, Location::RequiresRegister());
1113 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1114 locations->SetOut(Location::RequiresFpuRegister());
1115 } else {
1116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1117 }
1118}
1119
1120void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1121 const FieldInfo& field_info) {
1122 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001123 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001124
1125 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1126 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1127
1128 if (field_info.IsVolatile()) {
1129 if (use_acquire_release) {
1130 // NB: LoadAcquire will record the pc info if needed.
1131 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1132 } else {
1133 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1134 codegen_->MaybeRecordImplicitNullCheck(instruction);
1135 // For IRIW sequential consistency kLoadAny is not sufficient.
1136 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1137 }
1138 } else {
1139 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1140 codegen_->MaybeRecordImplicitNullCheck(instruction);
1141 }
1142}
1143
1144void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1145 LocationSummary* locations =
1146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1147 locations->SetInAt(0, Location::RequiresRegister());
1148 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1149 locations->SetInAt(1, Location::RequiresFpuRegister());
1150 } else {
1151 locations->SetInAt(1, Location::RequiresRegister());
1152 }
1153}
1154
1155void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
1156 const FieldInfo& field_info) {
1157 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001158 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001159
1160 Register obj = InputRegisterAt(instruction, 0);
1161 CPURegister value = InputCPURegisterAt(instruction, 1);
1162 Offset offset = field_info.GetFieldOffset();
1163 Primitive::Type field_type = field_info.GetFieldType();
1164 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1165
1166 if (field_info.IsVolatile()) {
1167 if (use_acquire_release) {
1168 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1169 codegen_->MaybeRecordImplicitNullCheck(instruction);
1170 } else {
1171 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1172 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1173 codegen_->MaybeRecordImplicitNullCheck(instruction);
1174 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1175 }
1176 } else {
1177 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1178 codegen_->MaybeRecordImplicitNullCheck(instruction);
1179 }
1180
1181 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
1182 codegen_->MarkGCCard(obj, Register(value));
1183 }
1184}
1185
Alexandre Rames67555f72014-11-18 10:55:16 +00001186void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001187 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001188
1189 switch (type) {
1190 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001191 case Primitive::kPrimLong: {
1192 Register dst = OutputRegister(instr);
1193 Register lhs = InputRegisterAt(instr, 0);
1194 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001195 if (instr->IsAdd()) {
1196 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001197 } else if (instr->IsAnd()) {
1198 __ And(dst, lhs, rhs);
1199 } else if (instr->IsOr()) {
1200 __ Orr(dst, lhs, rhs);
1201 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001202 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001203 } else {
1204 DCHECK(instr->IsXor());
1205 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001206 }
1207 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001208 }
1209 case Primitive::kPrimFloat:
1210 case Primitive::kPrimDouble: {
1211 FPRegister dst = OutputFPRegister(instr);
1212 FPRegister lhs = InputFPRegisterAt(instr, 0);
1213 FPRegister rhs = InputFPRegisterAt(instr, 1);
1214 if (instr->IsAdd()) {
1215 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001216 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001217 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001218 } else {
1219 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001220 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001221 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001222 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001223 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001224 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001225 }
1226}
1227
Serban Constantinescu02164b32014-11-13 14:05:07 +00001228void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1229 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1230
1231 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1232 Primitive::Type type = instr->GetResultType();
1233 switch (type) {
1234 case Primitive::kPrimInt:
1235 case Primitive::kPrimLong: {
1236 locations->SetInAt(0, Location::RequiresRegister());
1237 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1238 locations->SetOut(Location::RequiresRegister());
1239 break;
1240 }
1241 default:
1242 LOG(FATAL) << "Unexpected shift type " << type;
1243 }
1244}
1245
1246void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1247 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1248
1249 Primitive::Type type = instr->GetType();
1250 switch (type) {
1251 case Primitive::kPrimInt:
1252 case Primitive::kPrimLong: {
1253 Register dst = OutputRegister(instr);
1254 Register lhs = InputRegisterAt(instr, 0);
1255 Operand rhs = InputOperandAt(instr, 1);
1256 if (rhs.IsImmediate()) {
1257 uint32_t shift_value = (type == Primitive::kPrimInt)
1258 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1259 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1260 if (instr->IsShl()) {
1261 __ Lsl(dst, lhs, shift_value);
1262 } else if (instr->IsShr()) {
1263 __ Asr(dst, lhs, shift_value);
1264 } else {
1265 __ Lsr(dst, lhs, shift_value);
1266 }
1267 } else {
1268 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1269
1270 if (instr->IsShl()) {
1271 __ Lsl(dst, lhs, rhs_reg);
1272 } else if (instr->IsShr()) {
1273 __ Asr(dst, lhs, rhs_reg);
1274 } else {
1275 __ Lsr(dst, lhs, rhs_reg);
1276 }
1277 }
1278 break;
1279 }
1280 default:
1281 LOG(FATAL) << "Unexpected shift operation type " << type;
1282 }
1283}
1284
Alexandre Rames5319def2014-10-23 10:03:10 +01001285void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001286 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001287}
1288
1289void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001290 HandleBinaryOp(instruction);
1291}
1292
1293void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1294 HandleBinaryOp(instruction);
1295}
1296
1297void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1298 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001299}
1300
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001301void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1302 LocationSummary* locations =
1303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1304 locations->SetInAt(0, Location::RequiresRegister());
1305 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001306 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1307 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1308 } else {
1309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1310 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001311}
1312
1313void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1314 LocationSummary* locations = instruction->GetLocations();
1315 Primitive::Type type = instruction->GetType();
1316 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001317 Location index = locations->InAt(1);
1318 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001319 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001320 MacroAssembler* masm = GetVIXLAssembler();
1321 UseScratchRegisterScope temps(masm);
1322 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323
1324 if (index.IsConstant()) {
1325 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001326 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 } else {
1328 Register temp = temps.AcquireSameSizeAs(obj);
1329 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1330 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001331 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001332 }
1333
Alexandre Rames67555f72014-11-18 10:55:16 +00001334 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001335 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001336}
1337
Alexandre Rames5319def2014-10-23 10:03:10 +01001338void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1339 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1340 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001342}
1343
1344void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001345 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001346 __ Ldr(OutputRegister(instruction),
1347 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001348 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001349}
1350
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001351void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001352 if (instruction->NeedsTypeCheck()) {
1353 LocationSummary* locations =
1354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001355 InvokeRuntimeCallingConvention calling_convention;
1356 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1357 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1358 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1359 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001360 LocationSummary* locations =
1361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001362 locations->SetInAt(0, Location::RequiresRegister());
1363 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001364 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1365 locations->SetInAt(2, Location::RequiresFpuRegister());
1366 } else {
1367 locations->SetInAt(2, Location::RequiresRegister());
1368 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001369 }
1370}
1371
1372void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1373 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001374 LocationSummary* locations = instruction->GetLocations();
1375 bool needs_runtime_call = locations->WillCall();
1376
1377 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001378 codegen_->InvokeRuntime(
1379 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001380 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001381 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001382 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001383 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001384 Location index = locations->InAt(1);
1385 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001386 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001387 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001388 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001389 {
1390 // We use a block to end the scratch scope before the write barrier, thus
1391 // freeing the temporary registers so they can be used in `MarkGCCard`.
1392 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001393
Alexandre Rames97833a02015-04-16 15:07:12 +01001394 if (index.IsConstant()) {
1395 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1396 destination = HeapOperand(obj, offset);
1397 } else {
1398 Register temp = temps.AcquireSameSizeAs(obj);
1399 Register index_reg = InputRegisterAt(instruction, 1);
1400 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1401 destination = HeapOperand(temp, offset);
1402 }
1403
1404 codegen_->Store(value_type, value, destination);
1405 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001406 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001407 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
1408 codegen_->MarkGCCard(obj, value.W());
1409 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410 }
1411}
1412
Alexandre Rames67555f72014-11-18 10:55:16 +00001413void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1414 LocationSummary* locations =
1415 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1416 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001417 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001418 if (instruction->HasUses()) {
1419 locations->SetOut(Location::SameAsFirstInput());
1420 }
1421}
1422
1423void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001424 LocationSummary* locations = instruction->GetLocations();
1425 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1426 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001427 codegen_->AddSlowPath(slow_path);
1428
1429 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1430 __ B(slow_path->GetEntryLabel(), hs);
1431}
1432
1433void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1435 instruction, LocationSummary::kCallOnSlowPath);
1436 locations->SetInAt(0, Location::RequiresRegister());
1437 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001438 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001439}
1440
1441void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001442 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001443 Register obj = InputRegisterAt(instruction, 0);;
1444 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001446
Alexandre Rames3e69f162014-12-10 10:36:50 +00001447 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1448 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001449 codegen_->AddSlowPath(slow_path);
1450
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001451 // Avoid null check if we know obj is not null.
1452 if (instruction->MustDoNullCheck()) {
1453 __ Cbz(obj, slow_path->GetExitLabel());
1454 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001455 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001456 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1457 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001458 __ B(ne, slow_path->GetEntryLabel());
1459 __ Bind(slow_path->GetExitLabel());
1460}
1461
1462void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1463 LocationSummary* locations =
1464 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1465 locations->SetInAt(0, Location::RequiresRegister());
1466 if (check->HasUses()) {
1467 locations->SetOut(Location::SameAsFirstInput());
1468 }
1469}
1470
1471void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1472 // We assume the class is not null.
1473 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1474 check->GetLoadClass(), check, check->GetDexPc(), true);
1475 codegen_->AddSlowPath(slow_path);
1476 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1477}
1478
Serban Constantinescu02164b32014-11-13 14:05:07 +00001479void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001480 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001481 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1482 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001483 switch (in_type) {
1484 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001485 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001486 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1488 break;
1489 }
1490 case Primitive::kPrimFloat:
1491 case Primitive::kPrimDouble: {
1492 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001493 HInstruction* right = compare->InputAt(1);
1494 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1495 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1496 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1497 } else {
1498 locations->SetInAt(1, Location::RequiresFpuRegister());
1499 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001500 locations->SetOut(Location::RequiresRegister());
1501 break;
1502 }
1503 default:
1504 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1505 }
1506}
1507
1508void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1509 Primitive::Type in_type = compare->InputAt(0)->GetType();
1510
1511 // 0 if: left == right
1512 // 1 if: left > right
1513 // -1 if: left < right
1514 switch (in_type) {
1515 case Primitive::kPrimLong: {
1516 Register result = OutputRegister(compare);
1517 Register left = InputRegisterAt(compare, 0);
1518 Operand right = InputOperandAt(compare, 1);
1519
1520 __ Cmp(left, right);
1521 __ Cset(result, ne);
1522 __ Cneg(result, result, lt);
1523 break;
1524 }
1525 case Primitive::kPrimFloat:
1526 case Primitive::kPrimDouble: {
1527 Register result = OutputRegister(compare);
1528 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001529 if (compare->GetLocations()->InAt(1).IsConstant()) {
1530 if (kIsDebugBuild) {
1531 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1532 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1533 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1534 }
1535 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1536 __ Fcmp(left, 0.0);
1537 } else {
1538 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1539 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001540 if (compare->IsGtBias()) {
1541 __ Cset(result, ne);
1542 } else {
1543 __ Csetm(result, ne);
1544 }
1545 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001546 break;
1547 }
1548 default:
1549 LOG(FATAL) << "Unimplemented compare type " << in_type;
1550 }
1551}
1552
1553void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1555 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001556 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001557 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001559 }
1560}
1561
1562void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1563 if (!instruction->NeedsMaterialization()) {
1564 return;
1565 }
1566
1567 LocationSummary* locations = instruction->GetLocations();
1568 Register lhs = InputRegisterAt(instruction, 0);
1569 Operand rhs = InputOperandAt(instruction, 1);
1570 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1571 Condition cond = ARM64Condition(instruction->GetCondition());
1572
1573 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001574 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001575}
1576
1577#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1578 M(Equal) \
1579 M(NotEqual) \
1580 M(LessThan) \
1581 M(LessThanOrEqual) \
1582 M(GreaterThan) \
1583 M(GreaterThanOrEqual)
1584#define DEFINE_CONDITION_VISITORS(Name) \
1585void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1586void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1587FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001588#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001589#undef FOR_EACH_CONDITION_INSTRUCTION
1590
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001591void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1592 LocationSummary* locations =
1593 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1594 switch (div->GetResultType()) {
1595 case Primitive::kPrimInt:
1596 case Primitive::kPrimLong:
1597 locations->SetInAt(0, Location::RequiresRegister());
1598 locations->SetInAt(1, Location::RequiresRegister());
1599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1600 break;
1601
1602 case Primitive::kPrimFloat:
1603 case Primitive::kPrimDouble:
1604 locations->SetInAt(0, Location::RequiresFpuRegister());
1605 locations->SetInAt(1, Location::RequiresFpuRegister());
1606 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1607 break;
1608
1609 default:
1610 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1611 }
1612}
1613
1614void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1615 Primitive::Type type = div->GetResultType();
1616 switch (type) {
1617 case Primitive::kPrimInt:
1618 case Primitive::kPrimLong:
1619 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1620 break;
1621
1622 case Primitive::kPrimFloat:
1623 case Primitive::kPrimDouble:
1624 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1625 break;
1626
1627 default:
1628 LOG(FATAL) << "Unexpected div type " << type;
1629 }
1630}
1631
Alexandre Rames67555f72014-11-18 10:55:16 +00001632void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1633 LocationSummary* locations =
1634 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1635 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1636 if (instruction->HasUses()) {
1637 locations->SetOut(Location::SameAsFirstInput());
1638 }
1639}
1640
1641void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1642 SlowPathCodeARM64* slow_path =
1643 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1644 codegen_->AddSlowPath(slow_path);
1645 Location value = instruction->GetLocations()->InAt(0);
1646
Alexandre Rames3e69f162014-12-10 10:36:50 +00001647 Primitive::Type type = instruction->GetType();
1648
1649 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1650 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1651 return;
1652 }
1653
Alexandre Rames67555f72014-11-18 10:55:16 +00001654 if (value.IsConstant()) {
1655 int64_t divisor = Int64ConstantFrom(value);
1656 if (divisor == 0) {
1657 __ B(slow_path->GetEntryLabel());
1658 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001659 // A division by a non-null constant is valid. We don't need to perform
1660 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001661 }
1662 } else {
1663 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1664 }
1665}
1666
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001667void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1668 LocationSummary* locations =
1669 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1670 locations->SetOut(Location::ConstantLocation(constant));
1671}
1672
1673void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1674 UNUSED(constant);
1675 // Will be generated at use site.
1676}
1677
Alexandre Rames5319def2014-10-23 10:03:10 +01001678void LocationsBuilderARM64::VisitExit(HExit* exit) {
1679 exit->SetLocations(nullptr);
1680}
1681
1682void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001683 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001684}
1685
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001686void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1687 LocationSummary* locations =
1688 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1689 locations->SetOut(Location::ConstantLocation(constant));
1690}
1691
1692void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1693 UNUSED(constant);
1694 // Will be generated at use site.
1695}
1696
Alexandre Rames5319def2014-10-23 10:03:10 +01001697void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1698 got->SetLocations(nullptr);
1699}
1700
1701void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1702 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001703 DCHECK(!successor->IsExitBlock());
1704 HBasicBlock* block = got->GetBlock();
1705 HInstruction* previous = got->GetPrevious();
1706 HLoopInformation* info = block->GetLoopInformation();
1707
David Brazdil46e2a392015-03-16 17:31:52 +00001708 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001709 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1710 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1711 return;
1712 }
1713 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1714 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1715 }
1716 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001717 __ B(codegen_->GetLabelOf(successor));
1718 }
1719}
1720
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001721void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1722 vixl::Label* true_target,
1723 vixl::Label* false_target,
1724 vixl::Label* always_true_target) {
1725 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001726 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001727
Serban Constantinescu02164b32014-11-13 14:05:07 +00001728 if (cond->IsIntConstant()) {
1729 int32_t cond_value = cond->AsIntConstant()->GetValue();
1730 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001731 if (always_true_target != nullptr) {
1732 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001733 }
1734 return;
1735 } else {
1736 DCHECK_EQ(cond_value, 0);
1737 }
1738 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001739 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001740 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001741 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001742 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001743 } else {
1744 // The condition instruction has not been materialized, use its inputs as
1745 // the comparison and its condition as the branch condition.
1746 Register lhs = InputRegisterAt(condition, 0);
1747 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001748 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00001749 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1750 switch (arm64_cond) {
1751 case eq:
1752 __ Cbz(lhs, true_target);
1753 break;
1754 case ne:
1755 __ Cbnz(lhs, true_target);
1756 break;
1757 case lt:
1758 // Test the sign bit and branch accordingly.
1759 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1760 break;
1761 case ge:
1762 // Test the sign bit and branch accordingly.
1763 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1764 break;
1765 default:
1766 // Without the `static_cast` the compiler throws an error for
1767 // `-Werror=sign-promo`.
1768 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001769 }
1770 } else {
1771 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001772 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001773 }
1774 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001775 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001776 __ B(false_target);
1777 }
1778}
1779
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001780void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1782 HInstruction* cond = if_instr->InputAt(0);
1783 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1784 locations->SetInAt(0, Location::RequiresRegister());
1785 }
1786}
1787
1788void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1789 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1790 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1791 vixl::Label* always_true_target = true_target;
1792 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1793 if_instr->IfTrueSuccessor())) {
1794 always_true_target = nullptr;
1795 }
1796 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1797 if_instr->IfFalseSuccessor())) {
1798 false_target = nullptr;
1799 }
1800 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1801}
1802
1803void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1804 LocationSummary* locations = new (GetGraph()->GetArena())
1805 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1806 HInstruction* cond = deoptimize->InputAt(0);
1807 DCHECK(cond->IsCondition());
1808 if (cond->AsCondition()->NeedsMaterialization()) {
1809 locations->SetInAt(0, Location::RequiresRegister());
1810 }
1811}
1812
1813void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1814 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
1815 DeoptimizationSlowPathARM64(deoptimize);
1816 codegen_->AddSlowPath(slow_path);
1817 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
1818 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1819}
1820
Alexandre Rames5319def2014-10-23 10:03:10 +01001821void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001822 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001823}
1824
1825void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001826 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001827}
1828
1829void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001830 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001831}
1832
1833void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001834 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001835}
1836
Alexandre Rames67555f72014-11-18 10:55:16 +00001837void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1838 LocationSummary::CallKind call_kind =
1839 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1841 locations->SetInAt(0, Location::RequiresRegister());
1842 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001843 // The output does overlap inputs.
1844 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001845}
1846
1847void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1848 LocationSummary* locations = instruction->GetLocations();
1849 Register obj = InputRegisterAt(instruction, 0);;
1850 Register cls = InputRegisterAt(instruction, 1);;
1851 Register out = OutputRegister(instruction);
1852
1853 vixl::Label done;
1854
1855 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001856 // Avoid null check if we know `obj` is not null.
1857 if (instruction->MustDoNullCheck()) {
1858 __ Mov(out, 0);
1859 __ Cbz(obj, &done);
1860 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001861
1862 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001863 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001864 __ Cmp(out, cls);
1865 if (instruction->IsClassFinal()) {
1866 // Classes must be equal for the instanceof to succeed.
1867 __ Cset(out, eq);
1868 } else {
1869 // If the classes are not equal, we go into a slow path.
1870 DCHECK(locations->OnlyCallsOnSlowPath());
1871 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001872 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1873 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001874 codegen_->AddSlowPath(slow_path);
1875 __ B(ne, slow_path->GetEntryLabel());
1876 __ Mov(out, 1);
1877 __ Bind(slow_path->GetExitLabel());
1878 }
1879
1880 __ Bind(&done);
1881}
1882
Alexandre Rames5319def2014-10-23 10:03:10 +01001883void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1885 locations->SetOut(Location::ConstantLocation(constant));
1886}
1887
1888void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1889 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001890 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001891}
1892
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001893void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1894 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1895 locations->SetOut(Location::ConstantLocation(constant));
1896}
1897
1898void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1899 // Will be generated at use site.
1900 UNUSED(constant);
1901}
1902
Alexandre Rames5319def2014-10-23 10:03:10 +01001903void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1904 LocationSummary* locations =
1905 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1906 locations->AddTemp(LocationFrom(x0));
1907
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001908 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001909 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001910 HInstruction* input = invoke->InputAt(i);
1911 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1912 }
1913
1914 Primitive::Type return_type = invoke->GetType();
1915 if (return_type != Primitive::kPrimVoid) {
1916 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1917 }
1918}
1919
Alexandre Rames67555f72014-11-18 10:55:16 +00001920void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1921 HandleInvoke(invoke);
1922}
1923
1924void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1925 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1926 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1927 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1928 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1929 Location receiver = invoke->GetLocations()->InAt(0);
1930 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001931 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001932
1933 // The register ip1 is required to be used for the hidden argument in
1934 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01001935 MacroAssembler* masm = GetVIXLAssembler();
1936 UseScratchRegisterScope scratch_scope(masm);
1937 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00001938 scratch_scope.Exclude(ip1);
1939 __ Mov(ip1, invoke->GetDexMethodIndex());
1940
1941 // temp = object->GetClass();
1942 if (receiver.IsStackSlot()) {
1943 __ Ldr(temp, StackOperandFrom(receiver));
1944 __ Ldr(temp, HeapOperand(temp, class_offset));
1945 } else {
1946 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1947 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001948 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001949 // temp = temp->GetImtEntryAt(method_offset);
1950 __ Ldr(temp, HeapOperand(temp, method_offset));
1951 // lr = temp->GetEntryPoint();
1952 __ Ldr(lr, HeapOperand(temp, entry_point));
1953 // lr();
1954 __ Blr(lr);
1955 DCHECK(!codegen_->IsLeafMethod());
1956 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1957}
1958
1959void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001960 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1961 if (intrinsic.TryDispatch(invoke)) {
1962 return;
1963 }
1964
Alexandre Rames67555f72014-11-18 10:55:16 +00001965 HandleInvoke(invoke);
1966}
1967
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001968void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001969 // When we do not run baseline, explicit clinit checks triggered by static
1970 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1971 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001972
Andreas Gampe878d58c2015-01-15 23:24:00 -08001973 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1974 if (intrinsic.TryDispatch(invoke)) {
1975 return;
1976 }
1977
Alexandre Rames67555f72014-11-18 10:55:16 +00001978 HandleInvoke(invoke);
1979}
1980
Andreas Gampe878d58c2015-01-15 23:24:00 -08001981static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1982 if (invoke->GetLocations()->Intrinsified()) {
1983 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1984 intrinsic.Dispatch(invoke);
1985 return true;
1986 }
1987 return false;
1988}
1989
1990void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1991 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1992 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001993 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001994 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001995
1996 // TODO: Implement all kinds of calls:
1997 // 1) boot -> boot
1998 // 2) app -> boot
1999 // 3) app -> app
2000 //
2001 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2002
Jeff Hao848f70a2014-01-15 13:49:50 -08002003 if (invoke->IsStringInit()) {
2004 // temp = thread->string_init_entrypoint
2005 __ Ldr(temp, HeapOperand(tr, invoke->GetStringInitOffset()));
2006 // LR = temp->entry_point_from_quick_compiled_code_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002007 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2008 kArm64WordSize)));
Jeff Hao848f70a2014-01-15 13:49:50 -08002009 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002010 __ Blr(lr);
2011 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08002012 // temp = method;
2013 LoadCurrentMethod(temp);
2014 if (!invoke->IsRecursive()) {
2015 // temp = temp->dex_cache_resolved_methods_;
2016 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
2017 // temp = temp[index_in_cache];
2018 __ Ldr(temp, HeapOperand(temp, index_in_cache));
2019 // lr = temp->entry_point_from_quick_compiled_code_;
2020 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2021 kArm64WordSize)));
2022 // lr();
2023 __ Blr(lr);
2024 } else {
2025 __ Bl(&frame_entry_label_);
2026 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002027 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002028
Andreas Gampe878d58c2015-01-15 23:24:00 -08002029 DCHECK(!IsLeafMethod());
2030}
2031
2032void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002033 // When we do not run baseline, explicit clinit checks triggered by static
2034 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2035 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002036
Andreas Gampe878d58c2015-01-15 23:24:00 -08002037 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2038 return;
2039 }
2040
Alexandre Ramesd921d642015-04-16 15:07:16 +01002041 BlockPoolsScope block_pools(GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -08002042 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
2043 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002044 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002045}
2046
2047void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002048 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2049 return;
2050 }
2051
Alexandre Rames5319def2014-10-23 10:03:10 +01002052 LocationSummary* locations = invoke->GetLocations();
2053 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002054 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002055 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
2056 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
2057 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002058 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002059
Alexandre Ramesd921d642015-04-16 15:07:16 +01002060 BlockPoolsScope block_pools(GetVIXLAssembler());
2061
Alexandre Rames5319def2014-10-23 10:03:10 +01002062 // temp = object->GetClass();
2063 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002064 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
2065 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002066 } else {
2067 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002068 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002069 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002070 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002071 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002072 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002073 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002074 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002075 // lr();
2076 __ Blr(lr);
2077 DCHECK(!codegen_->IsLeafMethod());
2078 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2079}
2080
Alexandre Rames67555f72014-11-18 10:55:16 +00002081void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2082 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2083 : LocationSummary::kNoCall;
2084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2085 locations->SetOut(Location::RequiresRegister());
2086}
2087
2088void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2089 Register out = OutputRegister(cls);
2090 if (cls->IsReferrersClass()) {
2091 DCHECK(!cls->CanCallRuntime());
2092 DCHECK(!cls->MustGenerateClinitCheck());
2093 codegen_->LoadCurrentMethod(out);
2094 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2095 } else {
2096 DCHECK(cls->CanCallRuntime());
2097 codegen_->LoadCurrentMethod(out);
2098 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002099 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002100
2101 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2102 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2103 codegen_->AddSlowPath(slow_path);
2104 __ Cbz(out, slow_path->GetEntryLabel());
2105 if (cls->MustGenerateClinitCheck()) {
2106 GenerateClassInitializationCheck(slow_path, out);
2107 } else {
2108 __ Bind(slow_path->GetExitLabel());
2109 }
2110 }
2111}
2112
2113void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2114 LocationSummary* locations =
2115 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2116 locations->SetOut(Location::RequiresRegister());
2117}
2118
2119void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2120 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2121 __ Ldr(OutputRegister(instruction), exception);
2122 __ Str(wzr, exception);
2123}
2124
Alexandre Rames5319def2014-10-23 10:03:10 +01002125void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2126 load->SetLocations(nullptr);
2127}
2128
2129void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2130 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002131 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002132}
2133
Alexandre Rames67555f72014-11-18 10:55:16 +00002134void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2135 LocationSummary* locations =
2136 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2137 locations->SetOut(Location::RequiresRegister());
2138}
2139
2140void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2141 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2142 codegen_->AddSlowPath(slow_path);
2143
2144 Register out = OutputRegister(load);
2145 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002146 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2147 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002148 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002149 __ Cbz(out, slow_path->GetEntryLabel());
2150 __ Bind(slow_path->GetExitLabel());
2151}
2152
Alexandre Rames5319def2014-10-23 10:03:10 +01002153void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2154 local->SetLocations(nullptr);
2155}
2156
2157void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2158 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2159}
2160
2161void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2162 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2163 locations->SetOut(Location::ConstantLocation(constant));
2164}
2165
2166void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2167 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002168 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002169}
2170
Alexandre Rames67555f72014-11-18 10:55:16 +00002171void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2172 LocationSummary* locations =
2173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2174 InvokeRuntimeCallingConvention calling_convention;
2175 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2176}
2177
2178void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2179 codegen_->InvokeRuntime(instruction->IsEnter()
2180 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2181 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002182 instruction->GetDexPc(),
2183 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002184 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002185}
2186
Alexandre Rames42d641b2014-10-27 14:00:51 +00002187void LocationsBuilderARM64::VisitMul(HMul* mul) {
2188 LocationSummary* locations =
2189 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2190 switch (mul->GetResultType()) {
2191 case Primitive::kPrimInt:
2192 case Primitive::kPrimLong:
2193 locations->SetInAt(0, Location::RequiresRegister());
2194 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002196 break;
2197
2198 case Primitive::kPrimFloat:
2199 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002200 locations->SetInAt(0, Location::RequiresFpuRegister());
2201 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002202 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002203 break;
2204
2205 default:
2206 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2207 }
2208}
2209
2210void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2211 switch (mul->GetResultType()) {
2212 case Primitive::kPrimInt:
2213 case Primitive::kPrimLong:
2214 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2215 break;
2216
2217 case Primitive::kPrimFloat:
2218 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002219 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002220 break;
2221
2222 default:
2223 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2224 }
2225}
2226
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002227void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2228 LocationSummary* locations =
2229 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2230 switch (neg->GetResultType()) {
2231 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002232 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002233 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002235 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002236
2237 case Primitive::kPrimFloat:
2238 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002239 locations->SetInAt(0, Location::RequiresFpuRegister());
2240 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002241 break;
2242
2243 default:
2244 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2245 }
2246}
2247
2248void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2249 switch (neg->GetResultType()) {
2250 case Primitive::kPrimInt:
2251 case Primitive::kPrimLong:
2252 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2253 break;
2254
2255 case Primitive::kPrimFloat:
2256 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002257 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2262 }
2263}
2264
2265void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2266 LocationSummary* locations =
2267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2268 InvokeRuntimeCallingConvention calling_convention;
2269 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002270 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002271 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002272 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2273 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2274 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002275}
2276
2277void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2278 LocationSummary* locations = instruction->GetLocations();
2279 InvokeRuntimeCallingConvention calling_convention;
2280 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2281 DCHECK(type_index.Is(w0));
2282 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002283 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002284 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002285 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002286 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002287 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2288 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002289 instruction->GetDexPc(),
2290 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002291 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2292 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002293}
2294
Alexandre Rames5319def2014-10-23 10:03:10 +01002295void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2296 LocationSummary* locations =
2297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2298 InvokeRuntimeCallingConvention calling_convention;
2299 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2300 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2301 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002302 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002303}
2304
2305void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2306 LocationSummary* locations = instruction->GetLocations();
2307 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2308 DCHECK(type_index.Is(w0));
2309 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2310 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002311 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002312 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002313 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002314 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2315 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002316 instruction->GetDexPc(),
2317 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002318 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002319}
2320
2321void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2322 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002323 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002325}
2326
2327void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002328 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002329 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002330 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002331 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002332 break;
2333
2334 default:
2335 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2336 }
2337}
2338
David Brazdil66d126e2015-04-03 16:02:44 +01002339void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2341 locations->SetInAt(0, Location::RequiresRegister());
2342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2343}
2344
2345void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002346 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2347}
2348
Alexandre Rames5319def2014-10-23 10:03:10 +01002349void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2350 LocationSummary* locations =
2351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2352 locations->SetInAt(0, Location::RequiresRegister());
2353 if (instruction->HasUses()) {
2354 locations->SetOut(Location::SameAsFirstInput());
2355 }
2356}
2357
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002358void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002359 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2360 return;
2361 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002362
Alexandre Ramesd921d642015-04-16 15:07:16 +01002363 BlockPoolsScope block_pools(GetVIXLAssembler());
2364 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002365 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2366 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2367}
2368
2369void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002370 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2371 codegen_->AddSlowPath(slow_path);
2372
2373 LocationSummary* locations = instruction->GetLocations();
2374 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002375
2376 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002377}
2378
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002379void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2380 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2381 GenerateImplicitNullCheck(instruction);
2382 } else {
2383 GenerateExplicitNullCheck(instruction);
2384 }
2385}
2386
Alexandre Rames67555f72014-11-18 10:55:16 +00002387void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2388 HandleBinaryOp(instruction);
2389}
2390
2391void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2392 HandleBinaryOp(instruction);
2393}
2394
Alexandre Rames3e69f162014-12-10 10:36:50 +00002395void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2396 LOG(FATAL) << "Unreachable";
2397}
2398
2399void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2400 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2401}
2402
Alexandre Rames5319def2014-10-23 10:03:10 +01002403void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2404 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2405 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2406 if (location.IsStackSlot()) {
2407 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2408 } else if (location.IsDoubleStackSlot()) {
2409 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2410 }
2411 locations->SetOut(location);
2412}
2413
2414void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2415 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002416 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002417}
2418
2419void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2420 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2421 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2422 locations->SetInAt(i, Location::Any());
2423 }
2424 locations->SetOut(Location::Any());
2425}
2426
2427void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002428 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002429 LOG(FATAL) << "Unreachable";
2430}
2431
Serban Constantinescu02164b32014-11-13 14:05:07 +00002432void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002433 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002434 LocationSummary::CallKind call_kind =
2435 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2437
2438 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002439 case Primitive::kPrimInt:
2440 case Primitive::kPrimLong:
2441 locations->SetInAt(0, Location::RequiresRegister());
2442 locations->SetInAt(1, Location::RequiresRegister());
2443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2444 break;
2445
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002446 case Primitive::kPrimFloat:
2447 case Primitive::kPrimDouble: {
2448 InvokeRuntimeCallingConvention calling_convention;
2449 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2450 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2451 locations->SetOut(calling_convention.GetReturnLocation(type));
2452
2453 break;
2454 }
2455
Serban Constantinescu02164b32014-11-13 14:05:07 +00002456 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002457 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002458 }
2459}
2460
2461void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2462 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002463
Serban Constantinescu02164b32014-11-13 14:05:07 +00002464 switch (type) {
2465 case Primitive::kPrimInt:
2466 case Primitive::kPrimLong: {
2467 UseScratchRegisterScope temps(GetVIXLAssembler());
2468 Register dividend = InputRegisterAt(rem, 0);
2469 Register divisor = InputRegisterAt(rem, 1);
2470 Register output = OutputRegister(rem);
2471 Register temp = temps.AcquireSameSizeAs(output);
2472
2473 __ Sdiv(temp, dividend, divisor);
2474 __ Msub(output, temp, divisor, dividend);
2475 break;
2476 }
2477
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002478 case Primitive::kPrimFloat:
2479 case Primitive::kPrimDouble: {
2480 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2481 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002482 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002483 break;
2484 }
2485
Serban Constantinescu02164b32014-11-13 14:05:07 +00002486 default:
2487 LOG(FATAL) << "Unexpected rem type " << type;
2488 }
2489}
2490
Calin Juravle27df7582015-04-17 19:12:31 +01002491void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2492 memory_barrier->SetLocations(nullptr);
2493}
2494
2495void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2496 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2497}
2498
Alexandre Rames5319def2014-10-23 10:03:10 +01002499void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2501 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002502 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002503}
2504
2505void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002506 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002507 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002508}
2509
2510void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2511 instruction->SetLocations(nullptr);
2512}
2513
2514void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002515 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002516 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002517}
2518
Serban Constantinescu02164b32014-11-13 14:05:07 +00002519void LocationsBuilderARM64::VisitShl(HShl* shl) {
2520 HandleShift(shl);
2521}
2522
2523void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2524 HandleShift(shl);
2525}
2526
2527void LocationsBuilderARM64::VisitShr(HShr* shr) {
2528 HandleShift(shr);
2529}
2530
2531void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2532 HandleShift(shr);
2533}
2534
Alexandre Rames5319def2014-10-23 10:03:10 +01002535void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2537 Primitive::Type field_type = store->InputAt(1)->GetType();
2538 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002539 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002540 case Primitive::kPrimBoolean:
2541 case Primitive::kPrimByte:
2542 case Primitive::kPrimChar:
2543 case Primitive::kPrimShort:
2544 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002545 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002546 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2547 break;
2548
2549 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002550 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002551 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2552 break;
2553
2554 default:
2555 LOG(FATAL) << "Unimplemented local type " << field_type;
2556 }
2557}
2558
2559void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002560 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002561}
2562
2563void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002564 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002565}
2566
2567void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002568 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002569}
2570
Alexandre Rames67555f72014-11-18 10:55:16 +00002571void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002572 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002573}
2574
2575void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002576 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002577}
2578
2579void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002580 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002581}
2582
Alexandre Rames67555f72014-11-18 10:55:16 +00002583void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002584 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002585}
2586
2587void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2589}
2590
2591void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002592 HBasicBlock* block = instruction->GetBlock();
2593 if (block->GetLoopInformation() != nullptr) {
2594 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2595 // The back edge will generate the suspend check.
2596 return;
2597 }
2598 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2599 // The goto will generate the suspend check.
2600 return;
2601 }
2602 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002603}
2604
2605void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2606 temp->SetLocations(nullptr);
2607}
2608
2609void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2610 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002611 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002612}
2613
Alexandre Rames67555f72014-11-18 10:55:16 +00002614void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2615 LocationSummary* locations =
2616 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2617 InvokeRuntimeCallingConvention calling_convention;
2618 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2619}
2620
2621void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2622 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002623 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002624 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002625}
2626
2627void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2628 LocationSummary* locations =
2629 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2630 Primitive::Type input_type = conversion->GetInputType();
2631 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002632 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002633 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2634 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2635 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2636 }
2637
Alexandre Rames542361f2015-01-29 16:57:31 +00002638 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002639 locations->SetInAt(0, Location::RequiresFpuRegister());
2640 } else {
2641 locations->SetInAt(0, Location::RequiresRegister());
2642 }
2643
Alexandre Rames542361f2015-01-29 16:57:31 +00002644 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002645 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2646 } else {
2647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2648 }
2649}
2650
2651void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2652 Primitive::Type result_type = conversion->GetResultType();
2653 Primitive::Type input_type = conversion->GetInputType();
2654
2655 DCHECK_NE(input_type, result_type);
2656
Alexandre Rames542361f2015-01-29 16:57:31 +00002657 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002658 int result_size = Primitive::ComponentSize(result_type);
2659 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002660 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002661 Register output = OutputRegister(conversion);
2662 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002663 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2664 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2665 } else if ((result_type == Primitive::kPrimChar) ||
2666 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2667 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002668 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002669 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002670 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002671 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002672 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002673 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002674 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2675 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002676 } else if (Primitive::IsFloatingPointType(result_type) &&
2677 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002678 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2679 } else {
2680 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2681 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002682 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002683}
Alexandre Rames67555f72014-11-18 10:55:16 +00002684
Serban Constantinescu02164b32014-11-13 14:05:07 +00002685void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2686 HandleShift(ushr);
2687}
2688
2689void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2690 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002691}
2692
2693void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2694 HandleBinaryOp(instruction);
2695}
2696
2697void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2698 HandleBinaryOp(instruction);
2699}
2700
Calin Juravleb1498f62015-02-16 13:13:29 +00002701void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2702 // Nothing to do, this should be removed during prepare for register allocator.
2703 UNUSED(instruction);
2704 LOG(FATAL) << "Unreachable";
2705}
2706
2707void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2708 // Nothing to do, this should be removed during prepare for register allocator.
2709 UNUSED(instruction);
2710 LOG(FATAL) << "Unreachable";
2711}
2712
Alexandre Rames67555f72014-11-18 10:55:16 +00002713#undef __
2714#undef QUICK_ENTRY_POINT
2715
Alexandre Rames5319def2014-10-23 10:03:10 +01002716} // namespace arm64
2717} // namespace art