blob: caf7cc783e4eca9fb2eb5585d926656e328af5e2 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080020#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010021#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080022#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080024#include "intrinsics.h"
25#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "mirror/array-inl.h"
27#include "mirror/art_method.h"
28#include "mirror/class.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000029#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "thread.h"
31#include "utils/arm64/assembler_arm64.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35
36using namespace vixl; // NOLINT(build/namespaces)
37
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
44namespace arm64 {
45
Andreas Gampe878d58c2015-01-15 23:24:00 -080046using helpers::CPURegisterFrom;
47using helpers::DRegisterFrom;
48using helpers::FPRegisterFrom;
49using helpers::HeapOperand;
50using helpers::HeapOperandFrom;
51using helpers::InputCPURegisterAt;
52using helpers::InputFPRegisterAt;
53using helpers::InputRegisterAt;
54using helpers::InputOperandAt;
55using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::LocationFrom;
57using helpers::OperandFromMemOperand;
58using helpers::OutputCPURegister;
59using helpers::OutputFPRegister;
60using helpers::OutputRegister;
61using helpers::RegisterFrom;
62using helpers::StackOperandFrom;
63using helpers::VIXLRegCodeFromART;
64using helpers::WRegisterFrom;
65using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000066using helpers::ARM64EncodableConstantOrRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067
Alexandre Rames5319def2014-10-23 10:03:10 +010068static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>);
69static constexpr int kCurrentMethodStackOffset = 0;
70
Alexandre Rames5319def2014-10-23 10:03:10 +010071inline Condition ARM64Condition(IfCondition cond) {
72 switch (cond) {
73 case kCondEQ: return eq;
74 case kCondNE: return ne;
75 case kCondLT: return lt;
76 case kCondLE: return le;
77 case kCondGT: return gt;
78 case kCondGE: return ge;
79 default:
80 LOG(FATAL) << "Unknown if condition";
81 }
82 return nv; // Unreachable.
83}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
86 DCHECK_NE(return_type, Primitive::kPrimVoid);
87 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
96 } else {
97 return LocationFrom(w0);
98 }
99}
100
Alexandre Rames5319def2014-10-23 10:03:10 +0100101Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000102 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100103}
104
Alexandre Rames67555f72014-11-18 10:55:16 +0000105#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100107
Alexandre Rames5319def2014-10-23 10:03:10 +0100108class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
109 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000110 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
111 Location index_location,
112 Location length_location)
113 : instruction_(instruction),
114 index_location_(index_location),
115 length_location_(length_location) {}
116
Alexandre Rames5319def2014-10-23 10:03:10 +0100117
Alexandre Rames67555f72014-11-18 10:55:16 +0000118 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000119 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100120 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000121 // We're moving two locations to locations that could overlap, so we need a parallel
122 // move resolver.
123 InvokeRuntimeCallingConvention calling_convention;
124 codegen->EmitParallelMoves(
125 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)),
126 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)));
127 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000128 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800129 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100130 }
131
132 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000133 HBoundsCheck* const instruction_;
134 const Location index_location_;
135 const Location length_location_;
136
Alexandre Rames5319def2014-10-23 10:03:10 +0100137 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
138};
139
Alexandre Rames67555f72014-11-18 10:55:16 +0000140class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
141 public:
142 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
143
144 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
145 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
146 __ Bind(GetEntryLabel());
147 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000148 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800149 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000150 }
151
152 private:
153 HDivZeroCheck* const instruction_;
154 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
155};
156
157class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
158 public:
159 LoadClassSlowPathARM64(HLoadClass* cls,
160 HInstruction* at,
161 uint32_t dex_pc,
162 bool do_clinit)
163 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
164 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
165 }
166
167 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
168 LocationSummary* locations = at_->GetLocations();
169 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
170
171 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000173
174 InvokeRuntimeCallingConvention calling_convention;
175 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
176 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
177 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_) {
181 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t, mirror::ArtMethod*>();
182 } else {
183 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t, mirror::ArtMethod*>();
184 }
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 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
229 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000230 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000231 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800232 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000233 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000234 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000235
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000236 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000237 __ B(GetExitLabel());
238 }
239
240 private:
241 HLoadString* const instruction_;
242
243 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
244};
245
Alexandre Rames5319def2014-10-23 10:03:10 +0100246class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
247 public:
248 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
251 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100252 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000254 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100256 }
257
258 private:
259 HNullCheck* const instruction_;
260
261 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
262};
263
264class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
265 public:
266 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
267 HBasicBlock* successor)
268 : instruction_(instruction), successor_(successor) {}
269
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
271 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100272 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000275 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000278 if (successor_ == nullptr) {
279 __ B(GetReturnLabel());
280 } else {
281 __ B(arm64_codegen->GetLabelOf(successor_));
282 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100283 }
284
285 vixl::Label* GetReturnLabel() {
286 DCHECK(successor_ == nullptr);
287 return &return_label_;
288 }
289
Alexandre Rames5319def2014-10-23 10:03:10 +0100290 private:
291 HSuspendCheck* const instruction_;
292 // If not null, the block to branch to after the suspend check.
293 HBasicBlock* const successor_;
294
295 // If `successor_` is null, the label to branch to after the suspend check.
296 vixl::Label return_label_;
297
298 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
299};
300
Alexandre Rames67555f72014-11-18 10:55:16 +0000301class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
302 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000303 TypeCheckSlowPathARM64(HInstruction* instruction,
304 Location class_to_check,
305 Location object_class,
306 uint32_t dex_pc)
307 : instruction_(instruction),
308 class_to_check_(class_to_check),
309 object_class_(object_class),
310 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000311
312 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000313 LocationSummary* locations = instruction_->GetLocations();
314 DCHECK(instruction_->IsCheckCast()
315 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
316 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
317
Alexandre Rames67555f72014-11-18 10:55:16 +0000318 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000319 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000320
321 // We're moving two locations to locations that could overlap, so we need a parallel
322 // move resolver.
323 InvokeRuntimeCallingConvention calling_convention;
324 codegen->EmitParallelMoves(
325 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)),
326 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)));
327
328 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000329 arm64_codegen->InvokeRuntime(
330 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000331 Primitive::Type ret_type = instruction_->GetType();
332 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
333 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800334 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
335 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000336 } else {
337 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000338 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800339 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000340 }
341
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000342 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000343 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000344 }
345
346 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000347 HInstruction* const instruction_;
348 const Location class_to_check_;
349 const Location object_class_;
350 uint32_t dex_pc_;
351
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
353};
354
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700355class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
356 public:
357 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
358 : instruction_(instruction) {}
359
360 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
361 __ Bind(GetEntryLabel());
362 SaveLiveRegisters(codegen, instruction_->GetLocations());
363 DCHECK(instruction_->IsDeoptimize());
364 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
365 uint32_t dex_pc = deoptimize->GetDexPc();
366 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
367 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
368 }
369
370 private:
371 HInstruction* const instruction_;
372 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
373};
374
Alexandre Rames5319def2014-10-23 10:03:10 +0100375#undef __
376
377Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
378 Location next_location;
379 if (type == Primitive::kPrimVoid) {
380 LOG(FATAL) << "Unreachable type " << type;
381 }
382
Alexandre Rames542361f2015-01-29 16:57:31 +0000383 if (Primitive::IsFloatingPointType(type) &&
384 (fp_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000385 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(fp_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000386 } else if (!Primitive::IsFloatingPointType(type) &&
387 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000388 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
389 } else {
390 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000391 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
392 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 }
394
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000395 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000396 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 return next_location;
398}
399
Serban Constantinescu579885a2015-02-22 20:51:33 +0000400CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
401 const Arm64InstructionSetFeatures& isa_features,
402 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100403 : CodeGenerator(graph,
404 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000405 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000406 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000407 callee_saved_core_registers.list(),
408 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000409 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100410 block_labels_(nullptr),
411 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000412 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000413 move_resolver_(graph->GetArena(), this),
414 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000415 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000416 AddAllocatedRegister(LocationFrom(lr));
Andreas Gampe75fda572015-04-01 00:36:42 -0700417
418 // Workaround for valgrind undefined recommended_checkpoint_.
419 // This won't do anything, as the literal pool is empty, but initialize the field.
420 GetVIXLAssembler()->EmitLiteralPool(LiteralPool::EmitOption::kNoBranchRequired);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000421}
Alexandre Rames5319def2014-10-23 10:03:10 +0100422
Alexandre Rames67555f72014-11-18 10:55:16 +0000423#undef __
424#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100425
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000426void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
427 // Ensure we emit the literal pool.
428 __ FinalizeCode();
429 CodeGenerator::Finalize(allocator);
430}
431
Alexandre Rames3e69f162014-12-10 10:36:50 +0000432void ParallelMoveResolverARM64::EmitMove(size_t index) {
433 MoveOperands* move = moves_.Get(index);
434 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
435}
436
437void ParallelMoveResolverARM64::EmitSwap(size_t index) {
438 MoveOperands* move = moves_.Get(index);
439 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
440}
441
442void ParallelMoveResolverARM64::RestoreScratch(int reg) {
443 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
444}
445
446void ParallelMoveResolverARM64::SpillScratch(int reg) {
447 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
448}
449
Alexandre Rames5319def2014-10-23 10:03:10 +0100450void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000451 __ Bind(&frame_entry_label_);
452
Serban Constantinescu02164b32014-11-13 14:05:07 +0000453 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
454 if (do_overflow_check) {
455 UseScratchRegisterScope temps(GetVIXLAssembler());
456 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000457 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000458 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000459 __ Ldr(wzr, MemOperand(temp, 0));
460 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000461 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100462
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000463 if (!HasEmptyFrame()) {
464 int frame_size = GetFrameSize();
465 // Stack layout:
466 // sp[frame_size - 8] : lr.
467 // ... : other preserved core registers.
468 // ... : other preserved fp registers.
469 // ... : reserved frame space.
470 // sp[0] : current method.
471 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
472 __ PokeCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
473 __ PokeCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
474 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100475}
476
477void CodeGeneratorARM64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000478 if (!HasEmptyFrame()) {
479 int frame_size = GetFrameSize();
480 __ PeekCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
481 __ PeekCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
482 __ Drop(frame_size);
483 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100484}
485
486void CodeGeneratorARM64::Bind(HBasicBlock* block) {
487 __ Bind(GetLabelOf(block));
488}
489
Alexandre Rames5319def2014-10-23 10:03:10 +0100490void CodeGeneratorARM64::Move(HInstruction* instruction,
491 Location location,
492 HInstruction* move_for) {
493 LocationSummary* locations = instruction->GetLocations();
494 if (locations != nullptr && locations->Out().Equals(location)) {
495 return;
496 }
497
498 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000499 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100500
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000501 if (instruction->IsIntConstant()
502 || instruction->IsLongConstant()
503 || instruction->IsNullConstant()) {
504 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100505 if (location.IsRegister()) {
506 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000507 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100508 (instruction->IsLongConstant() && dst.Is64Bits()));
509 __ Mov(dst, value);
510 } else {
511 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000512 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000513 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
514 ? temps.AcquireW()
515 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100516 __ Mov(temp, value);
517 __ Str(temp, StackOperandFrom(location));
518 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000519 } else if (instruction->IsTemporary()) {
520 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000521 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100522 } else if (instruction->IsLoadLocal()) {
523 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000524 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000525 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000526 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000527 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100528 }
529
530 } else {
531 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000532 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100533 }
534}
535
Alexandre Rames5319def2014-10-23 10:03:10 +0100536Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
537 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000538
Alexandre Rames5319def2014-10-23 10:03:10 +0100539 switch (type) {
540 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000541 case Primitive::kPrimInt:
542 case Primitive::kPrimFloat:
543 return Location::StackSlot(GetStackSlot(load->GetLocal()));
544
545 case Primitive::kPrimLong:
546 case Primitive::kPrimDouble:
547 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
548
Alexandre Rames5319def2014-10-23 10:03:10 +0100549 case Primitive::kPrimBoolean:
550 case Primitive::kPrimByte:
551 case Primitive::kPrimChar:
552 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100553 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100554 LOG(FATAL) << "Unexpected type " << type;
555 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000556
Alexandre Rames5319def2014-10-23 10:03:10 +0100557 LOG(FATAL) << "Unreachable";
558 return Location::NoLocation();
559}
560
561void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000562 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100563 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000564 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100565 vixl::Label done;
566 __ Cbz(value, &done);
567 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
568 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000569 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100570 __ Bind(&done);
571}
572
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000573void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
574 // Blocked core registers:
575 // lr : Runtime reserved.
576 // tr : Runtime reserved.
577 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
578 // ip1 : VIXL core temp.
579 // ip0 : VIXL core temp.
580 //
581 // Blocked fp registers:
582 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100583 CPURegList reserved_core_registers = vixl_reserved_core_registers;
584 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100585 while (!reserved_core_registers.IsEmpty()) {
586 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
587 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000588
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000589 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800590 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000591 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
592 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000593
594 if (is_baseline) {
595 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
596 while (!reserved_core_baseline_registers.IsEmpty()) {
597 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
598 }
599
600 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
601 while (!reserved_fp_baseline_registers.IsEmpty()) {
602 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
603 }
604 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100605}
606
607Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
608 if (type == Primitive::kPrimVoid) {
609 LOG(FATAL) << "Unreachable type " << type;
610 }
611
Alexandre Rames542361f2015-01-29 16:57:31 +0000612 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000613 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
614 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100615 return Location::FpuRegisterLocation(reg);
616 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000617 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
618 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100619 return Location::RegisterLocation(reg);
620 }
621}
622
Alexandre Rames3e69f162014-12-10 10:36:50 +0000623size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
624 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
625 __ Str(reg, MemOperand(sp, stack_index));
626 return kArm64WordSize;
627}
628
629size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
630 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
631 __ Ldr(reg, MemOperand(sp, stack_index));
632 return kArm64WordSize;
633}
634
635size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
636 FPRegister reg = FPRegister(reg_id, kDRegSize);
637 __ Str(reg, MemOperand(sp, stack_index));
638 return kArm64WordSize;
639}
640
641size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
642 FPRegister reg = FPRegister(reg_id, kDRegSize);
643 __ Ldr(reg, MemOperand(sp, stack_index));
644 return kArm64WordSize;
645}
646
Alexandre Rames5319def2014-10-23 10:03:10 +0100647void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
648 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
649}
650
651void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
652 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
653}
654
Alexandre Rames67555f72014-11-18 10:55:16 +0000655void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000656 if (constant->IsIntConstant()) {
657 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
658 } else if (constant->IsLongConstant()) {
659 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
660 } else if (constant->IsNullConstant()) {
661 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000662 } else if (constant->IsFloatConstant()) {
663 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
664 } else {
665 DCHECK(constant->IsDoubleConstant());
666 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
667 }
668}
669
Alexandre Rames3e69f162014-12-10 10:36:50 +0000670
671static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
672 DCHECK(constant.IsConstant());
673 HConstant* cst = constant.GetConstant();
674 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000675 // Null is mapped to a core W register, which we associate with kPrimInt.
676 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000677 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
678 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
679 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
680}
681
682void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000683 if (source.Equals(destination)) {
684 return;
685 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000686
687 // A valid move can always be inferred from the destination and source
688 // locations. When moving from and to a register, the argument type can be
689 // used to generate 32bit instead of 64bit moves. In debug mode we also
690 // checks the coherency of the locations and the type.
691 bool unspecified_type = (type == Primitive::kPrimVoid);
692
693 if (destination.IsRegister() || destination.IsFpuRegister()) {
694 if (unspecified_type) {
695 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
696 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000697 (src_cst != nullptr && (src_cst->IsIntConstant()
698 || src_cst->IsFloatConstant()
699 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000700 // For stack slots and 32bit constants, a 64bit type is appropriate.
701 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000702 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000703 // If the source is a double stack slot or a 64bit constant, a 64bit
704 // type is appropriate. Else the source is a register, and since the
705 // type has not been specified, we chose a 64bit type to force a 64bit
706 // move.
707 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000708 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000709 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000710 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
711 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000712 CPURegister dst = CPURegisterFrom(destination, type);
713 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
714 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
715 __ Ldr(dst, StackOperandFrom(source));
716 } else if (source.IsConstant()) {
717 DCHECK(CoherentConstantAndType(source, type));
718 MoveConstant(dst, source.GetConstant());
719 } else {
720 if (destination.IsRegister()) {
721 __ Mov(Register(dst), RegisterFrom(source, type));
722 } else {
723 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
724 }
725 }
726
727 } else { // The destination is not a register. It must be a stack slot.
728 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
729 if (source.IsRegister() || source.IsFpuRegister()) {
730 if (unspecified_type) {
731 if (source.IsRegister()) {
732 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
733 } else {
734 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
735 }
736 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000737 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
738 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000739 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
740 } else if (source.IsConstant()) {
741 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
742 UseScratchRegisterScope temps(GetVIXLAssembler());
743 HConstant* src_cst = source.GetConstant();
744 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000745 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000746 temp = temps.AcquireW();
747 } else if (src_cst->IsLongConstant()) {
748 temp = temps.AcquireX();
749 } else if (src_cst->IsFloatConstant()) {
750 temp = temps.AcquireS();
751 } else {
752 DCHECK(src_cst->IsDoubleConstant());
753 temp = temps.AcquireD();
754 }
755 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000756 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000757 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000758 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000759 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000760 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000761 // There is generally less pressure on FP registers.
762 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000763 __ Ldr(temp, StackOperandFrom(source));
764 __ Str(temp, StackOperandFrom(destination));
765 }
766 }
767}
768
Alexandre Rames3e69f162014-12-10 10:36:50 +0000769void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
770 DCHECK(!loc1.IsConstant());
771 DCHECK(!loc2.IsConstant());
772
773 if (loc1.Equals(loc2)) {
774 return;
775 }
776
777 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
778
779 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
780 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
781 bool is_fp_reg1 = loc1.IsFpuRegister();
782 bool is_fp_reg2 = loc2.IsFpuRegister();
783
784 if (loc2.IsRegister() && loc1.IsRegister()) {
785 Register r1 = XRegisterFrom(loc1);
786 Register r2 = XRegisterFrom(loc2);
787 Register tmp = temps.AcquireSameSizeAs(r1);
788 __ Mov(tmp, r2);
789 __ Mov(r2, r1);
790 __ Mov(r1, tmp);
791 } else if (is_fp_reg2 && is_fp_reg1) {
792 FPRegister r1 = DRegisterFrom(loc1);
793 FPRegister r2 = DRegisterFrom(loc2);
794 FPRegister tmp = temps.AcquireSameSizeAs(r1);
795 __ Fmov(tmp, r2);
796 __ Fmov(r2, r1);
797 __ Fmov(r1, tmp);
798 } else if (is_slot1 != is_slot2) {
799 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
800 Location reg_loc = is_slot1 ? loc2 : loc1;
801 CPURegister reg, tmp;
802 if (reg_loc.IsFpuRegister()) {
803 reg = DRegisterFrom(reg_loc);
804 tmp = temps.AcquireD();
805 } else {
806 reg = XRegisterFrom(reg_loc);
807 tmp = temps.AcquireX();
808 }
809 __ Ldr(tmp, mem);
810 __ Str(reg, mem);
811 if (reg_loc.IsFpuRegister()) {
812 __ Fmov(FPRegister(reg), FPRegister(tmp));
813 } else {
814 __ Mov(Register(reg), Register(tmp));
815 }
816 } else if (is_slot1 && is_slot2) {
817 MemOperand mem1 = StackOperandFrom(loc1);
818 MemOperand mem2 = StackOperandFrom(loc2);
819 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
820 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
821 __ Ldr(tmp1, mem1);
822 __ Ldr(tmp2, mem2);
823 __ Str(tmp1, mem2);
824 __ Str(tmp2, mem1);
825 } else {
826 LOG(FATAL) << "Unimplemented";
827 }
828}
829
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000830void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000831 CPURegister dst,
832 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000833 switch (type) {
834 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000835 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000836 break;
837 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000838 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000839 break;
840 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000841 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000842 break;
843 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000844 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000845 break;
846 case Primitive::kPrimInt:
847 case Primitive::kPrimNot:
848 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000849 case Primitive::kPrimFloat:
850 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000851 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000852 __ Ldr(dst, src);
853 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000854 case Primitive::kPrimVoid:
855 LOG(FATAL) << "Unreachable type " << type;
856 }
857}
858
Calin Juravle77520bc2015-01-12 18:45:46 +0000859void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000860 CPURegister dst,
861 const MemOperand& src) {
862 UseScratchRegisterScope temps(GetVIXLAssembler());
863 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000864 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000865
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000866 DCHECK(!src.IsPreIndex());
867 DCHECK(!src.IsPostIndex());
868
869 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800870 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000871 MemOperand base = MemOperand(temp_base);
872 switch (type) {
873 case Primitive::kPrimBoolean:
874 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000875 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000876 break;
877 case Primitive::kPrimByte:
878 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000879 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000880 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
881 break;
882 case Primitive::kPrimChar:
883 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000884 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000885 break;
886 case Primitive::kPrimShort:
887 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000888 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000889 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
890 break;
891 case Primitive::kPrimInt:
892 case Primitive::kPrimNot:
893 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000894 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000895 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000896 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000897 break;
898 case Primitive::kPrimFloat:
899 case Primitive::kPrimDouble: {
900 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000901 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000902
903 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
904 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000905 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000906 __ Fmov(FPRegister(dst), temp);
907 break;
908 }
909 case Primitive::kPrimVoid:
910 LOG(FATAL) << "Unreachable type " << type;
911 }
912}
913
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000914void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000915 CPURegister src,
916 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000917 switch (type) {
918 case Primitive::kPrimBoolean:
919 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000920 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000921 break;
922 case Primitive::kPrimChar:
923 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000924 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000925 break;
926 case Primitive::kPrimInt:
927 case Primitive::kPrimNot:
928 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000929 case Primitive::kPrimFloat:
930 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000931 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000932 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000933 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000934 case Primitive::kPrimVoid:
935 LOG(FATAL) << "Unreachable type " << type;
936 }
937}
938
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000939void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
940 CPURegister src,
941 const MemOperand& dst) {
942 UseScratchRegisterScope temps(GetVIXLAssembler());
943 Register temp_base = temps.AcquireX();
944
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000945 DCHECK(!dst.IsPreIndex());
946 DCHECK(!dst.IsPostIndex());
947
948 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800949 Operand op = OperandFromMemOperand(dst);
950 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000951 MemOperand base = MemOperand(temp_base);
952 switch (type) {
953 case Primitive::kPrimBoolean:
954 case Primitive::kPrimByte:
955 __ Stlrb(Register(src), base);
956 break;
957 case Primitive::kPrimChar:
958 case Primitive::kPrimShort:
959 __ Stlrh(Register(src), base);
960 break;
961 case Primitive::kPrimInt:
962 case Primitive::kPrimNot:
963 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000964 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000965 __ Stlr(Register(src), base);
966 break;
967 case Primitive::kPrimFloat:
968 case Primitive::kPrimDouble: {
969 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000970 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000971
972 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
973 __ Fmov(temp, FPRegister(src));
974 __ Stlr(temp, base);
975 break;
976 }
977 case Primitive::kPrimVoid:
978 LOG(FATAL) << "Unreachable type " << type;
979 }
980}
981
Alexandre Rames67555f72014-11-18 10:55:16 +0000982void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000983 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000984 DCHECK(current_method.IsW());
985 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
986}
987
988void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
989 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000990 uint32_t dex_pc,
991 SlowPathCode* slow_path) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000992 __ Ldr(lr, MemOperand(tr, entry_point_offset));
993 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000994 if (instruction != nullptr) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000995 RecordPcInfo(instruction, dex_pc, slow_path);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000996 DCHECK(instruction->IsSuspendCheck()
997 || instruction->IsBoundsCheck()
998 || instruction->IsNullCheck()
999 || instruction->IsDivZeroCheck()
1000 || !IsLeafMethod());
1001 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001002}
1003
1004void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1005 vixl::Register class_reg) {
1006 UseScratchRegisterScope temps(GetVIXLAssembler());
1007 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001008 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001009 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001010
Serban Constantinescu02164b32014-11-13 14:05:07 +00001011 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001012 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001013 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1014 __ Add(temp, class_reg, status_offset);
1015 __ Ldar(temp, HeapOperand(temp));
1016 __ Cmp(temp, mirror::Class::kStatusInitialized);
1017 __ B(lt, slow_path->GetEntryLabel());
1018 } else {
1019 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1020 __ Cmp(temp, mirror::Class::kStatusInitialized);
1021 __ B(lt, slow_path->GetEntryLabel());
1022 __ Dmb(InnerShareable, BarrierReads);
1023 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001024 __ Bind(slow_path->GetExitLabel());
1025}
Alexandre Rames5319def2014-10-23 10:03:10 +01001026
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001027void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1028 BarrierType type = BarrierAll;
1029
1030 switch (kind) {
1031 case MemBarrierKind::kAnyAny:
1032 case MemBarrierKind::kAnyStore: {
1033 type = BarrierAll;
1034 break;
1035 }
1036 case MemBarrierKind::kLoadAny: {
1037 type = BarrierReads;
1038 break;
1039 }
1040 case MemBarrierKind::kStoreStore: {
1041 type = BarrierWrites;
1042 break;
1043 }
1044 default:
1045 LOG(FATAL) << "Unexpected memory barrier " << kind;
1046 }
1047 __ Dmb(InnerShareable, type);
1048}
1049
Serban Constantinescu02164b32014-11-13 14:05:07 +00001050void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1051 HBasicBlock* successor) {
1052 SuspendCheckSlowPathARM64* slow_path =
1053 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1054 codegen_->AddSlowPath(slow_path);
1055 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1056 Register temp = temps.AcquireW();
1057
1058 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1059 if (successor == nullptr) {
1060 __ Cbnz(temp, slow_path->GetEntryLabel());
1061 __ Bind(slow_path->GetReturnLabel());
1062 } else {
1063 __ Cbz(temp, codegen_->GetLabelOf(successor));
1064 __ B(slow_path->GetEntryLabel());
1065 // slow_path will return to GetLabelOf(successor).
1066 }
1067}
1068
Alexandre Rames5319def2014-10-23 10:03:10 +01001069InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1070 CodeGeneratorARM64* codegen)
1071 : HGraphVisitor(graph),
1072 assembler_(codegen->GetAssembler()),
1073 codegen_(codegen) {}
1074
1075#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001076 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001077
1078#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1079
1080enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001081 // Using a base helps identify when we hit such breakpoints.
1082 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001083#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1084 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1085#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1086};
1087
1088#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1089 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001090 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001091 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1092 } \
1093 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1095 locations->SetOut(Location::Any()); \
1096 }
1097 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1098#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1099
1100#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001101#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001102
Alexandre Rames67555f72014-11-18 10:55:16 +00001103void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001104 DCHECK_EQ(instr->InputCount(), 2U);
1105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1106 Primitive::Type type = instr->GetResultType();
1107 switch (type) {
1108 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001109 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001110 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001111 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001113 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001114
1115 case Primitive::kPrimFloat:
1116 case Primitive::kPrimDouble:
1117 locations->SetInAt(0, Location::RequiresFpuRegister());
1118 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001119 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001120 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001121
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001123 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001124 }
1125}
1126
Alexandre Rames67555f72014-11-18 10:55:16 +00001127void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001128 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001129
1130 switch (type) {
1131 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001132 case Primitive::kPrimLong: {
1133 Register dst = OutputRegister(instr);
1134 Register lhs = InputRegisterAt(instr, 0);
1135 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001136 if (instr->IsAdd()) {
1137 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001138 } else if (instr->IsAnd()) {
1139 __ And(dst, lhs, rhs);
1140 } else if (instr->IsOr()) {
1141 __ Orr(dst, lhs, rhs);
1142 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001143 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001144 } else {
1145 DCHECK(instr->IsXor());
1146 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001147 }
1148 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001149 }
1150 case Primitive::kPrimFloat:
1151 case Primitive::kPrimDouble: {
1152 FPRegister dst = OutputFPRegister(instr);
1153 FPRegister lhs = InputFPRegisterAt(instr, 0);
1154 FPRegister rhs = InputFPRegisterAt(instr, 1);
1155 if (instr->IsAdd()) {
1156 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001157 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001158 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001159 } else {
1160 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001161 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001162 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001163 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001164 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001165 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001166 }
1167}
1168
Serban Constantinescu02164b32014-11-13 14:05:07 +00001169void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1170 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1171
1172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1173 Primitive::Type type = instr->GetResultType();
1174 switch (type) {
1175 case Primitive::kPrimInt:
1176 case Primitive::kPrimLong: {
1177 locations->SetInAt(0, Location::RequiresRegister());
1178 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1179 locations->SetOut(Location::RequiresRegister());
1180 break;
1181 }
1182 default:
1183 LOG(FATAL) << "Unexpected shift type " << type;
1184 }
1185}
1186
1187void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1188 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1189
1190 Primitive::Type type = instr->GetType();
1191 switch (type) {
1192 case Primitive::kPrimInt:
1193 case Primitive::kPrimLong: {
1194 Register dst = OutputRegister(instr);
1195 Register lhs = InputRegisterAt(instr, 0);
1196 Operand rhs = InputOperandAt(instr, 1);
1197 if (rhs.IsImmediate()) {
1198 uint32_t shift_value = (type == Primitive::kPrimInt)
1199 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1200 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1201 if (instr->IsShl()) {
1202 __ Lsl(dst, lhs, shift_value);
1203 } else if (instr->IsShr()) {
1204 __ Asr(dst, lhs, shift_value);
1205 } else {
1206 __ Lsr(dst, lhs, shift_value);
1207 }
1208 } else {
1209 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1210
1211 if (instr->IsShl()) {
1212 __ Lsl(dst, lhs, rhs_reg);
1213 } else if (instr->IsShr()) {
1214 __ Asr(dst, lhs, rhs_reg);
1215 } else {
1216 __ Lsr(dst, lhs, rhs_reg);
1217 }
1218 }
1219 break;
1220 }
1221 default:
1222 LOG(FATAL) << "Unexpected shift operation type " << type;
1223 }
1224}
1225
Alexandre Rames5319def2014-10-23 10:03:10 +01001226void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001227 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001228}
1229
1230void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001231 HandleBinaryOp(instruction);
1232}
1233
1234void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1235 HandleBinaryOp(instruction);
1236}
1237
1238void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1239 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001240}
1241
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001242void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1243 LocationSummary* locations =
1244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1245 locations->SetInAt(0, Location::RequiresRegister());
1246 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1247 locations->SetOut(Location::RequiresRegister());
1248}
1249
1250void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1251 LocationSummary* locations = instruction->GetLocations();
1252 Primitive::Type type = instruction->GetType();
1253 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001254 Location index = locations->InAt(1);
1255 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001256 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001257 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001258
1259 if (index.IsConstant()) {
1260 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001261 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001262 } else {
1263 Register temp = temps.AcquireSameSizeAs(obj);
1264 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1265 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001266 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001267 }
1268
Alexandre Rames67555f72014-11-18 10:55:16 +00001269 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001270 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001271}
1272
Alexandre Rames5319def2014-10-23 10:03:10 +01001273void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1274 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1275 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001277}
1278
1279void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1280 __ Ldr(OutputRegister(instruction),
1281 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001283}
1284
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001285void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1286 Primitive::Type value_type = instruction->GetComponentType();
1287 bool is_object = value_type == Primitive::kPrimNot;
1288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1289 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1290 if (is_object) {
1291 InvokeRuntimeCallingConvention calling_convention;
1292 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1293 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1294 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1295 } else {
1296 locations->SetInAt(0, Location::RequiresRegister());
1297 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1298 locations->SetInAt(2, Location::RequiresRegister());
1299 }
1300}
1301
1302void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1303 Primitive::Type value_type = instruction->GetComponentType();
1304 if (value_type == Primitive::kPrimNot) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001305 codegen_->InvokeRuntime(
1306 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001307 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001308 } else {
1309 LocationSummary* locations = instruction->GetLocations();
1310 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001311 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001312 Location index = locations->InAt(1);
1313 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001314 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001315 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316
1317 if (index.IsConstant()) {
1318 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001319 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001320 } else {
1321 Register temp = temps.AcquireSameSizeAs(obj);
1322 Register index_reg = InputRegisterAt(instruction, 1);
1323 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001324 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001325 }
1326
1327 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001329 }
1330}
1331
Alexandre Rames67555f72014-11-18 10:55:16 +00001332void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1333 LocationSummary* locations =
1334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1335 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001336 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001337 if (instruction->HasUses()) {
1338 locations->SetOut(Location::SameAsFirstInput());
1339 }
1340}
1341
1342void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001343 LocationSummary* locations = instruction->GetLocations();
1344 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1345 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001346 codegen_->AddSlowPath(slow_path);
1347
1348 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1349 __ B(slow_path->GetEntryLabel(), hs);
1350}
1351
1352void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1353 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1354 instruction, LocationSummary::kCallOnSlowPath);
1355 locations->SetInAt(0, Location::RequiresRegister());
1356 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001357 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001358}
1359
1360void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001361 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001362 Register obj = InputRegisterAt(instruction, 0);;
1363 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001364 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001365
Alexandre Rames3e69f162014-12-10 10:36:50 +00001366 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1367 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001368 codegen_->AddSlowPath(slow_path);
1369
1370 // TODO: avoid this check if we know obj is not null.
1371 __ Cbz(obj, slow_path->GetExitLabel());
1372 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001373 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1374 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001375 __ B(ne, slow_path->GetEntryLabel());
1376 __ Bind(slow_path->GetExitLabel());
1377}
1378
1379void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1380 LocationSummary* locations =
1381 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1382 locations->SetInAt(0, Location::RequiresRegister());
1383 if (check->HasUses()) {
1384 locations->SetOut(Location::SameAsFirstInput());
1385 }
1386}
1387
1388void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1389 // We assume the class is not null.
1390 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1391 check->GetLoadClass(), check, check->GetDexPc(), true);
1392 codegen_->AddSlowPath(slow_path);
1393 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1394}
1395
Serban Constantinescu02164b32014-11-13 14:05:07 +00001396void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001397 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001398 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1399 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001400 switch (in_type) {
1401 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001402 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001403 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1405 break;
1406 }
1407 case Primitive::kPrimFloat:
1408 case Primitive::kPrimDouble: {
1409 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001410 HInstruction* right = compare->InputAt(1);
1411 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1412 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1413 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1414 } else {
1415 locations->SetInAt(1, Location::RequiresFpuRegister());
1416 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001417 locations->SetOut(Location::RequiresRegister());
1418 break;
1419 }
1420 default:
1421 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1422 }
1423}
1424
1425void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1426 Primitive::Type in_type = compare->InputAt(0)->GetType();
1427
1428 // 0 if: left == right
1429 // 1 if: left > right
1430 // -1 if: left < right
1431 switch (in_type) {
1432 case Primitive::kPrimLong: {
1433 Register result = OutputRegister(compare);
1434 Register left = InputRegisterAt(compare, 0);
1435 Operand right = InputOperandAt(compare, 1);
1436
1437 __ Cmp(left, right);
1438 __ Cset(result, ne);
1439 __ Cneg(result, result, lt);
1440 break;
1441 }
1442 case Primitive::kPrimFloat:
1443 case Primitive::kPrimDouble: {
1444 Register result = OutputRegister(compare);
1445 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001446 if (compare->GetLocations()->InAt(1).IsConstant()) {
1447 if (kIsDebugBuild) {
1448 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1449 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1450 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1451 }
1452 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1453 __ Fcmp(left, 0.0);
1454 } else {
1455 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1456 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001457 if (compare->IsGtBias()) {
1458 __ Cset(result, ne);
1459 } else {
1460 __ Csetm(result, ne);
1461 }
1462 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001463 break;
1464 }
1465 default:
1466 LOG(FATAL) << "Unimplemented compare type " << in_type;
1467 }
1468}
1469
1470void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1472 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001473 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001474 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001476 }
1477}
1478
1479void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1480 if (!instruction->NeedsMaterialization()) {
1481 return;
1482 }
1483
1484 LocationSummary* locations = instruction->GetLocations();
1485 Register lhs = InputRegisterAt(instruction, 0);
1486 Operand rhs = InputOperandAt(instruction, 1);
1487 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1488 Condition cond = ARM64Condition(instruction->GetCondition());
1489
1490 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001491 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001492}
1493
1494#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1495 M(Equal) \
1496 M(NotEqual) \
1497 M(LessThan) \
1498 M(LessThanOrEqual) \
1499 M(GreaterThan) \
1500 M(GreaterThanOrEqual)
1501#define DEFINE_CONDITION_VISITORS(Name) \
1502void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1503void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1504FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001505#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001506#undef FOR_EACH_CONDITION_INSTRUCTION
1507
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001508void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1509 LocationSummary* locations =
1510 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1511 switch (div->GetResultType()) {
1512 case Primitive::kPrimInt:
1513 case Primitive::kPrimLong:
1514 locations->SetInAt(0, Location::RequiresRegister());
1515 locations->SetInAt(1, Location::RequiresRegister());
1516 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1517 break;
1518
1519 case Primitive::kPrimFloat:
1520 case Primitive::kPrimDouble:
1521 locations->SetInAt(0, Location::RequiresFpuRegister());
1522 locations->SetInAt(1, Location::RequiresFpuRegister());
1523 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1524 break;
1525
1526 default:
1527 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1528 }
1529}
1530
1531void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1532 Primitive::Type type = div->GetResultType();
1533 switch (type) {
1534 case Primitive::kPrimInt:
1535 case Primitive::kPrimLong:
1536 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1537 break;
1538
1539 case Primitive::kPrimFloat:
1540 case Primitive::kPrimDouble:
1541 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1542 break;
1543
1544 default:
1545 LOG(FATAL) << "Unexpected div type " << type;
1546 }
1547}
1548
Alexandre Rames67555f72014-11-18 10:55:16 +00001549void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1550 LocationSummary* locations =
1551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1552 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1553 if (instruction->HasUses()) {
1554 locations->SetOut(Location::SameAsFirstInput());
1555 }
1556}
1557
1558void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1559 SlowPathCodeARM64* slow_path =
1560 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1561 codegen_->AddSlowPath(slow_path);
1562 Location value = instruction->GetLocations()->InAt(0);
1563
Alexandre Rames3e69f162014-12-10 10:36:50 +00001564 Primitive::Type type = instruction->GetType();
1565
1566 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1567 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1568 return;
1569 }
1570
Alexandre Rames67555f72014-11-18 10:55:16 +00001571 if (value.IsConstant()) {
1572 int64_t divisor = Int64ConstantFrom(value);
1573 if (divisor == 0) {
1574 __ B(slow_path->GetEntryLabel());
1575 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001576 // A division by a non-null constant is valid. We don't need to perform
1577 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001578 }
1579 } else {
1580 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1581 }
1582}
1583
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001584void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1585 LocationSummary* locations =
1586 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1587 locations->SetOut(Location::ConstantLocation(constant));
1588}
1589
1590void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1591 UNUSED(constant);
1592 // Will be generated at use site.
1593}
1594
Alexandre Rames5319def2014-10-23 10:03:10 +01001595void LocationsBuilderARM64::VisitExit(HExit* exit) {
1596 exit->SetLocations(nullptr);
1597}
1598
1599void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001600 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001601}
1602
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001603void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1604 LocationSummary* locations =
1605 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1606 locations->SetOut(Location::ConstantLocation(constant));
1607}
1608
1609void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1610 UNUSED(constant);
1611 // Will be generated at use site.
1612}
1613
Alexandre Rames5319def2014-10-23 10:03:10 +01001614void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1615 got->SetLocations(nullptr);
1616}
1617
1618void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1619 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001620 DCHECK(!successor->IsExitBlock());
1621 HBasicBlock* block = got->GetBlock();
1622 HInstruction* previous = got->GetPrevious();
1623 HLoopInformation* info = block->GetLoopInformation();
1624
David Brazdil46e2a392015-03-16 17:31:52 +00001625 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001626 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1627 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1628 return;
1629 }
1630 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1631 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1632 }
1633 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001634 __ B(codegen_->GetLabelOf(successor));
1635 }
1636}
1637
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001638void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1639 vixl::Label* true_target,
1640 vixl::Label* false_target,
1641 vixl::Label* always_true_target) {
1642 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001643 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001644
Serban Constantinescu02164b32014-11-13 14:05:07 +00001645 if (cond->IsIntConstant()) {
1646 int32_t cond_value = cond->AsIntConstant()->GetValue();
1647 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001648 if (always_true_target != nullptr) {
1649 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001650 }
1651 return;
1652 } else {
1653 DCHECK_EQ(cond_value, 0);
1654 }
1655 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001656 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001657 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001658 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001659 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001660 } else {
1661 // The condition instruction has not been materialized, use its inputs as
1662 // the comparison and its condition as the branch condition.
1663 Register lhs = InputRegisterAt(condition, 0);
1664 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001665 Condition arm64_cond = ARM64Condition(condition->GetCondition());
1666 if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1667 if (arm64_cond == eq) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001668 __ Cbz(lhs, true_target);
1669 } else {
1670 __ Cbnz(lhs, true_target);
1671 }
1672 } else {
1673 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001674 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001675 }
1676 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001677 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001678 __ B(false_target);
1679 }
1680}
1681
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001682void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1683 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1684 HInstruction* cond = if_instr->InputAt(0);
1685 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1686 locations->SetInAt(0, Location::RequiresRegister());
1687 }
1688}
1689
1690void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1691 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1692 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1693 vixl::Label* always_true_target = true_target;
1694 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1695 if_instr->IfTrueSuccessor())) {
1696 always_true_target = nullptr;
1697 }
1698 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1699 if_instr->IfFalseSuccessor())) {
1700 false_target = nullptr;
1701 }
1702 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1703}
1704
1705void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1706 LocationSummary* locations = new (GetGraph()->GetArena())
1707 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1708 HInstruction* cond = deoptimize->InputAt(0);
1709 DCHECK(cond->IsCondition());
1710 if (cond->AsCondition()->NeedsMaterialization()) {
1711 locations->SetInAt(0, Location::RequiresRegister());
1712 }
1713}
1714
1715void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
1716 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
1717 DeoptimizationSlowPathARM64(deoptimize);
1718 codegen_->AddSlowPath(slow_path);
1719 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
1720 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1721}
1722
Alexandre Rames5319def2014-10-23 10:03:10 +01001723void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001724 LocationSummary* locations =
1725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001726 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001728}
1729
1730void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001731 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00001732 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001733
1734 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001735 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001736 // NB: LoadAcquire will record the pc info if needed.
1737 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738 } else {
1739 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001741 // For IRIW sequential consistency kLoadAny is not sufficient.
1742 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1743 }
1744 } else {
1745 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001746 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001747 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001748}
1749
1750void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001751 LocationSummary* locations =
1752 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001753 locations->SetInAt(0, Location::RequiresRegister());
1754 locations->SetInAt(1, Location::RequiresRegister());
1755}
1756
1757void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001758 Register obj = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001759 CPURegister value = InputCPURegisterAt(instruction, 1);
1760 Offset offset = instruction->GetFieldOffset();
1761 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001762 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001763
1764 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00001765 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001766 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001768 } else {
1769 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1770 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001771 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001772 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1773 }
1774 } else {
1775 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001776 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001777 }
1778
1779 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001780 codegen_->MarkGCCard(obj, Register(value));
Alexandre Rames5319def2014-10-23 10:03:10 +01001781 }
1782}
1783
Alexandre Rames67555f72014-11-18 10:55:16 +00001784void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1785 LocationSummary::CallKind call_kind =
1786 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1787 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1788 locations->SetInAt(0, Location::RequiresRegister());
1789 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001790 // The output does overlap inputs.
1791 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001792}
1793
1794void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1795 LocationSummary* locations = instruction->GetLocations();
1796 Register obj = InputRegisterAt(instruction, 0);;
1797 Register cls = InputRegisterAt(instruction, 1);;
1798 Register out = OutputRegister(instruction);
1799
1800 vixl::Label done;
1801
1802 // Return 0 if `obj` is null.
1803 // TODO: Avoid this check if we know `obj` is not null.
1804 __ Mov(out, 0);
1805 __ Cbz(obj, &done);
1806
1807 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001808 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001809 __ Cmp(out, cls);
1810 if (instruction->IsClassFinal()) {
1811 // Classes must be equal for the instanceof to succeed.
1812 __ Cset(out, eq);
1813 } else {
1814 // If the classes are not equal, we go into a slow path.
1815 DCHECK(locations->OnlyCallsOnSlowPath());
1816 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001817 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1818 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001819 codegen_->AddSlowPath(slow_path);
1820 __ B(ne, slow_path->GetEntryLabel());
1821 __ Mov(out, 1);
1822 __ Bind(slow_path->GetExitLabel());
1823 }
1824
1825 __ Bind(&done);
1826}
1827
Alexandre Rames5319def2014-10-23 10:03:10 +01001828void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1829 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1830 locations->SetOut(Location::ConstantLocation(constant));
1831}
1832
1833void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1834 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001835 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001836}
1837
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001838void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
1839 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1840 locations->SetOut(Location::ConstantLocation(constant));
1841}
1842
1843void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
1844 // Will be generated at use site.
1845 UNUSED(constant);
1846}
1847
Alexandre Rames5319def2014-10-23 10:03:10 +01001848void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1849 LocationSummary* locations =
1850 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1851 locations->AddTemp(LocationFrom(x0));
1852
1853 InvokeDexCallingConventionVisitor calling_convention_visitor;
1854 for (size_t i = 0; i < invoke->InputCount(); i++) {
1855 HInstruction* input = invoke->InputAt(i);
1856 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1857 }
1858
1859 Primitive::Type return_type = invoke->GetType();
1860 if (return_type != Primitive::kPrimVoid) {
1861 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1862 }
1863}
1864
Alexandre Rames67555f72014-11-18 10:55:16 +00001865void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1866 HandleInvoke(invoke);
1867}
1868
1869void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1870 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1871 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1872 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1873 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1874 Location receiver = invoke->GetLocations()->InAt(0);
1875 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001876 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001877
1878 // The register ip1 is required to be used for the hidden argument in
1879 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1880 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1881 scratch_scope.Exclude(ip1);
1882 __ Mov(ip1, invoke->GetDexMethodIndex());
1883
1884 // temp = object->GetClass();
1885 if (receiver.IsStackSlot()) {
1886 __ Ldr(temp, StackOperandFrom(receiver));
1887 __ Ldr(temp, HeapOperand(temp, class_offset));
1888 } else {
1889 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1890 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001891 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001892 // temp = temp->GetImtEntryAt(method_offset);
1893 __ Ldr(temp, HeapOperand(temp, method_offset));
1894 // lr = temp->GetEntryPoint();
1895 __ Ldr(lr, HeapOperand(temp, entry_point));
1896 // lr();
1897 __ Blr(lr);
1898 DCHECK(!codegen_->IsLeafMethod());
1899 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1900}
1901
1902void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001903 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1904 if (intrinsic.TryDispatch(invoke)) {
1905 return;
1906 }
1907
Alexandre Rames67555f72014-11-18 10:55:16 +00001908 HandleInvoke(invoke);
1909}
1910
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001911void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001912 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1913 if (intrinsic.TryDispatch(invoke)) {
1914 return;
1915 }
1916
Alexandre Rames67555f72014-11-18 10:55:16 +00001917 HandleInvoke(invoke);
1918}
1919
Andreas Gampe878d58c2015-01-15 23:24:00 -08001920static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1921 if (invoke->GetLocations()->Intrinsified()) {
1922 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1923 intrinsic.Dispatch(invoke);
1924 return true;
1925 }
1926 return false;
1927}
1928
1929void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1930 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1931 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001932 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001933 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001934
1935 // TODO: Implement all kinds of calls:
1936 // 1) boot -> boot
1937 // 2) app -> boot
1938 // 3) app -> app
1939 //
1940 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1941
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00001942 // temp = method;
1943 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001944 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001945 // temp = temp->dex_cache_resolved_methods_;
1946 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
1947 // temp = temp[index_in_cache];
1948 __ Ldr(temp, HeapOperand(temp, index_in_cache));
1949 // lr = temp->entry_point_from_quick_compiled_code_;
1950 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1951 kArm64WordSize)));
1952 // lr();
1953 __ Blr(lr);
1954 } else {
1955 __ Bl(&frame_entry_label_);
1956 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001957
Andreas Gampe878d58c2015-01-15 23:24:00 -08001958 DCHECK(!IsLeafMethod());
1959}
1960
1961void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1962 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1963 return;
1964 }
1965
1966 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1967 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001968 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01001969}
1970
1971void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001972 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1973 return;
1974 }
1975
Alexandre Rames5319def2014-10-23 10:03:10 +01001976 LocationSummary* locations = invoke->GetLocations();
1977 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001978 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01001979 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1980 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1981 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001982 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01001983
1984 // temp = object->GetClass();
1985 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001986 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
1987 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001988 } else {
1989 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001990 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001991 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001992 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01001993 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001994 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001995 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001996 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001997 // lr();
1998 __ Blr(lr);
1999 DCHECK(!codegen_->IsLeafMethod());
2000 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2001}
2002
Alexandre Rames67555f72014-11-18 10:55:16 +00002003void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2004 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2005 : LocationSummary::kNoCall;
2006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2007 locations->SetOut(Location::RequiresRegister());
2008}
2009
2010void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2011 Register out = OutputRegister(cls);
2012 if (cls->IsReferrersClass()) {
2013 DCHECK(!cls->CanCallRuntime());
2014 DCHECK(!cls->MustGenerateClinitCheck());
2015 codegen_->LoadCurrentMethod(out);
2016 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2017 } else {
2018 DCHECK(cls->CanCallRuntime());
2019 codegen_->LoadCurrentMethod(out);
2020 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002021 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002022
2023 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2024 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2025 codegen_->AddSlowPath(slow_path);
2026 __ Cbz(out, slow_path->GetEntryLabel());
2027 if (cls->MustGenerateClinitCheck()) {
2028 GenerateClassInitializationCheck(slow_path, out);
2029 } else {
2030 __ Bind(slow_path->GetExitLabel());
2031 }
2032 }
2033}
2034
2035void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2036 LocationSummary* locations =
2037 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2038 locations->SetOut(Location::RequiresRegister());
2039}
2040
2041void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2042 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2043 __ Ldr(OutputRegister(instruction), exception);
2044 __ Str(wzr, exception);
2045}
2046
Alexandre Rames5319def2014-10-23 10:03:10 +01002047void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2048 load->SetLocations(nullptr);
2049}
2050
2051void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2052 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002053 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002054}
2055
Alexandre Rames67555f72014-11-18 10:55:16 +00002056void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2057 LocationSummary* locations =
2058 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2059 locations->SetOut(Location::RequiresRegister());
2060}
2061
2062void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2063 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2064 codegen_->AddSlowPath(slow_path);
2065
2066 Register out = OutputRegister(load);
2067 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08002068 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
2069 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002070 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002071 __ Cbz(out, slow_path->GetEntryLabel());
2072 __ Bind(slow_path->GetExitLabel());
2073}
2074
Alexandre Rames5319def2014-10-23 10:03:10 +01002075void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2076 local->SetLocations(nullptr);
2077}
2078
2079void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2080 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2081}
2082
2083void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2085 locations->SetOut(Location::ConstantLocation(constant));
2086}
2087
2088void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2089 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002090 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002091}
2092
Alexandre Rames67555f72014-11-18 10:55:16 +00002093void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2094 LocationSummary* locations =
2095 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2096 InvokeRuntimeCallingConvention calling_convention;
2097 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2098}
2099
2100void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2101 codegen_->InvokeRuntime(instruction->IsEnter()
2102 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2103 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002104 instruction->GetDexPc(),
2105 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002106 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002107}
2108
Alexandre Rames42d641b2014-10-27 14:00:51 +00002109void LocationsBuilderARM64::VisitMul(HMul* mul) {
2110 LocationSummary* locations =
2111 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2112 switch (mul->GetResultType()) {
2113 case Primitive::kPrimInt:
2114 case Primitive::kPrimLong:
2115 locations->SetInAt(0, Location::RequiresRegister());
2116 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002118 break;
2119
2120 case Primitive::kPrimFloat:
2121 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002122 locations->SetInAt(0, Location::RequiresFpuRegister());
2123 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002124 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002125 break;
2126
2127 default:
2128 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2129 }
2130}
2131
2132void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2133 switch (mul->GetResultType()) {
2134 case Primitive::kPrimInt:
2135 case Primitive::kPrimLong:
2136 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2137 break;
2138
2139 case Primitive::kPrimFloat:
2140 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002141 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002142 break;
2143
2144 default:
2145 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2146 }
2147}
2148
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002149void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2150 LocationSummary* locations =
2151 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2152 switch (neg->GetResultType()) {
2153 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002154 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002155 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002156 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002157 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002158
2159 case Primitive::kPrimFloat:
2160 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002161 locations->SetInAt(0, Location::RequiresFpuRegister());
2162 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002163 break;
2164
2165 default:
2166 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2167 }
2168}
2169
2170void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2171 switch (neg->GetResultType()) {
2172 case Primitive::kPrimInt:
2173 case Primitive::kPrimLong:
2174 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2175 break;
2176
2177 case Primitive::kPrimFloat:
2178 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002179 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002180 break;
2181
2182 default:
2183 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2184 }
2185}
2186
2187void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2188 LocationSummary* locations =
2189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2190 InvokeRuntimeCallingConvention calling_convention;
2191 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002192 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002193 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002194 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2195 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2196 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002197}
2198
2199void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2200 LocationSummary* locations = instruction->GetLocations();
2201 InvokeRuntimeCallingConvention calling_convention;
2202 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2203 DCHECK(type_index.Is(w0));
2204 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002205 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002206 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002207 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002208 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002209 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2210 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002211 instruction->GetDexPc(),
2212 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002213 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2214 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002215}
2216
Alexandre Rames5319def2014-10-23 10:03:10 +01002217void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2218 LocationSummary* locations =
2219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2220 InvokeRuntimeCallingConvention calling_convention;
2221 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2222 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2223 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002224 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002225}
2226
2227void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2228 LocationSummary* locations = instruction->GetLocations();
2229 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2230 DCHECK(type_index.Is(w0));
2231 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2232 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002233 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002234 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002235 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002236 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2237 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002238 instruction->GetDexPc(),
2239 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002240 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002241}
2242
2243void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002245 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002247}
2248
2249void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002250 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002251 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002252 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002253 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2258 }
2259}
2260
2261void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2262 LocationSummary* locations =
2263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2264 locations->SetInAt(0, Location::RequiresRegister());
2265 if (instruction->HasUses()) {
2266 locations->SetOut(Location::SameAsFirstInput());
2267 }
2268}
2269
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002270void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002271 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2272 return;
2273 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002274 Location obj = instruction->GetLocations()->InAt(0);
2275
2276 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2277 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2278}
2279
2280void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002281 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2282 codegen_->AddSlowPath(slow_path);
2283
2284 LocationSummary* locations = instruction->GetLocations();
2285 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002286
2287 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002288}
2289
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002290void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2291 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2292 GenerateImplicitNullCheck(instruction);
2293 } else {
2294 GenerateExplicitNullCheck(instruction);
2295 }
2296}
2297
Alexandre Rames67555f72014-11-18 10:55:16 +00002298void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2299 HandleBinaryOp(instruction);
2300}
2301
2302void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2303 HandleBinaryOp(instruction);
2304}
2305
Alexandre Rames3e69f162014-12-10 10:36:50 +00002306void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2307 LOG(FATAL) << "Unreachable";
2308}
2309
2310void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2311 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2312}
2313
Alexandre Rames5319def2014-10-23 10:03:10 +01002314void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2316 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2317 if (location.IsStackSlot()) {
2318 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2319 } else if (location.IsDoubleStackSlot()) {
2320 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2321 }
2322 locations->SetOut(location);
2323}
2324
2325void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2326 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002327 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002328}
2329
2330void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2332 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2333 locations->SetInAt(i, Location::Any());
2334 }
2335 locations->SetOut(Location::Any());
2336}
2337
2338void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002339 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002340 LOG(FATAL) << "Unreachable";
2341}
2342
Serban Constantinescu02164b32014-11-13 14:05:07 +00002343void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002344 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002345 LocationSummary::CallKind call_kind =
2346 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002347 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2348
2349 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002350 case Primitive::kPrimInt:
2351 case Primitive::kPrimLong:
2352 locations->SetInAt(0, Location::RequiresRegister());
2353 locations->SetInAt(1, Location::RequiresRegister());
2354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2355 break;
2356
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002357 case Primitive::kPrimFloat:
2358 case Primitive::kPrimDouble: {
2359 InvokeRuntimeCallingConvention calling_convention;
2360 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2361 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2362 locations->SetOut(calling_convention.GetReturnLocation(type));
2363
2364 break;
2365 }
2366
Serban Constantinescu02164b32014-11-13 14:05:07 +00002367 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002368 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002369 }
2370}
2371
2372void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2373 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002374
Serban Constantinescu02164b32014-11-13 14:05:07 +00002375 switch (type) {
2376 case Primitive::kPrimInt:
2377 case Primitive::kPrimLong: {
2378 UseScratchRegisterScope temps(GetVIXLAssembler());
2379 Register dividend = InputRegisterAt(rem, 0);
2380 Register divisor = InputRegisterAt(rem, 1);
2381 Register output = OutputRegister(rem);
2382 Register temp = temps.AcquireSameSizeAs(output);
2383
2384 __ Sdiv(temp, dividend, divisor);
2385 __ Msub(output, temp, divisor, dividend);
2386 break;
2387 }
2388
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002389 case Primitive::kPrimFloat:
2390 case Primitive::kPrimDouble: {
2391 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2392 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002393 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002394 break;
2395 }
2396
Serban Constantinescu02164b32014-11-13 14:05:07 +00002397 default:
2398 LOG(FATAL) << "Unexpected rem type " << type;
2399 }
2400}
2401
Alexandre Rames5319def2014-10-23 10:03:10 +01002402void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2404 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002405 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002406}
2407
2408void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002409 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002410 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002411 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002412}
2413
2414void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2415 instruction->SetLocations(nullptr);
2416}
2417
2418void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002419 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002420 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002421 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002422}
2423
Serban Constantinescu02164b32014-11-13 14:05:07 +00002424void LocationsBuilderARM64::VisitShl(HShl* shl) {
2425 HandleShift(shl);
2426}
2427
2428void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2429 HandleShift(shl);
2430}
2431
2432void LocationsBuilderARM64::VisitShr(HShr* shr) {
2433 HandleShift(shr);
2434}
2435
2436void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2437 HandleShift(shr);
2438}
2439
Alexandre Rames5319def2014-10-23 10:03:10 +01002440void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2442 Primitive::Type field_type = store->InputAt(1)->GetType();
2443 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002444 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002445 case Primitive::kPrimBoolean:
2446 case Primitive::kPrimByte:
2447 case Primitive::kPrimChar:
2448 case Primitive::kPrimShort:
2449 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002450 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002451 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2452 break;
2453
2454 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002455 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002456 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2457 break;
2458
2459 default:
2460 LOG(FATAL) << "Unimplemented local type " << field_type;
2461 }
2462}
2463
2464void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002465 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002466}
2467
2468void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002469 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002470}
2471
2472void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002473 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002474}
2475
Alexandre Rames67555f72014-11-18 10:55:16 +00002476void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2477 LocationSummary* locations =
2478 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2479 locations->SetInAt(0, Location::RequiresRegister());
2480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2481}
2482
2483void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002484 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu579885a2015-02-22 20:51:33 +00002485 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002486
2487 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002488 if (use_acquire_release) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002489 // NB: LoadAcquire will record the pc info if needed.
2490 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002491 } else {
2492 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2493 // For IRIW sequential consistency kLoadAny is not sufficient.
2494 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2495 }
2496 } else {
2497 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2498 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002499}
2500
2501void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002502 LocationSummary* locations =
2503 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2504 locations->SetInAt(0, Location::RequiresRegister());
2505 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01002506}
2507
Alexandre Rames67555f72014-11-18 10:55:16 +00002508void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002509 Register cls = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002510 CPURegister value = InputCPURegisterAt(instruction, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002511 Offset offset = instruction->GetFieldOffset();
Alexandre Rames67555f72014-11-18 10:55:16 +00002512 Primitive::Type field_type = instruction->GetFieldType();
Serban Constantinescu579885a2015-02-22 20:51:33 +00002513 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Alexandre Rames5319def2014-10-23 10:03:10 +01002514
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002515 if (instruction->IsVolatile()) {
Serban Constantinescu579885a2015-02-22 20:51:33 +00002516 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002517 codegen_->StoreRelease(field_type, value, HeapOperand(cls, offset));
2518 } else {
2519 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2520 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2521 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2522 }
2523 } else {
2524 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2525 }
2526
2527 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002528 codegen_->MarkGCCard(cls, Register(value));
2529 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002530}
2531
2532void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2533 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2534}
2535
2536void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002537 HBasicBlock* block = instruction->GetBlock();
2538 if (block->GetLoopInformation() != nullptr) {
2539 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2540 // The back edge will generate the suspend check.
2541 return;
2542 }
2543 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2544 // The goto will generate the suspend check.
2545 return;
2546 }
2547 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002548}
2549
2550void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2551 temp->SetLocations(nullptr);
2552}
2553
2554void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2555 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002556 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002557}
2558
Alexandre Rames67555f72014-11-18 10:55:16 +00002559void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2560 LocationSummary* locations =
2561 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2562 InvokeRuntimeCallingConvention calling_convention;
2563 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2564}
2565
2566void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2567 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002568 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002569 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002570}
2571
2572void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2573 LocationSummary* locations =
2574 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2575 Primitive::Type input_type = conversion->GetInputType();
2576 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002577 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002578 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2579 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2580 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2581 }
2582
Alexandre Rames542361f2015-01-29 16:57:31 +00002583 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002584 locations->SetInAt(0, Location::RequiresFpuRegister());
2585 } else {
2586 locations->SetInAt(0, Location::RequiresRegister());
2587 }
2588
Alexandre Rames542361f2015-01-29 16:57:31 +00002589 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2591 } else {
2592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2593 }
2594}
2595
2596void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2597 Primitive::Type result_type = conversion->GetResultType();
2598 Primitive::Type input_type = conversion->GetInputType();
2599
2600 DCHECK_NE(input_type, result_type);
2601
Alexandre Rames542361f2015-01-29 16:57:31 +00002602 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002603 int result_size = Primitive::ComponentSize(result_type);
2604 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002605 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002606 Register output = OutputRegister(conversion);
2607 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002608 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2609 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2610 } else if ((result_type == Primitive::kPrimChar) ||
2611 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2612 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002613 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002614 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002615 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002616 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002617 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002618 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002619 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2620 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002621 } else if (Primitive::IsFloatingPointType(result_type) &&
2622 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002623 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2624 } else {
2625 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2626 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002627 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002628}
Alexandre Rames67555f72014-11-18 10:55:16 +00002629
Serban Constantinescu02164b32014-11-13 14:05:07 +00002630void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2631 HandleShift(ushr);
2632}
2633
2634void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2635 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002636}
2637
2638void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2639 HandleBinaryOp(instruction);
2640}
2641
2642void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2643 HandleBinaryOp(instruction);
2644}
2645
Calin Juravleb1498f62015-02-16 13:13:29 +00002646void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2647 // Nothing to do, this should be removed during prepare for register allocator.
2648 UNUSED(instruction);
2649 LOG(FATAL) << "Unreachable";
2650}
2651
2652void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2653 // Nothing to do, this should be removed during prepare for register allocator.
2654 UNUSED(instruction);
2655 LOG(FATAL) << "Unreachable";
2656}
2657
Alexandre Rames67555f72014-11-18 10:55:16 +00002658#undef __
2659#undef QUICK_ENTRY_POINT
2660
Alexandre Rames5319def2014-10-23 10:03:10 +01002661} // namespace arm64
2662} // namespace art