blob: 00540e2d864fa12d35067d0c695aba5eb04e52e6 [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"
Zheng Xuc6667102015-05-15 16:08:45 +080020#include "code_generator_utils.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080021#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080023#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080025#include "intrinsics.h"
26#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "mirror/array-inl.h"
28#include "mirror/art_method.h"
29#include "mirror/class.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068
Alexandre Rames5319def2014-10-23 10:03:10 +010069static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>);
70static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
80 default:
81 LOG(FATAL) << "Unknown if condition";
82 }
83 return nv; // Unreachable.
84}
85
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086Location ARM64ReturnLocation(Primitive::Type return_type) {
87 DCHECK_NE(return_type, Primitive::kPrimVoid);
88 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
89 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
90 // but we use the exact registers for clarity.
91 if (return_type == Primitive::kPrimFloat) {
92 return LocationFrom(s0);
93 } else if (return_type == Primitive::kPrimDouble) {
94 return LocationFrom(d0);
95 } else if (return_type == Primitive::kPrimLong) {
96 return LocationFrom(x0);
97 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Alexandre Rames5319def2014-10-23 10:03:10 +0100109class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
110 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000111 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
112 Location index_location,
113 Location length_location)
114 : instruction_(instruction),
115 index_location_(index_location),
116 length_location_(length_location) {}
117
Alexandre Rames5319def2014-10-23 10:03:10 +0100118
Alexandre Rames67555f72014-11-18 10:55:16 +0000119 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000120 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100121 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000122 // We're moving two locations to locations that could overlap, so we need a parallel
123 // move resolver.
124 InvokeRuntimeCallingConvention calling_convention;
125 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100126 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
127 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000128 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000129 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800130 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100131 }
132
133 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000134 HBoundsCheck* const instruction_;
135 const Location index_location_;
136 const Location length_location_;
137
Alexandre Rames5319def2014-10-23 10:03:10 +0100138 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
139};
140
Alexandre Rames67555f72014-11-18 10:55:16 +0000141class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
142 public:
143 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
144
145 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
146 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
147 __ Bind(GetEntryLabel());
148 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000149 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800150 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000151 }
152
153 private:
154 HDivZeroCheck* const instruction_;
155 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
156};
157
158class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
159 public:
160 LoadClassSlowPathARM64(HLoadClass* cls,
161 HInstruction* at,
162 uint32_t dex_pc,
163 bool do_clinit)
164 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
165 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
166 }
167
168 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
169 LocationSummary* locations = at_->GetLocations();
170 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
171
172 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000173 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000174
175 InvokeRuntimeCallingConvention calling_convention;
176 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000177 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
178 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000179 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800180 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100181 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800182 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100183 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800184 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000185
186 // Move the class to the desired location.
187 Location out = locations->Out();
188 if (out.IsValid()) {
189 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
190 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000191 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000192 }
193
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000195 __ B(GetExitLabel());
196 }
197
198 private:
199 // The class this slow path will load.
200 HLoadClass* const cls_;
201
202 // The instruction where this slow path is happening.
203 // (Might be the load class or an initialization check).
204 HInstruction* const at_;
205
206 // The dex PC of `at_`.
207 const uint32_t dex_pc_;
208
209 // Whether to initialize the class.
210 const bool do_clinit_;
211
212 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
213};
214
215class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
216 public:
217 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = instruction_->GetLocations();
221 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
222 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
223
224 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000225 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000226
227 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800228 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000229 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000230 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100231 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000232 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000233 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000234
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000235 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000236 __ B(GetExitLabel());
237 }
238
239 private:
240 HLoadString* const instruction_;
241
242 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
243};
244
Alexandre Rames5319def2014-10-23 10:03:10 +0100245class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
246 public:
247 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
250 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000252 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000253 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800254 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 }
256
257 private:
258 HNullCheck* const instruction_;
259
260 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
261};
262
263class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
264 public:
265 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
266 HBasicBlock* successor)
267 : instruction_(instruction), successor_(successor) {}
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
270 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100271 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000272 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000274 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 if (successor_ == nullptr) {
278 __ B(GetReturnLabel());
279 } else {
280 __ B(arm64_codegen->GetLabelOf(successor_));
281 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100282 }
283
284 vixl::Label* GetReturnLabel() {
285 DCHECK(successor_ == nullptr);
286 return &return_label_;
287 }
288
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100289 HBasicBlock* GetSuccessor() const {
290 return successor_;
291 }
292
Alexandre Rames5319def2014-10-23 10:03:10 +0100293 private:
294 HSuspendCheck* const instruction_;
295 // If not null, the block to branch to after the suspend check.
296 HBasicBlock* const successor_;
297
298 // If `successor_` is null, the label to branch to after the suspend check.
299 vixl::Label return_label_;
300
301 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
302};
303
Alexandre Rames67555f72014-11-18 10:55:16 +0000304class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
305 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 TypeCheckSlowPathARM64(HInstruction* instruction,
307 Location class_to_check,
308 Location object_class,
309 uint32_t dex_pc)
310 : instruction_(instruction),
311 class_to_check_(class_to_check),
312 object_class_(object_class),
313 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000314
315 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000316 LocationSummary* locations = instruction_->GetLocations();
317 DCHECK(instruction_->IsCheckCast()
318 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
319 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
320
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000322 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000323
324 // We're moving two locations to locations that could overlap, so we need a parallel
325 // move resolver.
326 InvokeRuntimeCallingConvention calling_convention;
327 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100328 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
329 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000330
331 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000332 arm64_codegen->InvokeRuntime(
333 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000334 Primitive::Type ret_type = instruction_->GetType();
335 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
336 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800337 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
338 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000339 } else {
340 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000341 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800342 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000343 }
344
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000345 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000346 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000347 }
348
349 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000350 HInstruction* const instruction_;
351 const Location class_to_check_;
352 const Location object_class_;
353 uint32_t dex_pc_;
354
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
356};
357
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
359 public:
360 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
361 : instruction_(instruction) {}
362
363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
364 __ Bind(GetEntryLabel());
365 SaveLiveRegisters(codegen, instruction_->GetLocations());
366 DCHECK(instruction_->IsDeoptimize());
367 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
368 uint32_t dex_pc = deoptimize->GetDexPc();
369 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
370 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
371 }
372
373 private:
374 HInstruction* const instruction_;
375 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
376};
377
Alexandre Rames5319def2014-10-23 10:03:10 +0100378#undef __
379
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100380Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100381 Location next_location;
382 if (type == Primitive::kPrimVoid) {
383 LOG(FATAL) << "Unreachable type " << type;
384 }
385
Alexandre Rames542361f2015-01-29 16:57:31 +0000386 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100387 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
388 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000389 } else if (!Primitive::IsFloatingPointType(type) &&
390 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000391 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
392 } else {
393 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000394 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
395 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100396 }
397
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000398 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000399 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100400 return next_location;
401}
402
Serban Constantinescu579885a2015-02-22 20:51:33 +0000403CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
404 const Arm64InstructionSetFeatures& isa_features,
405 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100406 : CodeGenerator(graph,
407 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000408 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000409 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000410 callee_saved_core_registers.list(),
411 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000412 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 block_labels_(nullptr),
414 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000416 move_resolver_(graph->GetArena(), this),
417 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000418 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000419 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000420}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422#undef __
423#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100424
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000425void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
426 // Ensure we emit the literal pool.
427 __ FinalizeCode();
428 CodeGenerator::Finalize(allocator);
429}
430
Zheng Xuad4450e2015-04-17 18:48:56 +0800431void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
432 // Note: There are 6 kinds of moves:
433 // 1. constant -> GPR/FPR (non-cycle)
434 // 2. constant -> stack (non-cycle)
435 // 3. GPR/FPR -> GPR/FPR
436 // 4. GPR/FPR -> stack
437 // 5. stack -> GPR/FPR
438 // 6. stack -> stack (non-cycle)
439 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
440 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
441 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
442 // dependency.
443 vixl_temps_.Open(GetVIXLAssembler());
444}
445
446void ParallelMoveResolverARM64::FinishEmitNativeCode() {
447 vixl_temps_.Close();
448}
449
450Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
451 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
452 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
453 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
454 Location scratch = GetScratchLocation(kind);
455 if (!scratch.Equals(Location::NoLocation())) {
456 return scratch;
457 }
458 // Allocate from VIXL temp registers.
459 if (kind == Location::kRegister) {
460 scratch = LocationFrom(vixl_temps_.AcquireX());
461 } else {
462 DCHECK(kind == Location::kFpuRegister);
463 scratch = LocationFrom(vixl_temps_.AcquireD());
464 }
465 AddScratchLocation(scratch);
466 return scratch;
467}
468
469void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
470 if (loc.IsRegister()) {
471 vixl_temps_.Release(XRegisterFrom(loc));
472 } else {
473 DCHECK(loc.IsFpuRegister());
474 vixl_temps_.Release(DRegisterFrom(loc));
475 }
476 RemoveScratchLocation(loc);
477}
478
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479void ParallelMoveResolverARM64::EmitMove(size_t index) {
480 MoveOperands* move = moves_.Get(index);
481 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
482}
483
Alexandre Rames5319def2014-10-23 10:03:10 +0100484void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100485 MacroAssembler* masm = GetVIXLAssembler();
486 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000487 __ Bind(&frame_entry_label_);
488
Serban Constantinescu02164b32014-11-13 14:05:07 +0000489 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
490 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100491 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000492 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000493 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000494 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000495 __ Ldr(wzr, MemOperand(temp, 0));
496 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000497 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100498
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000499 if (!HasEmptyFrame()) {
500 int frame_size = GetFrameSize();
501 // Stack layout:
502 // sp[frame_size - 8] : lr.
503 // ... : other preserved core registers.
504 // ... : other preserved fp registers.
505 // ... : reserved frame space.
506 // sp[0] : current method.
507 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100508 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800509 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
510 frame_size - GetCoreSpillSize());
511 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
512 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000513 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100514}
515
516void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100517 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100518 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000519 if (!HasEmptyFrame()) {
520 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800521 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
522 frame_size - FrameEntrySpillSize());
523 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
524 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000525 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100526 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000527 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100528 __ Ret();
529 GetAssembler()->cfi().RestoreState();
530 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100531}
532
533void CodeGeneratorARM64::Bind(HBasicBlock* block) {
534 __ Bind(GetLabelOf(block));
535}
536
Alexandre Rames5319def2014-10-23 10:03:10 +0100537void CodeGeneratorARM64::Move(HInstruction* instruction,
538 Location location,
539 HInstruction* move_for) {
540 LocationSummary* locations = instruction->GetLocations();
541 if (locations != nullptr && locations->Out().Equals(location)) {
542 return;
543 }
544
545 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000546 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100547
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000548 if (instruction->IsIntConstant()
549 || instruction->IsLongConstant()
550 || instruction->IsNullConstant()) {
551 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100552 if (location.IsRegister()) {
553 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000554 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100555 (instruction->IsLongConstant() && dst.Is64Bits()));
556 __ Mov(dst, value);
557 } else {
558 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000559 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000560 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
561 ? temps.AcquireW()
562 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100563 __ Mov(temp, value);
564 __ Str(temp, StackOperandFrom(location));
565 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000566 } else if (instruction->IsTemporary()) {
567 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000568 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100569 } else if (instruction->IsLoadLocal()) {
570 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000571 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000572 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000573 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000574 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100575 }
576
577 } else {
578 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000579 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100580 }
581}
582
Alexandre Rames5319def2014-10-23 10:03:10 +0100583Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
584 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000585
Alexandre Rames5319def2014-10-23 10:03:10 +0100586 switch (type) {
587 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000588 case Primitive::kPrimInt:
589 case Primitive::kPrimFloat:
590 return Location::StackSlot(GetStackSlot(load->GetLocal()));
591
592 case Primitive::kPrimLong:
593 case Primitive::kPrimDouble:
594 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
595
Alexandre Rames5319def2014-10-23 10:03:10 +0100596 case Primitive::kPrimBoolean:
597 case Primitive::kPrimByte:
598 case Primitive::kPrimChar:
599 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100600 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100601 LOG(FATAL) << "Unexpected type " << type;
602 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000603
Alexandre Rames5319def2014-10-23 10:03:10 +0100604 LOG(FATAL) << "Unreachable";
605 return Location::NoLocation();
606}
607
608void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000609 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100610 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000611 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100612 vixl::Label done;
613 __ Cbz(value, &done);
614 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
615 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000616 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100617 __ Bind(&done);
618}
619
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000620void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
621 // Blocked core registers:
622 // lr : Runtime reserved.
623 // tr : Runtime reserved.
624 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
625 // ip1 : VIXL core temp.
626 // ip0 : VIXL core temp.
627 //
628 // Blocked fp registers:
629 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100630 CPURegList reserved_core_registers = vixl_reserved_core_registers;
631 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100632 while (!reserved_core_registers.IsEmpty()) {
633 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
634 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000635
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000636 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800637 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000638 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
639 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000640
641 if (is_baseline) {
642 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
643 while (!reserved_core_baseline_registers.IsEmpty()) {
644 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
645 }
646
647 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
648 while (!reserved_fp_baseline_registers.IsEmpty()) {
649 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
650 }
651 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100652}
653
654Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
655 if (type == Primitive::kPrimVoid) {
656 LOG(FATAL) << "Unreachable type " << type;
657 }
658
Alexandre Rames542361f2015-01-29 16:57:31 +0000659 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000660 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
661 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100662 return Location::FpuRegisterLocation(reg);
663 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000664 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
665 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100666 return Location::RegisterLocation(reg);
667 }
668}
669
Alexandre Rames3e69f162014-12-10 10:36:50 +0000670size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
671 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
672 __ Str(reg, MemOperand(sp, stack_index));
673 return kArm64WordSize;
674}
675
676size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
677 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
678 __ Ldr(reg, MemOperand(sp, stack_index));
679 return kArm64WordSize;
680}
681
682size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
683 FPRegister reg = FPRegister(reg_id, kDRegSize);
684 __ Str(reg, MemOperand(sp, stack_index));
685 return kArm64WordSize;
686}
687
688size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
689 FPRegister reg = FPRegister(reg_id, kDRegSize);
690 __ Ldr(reg, MemOperand(sp, stack_index));
691 return kArm64WordSize;
692}
693
Alexandre Rames5319def2014-10-23 10:03:10 +0100694void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
695 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
696}
697
698void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
699 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
700}
701
Alexandre Rames67555f72014-11-18 10:55:16 +0000702void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000703 if (constant->IsIntConstant()) {
704 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
705 } else if (constant->IsLongConstant()) {
706 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
707 } else if (constant->IsNullConstant()) {
708 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000709 } else if (constant->IsFloatConstant()) {
710 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
711 } else {
712 DCHECK(constant->IsDoubleConstant());
713 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
714 }
715}
716
Alexandre Rames3e69f162014-12-10 10:36:50 +0000717
718static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
719 DCHECK(constant.IsConstant());
720 HConstant* cst = constant.GetConstant();
721 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000722 // Null is mapped to a core W register, which we associate with kPrimInt.
723 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000724 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
725 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
726 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
727}
728
729void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000730 if (source.Equals(destination)) {
731 return;
732 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000733
734 // A valid move can always be inferred from the destination and source
735 // locations. When moving from and to a register, the argument type can be
736 // used to generate 32bit instead of 64bit moves. In debug mode we also
737 // checks the coherency of the locations and the type.
738 bool unspecified_type = (type == Primitive::kPrimVoid);
739
740 if (destination.IsRegister() || destination.IsFpuRegister()) {
741 if (unspecified_type) {
742 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
743 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000744 (src_cst != nullptr && (src_cst->IsIntConstant()
745 || src_cst->IsFloatConstant()
746 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000747 // For stack slots and 32bit constants, a 64bit type is appropriate.
748 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000749 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000750 // If the source is a double stack slot or a 64bit constant, a 64bit
751 // type is appropriate. Else the source is a register, and since the
752 // type has not been specified, we chose a 64bit type to force a 64bit
753 // move.
754 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000755 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000756 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000757 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
758 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000759 CPURegister dst = CPURegisterFrom(destination, type);
760 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
761 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
762 __ Ldr(dst, StackOperandFrom(source));
763 } else if (source.IsConstant()) {
764 DCHECK(CoherentConstantAndType(source, type));
765 MoveConstant(dst, source.GetConstant());
766 } else {
767 if (destination.IsRegister()) {
768 __ Mov(Register(dst), RegisterFrom(source, type));
769 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800770 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000771 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
772 }
773 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000774 } else { // The destination is not a register. It must be a stack slot.
775 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
776 if (source.IsRegister() || source.IsFpuRegister()) {
777 if (unspecified_type) {
778 if (source.IsRegister()) {
779 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
780 } else {
781 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
782 }
783 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000784 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
785 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000786 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
787 } else if (source.IsConstant()) {
788 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
789 UseScratchRegisterScope temps(GetVIXLAssembler());
790 HConstant* src_cst = source.GetConstant();
791 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000792 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000793 temp = temps.AcquireW();
794 } else if (src_cst->IsLongConstant()) {
795 temp = temps.AcquireX();
796 } else if (src_cst->IsFloatConstant()) {
797 temp = temps.AcquireS();
798 } else {
799 DCHECK(src_cst->IsDoubleConstant());
800 temp = temps.AcquireD();
801 }
802 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000803 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000804 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000805 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000806 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000807 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000808 // There is generally less pressure on FP registers.
809 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000810 __ Ldr(temp, StackOperandFrom(source));
811 __ Str(temp, StackOperandFrom(destination));
812 }
813 }
814}
815
816void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000817 CPURegister dst,
818 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000819 switch (type) {
820 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000821 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000822 break;
823 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000824 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000825 break;
826 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000827 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000828 break;
829 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000830 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000831 break;
832 case Primitive::kPrimInt:
833 case Primitive::kPrimNot:
834 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000835 case Primitive::kPrimFloat:
836 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000837 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000838 __ Ldr(dst, src);
839 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000840 case Primitive::kPrimVoid:
841 LOG(FATAL) << "Unreachable type " << type;
842 }
843}
844
Calin Juravle77520bc2015-01-12 18:45:46 +0000845void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000846 CPURegister dst,
847 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100848 MacroAssembler* masm = GetVIXLAssembler();
849 BlockPoolsScope block_pools(masm);
850 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000851 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000852 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000853
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000854 DCHECK(!src.IsPreIndex());
855 DCHECK(!src.IsPostIndex());
856
857 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800858 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000859 MemOperand base = MemOperand(temp_base);
860 switch (type) {
861 case Primitive::kPrimBoolean:
862 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000863 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000864 break;
865 case Primitive::kPrimByte:
866 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000867 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000868 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
869 break;
870 case Primitive::kPrimChar:
871 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000872 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000873 break;
874 case Primitive::kPrimShort:
875 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000876 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000877 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
878 break;
879 case Primitive::kPrimInt:
880 case Primitive::kPrimNot:
881 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000882 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000883 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000884 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000885 break;
886 case Primitive::kPrimFloat:
887 case Primitive::kPrimDouble: {
888 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000889 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000890
891 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
892 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000893 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000894 __ Fmov(FPRegister(dst), temp);
895 break;
896 }
897 case Primitive::kPrimVoid:
898 LOG(FATAL) << "Unreachable type " << type;
899 }
900}
901
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000902void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000903 CPURegister src,
904 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000905 switch (type) {
906 case Primitive::kPrimBoolean:
907 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000908 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000909 break;
910 case Primitive::kPrimChar:
911 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000912 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000913 break;
914 case Primitive::kPrimInt:
915 case Primitive::kPrimNot:
916 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000917 case Primitive::kPrimFloat:
918 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000919 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000920 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000921 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000922 case Primitive::kPrimVoid:
923 LOG(FATAL) << "Unreachable type " << type;
924 }
925}
926
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000927void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
928 CPURegister src,
929 const MemOperand& dst) {
930 UseScratchRegisterScope temps(GetVIXLAssembler());
931 Register temp_base = temps.AcquireX();
932
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000933 DCHECK(!dst.IsPreIndex());
934 DCHECK(!dst.IsPostIndex());
935
936 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800937 Operand op = OperandFromMemOperand(dst);
938 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000939 MemOperand base = MemOperand(temp_base);
940 switch (type) {
941 case Primitive::kPrimBoolean:
942 case Primitive::kPrimByte:
943 __ Stlrb(Register(src), base);
944 break;
945 case Primitive::kPrimChar:
946 case Primitive::kPrimShort:
947 __ Stlrh(Register(src), base);
948 break;
949 case Primitive::kPrimInt:
950 case Primitive::kPrimNot:
951 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000952 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000953 __ Stlr(Register(src), base);
954 break;
955 case Primitive::kPrimFloat:
956 case Primitive::kPrimDouble: {
957 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000958 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000959
960 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
961 __ Fmov(temp, FPRegister(src));
962 __ Stlr(temp, base);
963 break;
964 }
965 case Primitive::kPrimVoid:
966 LOG(FATAL) << "Unreachable type " << type;
967 }
968}
969
Alexandre Rames67555f72014-11-18 10:55:16 +0000970void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000971 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000972 DCHECK(current_method.IsW());
973 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
974}
975
976void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
977 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000978 uint32_t dex_pc,
979 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100980 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +0000981 __ Ldr(lr, MemOperand(tr, entry_point_offset));
982 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +0100983 RecordPcInfo(instruction, dex_pc, slow_path);
984 DCHECK(instruction->IsSuspendCheck()
985 || instruction->IsBoundsCheck()
986 || instruction->IsNullCheck()
987 || instruction->IsDivZeroCheck()
988 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000989}
990
991void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
992 vixl::Register class_reg) {
993 UseScratchRegisterScope temps(GetVIXLAssembler());
994 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000995 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +0000996 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000997
Serban Constantinescu02164b32014-11-13 14:05:07 +0000998 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +0000999 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001000 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1001 __ Add(temp, class_reg, status_offset);
1002 __ Ldar(temp, HeapOperand(temp));
1003 __ Cmp(temp, mirror::Class::kStatusInitialized);
1004 __ B(lt, slow_path->GetEntryLabel());
1005 } else {
1006 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1007 __ Cmp(temp, mirror::Class::kStatusInitialized);
1008 __ B(lt, slow_path->GetEntryLabel());
1009 __ Dmb(InnerShareable, BarrierReads);
1010 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001011 __ Bind(slow_path->GetExitLabel());
1012}
Alexandre Rames5319def2014-10-23 10:03:10 +01001013
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001014void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1015 BarrierType type = BarrierAll;
1016
1017 switch (kind) {
1018 case MemBarrierKind::kAnyAny:
1019 case MemBarrierKind::kAnyStore: {
1020 type = BarrierAll;
1021 break;
1022 }
1023 case MemBarrierKind::kLoadAny: {
1024 type = BarrierReads;
1025 break;
1026 }
1027 case MemBarrierKind::kStoreStore: {
1028 type = BarrierWrites;
1029 break;
1030 }
1031 default:
1032 LOG(FATAL) << "Unexpected memory barrier " << kind;
1033 }
1034 __ Dmb(InnerShareable, type);
1035}
1036
Serban Constantinescu02164b32014-11-13 14:05:07 +00001037void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1038 HBasicBlock* successor) {
1039 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001040 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1041 if (slow_path == nullptr) {
1042 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1043 instruction->SetSlowPath(slow_path);
1044 codegen_->AddSlowPath(slow_path);
1045 if (successor != nullptr) {
1046 DCHECK(successor->IsLoopHeader());
1047 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1048 }
1049 } else {
1050 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1051 }
1052
Serban Constantinescu02164b32014-11-13 14:05:07 +00001053 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1054 Register temp = temps.AcquireW();
1055
1056 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1057 if (successor == nullptr) {
1058 __ Cbnz(temp, slow_path->GetEntryLabel());
1059 __ Bind(slow_path->GetReturnLabel());
1060 } else {
1061 __ Cbz(temp, codegen_->GetLabelOf(successor));
1062 __ B(slow_path->GetEntryLabel());
1063 // slow_path will return to GetLabelOf(successor).
1064 }
1065}
1066
Alexandre Rames5319def2014-10-23 10:03:10 +01001067InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1068 CodeGeneratorARM64* codegen)
1069 : HGraphVisitor(graph),
1070 assembler_(codegen->GetAssembler()),
1071 codegen_(codegen) {}
1072
1073#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001074 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001075
1076#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1077
1078enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001079 // Using a base helps identify when we hit such breakpoints.
1080 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001081#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1082 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1083#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1084};
1085
1086#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1087 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001088 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001089 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1090 } \
1091 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1093 locations->SetOut(Location::Any()); \
1094 }
1095 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1096#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1097
1098#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001099#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001100
Alexandre Rames67555f72014-11-18 10:55:16 +00001101void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 DCHECK_EQ(instr->InputCount(), 2U);
1103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1104 Primitive::Type type = instr->GetResultType();
1105 switch (type) {
1106 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001107 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001108 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001109 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001111 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001112
1113 case Primitive::kPrimFloat:
1114 case Primitive::kPrimDouble:
1115 locations->SetInAt(0, Location::RequiresFpuRegister());
1116 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001117 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001119
Alexandre Rames5319def2014-10-23 10:03:10 +01001120 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001121 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 }
1123}
1124
Alexandre Rames09a99962015-04-15 11:47:56 +01001125void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1126 LocationSummary* locations =
1127 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1128 locations->SetInAt(0, Location::RequiresRegister());
1129 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1130 locations->SetOut(Location::RequiresFpuRegister());
1131 } else {
1132 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1133 }
1134}
1135
1136void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1137 const FieldInfo& field_info) {
1138 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001139 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001140
1141 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1142 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1143
1144 if (field_info.IsVolatile()) {
1145 if (use_acquire_release) {
1146 // NB: LoadAcquire will record the pc info if needed.
1147 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1148 } else {
1149 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1150 codegen_->MaybeRecordImplicitNullCheck(instruction);
1151 // For IRIW sequential consistency kLoadAny is not sufficient.
1152 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1153 }
1154 } else {
1155 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1156 codegen_->MaybeRecordImplicitNullCheck(instruction);
1157 }
1158}
1159
1160void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1161 LocationSummary* locations =
1162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1163 locations->SetInAt(0, Location::RequiresRegister());
1164 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1165 locations->SetInAt(1, Location::RequiresFpuRegister());
1166 } else {
1167 locations->SetInAt(1, Location::RequiresRegister());
1168 }
1169}
1170
1171void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
1172 const FieldInfo& field_info) {
1173 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001174 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001175
1176 Register obj = InputRegisterAt(instruction, 0);
1177 CPURegister value = InputCPURegisterAt(instruction, 1);
1178 Offset offset = field_info.GetFieldOffset();
1179 Primitive::Type field_type = field_info.GetFieldType();
1180 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1181
1182 if (field_info.IsVolatile()) {
1183 if (use_acquire_release) {
1184 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1185 codegen_->MaybeRecordImplicitNullCheck(instruction);
1186 } else {
1187 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1188 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1189 codegen_->MaybeRecordImplicitNullCheck(instruction);
1190 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1191 }
1192 } else {
1193 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1194 codegen_->MaybeRecordImplicitNullCheck(instruction);
1195 }
1196
1197 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
1198 codegen_->MarkGCCard(obj, Register(value));
1199 }
1200}
1201
Alexandre Rames67555f72014-11-18 10:55:16 +00001202void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001203 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001204
1205 switch (type) {
1206 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001207 case Primitive::kPrimLong: {
1208 Register dst = OutputRegister(instr);
1209 Register lhs = InputRegisterAt(instr, 0);
1210 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001211 if (instr->IsAdd()) {
1212 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001213 } else if (instr->IsAnd()) {
1214 __ And(dst, lhs, rhs);
1215 } else if (instr->IsOr()) {
1216 __ Orr(dst, lhs, rhs);
1217 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001218 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001219 } else {
1220 DCHECK(instr->IsXor());
1221 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001222 }
1223 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001224 }
1225 case Primitive::kPrimFloat:
1226 case Primitive::kPrimDouble: {
1227 FPRegister dst = OutputFPRegister(instr);
1228 FPRegister lhs = InputFPRegisterAt(instr, 0);
1229 FPRegister rhs = InputFPRegisterAt(instr, 1);
1230 if (instr->IsAdd()) {
1231 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001232 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001233 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001234 } else {
1235 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001236 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001237 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001238 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001239 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001240 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001241 }
1242}
1243
Serban Constantinescu02164b32014-11-13 14:05:07 +00001244void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1245 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1246
1247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1248 Primitive::Type type = instr->GetResultType();
1249 switch (type) {
1250 case Primitive::kPrimInt:
1251 case Primitive::kPrimLong: {
1252 locations->SetInAt(0, Location::RequiresRegister());
1253 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1254 locations->SetOut(Location::RequiresRegister());
1255 break;
1256 }
1257 default:
1258 LOG(FATAL) << "Unexpected shift type " << type;
1259 }
1260}
1261
1262void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1263 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1264
1265 Primitive::Type type = instr->GetType();
1266 switch (type) {
1267 case Primitive::kPrimInt:
1268 case Primitive::kPrimLong: {
1269 Register dst = OutputRegister(instr);
1270 Register lhs = InputRegisterAt(instr, 0);
1271 Operand rhs = InputOperandAt(instr, 1);
1272 if (rhs.IsImmediate()) {
1273 uint32_t shift_value = (type == Primitive::kPrimInt)
1274 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1275 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1276 if (instr->IsShl()) {
1277 __ Lsl(dst, lhs, shift_value);
1278 } else if (instr->IsShr()) {
1279 __ Asr(dst, lhs, shift_value);
1280 } else {
1281 __ Lsr(dst, lhs, shift_value);
1282 }
1283 } else {
1284 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1285
1286 if (instr->IsShl()) {
1287 __ Lsl(dst, lhs, rhs_reg);
1288 } else if (instr->IsShr()) {
1289 __ Asr(dst, lhs, rhs_reg);
1290 } else {
1291 __ Lsr(dst, lhs, rhs_reg);
1292 }
1293 }
1294 break;
1295 }
1296 default:
1297 LOG(FATAL) << "Unexpected shift operation type " << type;
1298 }
1299}
1300
Alexandre Rames5319def2014-10-23 10:03:10 +01001301void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001303}
1304
1305void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001306 HandleBinaryOp(instruction);
1307}
1308
1309void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1310 HandleBinaryOp(instruction);
1311}
1312
1313void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1314 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001315}
1316
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001317void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1318 LocationSummary* locations =
1319 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1320 locations->SetInAt(0, Location::RequiresRegister());
1321 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001322 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1323 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1324 } else {
1325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1326 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327}
1328
1329void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1330 LocationSummary* locations = instruction->GetLocations();
1331 Primitive::Type type = instruction->GetType();
1332 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001333 Location index = locations->InAt(1);
1334 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001335 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001336 MacroAssembler* masm = GetVIXLAssembler();
1337 UseScratchRegisterScope temps(masm);
1338 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339
1340 if (index.IsConstant()) {
1341 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001342 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001343 } else {
1344 Register temp = temps.AcquireSameSizeAs(obj);
1345 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1346 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001347 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001348 }
1349
Alexandre Rames67555f72014-11-18 10:55:16 +00001350 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001352}
1353
Alexandre Rames5319def2014-10-23 10:03:10 +01001354void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1355 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1356 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001358}
1359
1360void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001361 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001362 __ Ldr(OutputRegister(instruction),
1363 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001364 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001365}
1366
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001367void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001368 if (instruction->NeedsTypeCheck()) {
1369 LocationSummary* locations =
1370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001371 InvokeRuntimeCallingConvention calling_convention;
1372 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1373 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1374 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1375 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001376 LocationSummary* locations =
1377 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001378 locations->SetInAt(0, Location::RequiresRegister());
1379 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001380 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1381 locations->SetInAt(2, Location::RequiresFpuRegister());
1382 } else {
1383 locations->SetInAt(2, Location::RequiresRegister());
1384 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001385 }
1386}
1387
1388void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1389 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001390 LocationSummary* locations = instruction->GetLocations();
1391 bool needs_runtime_call = locations->WillCall();
1392
1393 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001394 codegen_->InvokeRuntime(
1395 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001396 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001397 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001398 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001399 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001400 Location index = locations->InAt(1);
1401 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001402 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001403 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001404 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001405 {
1406 // We use a block to end the scratch scope before the write barrier, thus
1407 // freeing the temporary registers so they can be used in `MarkGCCard`.
1408 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001409
Alexandre Rames97833a02015-04-16 15:07:12 +01001410 if (index.IsConstant()) {
1411 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1412 destination = HeapOperand(obj, offset);
1413 } else {
1414 Register temp = temps.AcquireSameSizeAs(obj);
1415 Register index_reg = InputRegisterAt(instruction, 1);
1416 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1417 destination = HeapOperand(temp, offset);
1418 }
1419
1420 codegen_->Store(value_type, value, destination);
1421 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001422 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001423 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
1424 codegen_->MarkGCCard(obj, value.W());
1425 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001426 }
1427}
1428
Alexandre Rames67555f72014-11-18 10:55:16 +00001429void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1430 LocationSummary* locations =
1431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1432 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001433 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001434 if (instruction->HasUses()) {
1435 locations->SetOut(Location::SameAsFirstInput());
1436 }
1437}
1438
1439void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001440 LocationSummary* locations = instruction->GetLocations();
1441 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1442 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001443 codegen_->AddSlowPath(slow_path);
1444
1445 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1446 __ B(slow_path->GetEntryLabel(), hs);
1447}
1448
1449void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1451 instruction, LocationSummary::kCallOnSlowPath);
1452 locations->SetInAt(0, Location::RequiresRegister());
1453 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001454 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001455}
1456
1457void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001458 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001459 Register obj = InputRegisterAt(instruction, 0);;
1460 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001462
Alexandre Rames3e69f162014-12-10 10:36:50 +00001463 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1464 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001465 codegen_->AddSlowPath(slow_path);
1466
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001467 // Avoid null check if we know obj is not null.
1468 if (instruction->MustDoNullCheck()) {
1469 __ Cbz(obj, slow_path->GetExitLabel());
1470 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001471 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001472 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1473 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001474 __ B(ne, slow_path->GetEntryLabel());
1475 __ Bind(slow_path->GetExitLabel());
1476}
1477
1478void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1479 LocationSummary* locations =
1480 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1481 locations->SetInAt(0, Location::RequiresRegister());
1482 if (check->HasUses()) {
1483 locations->SetOut(Location::SameAsFirstInput());
1484 }
1485}
1486
1487void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1488 // We assume the class is not null.
1489 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1490 check->GetLoadClass(), check, check->GetDexPc(), true);
1491 codegen_->AddSlowPath(slow_path);
1492 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1493}
1494
Serban Constantinescu02164b32014-11-13 14:05:07 +00001495void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001496 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001497 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1498 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001499 switch (in_type) {
1500 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001501 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001502 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1504 break;
1505 }
1506 case Primitive::kPrimFloat:
1507 case Primitive::kPrimDouble: {
1508 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001509 HInstruction* right = compare->InputAt(1);
1510 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1511 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1512 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1513 } else {
1514 locations->SetInAt(1, Location::RequiresFpuRegister());
1515 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001516 locations->SetOut(Location::RequiresRegister());
1517 break;
1518 }
1519 default:
1520 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1521 }
1522}
1523
1524void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1525 Primitive::Type in_type = compare->InputAt(0)->GetType();
1526
1527 // 0 if: left == right
1528 // 1 if: left > right
1529 // -1 if: left < right
1530 switch (in_type) {
1531 case Primitive::kPrimLong: {
1532 Register result = OutputRegister(compare);
1533 Register left = InputRegisterAt(compare, 0);
1534 Operand right = InputOperandAt(compare, 1);
1535
1536 __ Cmp(left, right);
1537 __ Cset(result, ne);
1538 __ Cneg(result, result, lt);
1539 break;
1540 }
1541 case Primitive::kPrimFloat:
1542 case Primitive::kPrimDouble: {
1543 Register result = OutputRegister(compare);
1544 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001545 if (compare->GetLocations()->InAt(1).IsConstant()) {
1546 if (kIsDebugBuild) {
1547 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1548 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1549 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1550 }
1551 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1552 __ Fcmp(left, 0.0);
1553 } else {
1554 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1555 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001556 if (compare->IsGtBias()) {
1557 __ Cset(result, ne);
1558 } else {
1559 __ Csetm(result, ne);
1560 }
1561 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001562 break;
1563 }
1564 default:
1565 LOG(FATAL) << "Unimplemented compare type " << in_type;
1566 }
1567}
1568
1569void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1570 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1571 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001572 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001573 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001575 }
1576}
1577
1578void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1579 if (!instruction->NeedsMaterialization()) {
1580 return;
1581 }
1582
1583 LocationSummary* locations = instruction->GetLocations();
1584 Register lhs = InputRegisterAt(instruction, 0);
1585 Operand rhs = InputOperandAt(instruction, 1);
1586 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1587 Condition cond = ARM64Condition(instruction->GetCondition());
1588
1589 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001590 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001591}
1592
1593#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1594 M(Equal) \
1595 M(NotEqual) \
1596 M(LessThan) \
1597 M(LessThanOrEqual) \
1598 M(GreaterThan) \
1599 M(GreaterThanOrEqual)
1600#define DEFINE_CONDITION_VISITORS(Name) \
1601void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1602void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1603FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001604#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001605#undef FOR_EACH_CONDITION_INSTRUCTION
1606
Zheng Xuc6667102015-05-15 16:08:45 +08001607void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1608 DCHECK(instruction->IsDiv() || instruction->IsRem());
1609
1610 LocationSummary* locations = instruction->GetLocations();
1611 Location second = locations->InAt(1);
1612 DCHECK(second.IsConstant());
1613
1614 Register out = OutputRegister(instruction);
1615 Register dividend = InputRegisterAt(instruction, 0);
1616 int64_t imm = Int64FromConstant(second.GetConstant());
1617 DCHECK(imm == 1 || imm == -1);
1618
1619 if (instruction->IsRem()) {
1620 __ Mov(out, 0);
1621 } else {
1622 if (imm == 1) {
1623 __ Mov(out, dividend);
1624 } else {
1625 __ Neg(out, dividend);
1626 }
1627 }
1628}
1629
1630void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1631 DCHECK(instruction->IsDiv() || instruction->IsRem());
1632
1633 LocationSummary* locations = instruction->GetLocations();
1634 Location second = locations->InAt(1);
1635 DCHECK(second.IsConstant());
1636
1637 Register out = OutputRegister(instruction);
1638 Register dividend = InputRegisterAt(instruction, 0);
1639 int64_t imm = Int64FromConstant(second.GetConstant());
1640 int64_t abs_imm = std::abs(imm);
1641 DCHECK(IsPowerOfTwo(abs_imm));
1642 int ctz_imm = CTZ(abs_imm);
1643
1644 UseScratchRegisterScope temps(GetVIXLAssembler());
1645 Register temp = temps.AcquireSameSizeAs(out);
1646
1647 if (instruction->IsDiv()) {
1648 __ Add(temp, dividend, abs_imm - 1);
1649 __ Cmp(dividend, 0);
1650 __ Csel(out, temp, dividend, lt);
1651 if (imm > 0) {
1652 __ Asr(out, out, ctz_imm);
1653 } else {
1654 __ Neg(out, Operand(out, ASR, ctz_imm));
1655 }
1656 } else {
1657 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1658 __ Asr(temp, dividend, bits - 1);
1659 __ Lsr(temp, temp, bits - ctz_imm);
1660 __ Add(out, dividend, temp);
1661 __ And(out, out, abs_imm - 1);
1662 __ Sub(out, out, temp);
1663 }
1664}
1665
1666void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1667 DCHECK(instruction->IsDiv() || instruction->IsRem());
1668
1669 LocationSummary* locations = instruction->GetLocations();
1670 Location second = locations->InAt(1);
1671 DCHECK(second.IsConstant());
1672
1673 Register out = OutputRegister(instruction);
1674 Register dividend = InputRegisterAt(instruction, 0);
1675 int64_t imm = Int64FromConstant(second.GetConstant());
1676
1677 Primitive::Type type = instruction->GetResultType();
1678 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1679
1680 int64_t magic;
1681 int shift;
1682 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1683
1684 UseScratchRegisterScope temps(GetVIXLAssembler());
1685 Register temp = temps.AcquireSameSizeAs(out);
1686
1687 // temp = get_high(dividend * magic)
1688 __ Mov(temp, magic);
1689 if (type == Primitive::kPrimLong) {
1690 __ Smulh(temp, dividend, temp);
1691 } else {
1692 __ Smull(temp.X(), dividend, temp);
1693 __ Lsr(temp.X(), temp.X(), 32);
1694 }
1695
1696 if (imm > 0 && magic < 0) {
1697 __ Add(temp, temp, dividend);
1698 } else if (imm < 0 && magic > 0) {
1699 __ Sub(temp, temp, dividend);
1700 }
1701
1702 if (shift != 0) {
1703 __ Asr(temp, temp, shift);
1704 }
1705
1706 if (instruction->IsDiv()) {
1707 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1708 } else {
1709 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1710 // TODO: Strength reduction for msub.
1711 Register temp_imm = temps.AcquireSameSizeAs(out);
1712 __ Mov(temp_imm, imm);
1713 __ Msub(out, temp, temp_imm, dividend);
1714 }
1715}
1716
1717void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1718 DCHECK(instruction->IsDiv() || instruction->IsRem());
1719 Primitive::Type type = instruction->GetResultType();
1720 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1721
1722 LocationSummary* locations = instruction->GetLocations();
1723 Register out = OutputRegister(instruction);
1724 Location second = locations->InAt(1);
1725
1726 if (second.IsConstant()) {
1727 int64_t imm = Int64FromConstant(second.GetConstant());
1728
1729 if (imm == 0) {
1730 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1731 } else if (imm == 1 || imm == -1) {
1732 DivRemOneOrMinusOne(instruction);
1733 } else if (IsPowerOfTwo(std::abs(imm))) {
1734 DivRemByPowerOfTwo(instruction);
1735 } else {
1736 DCHECK(imm <= -2 || imm >= 2);
1737 GenerateDivRemWithAnyConstant(instruction);
1738 }
1739 } else {
1740 Register dividend = InputRegisterAt(instruction, 0);
1741 Register divisor = InputRegisterAt(instruction, 1);
1742 if (instruction->IsDiv()) {
1743 __ Sdiv(out, dividend, divisor);
1744 } else {
1745 UseScratchRegisterScope temps(GetVIXLAssembler());
1746 Register temp = temps.AcquireSameSizeAs(out);
1747 __ Sdiv(temp, dividend, divisor);
1748 __ Msub(out, temp, divisor, dividend);
1749 }
1750 }
1751}
1752
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001753void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1754 LocationSummary* locations =
1755 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1756 switch (div->GetResultType()) {
1757 case Primitive::kPrimInt:
1758 case Primitive::kPrimLong:
1759 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001760 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1762 break;
1763
1764 case Primitive::kPrimFloat:
1765 case Primitive::kPrimDouble:
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
1767 locations->SetInAt(1, Location::RequiresFpuRegister());
1768 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1773 }
1774}
1775
1776void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1777 Primitive::Type type = div->GetResultType();
1778 switch (type) {
1779 case Primitive::kPrimInt:
1780 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001781 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001782 break;
1783
1784 case Primitive::kPrimFloat:
1785 case Primitive::kPrimDouble:
1786 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1787 break;
1788
1789 default:
1790 LOG(FATAL) << "Unexpected div type " << type;
1791 }
1792}
1793
Alexandre Rames67555f72014-11-18 10:55:16 +00001794void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1795 LocationSummary* locations =
1796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1797 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1798 if (instruction->HasUses()) {
1799 locations->SetOut(Location::SameAsFirstInput());
1800 }
1801}
1802
1803void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1804 SlowPathCodeARM64* slow_path =
1805 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1806 codegen_->AddSlowPath(slow_path);
1807 Location value = instruction->GetLocations()->InAt(0);
1808
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 Primitive::Type type = instruction->GetType();
1810
1811 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1812 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1813 return;
1814 }
1815
Alexandre Rames67555f72014-11-18 10:55:16 +00001816 if (value.IsConstant()) {
1817 int64_t divisor = Int64ConstantFrom(value);
1818 if (divisor == 0) {
1819 __ B(slow_path->GetEntryLabel());
1820 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001821 // A division by a non-null constant is valid. We don't need to perform
1822 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001823 }
1824 } else {
1825 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1826 }
1827}
1828
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001829void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1832 locations->SetOut(Location::ConstantLocation(constant));
1833}
1834
1835void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1836 UNUSED(constant);
1837 // Will be generated at use site.
1838}
1839
Alexandre Rames5319def2014-10-23 10:03:10 +01001840void LocationsBuilderARM64::VisitExit(HExit* exit) {
1841 exit->SetLocations(nullptr);
1842}
1843
1844void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001845 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001846}
1847
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001848void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1849 LocationSummary* locations =
1850 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1851 locations->SetOut(Location::ConstantLocation(constant));
1852}
1853
1854void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1855 UNUSED(constant);
1856 // Will be generated at use site.
1857}
1858
Alexandre Rames5319def2014-10-23 10:03:10 +01001859void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1860 got->SetLocations(nullptr);
1861}
1862
1863void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1864 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001865 DCHECK(!successor->IsExitBlock());
1866 HBasicBlock* block = got->GetBlock();
1867 HInstruction* previous = got->GetPrevious();
1868 HLoopInformation* info = block->GetLoopInformation();
1869
David Brazdil46e2a392015-03-16 17:31:52 +00001870 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001871 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1872 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1873 return;
1874 }
1875 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1876 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1877 }
1878 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001879 __ B(codegen_->GetLabelOf(successor));
1880 }
1881}
1882
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001883void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1884 vixl::Label* true_target,
1885 vixl::Label* false_target,
1886 vixl::Label* always_true_target) {
1887 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001888 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001889
Serban Constantinescu02164b32014-11-13 14:05:07 +00001890 if (cond->IsIntConstant()) {
1891 int32_t cond_value = cond->AsIntConstant()->GetValue();
1892 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001893 if (always_true_target != nullptr) {
1894 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001895 }
1896 return;
1897 } else {
1898 DCHECK_EQ(cond_value, 0);
1899 }
1900 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001901 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001902 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001903 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001904 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001905 } else {
1906 // The condition instruction has not been materialized, use its inputs as
1907 // the comparison and its condition as the branch condition.
1908 Register lhs = InputRegisterAt(condition, 0);
1909 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001910 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00001911 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1912 switch (arm64_cond) {
1913 case eq:
1914 __ Cbz(lhs, true_target);
1915 break;
1916 case ne:
1917 __ Cbnz(lhs, true_target);
1918 break;
1919 case lt:
1920 // Test the sign bit and branch accordingly.
1921 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1922 break;
1923 case ge:
1924 // Test the sign bit and branch accordingly.
1925 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1926 break;
1927 default:
1928 // Without the `static_cast` the compiler throws an error for
1929 // `-Werror=sign-promo`.
1930 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001931 }
1932 } else {
1933 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001934 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001935 }
1936 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001937 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001938 __ B(false_target);
1939 }
1940}
1941
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001942void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1944 HInstruction* cond = if_instr->InputAt(0);
1945 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1946 locations->SetInAt(0, Location::RequiresRegister());
1947 }
1948}
1949
1950void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1951 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1952 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1953 vixl::Label* always_true_target = true_target;
1954 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1955 if_instr->IfTrueSuccessor())) {
1956 always_true_target = nullptr;
1957 }
1958 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1959 if_instr->IfFalseSuccessor())) {
1960 false_target = nullptr;
1961 }
1962 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1963}
1964
1965void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1966 LocationSummary* locations = new (GetGraph()->GetArena())
1967 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1968 HInstruction* cond = deoptimize->InputAt(0);
1969 DCHECK(cond->IsCondition());
1970 if (cond->AsCondition()->NeedsMaterialization()) {
1971 locations->SetInAt(0, Location::RequiresRegister());
1972 }
1973}
1974
1975void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1976 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
1977 DeoptimizationSlowPathARM64(deoptimize);
1978 codegen_->AddSlowPath(slow_path);
1979 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
1980 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1981}
1982
Alexandre Rames5319def2014-10-23 10:03:10 +01001983void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001984 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001985}
1986
1987void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001988 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001989}
1990
1991void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001992 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001993}
1994
1995void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001996 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001997}
1998
Alexandre Rames67555f72014-11-18 10:55:16 +00001999void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2000 LocationSummary::CallKind call_kind =
2001 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2002 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2003 locations->SetInAt(0, Location::RequiresRegister());
2004 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002005 // The output does overlap inputs.
2006 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002007}
2008
2009void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2010 LocationSummary* locations = instruction->GetLocations();
2011 Register obj = InputRegisterAt(instruction, 0);;
2012 Register cls = InputRegisterAt(instruction, 1);;
2013 Register out = OutputRegister(instruction);
2014
2015 vixl::Label done;
2016
2017 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002018 // Avoid null check if we know `obj` is not null.
2019 if (instruction->MustDoNullCheck()) {
2020 __ Mov(out, 0);
2021 __ Cbz(obj, &done);
2022 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002023
2024 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002025 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002026 __ Cmp(out, cls);
2027 if (instruction->IsClassFinal()) {
2028 // Classes must be equal for the instanceof to succeed.
2029 __ Cset(out, eq);
2030 } else {
2031 // If the classes are not equal, we go into a slow path.
2032 DCHECK(locations->OnlyCallsOnSlowPath());
2033 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002034 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2035 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002036 codegen_->AddSlowPath(slow_path);
2037 __ B(ne, slow_path->GetEntryLabel());
2038 __ Mov(out, 1);
2039 __ Bind(slow_path->GetExitLabel());
2040 }
2041
2042 __ Bind(&done);
2043}
2044
Alexandre Rames5319def2014-10-23 10:03:10 +01002045void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2046 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2047 locations->SetOut(Location::ConstantLocation(constant));
2048}
2049
2050void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2051 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002052 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002053}
2054
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002055void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2056 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2057 locations->SetOut(Location::ConstantLocation(constant));
2058}
2059
2060void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2061 // Will be generated at use site.
2062 UNUSED(constant);
2063}
2064
Alexandre Rames5319def2014-10-23 10:03:10 +01002065void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
2066 LocationSummary* locations =
2067 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
2068 locations->AddTemp(LocationFrom(x0));
2069
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002070 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01002071 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002072 HInstruction* input = invoke->InputAt(i);
2073 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
2074 }
2075
2076 Primitive::Type return_type = invoke->GetType();
2077 if (return_type != Primitive::kPrimVoid) {
2078 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
2079 }
2080}
2081
Alexandre Rames67555f72014-11-18 10:55:16 +00002082void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2083 HandleInvoke(invoke);
2084}
2085
2086void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2087 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2088 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
2089 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
2090 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
2091 Location receiver = invoke->GetLocations()->InAt(0);
2092 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002093 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002094
2095 // The register ip1 is required to be used for the hidden argument in
2096 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002097 MacroAssembler* masm = GetVIXLAssembler();
2098 UseScratchRegisterScope scratch_scope(masm);
2099 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002100 scratch_scope.Exclude(ip1);
2101 __ Mov(ip1, invoke->GetDexMethodIndex());
2102
2103 // temp = object->GetClass();
2104 if (receiver.IsStackSlot()) {
2105 __ Ldr(temp, StackOperandFrom(receiver));
2106 __ Ldr(temp, HeapOperand(temp, class_offset));
2107 } else {
2108 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
2109 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002110 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00002111 // temp = temp->GetImtEntryAt(method_offset);
2112 __ Ldr(temp, HeapOperand(temp, method_offset));
2113 // lr = temp->GetEntryPoint();
2114 __ Ldr(lr, HeapOperand(temp, entry_point));
2115 // lr();
2116 __ Blr(lr);
2117 DCHECK(!codegen_->IsLeafMethod());
2118 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2119}
2120
2121void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002122 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2123 if (intrinsic.TryDispatch(invoke)) {
2124 return;
2125 }
2126
Alexandre Rames67555f72014-11-18 10:55:16 +00002127 HandleInvoke(invoke);
2128}
2129
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002130void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002131 // When we do not run baseline, explicit clinit checks triggered by static
2132 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2133 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002134
Andreas Gampe878d58c2015-01-15 23:24:00 -08002135 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2136 if (intrinsic.TryDispatch(invoke)) {
2137 return;
2138 }
2139
Alexandre Rames67555f72014-11-18 10:55:16 +00002140 HandleInvoke(invoke);
2141}
2142
Andreas Gampe878d58c2015-01-15 23:24:00 -08002143static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2144 if (invoke->GetLocations()->Intrinsified()) {
2145 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2146 intrinsic.Dispatch(invoke);
2147 return true;
2148 }
2149 return false;
2150}
2151
2152void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
2153 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
2154 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01002155 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08002156 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01002157
2158 // TODO: Implement all kinds of calls:
2159 // 1) boot -> boot
2160 // 2) app -> boot
2161 // 3) app -> app
2162 //
2163 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2164
Jeff Hao848f70a2014-01-15 13:49:50 -08002165 if (invoke->IsStringInit()) {
2166 // temp = thread->string_init_entrypoint
2167 __ Ldr(temp, HeapOperand(tr, invoke->GetStringInitOffset()));
2168 // LR = temp->entry_point_from_quick_compiled_code_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002169 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2170 kArm64WordSize)));
Jeff Hao848f70a2014-01-15 13:49:50 -08002171 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002172 __ Blr(lr);
2173 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08002174 // temp = method;
2175 LoadCurrentMethod(temp);
2176 if (!invoke->IsRecursive()) {
2177 // temp = temp->dex_cache_resolved_methods_;
2178 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
2179 // temp = temp[index_in_cache];
2180 __ Ldr(temp, HeapOperand(temp, index_in_cache));
2181 // lr = temp->entry_point_from_quick_compiled_code_;
2182 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2183 kArm64WordSize)));
2184 // lr();
2185 __ Blr(lr);
2186 } else {
2187 __ Bl(&frame_entry_label_);
2188 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002189 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002190
Andreas Gampe878d58c2015-01-15 23:24:00 -08002191 DCHECK(!IsLeafMethod());
2192}
2193
2194void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002195 // When we do not run baseline, explicit clinit checks triggered by static
2196 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2197 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002198
Andreas Gampe878d58c2015-01-15 23:24:00 -08002199 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2200 return;
2201 }
2202
Alexandre Ramesd921d642015-04-16 15:07:16 +01002203 BlockPoolsScope block_pools(GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -08002204 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
2205 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002206 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002207}
2208
2209void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002210 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2211 return;
2212 }
2213
Alexandre Rames5319def2014-10-23 10:03:10 +01002214 LocationSummary* locations = invoke->GetLocations();
2215 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002216 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002217 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
2218 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
2219 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002220 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002221
Alexandre Ramesd921d642015-04-16 15:07:16 +01002222 BlockPoolsScope block_pools(GetVIXLAssembler());
2223
Alexandre Rames5319def2014-10-23 10:03:10 +01002224 // temp = object->GetClass();
2225 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002226 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
2227 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002228 } else {
2229 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002230 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002231 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002232 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002233 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002234 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002235 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002236 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002237 // lr();
2238 __ Blr(lr);
2239 DCHECK(!codegen_->IsLeafMethod());
2240 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2241}
2242
Alexandre Rames67555f72014-11-18 10:55:16 +00002243void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2244 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2245 : LocationSummary::kNoCall;
2246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2247 locations->SetOut(Location::RequiresRegister());
2248}
2249
2250void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2251 Register out = OutputRegister(cls);
2252 if (cls->IsReferrersClass()) {
2253 DCHECK(!cls->CanCallRuntime());
2254 DCHECK(!cls->MustGenerateClinitCheck());
2255 codegen_->LoadCurrentMethod(out);
2256 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2257 } else {
2258 DCHECK(cls->CanCallRuntime());
2259 codegen_->LoadCurrentMethod(out);
2260 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002261 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002262
2263 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2264 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2265 codegen_->AddSlowPath(slow_path);
2266 __ Cbz(out, slow_path->GetEntryLabel());
2267 if (cls->MustGenerateClinitCheck()) {
2268 GenerateClassInitializationCheck(slow_path, out);
2269 } else {
2270 __ Bind(slow_path->GetExitLabel());
2271 }
2272 }
2273}
2274
2275void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2276 LocationSummary* locations =
2277 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2278 locations->SetOut(Location::RequiresRegister());
2279}
2280
2281void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2282 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2283 __ Ldr(OutputRegister(instruction), exception);
2284 __ Str(wzr, exception);
2285}
2286
Alexandre Rames5319def2014-10-23 10:03:10 +01002287void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2288 load->SetLocations(nullptr);
2289}
2290
2291void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2292 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002293 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002294}
2295
Alexandre Rames67555f72014-11-18 10:55:16 +00002296void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2297 LocationSummary* locations =
2298 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2299 locations->SetOut(Location::RequiresRegister());
2300}
2301
2302void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2303 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2304 codegen_->AddSlowPath(slow_path);
2305
2306 Register out = OutputRegister(load);
2307 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002308 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2309 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002310 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002311 __ Cbz(out, slow_path->GetEntryLabel());
2312 __ Bind(slow_path->GetExitLabel());
2313}
2314
Alexandre Rames5319def2014-10-23 10:03:10 +01002315void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2316 local->SetLocations(nullptr);
2317}
2318
2319void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2320 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2321}
2322
2323void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2324 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2325 locations->SetOut(Location::ConstantLocation(constant));
2326}
2327
2328void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2329 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002330 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002331}
2332
Alexandre Rames67555f72014-11-18 10:55:16 +00002333void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2334 LocationSummary* locations =
2335 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2336 InvokeRuntimeCallingConvention calling_convention;
2337 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2338}
2339
2340void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2341 codegen_->InvokeRuntime(instruction->IsEnter()
2342 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2343 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002344 instruction->GetDexPc(),
2345 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002346 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002347}
2348
Alexandre Rames42d641b2014-10-27 14:00:51 +00002349void LocationsBuilderARM64::VisitMul(HMul* mul) {
2350 LocationSummary* locations =
2351 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2352 switch (mul->GetResultType()) {
2353 case Primitive::kPrimInt:
2354 case Primitive::kPrimLong:
2355 locations->SetInAt(0, Location::RequiresRegister());
2356 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002358 break;
2359
2360 case Primitive::kPrimFloat:
2361 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002362 locations->SetInAt(0, Location::RequiresFpuRegister());
2363 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002364 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002365 break;
2366
2367 default:
2368 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2369 }
2370}
2371
2372void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2373 switch (mul->GetResultType()) {
2374 case Primitive::kPrimInt:
2375 case Primitive::kPrimLong:
2376 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2377 break;
2378
2379 case Primitive::kPrimFloat:
2380 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002381 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002382 break;
2383
2384 default:
2385 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2386 }
2387}
2388
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002389void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2390 LocationSummary* locations =
2391 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2392 switch (neg->GetResultType()) {
2393 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002394 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002395 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002397 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002398
2399 case Primitive::kPrimFloat:
2400 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002401 locations->SetInAt(0, Location::RequiresFpuRegister());
2402 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002403 break;
2404
2405 default:
2406 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2407 }
2408}
2409
2410void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2411 switch (neg->GetResultType()) {
2412 case Primitive::kPrimInt:
2413 case Primitive::kPrimLong:
2414 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2415 break;
2416
2417 case Primitive::kPrimFloat:
2418 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002419 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2424 }
2425}
2426
2427void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2428 LocationSummary* locations =
2429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2430 InvokeRuntimeCallingConvention calling_convention;
2431 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002432 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002433 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002434 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2435 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2436 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002437}
2438
2439void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2440 LocationSummary* locations = instruction->GetLocations();
2441 InvokeRuntimeCallingConvention calling_convention;
2442 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2443 DCHECK(type_index.Is(w0));
2444 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002445 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002446 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002447 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002449 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2450 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002451 instruction->GetDexPc(),
2452 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002453 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2454 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002455}
2456
Alexandre Rames5319def2014-10-23 10:03:10 +01002457void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2458 LocationSummary* locations =
2459 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2460 InvokeRuntimeCallingConvention calling_convention;
2461 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2462 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2463 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002464 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002465}
2466
2467void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2468 LocationSummary* locations = instruction->GetLocations();
2469 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2470 DCHECK(type_index.Is(w0));
2471 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2472 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002473 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002474 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002475 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002476 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2477 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002478 instruction->GetDexPc(),
2479 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002480 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002481}
2482
2483void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002485 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002487}
2488
2489void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002490 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002491 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002492 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002493 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002494 break;
2495
2496 default:
2497 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2498 }
2499}
2500
David Brazdil66d126e2015-04-03 16:02:44 +01002501void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2502 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2503 locations->SetInAt(0, Location::RequiresRegister());
2504 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2505}
2506
2507void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002508 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2509}
2510
Alexandre Rames5319def2014-10-23 10:03:10 +01002511void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2512 LocationSummary* locations =
2513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2514 locations->SetInAt(0, Location::RequiresRegister());
2515 if (instruction->HasUses()) {
2516 locations->SetOut(Location::SameAsFirstInput());
2517 }
2518}
2519
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002520void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002521 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2522 return;
2523 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002524
Alexandre Ramesd921d642015-04-16 15:07:16 +01002525 BlockPoolsScope block_pools(GetVIXLAssembler());
2526 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002527 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2528 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2529}
2530
2531void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002532 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2533 codegen_->AddSlowPath(slow_path);
2534
2535 LocationSummary* locations = instruction->GetLocations();
2536 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002537
2538 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002539}
2540
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002541void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2542 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2543 GenerateImplicitNullCheck(instruction);
2544 } else {
2545 GenerateExplicitNullCheck(instruction);
2546 }
2547}
2548
Alexandre Rames67555f72014-11-18 10:55:16 +00002549void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2550 HandleBinaryOp(instruction);
2551}
2552
2553void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2554 HandleBinaryOp(instruction);
2555}
2556
Alexandre Rames3e69f162014-12-10 10:36:50 +00002557void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2558 LOG(FATAL) << "Unreachable";
2559}
2560
2561void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2562 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2563}
2564
Alexandre Rames5319def2014-10-23 10:03:10 +01002565void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2567 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2568 if (location.IsStackSlot()) {
2569 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2570 } else if (location.IsDoubleStackSlot()) {
2571 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2572 }
2573 locations->SetOut(location);
2574}
2575
2576void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2577 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002578 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002579}
2580
2581void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2583 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2584 locations->SetInAt(i, Location::Any());
2585 }
2586 locations->SetOut(Location::Any());
2587}
2588
2589void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002590 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002591 LOG(FATAL) << "Unreachable";
2592}
2593
Serban Constantinescu02164b32014-11-13 14:05:07 +00002594void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002595 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002596 LocationSummary::CallKind call_kind =
2597 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2599
2600 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002601 case Primitive::kPrimInt:
2602 case Primitive::kPrimLong:
2603 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002604 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 break;
2607
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002608 case Primitive::kPrimFloat:
2609 case Primitive::kPrimDouble: {
2610 InvokeRuntimeCallingConvention calling_convention;
2611 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2612 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2613 locations->SetOut(calling_convention.GetReturnLocation(type));
2614
2615 break;
2616 }
2617
Serban Constantinescu02164b32014-11-13 14:05:07 +00002618 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002619 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002620 }
2621}
2622
2623void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2624 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002625
Serban Constantinescu02164b32014-11-13 14:05:07 +00002626 switch (type) {
2627 case Primitive::kPrimInt:
2628 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002629 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002630 break;
2631 }
2632
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002633 case Primitive::kPrimFloat:
2634 case Primitive::kPrimDouble: {
2635 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2636 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002637 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002638 break;
2639 }
2640
Serban Constantinescu02164b32014-11-13 14:05:07 +00002641 default:
2642 LOG(FATAL) << "Unexpected rem type " << type;
2643 }
2644}
2645
Calin Juravle27df7582015-04-17 19:12:31 +01002646void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2647 memory_barrier->SetLocations(nullptr);
2648}
2649
2650void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2651 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2652}
2653
Alexandre Rames5319def2014-10-23 10:03:10 +01002654void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2655 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2656 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002657 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002658}
2659
2660void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002661 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002662 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002663}
2664
2665void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2666 instruction->SetLocations(nullptr);
2667}
2668
2669void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002670 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002671 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002672}
2673
Serban Constantinescu02164b32014-11-13 14:05:07 +00002674void LocationsBuilderARM64::VisitShl(HShl* shl) {
2675 HandleShift(shl);
2676}
2677
2678void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2679 HandleShift(shl);
2680}
2681
2682void LocationsBuilderARM64::VisitShr(HShr* shr) {
2683 HandleShift(shr);
2684}
2685
2686void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2687 HandleShift(shr);
2688}
2689
Alexandre Rames5319def2014-10-23 10:03:10 +01002690void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2692 Primitive::Type field_type = store->InputAt(1)->GetType();
2693 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002694 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002695 case Primitive::kPrimBoolean:
2696 case Primitive::kPrimByte:
2697 case Primitive::kPrimChar:
2698 case Primitive::kPrimShort:
2699 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002700 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002701 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2702 break;
2703
2704 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002705 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002706 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2707 break;
2708
2709 default:
2710 LOG(FATAL) << "Unimplemented local type " << field_type;
2711 }
2712}
2713
2714void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002715 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002716}
2717
2718void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002719 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002720}
2721
2722void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002723 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002724}
2725
Alexandre Rames67555f72014-11-18 10:55:16 +00002726void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002727 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002728}
2729
2730void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002731 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002732}
2733
2734void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002735 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002736}
2737
Alexandre Rames67555f72014-11-18 10:55:16 +00002738void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002739 HandleFieldSet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002740}
2741
2742void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2743 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2744}
2745
2746void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002747 HBasicBlock* block = instruction->GetBlock();
2748 if (block->GetLoopInformation() != nullptr) {
2749 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2750 // The back edge will generate the suspend check.
2751 return;
2752 }
2753 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2754 // The goto will generate the suspend check.
2755 return;
2756 }
2757 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002758}
2759
2760void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2761 temp->SetLocations(nullptr);
2762}
2763
2764void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2765 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002766 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002767}
2768
Alexandre Rames67555f72014-11-18 10:55:16 +00002769void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2770 LocationSummary* locations =
2771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2772 InvokeRuntimeCallingConvention calling_convention;
2773 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2774}
2775
2776void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2777 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002778 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002779 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002780}
2781
2782void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2783 LocationSummary* locations =
2784 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2785 Primitive::Type input_type = conversion->GetInputType();
2786 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002787 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002788 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2789 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2790 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2791 }
2792
Alexandre Rames542361f2015-01-29 16:57:31 +00002793 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002794 locations->SetInAt(0, Location::RequiresFpuRegister());
2795 } else {
2796 locations->SetInAt(0, Location::RequiresRegister());
2797 }
2798
Alexandre Rames542361f2015-01-29 16:57:31 +00002799 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002800 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2801 } else {
2802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2803 }
2804}
2805
2806void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2807 Primitive::Type result_type = conversion->GetResultType();
2808 Primitive::Type input_type = conversion->GetInputType();
2809
2810 DCHECK_NE(input_type, result_type);
2811
Alexandre Rames542361f2015-01-29 16:57:31 +00002812 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002813 int result_size = Primitive::ComponentSize(result_type);
2814 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002815 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002816 Register output = OutputRegister(conversion);
2817 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002818 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2819 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2820 } else if ((result_type == Primitive::kPrimChar) ||
2821 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2822 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002823 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002824 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002825 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002826 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002827 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002828 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002829 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2830 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002831 } else if (Primitive::IsFloatingPointType(result_type) &&
2832 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002833 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2834 } else {
2835 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2836 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002837 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002838}
Alexandre Rames67555f72014-11-18 10:55:16 +00002839
Serban Constantinescu02164b32014-11-13 14:05:07 +00002840void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2841 HandleShift(ushr);
2842}
2843
2844void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2845 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002846}
2847
2848void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2849 HandleBinaryOp(instruction);
2850}
2851
2852void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2853 HandleBinaryOp(instruction);
2854}
2855
Calin Juravleb1498f62015-02-16 13:13:29 +00002856void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2857 // Nothing to do, this should be removed during prepare for register allocator.
2858 UNUSED(instruction);
2859 LOG(FATAL) << "Unreachable";
2860}
2861
2862void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2863 // Nothing to do, this should be removed during prepare for register allocator.
2864 UNUSED(instruction);
2865 LOG(FATAL) << "Unreachable";
2866}
2867
Alexandre Rames67555f72014-11-18 10:55:16 +00002868#undef __
2869#undef QUICK_ENTRY_POINT
2870
Alexandre Rames5319def2014-10-23 10:03:10 +01002871} // namespace arm64
2872} // namespace art