blob: 89d809df0f765f0c35a28f9b4508530f7b0140a3 [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
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100608void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
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;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100613 if (value_can_be_null) {
614 __ Cbz(value, &done);
615 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100616 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
617 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000618 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100619 if (value_can_be_null) {
620 __ Bind(&done);
621 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100622}
623
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000624void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
625 // Blocked core registers:
626 // lr : Runtime reserved.
627 // tr : Runtime reserved.
628 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
629 // ip1 : VIXL core temp.
630 // ip0 : VIXL core temp.
631 //
632 // Blocked fp registers:
633 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100634 CPURegList reserved_core_registers = vixl_reserved_core_registers;
635 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100636 while (!reserved_core_registers.IsEmpty()) {
637 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
638 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000639
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000640 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800641 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000642 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
643 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000644
645 if (is_baseline) {
646 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
647 while (!reserved_core_baseline_registers.IsEmpty()) {
648 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
649 }
650
651 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
652 while (!reserved_fp_baseline_registers.IsEmpty()) {
653 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
654 }
655 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100656}
657
658Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
659 if (type == Primitive::kPrimVoid) {
660 LOG(FATAL) << "Unreachable type " << type;
661 }
662
Alexandre Rames542361f2015-01-29 16:57:31 +0000663 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000664 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
665 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100666 return Location::FpuRegisterLocation(reg);
667 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000668 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
669 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100670 return Location::RegisterLocation(reg);
671 }
672}
673
Alexandre Rames3e69f162014-12-10 10:36:50 +0000674size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
675 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
676 __ Str(reg, MemOperand(sp, stack_index));
677 return kArm64WordSize;
678}
679
680size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
681 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
682 __ Ldr(reg, MemOperand(sp, stack_index));
683 return kArm64WordSize;
684}
685
686size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
687 FPRegister reg = FPRegister(reg_id, kDRegSize);
688 __ Str(reg, MemOperand(sp, stack_index));
689 return kArm64WordSize;
690}
691
692size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
693 FPRegister reg = FPRegister(reg_id, kDRegSize);
694 __ Ldr(reg, MemOperand(sp, stack_index));
695 return kArm64WordSize;
696}
697
Alexandre Rames5319def2014-10-23 10:03:10 +0100698void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100699 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100700}
701
702void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100703 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100704}
705
Alexandre Rames67555f72014-11-18 10:55:16 +0000706void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000707 if (constant->IsIntConstant()) {
708 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
709 } else if (constant->IsLongConstant()) {
710 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
711 } else if (constant->IsNullConstant()) {
712 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000713 } else if (constant->IsFloatConstant()) {
714 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
715 } else {
716 DCHECK(constant->IsDoubleConstant());
717 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
718 }
719}
720
Alexandre Rames3e69f162014-12-10 10:36:50 +0000721
722static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
723 DCHECK(constant.IsConstant());
724 HConstant* cst = constant.GetConstant();
725 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000726 // Null is mapped to a core W register, which we associate with kPrimInt.
727 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000728 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
729 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
730 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
731}
732
733void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000734 if (source.Equals(destination)) {
735 return;
736 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000737
738 // A valid move can always be inferred from the destination and source
739 // locations. When moving from and to a register, the argument type can be
740 // used to generate 32bit instead of 64bit moves. In debug mode we also
741 // checks the coherency of the locations and the type.
742 bool unspecified_type = (type == Primitive::kPrimVoid);
743
744 if (destination.IsRegister() || destination.IsFpuRegister()) {
745 if (unspecified_type) {
746 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
747 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000748 (src_cst != nullptr && (src_cst->IsIntConstant()
749 || src_cst->IsFloatConstant()
750 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000751 // For stack slots and 32bit constants, a 64bit type is appropriate.
752 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000753 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000754 // If the source is a double stack slot or a 64bit constant, a 64bit
755 // type is appropriate. Else the source is a register, and since the
756 // type has not been specified, we chose a 64bit type to force a 64bit
757 // move.
758 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000759 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000760 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000761 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
762 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000763 CPURegister dst = CPURegisterFrom(destination, type);
764 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
765 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
766 __ Ldr(dst, StackOperandFrom(source));
767 } else if (source.IsConstant()) {
768 DCHECK(CoherentConstantAndType(source, type));
769 MoveConstant(dst, source.GetConstant());
770 } else {
771 if (destination.IsRegister()) {
772 __ Mov(Register(dst), RegisterFrom(source, type));
773 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800774 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000775 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
776 }
777 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000778 } else { // The destination is not a register. It must be a stack slot.
779 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
780 if (source.IsRegister() || source.IsFpuRegister()) {
781 if (unspecified_type) {
782 if (source.IsRegister()) {
783 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
784 } else {
785 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
786 }
787 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000788 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
789 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000790 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
791 } else if (source.IsConstant()) {
792 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
793 UseScratchRegisterScope temps(GetVIXLAssembler());
794 HConstant* src_cst = source.GetConstant();
795 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000796 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000797 temp = temps.AcquireW();
798 } else if (src_cst->IsLongConstant()) {
799 temp = temps.AcquireX();
800 } else if (src_cst->IsFloatConstant()) {
801 temp = temps.AcquireS();
802 } else {
803 DCHECK(src_cst->IsDoubleConstant());
804 temp = temps.AcquireD();
805 }
806 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000807 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000808 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000809 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000810 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000811 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000812 // There is generally less pressure on FP registers.
813 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000814 __ Ldr(temp, StackOperandFrom(source));
815 __ Str(temp, StackOperandFrom(destination));
816 }
817 }
818}
819
820void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000821 CPURegister dst,
822 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000823 switch (type) {
824 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000825 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000826 break;
827 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000828 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000829 break;
830 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000831 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000832 break;
833 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000834 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000835 break;
836 case Primitive::kPrimInt:
837 case Primitive::kPrimNot:
838 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000839 case Primitive::kPrimFloat:
840 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000841 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000842 __ Ldr(dst, src);
843 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000844 case Primitive::kPrimVoid:
845 LOG(FATAL) << "Unreachable type " << type;
846 }
847}
848
Calin Juravle77520bc2015-01-12 18:45:46 +0000849void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000850 CPURegister dst,
851 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100852 MacroAssembler* masm = GetVIXLAssembler();
853 BlockPoolsScope block_pools(masm);
854 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000855 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000856 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000857
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000858 DCHECK(!src.IsPreIndex());
859 DCHECK(!src.IsPostIndex());
860
861 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800862 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000863 MemOperand base = MemOperand(temp_base);
864 switch (type) {
865 case Primitive::kPrimBoolean:
866 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000867 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000868 break;
869 case Primitive::kPrimByte:
870 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000871 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000872 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
873 break;
874 case Primitive::kPrimChar:
875 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000876 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000877 break;
878 case Primitive::kPrimShort:
879 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000880 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000881 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
882 break;
883 case Primitive::kPrimInt:
884 case Primitive::kPrimNot:
885 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000886 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000887 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000888 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000889 break;
890 case Primitive::kPrimFloat:
891 case Primitive::kPrimDouble: {
892 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000893 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000894
895 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
896 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000897 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000898 __ Fmov(FPRegister(dst), temp);
899 break;
900 }
901 case Primitive::kPrimVoid:
902 LOG(FATAL) << "Unreachable type " << type;
903 }
904}
905
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000906void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000907 CPURegister src,
908 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000909 switch (type) {
910 case Primitive::kPrimBoolean:
911 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000912 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000913 break;
914 case Primitive::kPrimChar:
915 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000916 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000917 break;
918 case Primitive::kPrimInt:
919 case Primitive::kPrimNot:
920 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000921 case Primitive::kPrimFloat:
922 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000923 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000924 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000925 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000926 case Primitive::kPrimVoid:
927 LOG(FATAL) << "Unreachable type " << type;
928 }
929}
930
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000931void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
932 CPURegister src,
933 const MemOperand& dst) {
934 UseScratchRegisterScope temps(GetVIXLAssembler());
935 Register temp_base = temps.AcquireX();
936
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000937 DCHECK(!dst.IsPreIndex());
938 DCHECK(!dst.IsPostIndex());
939
940 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800941 Operand op = OperandFromMemOperand(dst);
942 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000943 MemOperand base = MemOperand(temp_base);
944 switch (type) {
945 case Primitive::kPrimBoolean:
946 case Primitive::kPrimByte:
947 __ Stlrb(Register(src), base);
948 break;
949 case Primitive::kPrimChar:
950 case Primitive::kPrimShort:
951 __ Stlrh(Register(src), base);
952 break;
953 case Primitive::kPrimInt:
954 case Primitive::kPrimNot:
955 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000956 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000957 __ Stlr(Register(src), base);
958 break;
959 case Primitive::kPrimFloat:
960 case Primitive::kPrimDouble: {
961 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000962 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000963
964 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
965 __ Fmov(temp, FPRegister(src));
966 __ Stlr(temp, base);
967 break;
968 }
969 case Primitive::kPrimVoid:
970 LOG(FATAL) << "Unreachable type " << type;
971 }
972}
973
Alexandre Rames67555f72014-11-18 10:55:16 +0000974void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000975 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000976 DCHECK(current_method.IsW());
977 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
978}
979
980void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
981 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000982 uint32_t dex_pc,
983 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100984 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +0000985 __ Ldr(lr, MemOperand(tr, entry_point_offset));
986 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +0100987 RecordPcInfo(instruction, dex_pc, slow_path);
988 DCHECK(instruction->IsSuspendCheck()
989 || instruction->IsBoundsCheck()
990 || instruction->IsNullCheck()
991 || instruction->IsDivZeroCheck()
992 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000993}
994
995void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
996 vixl::Register class_reg) {
997 UseScratchRegisterScope temps(GetVIXLAssembler());
998 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001000 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001001
Serban Constantinescu02164b32014-11-13 14:05:07 +00001002 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001003 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001004 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1005 __ Add(temp, class_reg, status_offset);
1006 __ Ldar(temp, HeapOperand(temp));
1007 __ Cmp(temp, mirror::Class::kStatusInitialized);
1008 __ B(lt, slow_path->GetEntryLabel());
1009 } else {
1010 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1011 __ Cmp(temp, mirror::Class::kStatusInitialized);
1012 __ B(lt, slow_path->GetEntryLabel());
1013 __ Dmb(InnerShareable, BarrierReads);
1014 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001015 __ Bind(slow_path->GetExitLabel());
1016}
Alexandre Rames5319def2014-10-23 10:03:10 +01001017
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001018void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1019 BarrierType type = BarrierAll;
1020
1021 switch (kind) {
1022 case MemBarrierKind::kAnyAny:
1023 case MemBarrierKind::kAnyStore: {
1024 type = BarrierAll;
1025 break;
1026 }
1027 case MemBarrierKind::kLoadAny: {
1028 type = BarrierReads;
1029 break;
1030 }
1031 case MemBarrierKind::kStoreStore: {
1032 type = BarrierWrites;
1033 break;
1034 }
1035 default:
1036 LOG(FATAL) << "Unexpected memory barrier " << kind;
1037 }
1038 __ Dmb(InnerShareable, type);
1039}
1040
Serban Constantinescu02164b32014-11-13 14:05:07 +00001041void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1042 HBasicBlock* successor) {
1043 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001044 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1045 if (slow_path == nullptr) {
1046 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1047 instruction->SetSlowPath(slow_path);
1048 codegen_->AddSlowPath(slow_path);
1049 if (successor != nullptr) {
1050 DCHECK(successor->IsLoopHeader());
1051 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1052 }
1053 } else {
1054 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1055 }
1056
Serban Constantinescu02164b32014-11-13 14:05:07 +00001057 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1058 Register temp = temps.AcquireW();
1059
1060 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1061 if (successor == nullptr) {
1062 __ Cbnz(temp, slow_path->GetEntryLabel());
1063 __ Bind(slow_path->GetReturnLabel());
1064 } else {
1065 __ Cbz(temp, codegen_->GetLabelOf(successor));
1066 __ B(slow_path->GetEntryLabel());
1067 // slow_path will return to GetLabelOf(successor).
1068 }
1069}
1070
Alexandre Rames5319def2014-10-23 10:03:10 +01001071InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1072 CodeGeneratorARM64* codegen)
1073 : HGraphVisitor(graph),
1074 assembler_(codegen->GetAssembler()),
1075 codegen_(codegen) {}
1076
1077#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001078 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001079
1080#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1081
1082enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001083 // Using a base helps identify when we hit such breakpoints.
1084 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001085#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1086 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1087#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1088};
1089
1090#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1091 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001092 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001093 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1094 } \
1095 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1097 locations->SetOut(Location::Any()); \
1098 }
1099 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1100#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1101
1102#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001103#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001104
Alexandre Rames67555f72014-11-18 10:55:16 +00001105void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001106 DCHECK_EQ(instr->InputCount(), 2U);
1107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1108 Primitive::Type type = instr->GetResultType();
1109 switch (type) {
1110 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001111 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001112 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001113 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001115 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001116
1117 case Primitive::kPrimFloat:
1118 case Primitive::kPrimDouble:
1119 locations->SetInAt(0, Location::RequiresFpuRegister());
1120 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001121 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001123
Alexandre Rames5319def2014-10-23 10:03:10 +01001124 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001125 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 }
1127}
1128
Alexandre Rames09a99962015-04-15 11:47:56 +01001129void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1130 LocationSummary* locations =
1131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1132 locations->SetInAt(0, Location::RequiresRegister());
1133 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1134 locations->SetOut(Location::RequiresFpuRegister());
1135 } else {
1136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1137 }
1138}
1139
1140void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1141 const FieldInfo& field_info) {
1142 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001143 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001144
1145 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1146 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1147
1148 if (field_info.IsVolatile()) {
1149 if (use_acquire_release) {
1150 // NB: LoadAcquire will record the pc info if needed.
1151 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1152 } else {
1153 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1154 codegen_->MaybeRecordImplicitNullCheck(instruction);
1155 // For IRIW sequential consistency kLoadAny is not sufficient.
1156 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1157 }
1158 } else {
1159 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1160 codegen_->MaybeRecordImplicitNullCheck(instruction);
1161 }
1162}
1163
1164void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1165 LocationSummary* locations =
1166 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1167 locations->SetInAt(0, Location::RequiresRegister());
1168 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1169 locations->SetInAt(1, Location::RequiresFpuRegister());
1170 } else {
1171 locations->SetInAt(1, Location::RequiresRegister());
1172 }
1173}
1174
1175void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001176 const FieldInfo& field_info,
1177 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001178 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001179 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001180
1181 Register obj = InputRegisterAt(instruction, 0);
1182 CPURegister value = InputCPURegisterAt(instruction, 1);
1183 Offset offset = field_info.GetFieldOffset();
1184 Primitive::Type field_type = field_info.GetFieldType();
1185 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1186
1187 if (field_info.IsVolatile()) {
1188 if (use_acquire_release) {
1189 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1190 codegen_->MaybeRecordImplicitNullCheck(instruction);
1191 } else {
1192 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1193 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1194 codegen_->MaybeRecordImplicitNullCheck(instruction);
1195 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1196 }
1197 } else {
1198 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1199 codegen_->MaybeRecordImplicitNullCheck(instruction);
1200 }
1201
1202 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001203 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001204 }
1205}
1206
Alexandre Rames67555f72014-11-18 10:55:16 +00001207void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001208 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001209
1210 switch (type) {
1211 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001212 case Primitive::kPrimLong: {
1213 Register dst = OutputRegister(instr);
1214 Register lhs = InputRegisterAt(instr, 0);
1215 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001216 if (instr->IsAdd()) {
1217 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001218 } else if (instr->IsAnd()) {
1219 __ And(dst, lhs, rhs);
1220 } else if (instr->IsOr()) {
1221 __ Orr(dst, lhs, rhs);
1222 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001223 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001224 } else {
1225 DCHECK(instr->IsXor());
1226 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001227 }
1228 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001229 }
1230 case Primitive::kPrimFloat:
1231 case Primitive::kPrimDouble: {
1232 FPRegister dst = OutputFPRegister(instr);
1233 FPRegister lhs = InputFPRegisterAt(instr, 0);
1234 FPRegister rhs = InputFPRegisterAt(instr, 1);
1235 if (instr->IsAdd()) {
1236 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001237 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001238 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001239 } else {
1240 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001241 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001242 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001243 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001244 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001245 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001246 }
1247}
1248
Serban Constantinescu02164b32014-11-13 14:05:07 +00001249void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1250 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1251
1252 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1253 Primitive::Type type = instr->GetResultType();
1254 switch (type) {
1255 case Primitive::kPrimInt:
1256 case Primitive::kPrimLong: {
1257 locations->SetInAt(0, Location::RequiresRegister());
1258 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1259 locations->SetOut(Location::RequiresRegister());
1260 break;
1261 }
1262 default:
1263 LOG(FATAL) << "Unexpected shift type " << type;
1264 }
1265}
1266
1267void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1268 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1269
1270 Primitive::Type type = instr->GetType();
1271 switch (type) {
1272 case Primitive::kPrimInt:
1273 case Primitive::kPrimLong: {
1274 Register dst = OutputRegister(instr);
1275 Register lhs = InputRegisterAt(instr, 0);
1276 Operand rhs = InputOperandAt(instr, 1);
1277 if (rhs.IsImmediate()) {
1278 uint32_t shift_value = (type == Primitive::kPrimInt)
1279 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1280 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1281 if (instr->IsShl()) {
1282 __ Lsl(dst, lhs, shift_value);
1283 } else if (instr->IsShr()) {
1284 __ Asr(dst, lhs, shift_value);
1285 } else {
1286 __ Lsr(dst, lhs, shift_value);
1287 }
1288 } else {
1289 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1290
1291 if (instr->IsShl()) {
1292 __ Lsl(dst, lhs, rhs_reg);
1293 } else if (instr->IsShr()) {
1294 __ Asr(dst, lhs, rhs_reg);
1295 } else {
1296 __ Lsr(dst, lhs, rhs_reg);
1297 }
1298 }
1299 break;
1300 }
1301 default:
1302 LOG(FATAL) << "Unexpected shift operation type " << type;
1303 }
1304}
1305
Alexandre Rames5319def2014-10-23 10:03:10 +01001306void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001307 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001308}
1309
1310void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001311 HandleBinaryOp(instruction);
1312}
1313
1314void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1315 HandleBinaryOp(instruction);
1316}
1317
1318void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1319 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001320}
1321
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001322void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1323 LocationSummary* locations =
1324 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1325 locations->SetInAt(0, Location::RequiresRegister());
1326 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001327 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1328 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1329 } else {
1330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1331 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001332}
1333
1334void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1335 LocationSummary* locations = instruction->GetLocations();
1336 Primitive::Type type = instruction->GetType();
1337 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001338 Location index = locations->InAt(1);
1339 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001340 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001341 MacroAssembler* masm = GetVIXLAssembler();
1342 UseScratchRegisterScope temps(masm);
1343 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001344
1345 if (index.IsConstant()) {
1346 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001347 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001348 } else {
1349 Register temp = temps.AcquireSameSizeAs(obj);
1350 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1351 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001352 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001353 }
1354
Alexandre Rames67555f72014-11-18 10:55:16 +00001355 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001356 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001357}
1358
Alexandre Rames5319def2014-10-23 10:03:10 +01001359void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1361 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001362 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001363}
1364
1365void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001366 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001367 __ Ldr(OutputRegister(instruction),
1368 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001369 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001370}
1371
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001372void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001373 if (instruction->NeedsTypeCheck()) {
1374 LocationSummary* locations =
1375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001376 InvokeRuntimeCallingConvention calling_convention;
1377 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1378 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1379 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1380 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001381 LocationSummary* locations =
1382 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001383 locations->SetInAt(0, Location::RequiresRegister());
1384 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001385 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1386 locations->SetInAt(2, Location::RequiresFpuRegister());
1387 } else {
1388 locations->SetInAt(2, Location::RequiresRegister());
1389 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001390 }
1391}
1392
1393void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1394 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001395 LocationSummary* locations = instruction->GetLocations();
1396 bool needs_runtime_call = locations->WillCall();
1397
1398 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001399 codegen_->InvokeRuntime(
1400 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001401 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001402 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001403 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001404 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001405 Location index = locations->InAt(1);
1406 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001407 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001408 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001409 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001410 {
1411 // We use a block to end the scratch scope before the write barrier, thus
1412 // freeing the temporary registers so they can be used in `MarkGCCard`.
1413 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414
Alexandre Rames97833a02015-04-16 15:07:12 +01001415 if (index.IsConstant()) {
1416 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1417 destination = HeapOperand(obj, offset);
1418 } else {
1419 Register temp = temps.AcquireSameSizeAs(obj);
1420 Register index_reg = InputRegisterAt(instruction, 1);
1421 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1422 destination = HeapOperand(temp, offset);
1423 }
1424
1425 codegen_->Store(value_type, value, destination);
1426 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001427 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001428 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001429 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001430 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001431 }
1432}
1433
Alexandre Rames67555f72014-11-18 10:55:16 +00001434void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1435 LocationSummary* locations =
1436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1437 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001438 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001439 if (instruction->HasUses()) {
1440 locations->SetOut(Location::SameAsFirstInput());
1441 }
1442}
1443
1444void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445 LocationSummary* locations = instruction->GetLocations();
1446 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1447 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001448 codegen_->AddSlowPath(slow_path);
1449
1450 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1451 __ B(slow_path->GetEntryLabel(), hs);
1452}
1453
1454void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1455 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1456 instruction, LocationSummary::kCallOnSlowPath);
1457 locations->SetInAt(0, Location::RequiresRegister());
1458 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001459 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001460}
1461
1462void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001463 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001464 Register obj = InputRegisterAt(instruction, 0);;
1465 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001466 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001467
Alexandre Rames3e69f162014-12-10 10:36:50 +00001468 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1469 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001470 codegen_->AddSlowPath(slow_path);
1471
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001472 // Avoid null check if we know obj is not null.
1473 if (instruction->MustDoNullCheck()) {
1474 __ Cbz(obj, slow_path->GetExitLabel());
1475 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001476 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001477 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1478 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001479 __ B(ne, slow_path->GetEntryLabel());
1480 __ Bind(slow_path->GetExitLabel());
1481}
1482
1483void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1484 LocationSummary* locations =
1485 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1486 locations->SetInAt(0, Location::RequiresRegister());
1487 if (check->HasUses()) {
1488 locations->SetOut(Location::SameAsFirstInput());
1489 }
1490}
1491
1492void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1493 // We assume the class is not null.
1494 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1495 check->GetLoadClass(), check, check->GetDexPc(), true);
1496 codegen_->AddSlowPath(slow_path);
1497 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1498}
1499
Serban Constantinescu02164b32014-11-13 14:05:07 +00001500void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001501 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001502 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1503 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001504 switch (in_type) {
1505 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001506 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001507 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1509 break;
1510 }
1511 case Primitive::kPrimFloat:
1512 case Primitive::kPrimDouble: {
1513 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001514 HInstruction* right = compare->InputAt(1);
1515 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1516 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1517 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1518 } else {
1519 locations->SetInAt(1, Location::RequiresFpuRegister());
1520 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001521 locations->SetOut(Location::RequiresRegister());
1522 break;
1523 }
1524 default:
1525 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1526 }
1527}
1528
1529void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1530 Primitive::Type in_type = compare->InputAt(0)->GetType();
1531
1532 // 0 if: left == right
1533 // 1 if: left > right
1534 // -1 if: left < right
1535 switch (in_type) {
1536 case Primitive::kPrimLong: {
1537 Register result = OutputRegister(compare);
1538 Register left = InputRegisterAt(compare, 0);
1539 Operand right = InputOperandAt(compare, 1);
1540
1541 __ Cmp(left, right);
1542 __ Cset(result, ne);
1543 __ Cneg(result, result, lt);
1544 break;
1545 }
1546 case Primitive::kPrimFloat:
1547 case Primitive::kPrimDouble: {
1548 Register result = OutputRegister(compare);
1549 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001550 if (compare->GetLocations()->InAt(1).IsConstant()) {
1551 if (kIsDebugBuild) {
1552 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1553 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1554 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1555 }
1556 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1557 __ Fcmp(left, 0.0);
1558 } else {
1559 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1560 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001561 if (compare->IsGtBias()) {
1562 __ Cset(result, ne);
1563 } else {
1564 __ Csetm(result, ne);
1565 }
1566 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001567 break;
1568 }
1569 default:
1570 LOG(FATAL) << "Unimplemented compare type " << in_type;
1571 }
1572}
1573
1574void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1576 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001577 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001578 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001580 }
1581}
1582
1583void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1584 if (!instruction->NeedsMaterialization()) {
1585 return;
1586 }
1587
1588 LocationSummary* locations = instruction->GetLocations();
1589 Register lhs = InputRegisterAt(instruction, 0);
1590 Operand rhs = InputOperandAt(instruction, 1);
1591 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1592 Condition cond = ARM64Condition(instruction->GetCondition());
1593
1594 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001595 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001596}
1597
1598#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1599 M(Equal) \
1600 M(NotEqual) \
1601 M(LessThan) \
1602 M(LessThanOrEqual) \
1603 M(GreaterThan) \
1604 M(GreaterThanOrEqual)
1605#define DEFINE_CONDITION_VISITORS(Name) \
1606void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1607void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1608FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001609#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001610#undef FOR_EACH_CONDITION_INSTRUCTION
1611
Zheng Xuc6667102015-05-15 16:08:45 +08001612void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1613 DCHECK(instruction->IsDiv() || instruction->IsRem());
1614
1615 LocationSummary* locations = instruction->GetLocations();
1616 Location second = locations->InAt(1);
1617 DCHECK(second.IsConstant());
1618
1619 Register out = OutputRegister(instruction);
1620 Register dividend = InputRegisterAt(instruction, 0);
1621 int64_t imm = Int64FromConstant(second.GetConstant());
1622 DCHECK(imm == 1 || imm == -1);
1623
1624 if (instruction->IsRem()) {
1625 __ Mov(out, 0);
1626 } else {
1627 if (imm == 1) {
1628 __ Mov(out, dividend);
1629 } else {
1630 __ Neg(out, dividend);
1631 }
1632 }
1633}
1634
1635void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1636 DCHECK(instruction->IsDiv() || instruction->IsRem());
1637
1638 LocationSummary* locations = instruction->GetLocations();
1639 Location second = locations->InAt(1);
1640 DCHECK(second.IsConstant());
1641
1642 Register out = OutputRegister(instruction);
1643 Register dividend = InputRegisterAt(instruction, 0);
1644 int64_t imm = Int64FromConstant(second.GetConstant());
1645 int64_t abs_imm = std::abs(imm);
1646 DCHECK(IsPowerOfTwo(abs_imm));
1647 int ctz_imm = CTZ(abs_imm);
1648
1649 UseScratchRegisterScope temps(GetVIXLAssembler());
1650 Register temp = temps.AcquireSameSizeAs(out);
1651
1652 if (instruction->IsDiv()) {
1653 __ Add(temp, dividend, abs_imm - 1);
1654 __ Cmp(dividend, 0);
1655 __ Csel(out, temp, dividend, lt);
1656 if (imm > 0) {
1657 __ Asr(out, out, ctz_imm);
1658 } else {
1659 __ Neg(out, Operand(out, ASR, ctz_imm));
1660 }
1661 } else {
1662 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1663 __ Asr(temp, dividend, bits - 1);
1664 __ Lsr(temp, temp, bits - ctz_imm);
1665 __ Add(out, dividend, temp);
1666 __ And(out, out, abs_imm - 1);
1667 __ Sub(out, out, temp);
1668 }
1669}
1670
1671void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1672 DCHECK(instruction->IsDiv() || instruction->IsRem());
1673
1674 LocationSummary* locations = instruction->GetLocations();
1675 Location second = locations->InAt(1);
1676 DCHECK(second.IsConstant());
1677
1678 Register out = OutputRegister(instruction);
1679 Register dividend = InputRegisterAt(instruction, 0);
1680 int64_t imm = Int64FromConstant(second.GetConstant());
1681
1682 Primitive::Type type = instruction->GetResultType();
1683 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1684
1685 int64_t magic;
1686 int shift;
1687 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1688
1689 UseScratchRegisterScope temps(GetVIXLAssembler());
1690 Register temp = temps.AcquireSameSizeAs(out);
1691
1692 // temp = get_high(dividend * magic)
1693 __ Mov(temp, magic);
1694 if (type == Primitive::kPrimLong) {
1695 __ Smulh(temp, dividend, temp);
1696 } else {
1697 __ Smull(temp.X(), dividend, temp);
1698 __ Lsr(temp.X(), temp.X(), 32);
1699 }
1700
1701 if (imm > 0 && magic < 0) {
1702 __ Add(temp, temp, dividend);
1703 } else if (imm < 0 && magic > 0) {
1704 __ Sub(temp, temp, dividend);
1705 }
1706
1707 if (shift != 0) {
1708 __ Asr(temp, temp, shift);
1709 }
1710
1711 if (instruction->IsDiv()) {
1712 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1713 } else {
1714 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1715 // TODO: Strength reduction for msub.
1716 Register temp_imm = temps.AcquireSameSizeAs(out);
1717 __ Mov(temp_imm, imm);
1718 __ Msub(out, temp, temp_imm, dividend);
1719 }
1720}
1721
1722void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1723 DCHECK(instruction->IsDiv() || instruction->IsRem());
1724 Primitive::Type type = instruction->GetResultType();
1725 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1726
1727 LocationSummary* locations = instruction->GetLocations();
1728 Register out = OutputRegister(instruction);
1729 Location second = locations->InAt(1);
1730
1731 if (second.IsConstant()) {
1732 int64_t imm = Int64FromConstant(second.GetConstant());
1733
1734 if (imm == 0) {
1735 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1736 } else if (imm == 1 || imm == -1) {
1737 DivRemOneOrMinusOne(instruction);
1738 } else if (IsPowerOfTwo(std::abs(imm))) {
1739 DivRemByPowerOfTwo(instruction);
1740 } else {
1741 DCHECK(imm <= -2 || imm >= 2);
1742 GenerateDivRemWithAnyConstant(instruction);
1743 }
1744 } else {
1745 Register dividend = InputRegisterAt(instruction, 0);
1746 Register divisor = InputRegisterAt(instruction, 1);
1747 if (instruction->IsDiv()) {
1748 __ Sdiv(out, dividend, divisor);
1749 } else {
1750 UseScratchRegisterScope temps(GetVIXLAssembler());
1751 Register temp = temps.AcquireSameSizeAs(out);
1752 __ Sdiv(temp, dividend, divisor);
1753 __ Msub(out, temp, divisor, dividend);
1754 }
1755 }
1756}
1757
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001758void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1759 LocationSummary* locations =
1760 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1761 switch (div->GetResultType()) {
1762 case Primitive::kPrimInt:
1763 case Primitive::kPrimLong:
1764 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001765 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001766 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1767 break;
1768
1769 case Primitive::kPrimFloat:
1770 case Primitive::kPrimDouble:
1771 locations->SetInAt(0, Location::RequiresFpuRegister());
1772 locations->SetInAt(1, Location::RequiresFpuRegister());
1773 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1774 break;
1775
1776 default:
1777 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1778 }
1779}
1780
1781void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1782 Primitive::Type type = div->GetResultType();
1783 switch (type) {
1784 case Primitive::kPrimInt:
1785 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001786 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001787 break;
1788
1789 case Primitive::kPrimFloat:
1790 case Primitive::kPrimDouble:
1791 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1792 break;
1793
1794 default:
1795 LOG(FATAL) << "Unexpected div type " << type;
1796 }
1797}
1798
Alexandre Rames67555f72014-11-18 10:55:16 +00001799void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1800 LocationSummary* locations =
1801 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1802 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1803 if (instruction->HasUses()) {
1804 locations->SetOut(Location::SameAsFirstInput());
1805 }
1806}
1807
1808void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1809 SlowPathCodeARM64* slow_path =
1810 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1811 codegen_->AddSlowPath(slow_path);
1812 Location value = instruction->GetLocations()->InAt(0);
1813
Alexandre Rames3e69f162014-12-10 10:36:50 +00001814 Primitive::Type type = instruction->GetType();
1815
1816 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1817 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1818 return;
1819 }
1820
Alexandre Rames67555f72014-11-18 10:55:16 +00001821 if (value.IsConstant()) {
1822 int64_t divisor = Int64ConstantFrom(value);
1823 if (divisor == 0) {
1824 __ B(slow_path->GetEntryLabel());
1825 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001826 // A division by a non-null constant is valid. We don't need to perform
1827 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001828 }
1829 } else {
1830 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1831 }
1832}
1833
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001834void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1837 locations->SetOut(Location::ConstantLocation(constant));
1838}
1839
1840void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1841 UNUSED(constant);
1842 // Will be generated at use site.
1843}
1844
Alexandre Rames5319def2014-10-23 10:03:10 +01001845void LocationsBuilderARM64::VisitExit(HExit* exit) {
1846 exit->SetLocations(nullptr);
1847}
1848
1849void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001850 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001851}
1852
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001853void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1854 LocationSummary* locations =
1855 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1856 locations->SetOut(Location::ConstantLocation(constant));
1857}
1858
1859void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1860 UNUSED(constant);
1861 // Will be generated at use site.
1862}
1863
Alexandre Rames5319def2014-10-23 10:03:10 +01001864void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1865 got->SetLocations(nullptr);
1866}
1867
1868void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1869 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001870 DCHECK(!successor->IsExitBlock());
1871 HBasicBlock* block = got->GetBlock();
1872 HInstruction* previous = got->GetPrevious();
1873 HLoopInformation* info = block->GetLoopInformation();
1874
David Brazdil46e2a392015-03-16 17:31:52 +00001875 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001876 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1877 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1878 return;
1879 }
1880 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1881 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1882 }
1883 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001884 __ B(codegen_->GetLabelOf(successor));
1885 }
1886}
1887
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001888void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1889 vixl::Label* true_target,
1890 vixl::Label* false_target,
1891 vixl::Label* always_true_target) {
1892 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001893 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001894
Serban Constantinescu02164b32014-11-13 14:05:07 +00001895 if (cond->IsIntConstant()) {
1896 int32_t cond_value = cond->AsIntConstant()->GetValue();
1897 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001898 if (always_true_target != nullptr) {
1899 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001900 }
1901 return;
1902 } else {
1903 DCHECK_EQ(cond_value, 0);
1904 }
1905 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001906 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001907 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001908 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001909 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001910 } else {
1911 // The condition instruction has not been materialized, use its inputs as
1912 // the comparison and its condition as the branch condition.
1913 Register lhs = InputRegisterAt(condition, 0);
1914 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001915 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00001916 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1917 switch (arm64_cond) {
1918 case eq:
1919 __ Cbz(lhs, true_target);
1920 break;
1921 case ne:
1922 __ Cbnz(lhs, true_target);
1923 break;
1924 case lt:
1925 // Test the sign bit and branch accordingly.
1926 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1927 break;
1928 case ge:
1929 // Test the sign bit and branch accordingly.
1930 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
1931 break;
1932 default:
1933 // Without the `static_cast` the compiler throws an error for
1934 // `-Werror=sign-promo`.
1935 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001936 }
1937 } else {
1938 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001939 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001940 }
1941 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001942 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001943 __ B(false_target);
1944 }
1945}
1946
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001947void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1948 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1949 HInstruction* cond = if_instr->InputAt(0);
1950 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1951 locations->SetInAt(0, Location::RequiresRegister());
1952 }
1953}
1954
1955void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1956 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1957 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1958 vixl::Label* always_true_target = true_target;
1959 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1960 if_instr->IfTrueSuccessor())) {
1961 always_true_target = nullptr;
1962 }
1963 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1964 if_instr->IfFalseSuccessor())) {
1965 false_target = nullptr;
1966 }
1967 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1968}
1969
1970void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1971 LocationSummary* locations = new (GetGraph()->GetArena())
1972 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1973 HInstruction* cond = deoptimize->InputAt(0);
1974 DCHECK(cond->IsCondition());
1975 if (cond->AsCondition()->NeedsMaterialization()) {
1976 locations->SetInAt(0, Location::RequiresRegister());
1977 }
1978}
1979
1980void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1981 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
1982 DeoptimizationSlowPathARM64(deoptimize);
1983 codegen_->AddSlowPath(slow_path);
1984 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
1985 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1986}
1987
Alexandre Rames5319def2014-10-23 10:03:10 +01001988void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001989 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001990}
1991
1992void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001993 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01001994}
1995
1996void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001997 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001998}
1999
2000void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002001 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002002}
2003
Alexandre Rames67555f72014-11-18 10:55:16 +00002004void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2005 LocationSummary::CallKind call_kind =
2006 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2007 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2008 locations->SetInAt(0, Location::RequiresRegister());
2009 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002010 // The output does overlap inputs.
2011 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002012}
2013
2014void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2015 LocationSummary* locations = instruction->GetLocations();
2016 Register obj = InputRegisterAt(instruction, 0);;
2017 Register cls = InputRegisterAt(instruction, 1);;
2018 Register out = OutputRegister(instruction);
2019
2020 vixl::Label done;
2021
2022 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002023 // Avoid null check if we know `obj` is not null.
2024 if (instruction->MustDoNullCheck()) {
2025 __ Mov(out, 0);
2026 __ Cbz(obj, &done);
2027 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002028
2029 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002030 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002031 __ Cmp(out, cls);
2032 if (instruction->IsClassFinal()) {
2033 // Classes must be equal for the instanceof to succeed.
2034 __ Cset(out, eq);
2035 } else {
2036 // If the classes are not equal, we go into a slow path.
2037 DCHECK(locations->OnlyCallsOnSlowPath());
2038 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002039 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2040 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002041 codegen_->AddSlowPath(slow_path);
2042 __ B(ne, slow_path->GetEntryLabel());
2043 __ Mov(out, 1);
2044 __ Bind(slow_path->GetExitLabel());
2045 }
2046
2047 __ Bind(&done);
2048}
2049
Alexandre Rames5319def2014-10-23 10:03:10 +01002050void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2051 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2052 locations->SetOut(Location::ConstantLocation(constant));
2053}
2054
2055void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2056 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002057 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002058}
2059
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002060void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2062 locations->SetOut(Location::ConstantLocation(constant));
2063}
2064
2065void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2066 // Will be generated at use site.
2067 UNUSED(constant);
2068}
2069
Alexandre Rames5319def2014-10-23 10:03:10 +01002070void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
2071 LocationSummary* locations =
2072 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
2073 locations->AddTemp(LocationFrom(x0));
2074
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002075 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01002076 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002077 HInstruction* input = invoke->InputAt(i);
2078 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
2079 }
2080
2081 Primitive::Type return_type = invoke->GetType();
2082 if (return_type != Primitive::kPrimVoid) {
2083 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
2084 }
2085}
2086
Alexandre Rames67555f72014-11-18 10:55:16 +00002087void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2088 HandleInvoke(invoke);
2089}
2090
2091void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2092 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2093 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
2094 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
2095 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
2096 Location receiver = invoke->GetLocations()->InAt(0);
2097 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002098 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002099
2100 // The register ip1 is required to be used for the hidden argument in
2101 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002102 MacroAssembler* masm = GetVIXLAssembler();
2103 UseScratchRegisterScope scratch_scope(masm);
2104 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002105 scratch_scope.Exclude(ip1);
2106 __ Mov(ip1, invoke->GetDexMethodIndex());
2107
2108 // temp = object->GetClass();
2109 if (receiver.IsStackSlot()) {
2110 __ Ldr(temp, StackOperandFrom(receiver));
2111 __ Ldr(temp, HeapOperand(temp, class_offset));
2112 } else {
2113 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
2114 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002115 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00002116 // temp = temp->GetImtEntryAt(method_offset);
2117 __ Ldr(temp, HeapOperand(temp, method_offset));
2118 // lr = temp->GetEntryPoint();
2119 __ Ldr(lr, HeapOperand(temp, entry_point));
2120 // lr();
2121 __ Blr(lr);
2122 DCHECK(!codegen_->IsLeafMethod());
2123 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2124}
2125
2126void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002127 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2128 if (intrinsic.TryDispatch(invoke)) {
2129 return;
2130 }
2131
Alexandre Rames67555f72014-11-18 10:55:16 +00002132 HandleInvoke(invoke);
2133}
2134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002135void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002136 // When we do not run baseline, explicit clinit checks triggered by static
2137 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2138 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002139
Andreas Gampe878d58c2015-01-15 23:24:00 -08002140 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2141 if (intrinsic.TryDispatch(invoke)) {
2142 return;
2143 }
2144
Alexandre Rames67555f72014-11-18 10:55:16 +00002145 HandleInvoke(invoke);
2146}
2147
Andreas Gampe878d58c2015-01-15 23:24:00 -08002148static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2149 if (invoke->GetLocations()->Intrinsified()) {
2150 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2151 intrinsic.Dispatch(invoke);
2152 return true;
2153 }
2154 return false;
2155}
2156
2157void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
2158 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
2159 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01002160 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08002161 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01002162
2163 // TODO: Implement all kinds of calls:
2164 // 1) boot -> boot
2165 // 2) app -> boot
2166 // 3) app -> app
2167 //
2168 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2169
Jeff Hao848f70a2014-01-15 13:49:50 -08002170 if (invoke->IsStringInit()) {
2171 // temp = thread->string_init_entrypoint
2172 __ Ldr(temp, HeapOperand(tr, invoke->GetStringInitOffset()));
2173 // LR = temp->entry_point_from_quick_compiled_code_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002174 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2175 kArm64WordSize)));
Jeff Hao848f70a2014-01-15 13:49:50 -08002176 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002177 __ Blr(lr);
2178 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08002179 // temp = method;
2180 LoadCurrentMethod(temp);
2181 if (!invoke->IsRecursive()) {
2182 // temp = temp->dex_cache_resolved_methods_;
2183 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
2184 // temp = temp[index_in_cache];
2185 __ Ldr(temp, HeapOperand(temp, index_in_cache));
2186 // lr = temp->entry_point_from_quick_compiled_code_;
2187 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2188 kArm64WordSize)));
2189 // lr();
2190 __ Blr(lr);
2191 } else {
2192 __ Bl(&frame_entry_label_);
2193 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002194 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002195
Andreas Gampe878d58c2015-01-15 23:24:00 -08002196 DCHECK(!IsLeafMethod());
2197}
2198
2199void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002200 // When we do not run baseline, explicit clinit checks triggered by static
2201 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2202 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002203
Andreas Gampe878d58c2015-01-15 23:24:00 -08002204 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2205 return;
2206 }
2207
Alexandre Ramesd921d642015-04-16 15:07:16 +01002208 BlockPoolsScope block_pools(GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -08002209 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
2210 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002211 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002212}
2213
2214void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002215 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2216 return;
2217 }
2218
Alexandre Rames5319def2014-10-23 10:03:10 +01002219 LocationSummary* locations = invoke->GetLocations();
2220 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002221 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002222 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
2223 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
2224 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00002225 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002226
Alexandre Ramesd921d642015-04-16 15:07:16 +01002227 BlockPoolsScope block_pools(GetVIXLAssembler());
2228
Alexandre Rames5319def2014-10-23 10:03:10 +01002229 // temp = object->GetClass();
2230 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002231 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
2232 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002233 } else {
2234 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002235 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002236 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002237 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002238 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002239 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002240 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002241 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002242 // lr();
2243 __ Blr(lr);
2244 DCHECK(!codegen_->IsLeafMethod());
2245 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2246}
2247
Alexandre Rames67555f72014-11-18 10:55:16 +00002248void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2249 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2250 : LocationSummary::kNoCall;
2251 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2252 locations->SetOut(Location::RequiresRegister());
2253}
2254
2255void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2256 Register out = OutputRegister(cls);
2257 if (cls->IsReferrersClass()) {
2258 DCHECK(!cls->CanCallRuntime());
2259 DCHECK(!cls->MustGenerateClinitCheck());
2260 codegen_->LoadCurrentMethod(out);
2261 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2262 } else {
2263 DCHECK(cls->CanCallRuntime());
2264 codegen_->LoadCurrentMethod(out);
2265 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002266 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002267
2268 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2269 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2270 codegen_->AddSlowPath(slow_path);
2271 __ Cbz(out, slow_path->GetEntryLabel());
2272 if (cls->MustGenerateClinitCheck()) {
2273 GenerateClassInitializationCheck(slow_path, out);
2274 } else {
2275 __ Bind(slow_path->GetExitLabel());
2276 }
2277 }
2278}
2279
2280void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2281 LocationSummary* locations =
2282 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2283 locations->SetOut(Location::RequiresRegister());
2284}
2285
2286void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2287 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2288 __ Ldr(OutputRegister(instruction), exception);
2289 __ Str(wzr, exception);
2290}
2291
Alexandre Rames5319def2014-10-23 10:03:10 +01002292void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2293 load->SetLocations(nullptr);
2294}
2295
2296void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2297 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002298 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002299}
2300
Alexandre Rames67555f72014-11-18 10:55:16 +00002301void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2302 LocationSummary* locations =
2303 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2304 locations->SetOut(Location::RequiresRegister());
2305}
2306
2307void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2308 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2309 codegen_->AddSlowPath(slow_path);
2310
2311 Register out = OutputRegister(load);
2312 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002313 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2314 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002315 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002316 __ Cbz(out, slow_path->GetEntryLabel());
2317 __ Bind(slow_path->GetExitLabel());
2318}
2319
Alexandre Rames5319def2014-10-23 10:03:10 +01002320void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2321 local->SetLocations(nullptr);
2322}
2323
2324void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2325 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2326}
2327
2328void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2329 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2330 locations->SetOut(Location::ConstantLocation(constant));
2331}
2332
2333void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2334 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002335 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002336}
2337
Alexandre Rames67555f72014-11-18 10:55:16 +00002338void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2339 LocationSummary* locations =
2340 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2341 InvokeRuntimeCallingConvention calling_convention;
2342 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2343}
2344
2345void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2346 codegen_->InvokeRuntime(instruction->IsEnter()
2347 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2348 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002349 instruction->GetDexPc(),
2350 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002351 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002352}
2353
Alexandre Rames42d641b2014-10-27 14:00:51 +00002354void LocationsBuilderARM64::VisitMul(HMul* mul) {
2355 LocationSummary* locations =
2356 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2357 switch (mul->GetResultType()) {
2358 case Primitive::kPrimInt:
2359 case Primitive::kPrimLong:
2360 locations->SetInAt(0, Location::RequiresRegister());
2361 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002362 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002363 break;
2364
2365 case Primitive::kPrimFloat:
2366 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002367 locations->SetInAt(0, Location::RequiresFpuRegister());
2368 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002369 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002370 break;
2371
2372 default:
2373 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2374 }
2375}
2376
2377void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2378 switch (mul->GetResultType()) {
2379 case Primitive::kPrimInt:
2380 case Primitive::kPrimLong:
2381 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2382 break;
2383
2384 case Primitive::kPrimFloat:
2385 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002386 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2391 }
2392}
2393
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002394void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2395 LocationSummary* locations =
2396 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2397 switch (neg->GetResultType()) {
2398 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002399 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002400 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002402 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002403
2404 case Primitive::kPrimFloat:
2405 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002406 locations->SetInAt(0, Location::RequiresFpuRegister());
2407 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002408 break;
2409
2410 default:
2411 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2412 }
2413}
2414
2415void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2416 switch (neg->GetResultType()) {
2417 case Primitive::kPrimInt:
2418 case Primitive::kPrimLong:
2419 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2420 break;
2421
2422 case Primitive::kPrimFloat:
2423 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002424 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002425 break;
2426
2427 default:
2428 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2429 }
2430}
2431
2432void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2433 LocationSummary* locations =
2434 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2435 InvokeRuntimeCallingConvention calling_convention;
2436 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002437 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002438 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002439 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2440 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2441 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002442}
2443
2444void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2445 LocationSummary* locations = instruction->GetLocations();
2446 InvokeRuntimeCallingConvention calling_convention;
2447 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2448 DCHECK(type_index.Is(w0));
2449 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002450 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002451 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002452 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002453 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002454 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2455 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002456 instruction->GetDexPc(),
2457 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002458 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2459 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002460}
2461
Alexandre Rames5319def2014-10-23 10:03:10 +01002462void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2463 LocationSummary* locations =
2464 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2465 InvokeRuntimeCallingConvention calling_convention;
2466 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2467 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2468 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002469 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002470}
2471
2472void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2473 LocationSummary* locations = instruction->GetLocations();
2474 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2475 DCHECK(type_index.Is(w0));
2476 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2477 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002478 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002479 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002480 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002481 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2482 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002483 instruction->GetDexPc(),
2484 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002485 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002486}
2487
2488void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002490 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002492}
2493
2494void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002495 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002496 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002497 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002498 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002499 break;
2500
2501 default:
2502 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2503 }
2504}
2505
David Brazdil66d126e2015-04-03 16:02:44 +01002506void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2508 locations->SetInAt(0, Location::RequiresRegister());
2509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2510}
2511
2512void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002513 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2514}
2515
Alexandre Rames5319def2014-10-23 10:03:10 +01002516void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2517 LocationSummary* locations =
2518 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2519 locations->SetInAt(0, Location::RequiresRegister());
2520 if (instruction->HasUses()) {
2521 locations->SetOut(Location::SameAsFirstInput());
2522 }
2523}
2524
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002525void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002526 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2527 return;
2528 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002529
Alexandre Ramesd921d642015-04-16 15:07:16 +01002530 BlockPoolsScope block_pools(GetVIXLAssembler());
2531 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002532 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2533 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2534}
2535
2536void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002537 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2538 codegen_->AddSlowPath(slow_path);
2539
2540 LocationSummary* locations = instruction->GetLocations();
2541 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002542
2543 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002544}
2545
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002546void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2547 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2548 GenerateImplicitNullCheck(instruction);
2549 } else {
2550 GenerateExplicitNullCheck(instruction);
2551 }
2552}
2553
Alexandre Rames67555f72014-11-18 10:55:16 +00002554void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2555 HandleBinaryOp(instruction);
2556}
2557
2558void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2559 HandleBinaryOp(instruction);
2560}
2561
Alexandre Rames3e69f162014-12-10 10:36:50 +00002562void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2563 LOG(FATAL) << "Unreachable";
2564}
2565
2566void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2567 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2568}
2569
Alexandre Rames5319def2014-10-23 10:03:10 +01002570void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2572 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2573 if (location.IsStackSlot()) {
2574 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2575 } else if (location.IsDoubleStackSlot()) {
2576 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2577 }
2578 locations->SetOut(location);
2579}
2580
2581void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2582 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002583 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002584}
2585
2586void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2588 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2589 locations->SetInAt(i, Location::Any());
2590 }
2591 locations->SetOut(Location::Any());
2592}
2593
2594void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002595 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002596 LOG(FATAL) << "Unreachable";
2597}
2598
Serban Constantinescu02164b32014-11-13 14:05:07 +00002599void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002600 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002601 LocationSummary::CallKind call_kind =
2602 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2604
2605 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002606 case Primitive::kPrimInt:
2607 case Primitive::kPrimLong:
2608 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002609 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2611 break;
2612
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002613 case Primitive::kPrimFloat:
2614 case Primitive::kPrimDouble: {
2615 InvokeRuntimeCallingConvention calling_convention;
2616 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2617 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2618 locations->SetOut(calling_convention.GetReturnLocation(type));
2619
2620 break;
2621 }
2622
Serban Constantinescu02164b32014-11-13 14:05:07 +00002623 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002624 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002625 }
2626}
2627
2628void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2629 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002630
Serban Constantinescu02164b32014-11-13 14:05:07 +00002631 switch (type) {
2632 case Primitive::kPrimInt:
2633 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002634 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002635 break;
2636 }
2637
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002638 case Primitive::kPrimFloat:
2639 case Primitive::kPrimDouble: {
2640 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2641 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002642 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002643 break;
2644 }
2645
Serban Constantinescu02164b32014-11-13 14:05:07 +00002646 default:
2647 LOG(FATAL) << "Unexpected rem type " << type;
2648 }
2649}
2650
Calin Juravle27df7582015-04-17 19:12:31 +01002651void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2652 memory_barrier->SetLocations(nullptr);
2653}
2654
2655void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2656 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2657}
2658
Alexandre Rames5319def2014-10-23 10:03:10 +01002659void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2660 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2661 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002662 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002663}
2664
2665void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002666 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002667 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002668}
2669
2670void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2671 instruction->SetLocations(nullptr);
2672}
2673
2674void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002675 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002676 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002677}
2678
Serban Constantinescu02164b32014-11-13 14:05:07 +00002679void LocationsBuilderARM64::VisitShl(HShl* shl) {
2680 HandleShift(shl);
2681}
2682
2683void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2684 HandleShift(shl);
2685}
2686
2687void LocationsBuilderARM64::VisitShr(HShr* shr) {
2688 HandleShift(shr);
2689}
2690
2691void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2692 HandleShift(shr);
2693}
2694
Alexandre Rames5319def2014-10-23 10:03:10 +01002695void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2696 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2697 Primitive::Type field_type = store->InputAt(1)->GetType();
2698 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002699 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002700 case Primitive::kPrimBoolean:
2701 case Primitive::kPrimByte:
2702 case Primitive::kPrimChar:
2703 case Primitive::kPrimShort:
2704 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002705 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002706 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2707 break;
2708
2709 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002710 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002711 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2712 break;
2713
2714 default:
2715 LOG(FATAL) << "Unimplemented local type " << field_type;
2716 }
2717}
2718
2719void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002720 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002721}
2722
2723void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002724 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002725}
2726
2727void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002728 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002729}
2730
Alexandre Rames67555f72014-11-18 10:55:16 +00002731void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002732 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002733}
2734
2735void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002736 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002737}
2738
2739void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002740 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002741}
2742
Alexandre Rames67555f72014-11-18 10:55:16 +00002743void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002744 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002745}
2746
2747void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2748 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2749}
2750
2751void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002752 HBasicBlock* block = instruction->GetBlock();
2753 if (block->GetLoopInformation() != nullptr) {
2754 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2755 // The back edge will generate the suspend check.
2756 return;
2757 }
2758 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2759 // The goto will generate the suspend check.
2760 return;
2761 }
2762 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002763}
2764
2765void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2766 temp->SetLocations(nullptr);
2767}
2768
2769void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2770 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002771 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002772}
2773
Alexandre Rames67555f72014-11-18 10:55:16 +00002774void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2775 LocationSummary* locations =
2776 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2777 InvokeRuntimeCallingConvention calling_convention;
2778 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2779}
2780
2781void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2782 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002783 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002784 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002785}
2786
2787void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2788 LocationSummary* locations =
2789 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2790 Primitive::Type input_type = conversion->GetInputType();
2791 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002792 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002793 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2794 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2795 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2796 }
2797
Alexandre Rames542361f2015-01-29 16:57:31 +00002798 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002799 locations->SetInAt(0, Location::RequiresFpuRegister());
2800 } else {
2801 locations->SetInAt(0, Location::RequiresRegister());
2802 }
2803
Alexandre Rames542361f2015-01-29 16:57:31 +00002804 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002805 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2806 } else {
2807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2808 }
2809}
2810
2811void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2812 Primitive::Type result_type = conversion->GetResultType();
2813 Primitive::Type input_type = conversion->GetInputType();
2814
2815 DCHECK_NE(input_type, result_type);
2816
Alexandre Rames542361f2015-01-29 16:57:31 +00002817 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002818 int result_size = Primitive::ComponentSize(result_type);
2819 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002820 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002821 Register output = OutputRegister(conversion);
2822 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002823 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2824 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2825 } else if ((result_type == Primitive::kPrimChar) ||
2826 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2827 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002828 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002829 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002830 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002831 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002832 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002833 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002834 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2835 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002836 } else if (Primitive::IsFloatingPointType(result_type) &&
2837 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002838 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2839 } else {
2840 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2841 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002842 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002843}
Alexandre Rames67555f72014-11-18 10:55:16 +00002844
Serban Constantinescu02164b32014-11-13 14:05:07 +00002845void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2846 HandleShift(ushr);
2847}
2848
2849void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2850 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002851}
2852
2853void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2854 HandleBinaryOp(instruction);
2855}
2856
2857void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2858 HandleBinaryOp(instruction);
2859}
2860
Calin Juravleb1498f62015-02-16 13:13:29 +00002861void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2862 // Nothing to do, this should be removed during prepare for register allocator.
2863 UNUSED(instruction);
2864 LOG(FATAL) << "Unreachable";
2865}
2866
2867void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2868 // Nothing to do, this should be removed during prepare for register allocator.
2869 UNUSED(instruction);
2870 LOG(FATAL) << "Unreachable";
2871}
2872
Alexandre Rames67555f72014-11-18 10:55:16 +00002873#undef __
2874#undef QUICK_ENTRY_POINT
2875
Alexandre Rames5319def2014-10-23 10:03:10 +01002876} // namespace arm64
2877} // namespace art