blob: 46f1a9b51de288f627949c07c00b71dfcd429367 [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
Andreas Gampe878d58c2015-01-15 23:24:00 -080019#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080021#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010022#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080023#include "intrinsics.h"
24#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "mirror/array-inl.h"
26#include "mirror/art_method.h"
27#include "mirror/class.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000028#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "thread.h"
30#include "utils/arm64/assembler_arm64.h"
31#include "utils/assembler.h"
32#include "utils/stack_checks.h"
33
34
35using namespace vixl; // NOLINT(build/namespaces)
36
37#ifdef __
38#error "ARM64 Codegen VIXL macro-assembler macro already defined."
39#endif
40
Alexandre Rames5319def2014-10-23 10:03:10 +010041namespace art {
42
43namespace arm64 {
44
Andreas Gampe878d58c2015-01-15 23:24:00 -080045using helpers::CPURegisterFrom;
46using helpers::DRegisterFrom;
47using helpers::FPRegisterFrom;
48using helpers::HeapOperand;
49using helpers::HeapOperandFrom;
50using helpers::InputCPURegisterAt;
51using helpers::InputFPRegisterAt;
52using helpers::InputRegisterAt;
53using helpers::InputOperandAt;
54using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080055using helpers::LocationFrom;
56using helpers::OperandFromMemOperand;
57using helpers::OutputCPURegister;
58using helpers::OutputFPRegister;
59using helpers::OutputRegister;
60using helpers::RegisterFrom;
61using helpers::StackOperandFrom;
62using helpers::VIXLRegCodeFromART;
63using helpers::WRegisterFrom;
64using helpers::XRegisterFrom;
65
Alexandre Rames5319def2014-10-23 10:03:10 +010066static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>);
67static constexpr int kCurrentMethodStackOffset = 0;
68
Alexandre Rames5319def2014-10-23 10:03:10 +010069inline Condition ARM64Condition(IfCondition cond) {
70 switch (cond) {
71 case kCondEQ: return eq;
72 case kCondNE: return ne;
73 case kCondLT: return lt;
74 case kCondLE: return le;
75 case kCondGT: return gt;
76 case kCondGE: return ge;
77 default:
78 LOG(FATAL) << "Unknown if condition";
79 }
80 return nv; // Unreachable.
81}
82
Alexandre Ramesa89086e2014-11-07 17:13:25 +000083Location ARM64ReturnLocation(Primitive::Type return_type) {
84 DCHECK_NE(return_type, Primitive::kPrimVoid);
85 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
86 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
87 // but we use the exact registers for clarity.
88 if (return_type == Primitive::kPrimFloat) {
89 return LocationFrom(s0);
90 } else if (return_type == Primitive::kPrimDouble) {
91 return LocationFrom(d0);
92 } else if (return_type == Primitive::kPrimLong) {
93 return LocationFrom(x0);
94 } else {
95 return LocationFrom(w0);
96 }
97}
98
Alexandre Rames5319def2014-10-23 10:03:10 +010099static const Register kRuntimeParameterCoreRegisters[] = { x0, x1, x2, x3, x4, x5, x6, x7 };
100static constexpr size_t kRuntimeParameterCoreRegistersLength =
101 arraysize(kRuntimeParameterCoreRegisters);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000102static const FPRegister kRuntimeParameterFpuRegisters[] = { d0, d1, d2, d3, d4, d5, d6, d7 };
103static constexpr size_t kRuntimeParameterFpuRegistersLength =
104 arraysize(kRuntimeParameterCoreRegisters);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105
106class InvokeRuntimeCallingConvention : public CallingConvention<Register, FPRegister> {
107 public:
108 static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
109
110 InvokeRuntimeCallingConvention()
111 : CallingConvention(kRuntimeParameterCoreRegisters,
112 kRuntimeParameterCoreRegistersLength,
113 kRuntimeParameterFpuRegisters,
114 kRuntimeParameterFpuRegistersLength) {}
115
116 Location GetReturnLocation(Primitive::Type return_type);
117
118 private:
119 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
120};
121
122Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000123 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100124}
125
Alexandre Rames67555f72014-11-18 10:55:16 +0000126#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
127#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100128
Alexandre Rames5319def2014-10-23 10:03:10 +0100129class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
130 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000131 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
132 Location index_location,
133 Location length_location)
134 : instruction_(instruction),
135 index_location_(index_location),
136 length_location_(length_location) {}
137
Alexandre Rames5319def2014-10-23 10:03:10 +0100138
Alexandre Rames67555f72014-11-18 10:55:16 +0000139 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000140 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100141 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000142 // We're moving two locations to locations that could overlap, so we need a parallel
143 // move resolver.
144 InvokeRuntimeCallingConvention calling_convention;
145 codegen->EmitParallelMoves(
146 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)),
147 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)));
148 arm64_codegen->InvokeRuntime(
149 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800150 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100151 }
152
153 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000154 HBoundsCheck* const instruction_;
155 const Location index_location_;
156 const Location length_location_;
157
Alexandre Rames5319def2014-10-23 10:03:10 +0100158 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
159};
160
Alexandre Rames67555f72014-11-18 10:55:16 +0000161class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
162 public:
163 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
164
165 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
166 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
167 __ Bind(GetEntryLabel());
168 arm64_codegen->InvokeRuntime(
169 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800170 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000171 }
172
173 private:
174 HDivZeroCheck* const instruction_;
175 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
176};
177
178class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
179 public:
180 LoadClassSlowPathARM64(HLoadClass* cls,
181 HInstruction* at,
182 uint32_t dex_pc,
183 bool do_clinit)
184 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
185 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
186 }
187
188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
189 LocationSummary* locations = at_->GetLocations();
190 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
191
192 __ Bind(GetEntryLabel());
193 codegen->SaveLiveRegisters(locations);
194
195 InvokeRuntimeCallingConvention calling_convention;
196 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
197 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
198 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
199 : QUICK_ENTRY_POINT(pInitializeType);
200 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800201 if (do_clinit_) {
202 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t, mirror::ArtMethod*>();
203 } else {
204 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t, mirror::ArtMethod*>();
205 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000206
207 // Move the class to the desired location.
208 Location out = locations->Out();
209 if (out.IsValid()) {
210 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
211 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000212 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000213 }
214
215 codegen->RestoreLiveRegisters(locations);
216 __ B(GetExitLabel());
217 }
218
219 private:
220 // The class this slow path will load.
221 HLoadClass* const cls_;
222
223 // The instruction where this slow path is happening.
224 // (Might be the load class or an initialization check).
225 HInstruction* const at_;
226
227 // The dex PC of `at_`.
228 const uint32_t dex_pc_;
229
230 // Whether to initialize the class.
231 const bool do_clinit_;
232
233 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
234};
235
236class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
237 public:
238 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
239
240 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
241 LocationSummary* locations = instruction_->GetLocations();
242 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
243 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
244
245 __ Bind(GetEntryLabel());
246 codegen->SaveLiveRegisters(locations);
247
248 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800249 arm64_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1).W());
250 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 arm64_codegen->InvokeRuntime(
252 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000254 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000255 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000256
257 codegen->RestoreLiveRegisters(locations);
258 __ B(GetExitLabel());
259 }
260
261 private:
262 HLoadString* const instruction_;
263
264 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
265};
266
Alexandre Rames5319def2014-10-23 10:03:10 +0100267class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
268 public:
269 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
270
Alexandre Rames67555f72014-11-18 10:55:16 +0000271 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
272 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100273 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 arm64_codegen->InvokeRuntime(
275 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100277 }
278
279 private:
280 HNullCheck* const instruction_;
281
282 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
283};
284
285class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
286 public:
287 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
288 HBasicBlock* successor)
289 : instruction_(instruction), successor_(successor) {}
290
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100293 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000294 codegen->SaveLiveRegisters(instruction_->GetLocations());
295 arm64_codegen->InvokeRuntime(
296 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 codegen->RestoreLiveRegisters(instruction_->GetLocations());
299 if (successor_ == nullptr) {
300 __ B(GetReturnLabel());
301 } else {
302 __ B(arm64_codegen->GetLabelOf(successor_));
303 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100304 }
305
306 vixl::Label* GetReturnLabel() {
307 DCHECK(successor_ == nullptr);
308 return &return_label_;
309 }
310
Alexandre Rames5319def2014-10-23 10:03:10 +0100311 private:
312 HSuspendCheck* const instruction_;
313 // If not null, the block to branch to after the suspend check.
314 HBasicBlock* const successor_;
315
316 // If `successor_` is null, the label to branch to after the suspend check.
317 vixl::Label return_label_;
318
319 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
320};
321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
323 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000324 TypeCheckSlowPathARM64(HInstruction* instruction,
325 Location class_to_check,
326 Location object_class,
327 uint32_t dex_pc)
328 : instruction_(instruction),
329 class_to_check_(class_to_check),
330 object_class_(object_class),
331 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000332
333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000334 LocationSummary* locations = instruction_->GetLocations();
335 DCHECK(instruction_->IsCheckCast()
336 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
337 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000340 codegen->SaveLiveRegisters(locations);
341
342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
344 InvokeRuntimeCallingConvention calling_convention;
345 codegen->EmitParallelMoves(
346 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)),
347 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)));
348
349 if (instruction_->IsInstanceOf()) {
350 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
351 Primitive::Type ret_type = instruction_->GetType();
352 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
353 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800354 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
355 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000356 } else {
357 DCHECK(instruction_->IsCheckCast());
358 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800359 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000360 }
361
362 codegen->RestoreLiveRegisters(locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000363 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 }
365
366 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000367 HInstruction* const instruction_;
368 const Location class_to_check_;
369 const Location object_class_;
370 uint32_t dex_pc_;
371
Alexandre Rames67555f72014-11-18 10:55:16 +0000372 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
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
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000400CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph, const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100401 : CodeGenerator(graph,
402 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000403 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000404 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000405 callee_saved_core_registers.list(),
406 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000407 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 block_labels_(nullptr),
409 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000410 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000411 move_resolver_(graph->GetArena(), this) {
412 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000413 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000414}
Alexandre Rames5319def2014-10-23 10:03:10 +0100415
Alexandre Rames67555f72014-11-18 10:55:16 +0000416#undef __
417#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100418
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000419void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
420 // Ensure we emit the literal pool.
421 __ FinalizeCode();
422 CodeGenerator::Finalize(allocator);
423}
424
Alexandre Rames3e69f162014-12-10 10:36:50 +0000425void ParallelMoveResolverARM64::EmitMove(size_t index) {
426 MoveOperands* move = moves_.Get(index);
427 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
428}
429
430void ParallelMoveResolverARM64::EmitSwap(size_t index) {
431 MoveOperands* move = moves_.Get(index);
432 codegen_->SwapLocations(move->GetDestination(), move->GetSource());
433}
434
435void ParallelMoveResolverARM64::RestoreScratch(int reg) {
436 __ Pop(Register(VIXLRegCodeFromART(reg), kXRegSize));
437}
438
439void ParallelMoveResolverARM64::SpillScratch(int reg) {
440 __ Push(Register(VIXLRegCodeFromART(reg), kXRegSize));
441}
442
Alexandre Rames5319def2014-10-23 10:03:10 +0100443void CodeGeneratorARM64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000444 __ Bind(&frame_entry_label_);
445
Serban Constantinescu02164b32014-11-13 14:05:07 +0000446 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
447 if (do_overflow_check) {
448 UseScratchRegisterScope temps(GetVIXLAssembler());
449 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000450 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000451 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000452 __ Ldr(wzr, MemOperand(temp, 0));
453 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000454 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100455
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000456 if (!HasEmptyFrame()) {
457 int frame_size = GetFrameSize();
458 // Stack layout:
459 // sp[frame_size - 8] : lr.
460 // ... : other preserved core registers.
461 // ... : other preserved fp registers.
462 // ... : reserved frame space.
463 // sp[0] : current method.
464 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
465 __ PokeCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
466 __ PokeCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
467 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100468}
469
470void CodeGeneratorARM64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000471 if (!HasEmptyFrame()) {
472 int frame_size = GetFrameSize();
473 __ PeekCPURegList(GetFramePreservedFPRegisters(), frame_size - FrameEntrySpillSize());
474 __ PeekCPURegList(GetFramePreservedCoreRegisters(), frame_size - GetCoreSpillSize());
475 __ Drop(frame_size);
476 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100477}
478
479void CodeGeneratorARM64::Bind(HBasicBlock* block) {
480 __ Bind(GetLabelOf(block));
481}
482
Alexandre Rames5319def2014-10-23 10:03:10 +0100483void CodeGeneratorARM64::Move(HInstruction* instruction,
484 Location location,
485 HInstruction* move_for) {
486 LocationSummary* locations = instruction->GetLocations();
487 if (locations != nullptr && locations->Out().Equals(location)) {
488 return;
489 }
490
491 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000492 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100493
494 if (instruction->IsIntConstant() || instruction->IsLongConstant()) {
495 int64_t value = instruction->IsIntConstant() ? instruction->AsIntConstant()->GetValue()
496 : instruction->AsLongConstant()->GetValue();
497 if (location.IsRegister()) {
498 Register dst = RegisterFrom(location, type);
499 DCHECK((instruction->IsIntConstant() && dst.Is32Bits()) ||
500 (instruction->IsLongConstant() && dst.Is64Bits()));
501 __ Mov(dst, value);
502 } else {
503 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100505 Register temp = instruction->IsIntConstant() ? temps.AcquireW() : temps.AcquireX();
506 __ Mov(temp, value);
507 __ Str(temp, StackOperandFrom(location));
508 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000509 } else if (instruction->IsTemporary()) {
510 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100512 } else if (instruction->IsLoadLocal()) {
513 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000514 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000515 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000516 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000517 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100518 }
519
520 } else {
521 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000522 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100523 }
524}
525
Alexandre Rames5319def2014-10-23 10:03:10 +0100526Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
527 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000528
Alexandre Rames5319def2014-10-23 10:03:10 +0100529 switch (type) {
530 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000531 case Primitive::kPrimInt:
532 case Primitive::kPrimFloat:
533 return Location::StackSlot(GetStackSlot(load->GetLocal()));
534
535 case Primitive::kPrimLong:
536 case Primitive::kPrimDouble:
537 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
538
Alexandre Rames5319def2014-10-23 10:03:10 +0100539 case Primitive::kPrimBoolean:
540 case Primitive::kPrimByte:
541 case Primitive::kPrimChar:
542 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100543 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100544 LOG(FATAL) << "Unexpected type " << type;
545 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000546
Alexandre Rames5319def2014-10-23 10:03:10 +0100547 LOG(FATAL) << "Unreachable";
548 return Location::NoLocation();
549}
550
551void CodeGeneratorARM64::MarkGCCard(Register object, Register value) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000552 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100553 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000554 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100555 vixl::Label done;
556 __ Cbz(value, &done);
557 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
558 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000559 __ Strb(card, MemOperand(card, temp.X()));
Alexandre Rames5319def2014-10-23 10:03:10 +0100560 __ Bind(&done);
561}
562
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000563void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
564 // Blocked core registers:
565 // lr : Runtime reserved.
566 // tr : Runtime reserved.
567 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
568 // ip1 : VIXL core temp.
569 // ip0 : VIXL core temp.
570 //
571 // Blocked fp registers:
572 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100573 CPURegList reserved_core_registers = vixl_reserved_core_registers;
574 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100575 while (!reserved_core_registers.IsEmpty()) {
576 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
577 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000578
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000579 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000580 while (!reserved_core_registers.IsEmpty()) {
581 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
582 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000583
584 if (is_baseline) {
585 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
586 while (!reserved_core_baseline_registers.IsEmpty()) {
587 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
588 }
589
590 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
591 while (!reserved_fp_baseline_registers.IsEmpty()) {
592 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
593 }
594 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100595}
596
597Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
598 if (type == Primitive::kPrimVoid) {
599 LOG(FATAL) << "Unreachable type " << type;
600 }
601
Alexandre Rames542361f2015-01-29 16:57:31 +0000602 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000603 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
604 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100605 return Location::FpuRegisterLocation(reg);
606 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000607 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
608 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100609 return Location::RegisterLocation(reg);
610 }
611}
612
Alexandre Rames3e69f162014-12-10 10:36:50 +0000613size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
614 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
615 __ Str(reg, MemOperand(sp, stack_index));
616 return kArm64WordSize;
617}
618
619size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
620 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
621 __ Ldr(reg, MemOperand(sp, stack_index));
622 return kArm64WordSize;
623}
624
625size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
626 FPRegister reg = FPRegister(reg_id, kDRegSize);
627 __ Str(reg, MemOperand(sp, stack_index));
628 return kArm64WordSize;
629}
630
631size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
632 FPRegister reg = FPRegister(reg_id, kDRegSize);
633 __ Ldr(reg, MemOperand(sp, stack_index));
634 return kArm64WordSize;
635}
636
Alexandre Rames5319def2014-10-23 10:03:10 +0100637void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
638 stream << Arm64ManagedRegister::FromXRegister(XRegister(reg));
639}
640
641void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
642 stream << Arm64ManagedRegister::FromDRegister(DRegister(reg));
643}
644
Alexandre Rames67555f72014-11-18 10:55:16 +0000645void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
646 if (constant->IsIntConstant() || constant->IsLongConstant()) {
647 __ Mov(Register(destination),
648 constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
649 : constant->AsLongConstant()->GetValue());
650 } else if (constant->IsFloatConstant()) {
651 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
652 } else {
653 DCHECK(constant->IsDoubleConstant());
654 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
655 }
656}
657
Alexandre Rames3e69f162014-12-10 10:36:50 +0000658
659static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
660 DCHECK(constant.IsConstant());
661 HConstant* cst = constant.GetConstant();
662 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
663 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
664 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
665 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
666}
667
668void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000669 if (source.Equals(destination)) {
670 return;
671 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000672
673 // A valid move can always be inferred from the destination and source
674 // locations. When moving from and to a register, the argument type can be
675 // used to generate 32bit instead of 64bit moves. In debug mode we also
676 // checks the coherency of the locations and the type.
677 bool unspecified_type = (type == Primitive::kPrimVoid);
678
679 if (destination.IsRegister() || destination.IsFpuRegister()) {
680 if (unspecified_type) {
681 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
682 if (source.IsStackSlot() ||
683 (src_cst != nullptr && (src_cst->IsIntConstant() || src_cst->IsFloatConstant()))) {
684 // For stack slots and 32bit constants, a 64bit type is appropriate.
685 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000686 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000687 // If the source is a double stack slot or a 64bit constant, a 64bit
688 // type is appropriate. Else the source is a register, and since the
689 // type has not been specified, we chose a 64bit type to force a 64bit
690 // move.
691 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000692 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000693 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000694 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
695 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000696 CPURegister dst = CPURegisterFrom(destination, type);
697 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
698 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
699 __ Ldr(dst, StackOperandFrom(source));
700 } else if (source.IsConstant()) {
701 DCHECK(CoherentConstantAndType(source, type));
702 MoveConstant(dst, source.GetConstant());
703 } else {
704 if (destination.IsRegister()) {
705 __ Mov(Register(dst), RegisterFrom(source, type));
706 } else {
707 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
708 }
709 }
710
711 } else { // The destination is not a register. It must be a stack slot.
712 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
713 if (source.IsRegister() || source.IsFpuRegister()) {
714 if (unspecified_type) {
715 if (source.IsRegister()) {
716 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
717 } else {
718 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
719 }
720 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000721 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
722 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000723 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
724 } else if (source.IsConstant()) {
725 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
726 UseScratchRegisterScope temps(GetVIXLAssembler());
727 HConstant* src_cst = source.GetConstant();
728 CPURegister temp;
729 if (src_cst->IsIntConstant()) {
730 temp = temps.AcquireW();
731 } else if (src_cst->IsLongConstant()) {
732 temp = temps.AcquireX();
733 } else if (src_cst->IsFloatConstant()) {
734 temp = temps.AcquireS();
735 } else {
736 DCHECK(src_cst->IsDoubleConstant());
737 temp = temps.AcquireD();
738 }
739 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000740 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000741 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000742 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000743 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000744 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000745 // There is generally less pressure on FP registers.
746 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000747 __ Ldr(temp, StackOperandFrom(source));
748 __ Str(temp, StackOperandFrom(destination));
749 }
750 }
751}
752
Alexandre Rames3e69f162014-12-10 10:36:50 +0000753void CodeGeneratorARM64::SwapLocations(Location loc1, Location loc2) {
754 DCHECK(!loc1.IsConstant());
755 DCHECK(!loc2.IsConstant());
756
757 if (loc1.Equals(loc2)) {
758 return;
759 }
760
761 UseScratchRegisterScope temps(GetAssembler()->vixl_masm_);
762
763 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
764 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
765 bool is_fp_reg1 = loc1.IsFpuRegister();
766 bool is_fp_reg2 = loc2.IsFpuRegister();
767
768 if (loc2.IsRegister() && loc1.IsRegister()) {
769 Register r1 = XRegisterFrom(loc1);
770 Register r2 = XRegisterFrom(loc2);
771 Register tmp = temps.AcquireSameSizeAs(r1);
772 __ Mov(tmp, r2);
773 __ Mov(r2, r1);
774 __ Mov(r1, tmp);
775 } else if (is_fp_reg2 && is_fp_reg1) {
776 FPRegister r1 = DRegisterFrom(loc1);
777 FPRegister r2 = DRegisterFrom(loc2);
778 FPRegister tmp = temps.AcquireSameSizeAs(r1);
779 __ Fmov(tmp, r2);
780 __ Fmov(r2, r1);
781 __ Fmov(r1, tmp);
782 } else if (is_slot1 != is_slot2) {
783 MemOperand mem = StackOperandFrom(is_slot1 ? loc1 : loc2);
784 Location reg_loc = is_slot1 ? loc2 : loc1;
785 CPURegister reg, tmp;
786 if (reg_loc.IsFpuRegister()) {
787 reg = DRegisterFrom(reg_loc);
788 tmp = temps.AcquireD();
789 } else {
790 reg = XRegisterFrom(reg_loc);
791 tmp = temps.AcquireX();
792 }
793 __ Ldr(tmp, mem);
794 __ Str(reg, mem);
795 if (reg_loc.IsFpuRegister()) {
796 __ Fmov(FPRegister(reg), FPRegister(tmp));
797 } else {
798 __ Mov(Register(reg), Register(tmp));
799 }
800 } else if (is_slot1 && is_slot2) {
801 MemOperand mem1 = StackOperandFrom(loc1);
802 MemOperand mem2 = StackOperandFrom(loc2);
803 Register tmp1 = loc1.IsStackSlot() ? temps.AcquireW() : temps.AcquireX();
804 Register tmp2 = temps.AcquireSameSizeAs(tmp1);
805 __ Ldr(tmp1, mem1);
806 __ Ldr(tmp2, mem2);
807 __ Str(tmp1, mem2);
808 __ Str(tmp2, mem1);
809 } else {
810 LOG(FATAL) << "Unimplemented";
811 }
812}
813
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000814void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000815 CPURegister dst,
816 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000817 switch (type) {
818 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000819 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000820 break;
821 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000822 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000823 break;
824 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000825 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000826 break;
827 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000828 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000829 break;
830 case Primitive::kPrimInt:
831 case Primitive::kPrimNot:
832 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000833 case Primitive::kPrimFloat:
834 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000835 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000836 __ Ldr(dst, src);
837 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000838 case Primitive::kPrimVoid:
839 LOG(FATAL) << "Unreachable type " << type;
840 }
841}
842
Calin Juravle77520bc2015-01-12 18:45:46 +0000843void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000844 CPURegister dst,
845 const MemOperand& src) {
846 UseScratchRegisterScope temps(GetVIXLAssembler());
847 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000848 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000849
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000850 DCHECK(!src.IsPreIndex());
851 DCHECK(!src.IsPostIndex());
852
853 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800854 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000855 MemOperand base = MemOperand(temp_base);
856 switch (type) {
857 case Primitive::kPrimBoolean:
858 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000859 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000860 break;
861 case Primitive::kPrimByte:
862 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000863 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000864 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
865 break;
866 case Primitive::kPrimChar:
867 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000868 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000869 break;
870 case Primitive::kPrimShort:
871 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000872 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000873 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
874 break;
875 case Primitive::kPrimInt:
876 case Primitive::kPrimNot:
877 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000878 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000879 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000880 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000881 break;
882 case Primitive::kPrimFloat:
883 case Primitive::kPrimDouble: {
884 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000885 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000886
887 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
888 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000889 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000890 __ Fmov(FPRegister(dst), temp);
891 break;
892 }
893 case Primitive::kPrimVoid:
894 LOG(FATAL) << "Unreachable type " << type;
895 }
896}
897
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000898void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000899 CPURegister src,
900 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000901 switch (type) {
902 case Primitive::kPrimBoolean:
903 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000904 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000905 break;
906 case Primitive::kPrimChar:
907 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000908 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000909 break;
910 case Primitive::kPrimInt:
911 case Primitive::kPrimNot:
912 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000913 case Primitive::kPrimFloat:
914 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000915 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000916 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000917 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000918 case Primitive::kPrimVoid:
919 LOG(FATAL) << "Unreachable type " << type;
920 }
921}
922
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000923void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
924 CPURegister src,
925 const MemOperand& dst) {
926 UseScratchRegisterScope temps(GetVIXLAssembler());
927 Register temp_base = temps.AcquireX();
928
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000929 DCHECK(!dst.IsPreIndex());
930 DCHECK(!dst.IsPostIndex());
931
932 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800933 Operand op = OperandFromMemOperand(dst);
934 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000935 MemOperand base = MemOperand(temp_base);
936 switch (type) {
937 case Primitive::kPrimBoolean:
938 case Primitive::kPrimByte:
939 __ Stlrb(Register(src), base);
940 break;
941 case Primitive::kPrimChar:
942 case Primitive::kPrimShort:
943 __ Stlrh(Register(src), base);
944 break;
945 case Primitive::kPrimInt:
946 case Primitive::kPrimNot:
947 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000948 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000949 __ Stlr(Register(src), base);
950 break;
951 case Primitive::kPrimFloat:
952 case Primitive::kPrimDouble: {
953 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000954 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000955
956 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
957 __ Fmov(temp, FPRegister(src));
958 __ Stlr(temp, base);
959 break;
960 }
961 case Primitive::kPrimVoid:
962 LOG(FATAL) << "Unreachable type " << type;
963 }
964}
965
Alexandre Rames67555f72014-11-18 10:55:16 +0000966void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000967 DCHECK(RequiresCurrentMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +0000968 DCHECK(current_method.IsW());
969 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
970}
971
972void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
973 HInstruction* instruction,
974 uint32_t dex_pc) {
975 __ Ldr(lr, MemOperand(tr, entry_point_offset));
976 __ Blr(lr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000977 if (instruction != nullptr) {
978 RecordPcInfo(instruction, dex_pc);
979 DCHECK(instruction->IsSuspendCheck()
980 || instruction->IsBoundsCheck()
981 || instruction->IsNullCheck()
982 || instruction->IsDivZeroCheck()
983 || !IsLeafMethod());
984 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000985}
986
987void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
988 vixl::Register class_reg) {
989 UseScratchRegisterScope temps(GetVIXLAssembler());
990 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000991 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
992
Serban Constantinescu02164b32014-11-13 14:05:07 +0000993 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994 if (kUseAcquireRelease) {
995 // TODO(vixl): Let the MacroAssembler handle MemOperand.
996 __ Add(temp, class_reg, status_offset);
997 __ Ldar(temp, HeapOperand(temp));
998 __ Cmp(temp, mirror::Class::kStatusInitialized);
999 __ B(lt, slow_path->GetEntryLabel());
1000 } else {
1001 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1002 __ Cmp(temp, mirror::Class::kStatusInitialized);
1003 __ B(lt, slow_path->GetEntryLabel());
1004 __ Dmb(InnerShareable, BarrierReads);
1005 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001006 __ Bind(slow_path->GetExitLabel());
1007}
Alexandre Rames5319def2014-10-23 10:03:10 +01001008
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1010 BarrierType type = BarrierAll;
1011
1012 switch (kind) {
1013 case MemBarrierKind::kAnyAny:
1014 case MemBarrierKind::kAnyStore: {
1015 type = BarrierAll;
1016 break;
1017 }
1018 case MemBarrierKind::kLoadAny: {
1019 type = BarrierReads;
1020 break;
1021 }
1022 case MemBarrierKind::kStoreStore: {
1023 type = BarrierWrites;
1024 break;
1025 }
1026 default:
1027 LOG(FATAL) << "Unexpected memory barrier " << kind;
1028 }
1029 __ Dmb(InnerShareable, type);
1030}
1031
Serban Constantinescu02164b32014-11-13 14:05:07 +00001032void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1033 HBasicBlock* successor) {
1034 SuspendCheckSlowPathARM64* slow_path =
1035 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1036 codegen_->AddSlowPath(slow_path);
1037 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1038 Register temp = temps.AcquireW();
1039
1040 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1041 if (successor == nullptr) {
1042 __ Cbnz(temp, slow_path->GetEntryLabel());
1043 __ Bind(slow_path->GetReturnLabel());
1044 } else {
1045 __ Cbz(temp, codegen_->GetLabelOf(successor));
1046 __ B(slow_path->GetEntryLabel());
1047 // slow_path will return to GetLabelOf(successor).
1048 }
1049}
1050
Alexandre Rames5319def2014-10-23 10:03:10 +01001051InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1052 CodeGeneratorARM64* codegen)
1053 : HGraphVisitor(graph),
1054 assembler_(codegen->GetAssembler()),
1055 codegen_(codegen) {}
1056
1057#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001058 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001059
1060#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1061
1062enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001063 // Using a base helps identify when we hit such breakpoints.
1064 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001065#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1066 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1067#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1068};
1069
1070#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1071 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001072 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001073 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1074 } \
1075 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1076 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1077 locations->SetOut(Location::Any()); \
1078 }
1079 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1080#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1081
1082#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001083#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001084
Alexandre Rames67555f72014-11-18 10:55:16 +00001085void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001086 DCHECK_EQ(instr->InputCount(), 2U);
1087 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1088 Primitive::Type type = instr->GetResultType();
1089 switch (type) {
1090 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001091 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001092 locations->SetInAt(0, Location::RequiresRegister());
1093 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001094 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001096
1097 case Primitive::kPrimFloat:
1098 case Primitive::kPrimDouble:
1099 locations->SetInAt(0, Location::RequiresFpuRegister());
1100 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001103
Alexandre Rames5319def2014-10-23 10:03:10 +01001104 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001105 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001106 }
1107}
1108
Alexandre Rames67555f72014-11-18 10:55:16 +00001109void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001110 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001111
1112 switch (type) {
1113 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001114 case Primitive::kPrimLong: {
1115 Register dst = OutputRegister(instr);
1116 Register lhs = InputRegisterAt(instr, 0);
1117 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 if (instr->IsAdd()) {
1119 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001120 } else if (instr->IsAnd()) {
1121 __ And(dst, lhs, rhs);
1122 } else if (instr->IsOr()) {
1123 __ Orr(dst, lhs, rhs);
1124 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001125 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001126 } else {
1127 DCHECK(instr->IsXor());
1128 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001129 }
1130 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001131 }
1132 case Primitive::kPrimFloat:
1133 case Primitive::kPrimDouble: {
1134 FPRegister dst = OutputFPRegister(instr);
1135 FPRegister lhs = InputFPRegisterAt(instr, 0);
1136 FPRegister rhs = InputFPRegisterAt(instr, 1);
1137 if (instr->IsAdd()) {
1138 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001139 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001140 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001141 } else {
1142 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001143 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001144 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001145 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001146 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001147 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001148 }
1149}
1150
Serban Constantinescu02164b32014-11-13 14:05:07 +00001151void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1152 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1153
1154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1155 Primitive::Type type = instr->GetResultType();
1156 switch (type) {
1157 case Primitive::kPrimInt:
1158 case Primitive::kPrimLong: {
1159 locations->SetInAt(0, Location::RequiresRegister());
1160 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1161 locations->SetOut(Location::RequiresRegister());
1162 break;
1163 }
1164 default:
1165 LOG(FATAL) << "Unexpected shift type " << type;
1166 }
1167}
1168
1169void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1170 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1171
1172 Primitive::Type type = instr->GetType();
1173 switch (type) {
1174 case Primitive::kPrimInt:
1175 case Primitive::kPrimLong: {
1176 Register dst = OutputRegister(instr);
1177 Register lhs = InputRegisterAt(instr, 0);
1178 Operand rhs = InputOperandAt(instr, 1);
1179 if (rhs.IsImmediate()) {
1180 uint32_t shift_value = (type == Primitive::kPrimInt)
1181 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1182 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1183 if (instr->IsShl()) {
1184 __ Lsl(dst, lhs, shift_value);
1185 } else if (instr->IsShr()) {
1186 __ Asr(dst, lhs, shift_value);
1187 } else {
1188 __ Lsr(dst, lhs, shift_value);
1189 }
1190 } else {
1191 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1192
1193 if (instr->IsShl()) {
1194 __ Lsl(dst, lhs, rhs_reg);
1195 } else if (instr->IsShr()) {
1196 __ Asr(dst, lhs, rhs_reg);
1197 } else {
1198 __ Lsr(dst, lhs, rhs_reg);
1199 }
1200 }
1201 break;
1202 }
1203 default:
1204 LOG(FATAL) << "Unexpected shift operation type " << type;
1205 }
1206}
1207
Alexandre Rames5319def2014-10-23 10:03:10 +01001208void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001209 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001210}
1211
1212void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001213 HandleBinaryOp(instruction);
1214}
1215
1216void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1217 HandleBinaryOp(instruction);
1218}
1219
1220void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1221 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001222}
1223
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001224void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1225 LocationSummary* locations =
1226 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1227 locations->SetInAt(0, Location::RequiresRegister());
1228 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1229 locations->SetOut(Location::RequiresRegister());
1230}
1231
1232void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1233 LocationSummary* locations = instruction->GetLocations();
1234 Primitive::Type type = instruction->GetType();
1235 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001236 Location index = locations->InAt(1);
1237 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001238 MemOperand source = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001239 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001240
1241 if (index.IsConstant()) {
1242 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001243 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001244 } else {
1245 Register temp = temps.AcquireSameSizeAs(obj);
1246 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1247 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001248 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001249 }
1250
Alexandre Rames67555f72014-11-18 10:55:16 +00001251 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001252 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001253}
1254
Alexandre Rames5319def2014-10-23 10:03:10 +01001255void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1257 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001259}
1260
1261void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
1262 __ Ldr(OutputRegister(instruction),
1263 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001264 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001265}
1266
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001267void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
1268 Primitive::Type value_type = instruction->GetComponentType();
1269 bool is_object = value_type == Primitive::kPrimNot;
1270 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1271 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1272 if (is_object) {
1273 InvokeRuntimeCallingConvention calling_convention;
1274 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1275 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1276 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1277 } else {
1278 locations->SetInAt(0, Location::RequiresRegister());
1279 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1280 locations->SetInAt(2, Location::RequiresRegister());
1281 }
1282}
1283
1284void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1285 Primitive::Type value_type = instruction->GetComponentType();
1286 if (value_type == Primitive::kPrimNot) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001287 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001288 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001289 } else {
1290 LocationSummary* locations = instruction->GetLocations();
1291 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001292 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001293 Location index = locations->InAt(1);
1294 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001295 MemOperand destination = HeapOperand(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00001296 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001297
1298 if (index.IsConstant()) {
1299 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001300 destination = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001301 } else {
1302 Register temp = temps.AcquireSameSizeAs(obj);
1303 Register index_reg = InputRegisterAt(instruction, 1);
1304 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001305 destination = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001306 }
1307
1308 codegen_->Store(value_type, value, destination);
Calin Juravle77520bc2015-01-12 18:45:46 +00001309 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001310 }
1311}
1312
Alexandre Rames67555f72014-11-18 10:55:16 +00001313void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1314 LocationSummary* locations =
1315 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1316 locations->SetInAt(0, Location::RequiresRegister());
1317 locations->SetInAt(1, Location::RequiresRegister());
1318 if (instruction->HasUses()) {
1319 locations->SetOut(Location::SameAsFirstInput());
1320 }
1321}
1322
1323void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001324 LocationSummary* locations = instruction->GetLocations();
1325 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1326 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001327 codegen_->AddSlowPath(slow_path);
1328
1329 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1330 __ B(slow_path->GetEntryLabel(), hs);
1331}
1332
1333void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1335 instruction, LocationSummary::kCallOnSlowPath);
1336 locations->SetInAt(0, Location::RequiresRegister());
1337 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001338 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001339}
1340
1341void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001342 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001343 Register obj = InputRegisterAt(instruction, 0);;
1344 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001345 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001346
Alexandre Rames3e69f162014-12-10 10:36:50 +00001347 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1348 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001349 codegen_->AddSlowPath(slow_path);
1350
1351 // TODO: avoid this check if we know obj is not null.
1352 __ Cbz(obj, slow_path->GetExitLabel());
1353 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001354 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1355 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001356 __ B(ne, slow_path->GetEntryLabel());
1357 __ Bind(slow_path->GetExitLabel());
1358}
1359
1360void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1361 LocationSummary* locations =
1362 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1363 locations->SetInAt(0, Location::RequiresRegister());
1364 if (check->HasUses()) {
1365 locations->SetOut(Location::SameAsFirstInput());
1366 }
1367}
1368
1369void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1370 // We assume the class is not null.
1371 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1372 check->GetLoadClass(), check, check->GetDexPc(), true);
1373 codegen_->AddSlowPath(slow_path);
1374 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1375}
1376
Serban Constantinescu02164b32014-11-13 14:05:07 +00001377void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001378 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001379 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1380 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 switch (in_type) {
1382 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001383 locations->SetInAt(0, Location::RequiresRegister());
1384 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
1385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1386 break;
1387 }
1388 case Primitive::kPrimFloat:
1389 case Primitive::kPrimDouble: {
1390 locations->SetInAt(0, Location::RequiresFpuRegister());
1391 locations->SetInAt(1, Location::RequiresFpuRegister());
1392 locations->SetOut(Location::RequiresRegister());
1393 break;
1394 }
1395 default:
1396 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1397 }
1398}
1399
1400void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1401 Primitive::Type in_type = compare->InputAt(0)->GetType();
1402
1403 // 0 if: left == right
1404 // 1 if: left > right
1405 // -1 if: left < right
1406 switch (in_type) {
1407 case Primitive::kPrimLong: {
1408 Register result = OutputRegister(compare);
1409 Register left = InputRegisterAt(compare, 0);
1410 Operand right = InputOperandAt(compare, 1);
1411
1412 __ Cmp(left, right);
1413 __ Cset(result, ne);
1414 __ Cneg(result, result, lt);
1415 break;
1416 }
1417 case Primitive::kPrimFloat:
1418 case Primitive::kPrimDouble: {
1419 Register result = OutputRegister(compare);
1420 FPRegister left = InputFPRegisterAt(compare, 0);
1421 FPRegister right = InputFPRegisterAt(compare, 1);
1422
1423 __ Fcmp(left, right);
1424 if (compare->IsGtBias()) {
1425 __ Cset(result, ne);
1426 } else {
1427 __ Csetm(result, ne);
1428 }
1429 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001430 break;
1431 }
1432 default:
1433 LOG(FATAL) << "Unimplemented compare type " << in_type;
1434 }
1435}
1436
1437void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1438 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1439 locations->SetInAt(0, Location::RequiresRegister());
1440 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1441 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001442 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001443 }
1444}
1445
1446void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1447 if (!instruction->NeedsMaterialization()) {
1448 return;
1449 }
1450
1451 LocationSummary* locations = instruction->GetLocations();
1452 Register lhs = InputRegisterAt(instruction, 0);
1453 Operand rhs = InputOperandAt(instruction, 1);
1454 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1455 Condition cond = ARM64Condition(instruction->GetCondition());
1456
1457 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001458 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001459}
1460
1461#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1462 M(Equal) \
1463 M(NotEqual) \
1464 M(LessThan) \
1465 M(LessThanOrEqual) \
1466 M(GreaterThan) \
1467 M(GreaterThanOrEqual)
1468#define DEFINE_CONDITION_VISITORS(Name) \
1469void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1470void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1471FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001472#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001473#undef FOR_EACH_CONDITION_INSTRUCTION
1474
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001475void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1476 LocationSummary* locations =
1477 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1478 switch (div->GetResultType()) {
1479 case Primitive::kPrimInt:
1480 case Primitive::kPrimLong:
1481 locations->SetInAt(0, Location::RequiresRegister());
1482 locations->SetInAt(1, Location::RequiresRegister());
1483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1484 break;
1485
1486 case Primitive::kPrimFloat:
1487 case Primitive::kPrimDouble:
1488 locations->SetInAt(0, Location::RequiresFpuRegister());
1489 locations->SetInAt(1, Location::RequiresFpuRegister());
1490 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1491 break;
1492
1493 default:
1494 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1495 }
1496}
1497
1498void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1499 Primitive::Type type = div->GetResultType();
1500 switch (type) {
1501 case Primitive::kPrimInt:
1502 case Primitive::kPrimLong:
1503 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
1504 break;
1505
1506 case Primitive::kPrimFloat:
1507 case Primitive::kPrimDouble:
1508 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1509 break;
1510
1511 default:
1512 LOG(FATAL) << "Unexpected div type " << type;
1513 }
1514}
1515
Alexandre Rames67555f72014-11-18 10:55:16 +00001516void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1517 LocationSummary* locations =
1518 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1519 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1520 if (instruction->HasUses()) {
1521 locations->SetOut(Location::SameAsFirstInput());
1522 }
1523}
1524
1525void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1526 SlowPathCodeARM64* slow_path =
1527 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1528 codegen_->AddSlowPath(slow_path);
1529 Location value = instruction->GetLocations()->InAt(0);
1530
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531 Primitive::Type type = instruction->GetType();
1532
1533 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1534 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1535 return;
1536 }
1537
Alexandre Rames67555f72014-11-18 10:55:16 +00001538 if (value.IsConstant()) {
1539 int64_t divisor = Int64ConstantFrom(value);
1540 if (divisor == 0) {
1541 __ B(slow_path->GetEntryLabel());
1542 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001543 // A division by a non-null constant is valid. We don't need to perform
1544 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001545 }
1546 } else {
1547 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1548 }
1549}
1550
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001551void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1552 LocationSummary* locations =
1553 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1554 locations->SetOut(Location::ConstantLocation(constant));
1555}
1556
1557void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1558 UNUSED(constant);
1559 // Will be generated at use site.
1560}
1561
Alexandre Rames5319def2014-10-23 10:03:10 +01001562void LocationsBuilderARM64::VisitExit(HExit* exit) {
1563 exit->SetLocations(nullptr);
1564}
1565
1566void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001567 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001568 if (kIsDebugBuild) {
1569 down_cast<Arm64Assembler*>(GetAssembler())->Comment("Unreachable");
Alexandre Rames67555f72014-11-18 10:55:16 +00001570 __ Brk(__LINE__); // TODO: Introduce special markers for such code locations.
Alexandre Rames5319def2014-10-23 10:03:10 +01001571 }
1572}
1573
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001574void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1575 LocationSummary* locations =
1576 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1577 locations->SetOut(Location::ConstantLocation(constant));
1578}
1579
1580void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1581 UNUSED(constant);
1582 // Will be generated at use site.
1583}
1584
Alexandre Rames5319def2014-10-23 10:03:10 +01001585void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1586 got->SetLocations(nullptr);
1587}
1588
1589void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1590 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001591 DCHECK(!successor->IsExitBlock());
1592 HBasicBlock* block = got->GetBlock();
1593 HInstruction* previous = got->GetPrevious();
1594 HLoopInformation* info = block->GetLoopInformation();
1595
1596 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
1597 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1598 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1599 return;
1600 }
1601 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1602 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1603 }
1604 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001605 __ B(codegen_->GetLabelOf(successor));
1606 }
1607}
1608
1609void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
1610 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1611 HInstruction* cond = if_instr->InputAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001612 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001613 locations->SetInAt(0, Location::RequiresRegister());
1614 }
1615}
1616
1617void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
1618 HInstruction* cond = if_instr->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001619 HCondition* condition = cond->AsCondition();
1620 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1621 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1622
Serban Constantinescu02164b32014-11-13 14:05:07 +00001623 if (cond->IsIntConstant()) {
1624 int32_t cond_value = cond->AsIntConstant()->GetValue();
1625 if (cond_value == 1) {
1626 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) {
1627 __ B(true_target);
1628 }
1629 return;
1630 } else {
1631 DCHECK_EQ(cond_value, 0);
1632 }
1633 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001634 // The condition instruction has been materialized, compare the output to 0.
1635 Location cond_val = if_instr->GetLocations()->InAt(0);
1636 DCHECK(cond_val.IsRegister());
1637 __ Cbnz(InputRegisterAt(if_instr, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001638 } else {
1639 // The condition instruction has not been materialized, use its inputs as
1640 // the comparison and its condition as the branch condition.
1641 Register lhs = InputRegisterAt(condition, 0);
1642 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001643 Condition arm64_cond = ARM64Condition(condition->GetCondition());
1644 if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
1645 if (arm64_cond == eq) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001646 __ Cbz(lhs, true_target);
1647 } else {
1648 __ Cbnz(lhs, true_target);
1649 }
1650 } else {
1651 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001652 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01001653 }
1654 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001655 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
1656 __ B(false_target);
1657 }
1658}
1659
1660void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001661 LocationSummary* locations =
1662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001663 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001665}
1666
1667void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001668 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001669
1670 if (instruction->IsVolatile()) {
1671 if (kUseAcquireRelease) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001672 // NB: LoadAcquire will record the pc info if needed.
1673 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001674 } else {
1675 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001676 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001677 // For IRIW sequential consistency kLoadAny is not sufficient.
1678 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1679 }
1680 } else {
1681 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
Calin Juravle77520bc2015-01-12 18:45:46 +00001682 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001683 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001684}
1685
1686void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001687 LocationSummary* locations =
1688 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames5319def2014-10-23 10:03:10 +01001689 locations->SetInAt(0, Location::RequiresRegister());
1690 locations->SetInAt(1, Location::RequiresRegister());
1691}
1692
1693void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001694 Register obj = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001695 CPURegister value = InputCPURegisterAt(instruction, 1);
1696 Offset offset = instruction->GetFieldOffset();
1697 Primitive::Type field_type = instruction->GetFieldType();
1698
1699 if (instruction->IsVolatile()) {
1700 if (kUseAcquireRelease) {
1701 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001702 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001703 } else {
1704 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1705 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001706 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001707 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1708 }
1709 } else {
1710 codegen_->Store(field_type, value, HeapOperand(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001711 codegen_->MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001712 }
1713
1714 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001715 codegen_->MarkGCCard(obj, Register(value));
Alexandre Rames5319def2014-10-23 10:03:10 +01001716 }
1717}
1718
Alexandre Rames67555f72014-11-18 10:55:16 +00001719void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
1720 LocationSummary::CallKind call_kind =
1721 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
1722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1723 locations->SetInAt(0, Location::RequiresRegister());
1724 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001725 // The output does overlap inputs.
1726 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00001727}
1728
1729void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
1730 LocationSummary* locations = instruction->GetLocations();
1731 Register obj = InputRegisterAt(instruction, 0);;
1732 Register cls = InputRegisterAt(instruction, 1);;
1733 Register out = OutputRegister(instruction);
1734
1735 vixl::Label done;
1736
1737 // Return 0 if `obj` is null.
1738 // TODO: Avoid this check if we know `obj` is not null.
1739 __ Mov(out, 0);
1740 __ Cbz(obj, &done);
1741
1742 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00001743 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00001744 __ Cmp(out, cls);
1745 if (instruction->IsClassFinal()) {
1746 // Classes must be equal for the instanceof to succeed.
1747 __ Cset(out, eq);
1748 } else {
1749 // If the classes are not equal, we go into a slow path.
1750 DCHECK(locations->OnlyCallsOnSlowPath());
1751 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00001752 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1753 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001754 codegen_->AddSlowPath(slow_path);
1755 __ B(ne, slow_path->GetEntryLabel());
1756 __ Mov(out, 1);
1757 __ Bind(slow_path->GetExitLabel());
1758 }
1759
1760 __ Bind(&done);
1761}
1762
Alexandre Rames5319def2014-10-23 10:03:10 +01001763void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
1764 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
1765 locations->SetOut(Location::ConstantLocation(constant));
1766}
1767
1768void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
1769 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001770 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01001771}
1772
Alexandre Rames5319def2014-10-23 10:03:10 +01001773void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
1774 LocationSummary* locations =
1775 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
1776 locations->AddTemp(LocationFrom(x0));
1777
1778 InvokeDexCallingConventionVisitor calling_convention_visitor;
1779 for (size_t i = 0; i < invoke->InputCount(); i++) {
1780 HInstruction* input = invoke->InputAt(i);
1781 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1782 }
1783
1784 Primitive::Type return_type = invoke->GetType();
1785 if (return_type != Primitive::kPrimVoid) {
1786 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
1787 }
1788}
1789
Alexandre Rames67555f72014-11-18 10:55:16 +00001790void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1791 HandleInvoke(invoke);
1792}
1793
1794void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
1795 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1796 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1797 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1798 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1799 Location receiver = invoke->GetLocations()->InAt(0);
1800 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001801 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00001802
1803 // The register ip1 is required to be used for the hidden argument in
1804 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
1805 UseScratchRegisterScope scratch_scope(GetVIXLAssembler());
1806 scratch_scope.Exclude(ip1);
1807 __ Mov(ip1, invoke->GetDexMethodIndex());
1808
1809 // temp = object->GetClass();
1810 if (receiver.IsStackSlot()) {
1811 __ Ldr(temp, StackOperandFrom(receiver));
1812 __ Ldr(temp, HeapOperand(temp, class_offset));
1813 } else {
1814 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
1815 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001816 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00001817 // temp = temp->GetImtEntryAt(method_offset);
1818 __ Ldr(temp, HeapOperand(temp, method_offset));
1819 // lr = temp->GetEntryPoint();
1820 __ Ldr(lr, HeapOperand(temp, entry_point));
1821 // lr();
1822 __ Blr(lr);
1823 DCHECK(!codegen_->IsLeafMethod());
1824 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1825}
1826
1827void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001828 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1829 if (intrinsic.TryDispatch(invoke)) {
1830 return;
1831 }
1832
Alexandre Rames67555f72014-11-18 10:55:16 +00001833 HandleInvoke(invoke);
1834}
1835
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001836void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001837 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
1838 if (intrinsic.TryDispatch(invoke)) {
1839 return;
1840 }
1841
Alexandre Rames67555f72014-11-18 10:55:16 +00001842 HandleInvoke(invoke);
1843}
1844
Andreas Gampe878d58c2015-01-15 23:24:00 -08001845static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
1846 if (invoke->GetLocations()->Intrinsified()) {
1847 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
1848 intrinsic.Dispatch(invoke);
1849 return true;
1850 }
1851 return false;
1852}
1853
1854void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
1855 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
1856 DCHECK(temp.Is(kArtMethodRegister));
Alexandre Rames5319def2014-10-23 10:03:10 +01001857 size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() +
Andreas Gampe878d58c2015-01-15 23:24:00 -08001858 invoke->GetDexMethodIndex() * kHeapRefSize;
Alexandre Rames5319def2014-10-23 10:03:10 +01001859
1860 // TODO: Implement all kinds of calls:
1861 // 1) boot -> boot
1862 // 2) app -> boot
1863 // 3) app -> app
1864 //
1865 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1866
Nicolas Geoffray0a299b92015-01-29 11:39:44 +00001867 // temp = method;
1868 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001869 if (!invoke->IsRecursive()) {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001870 // temp = temp->dex_cache_resolved_methods_;
1871 __ Ldr(temp, HeapOperand(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset()));
1872 // temp = temp[index_in_cache];
1873 __ Ldr(temp, HeapOperand(temp, index_in_cache));
1874 // lr = temp->entry_point_from_quick_compiled_code_;
1875 __ Ldr(lr, HeapOperand(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1876 kArm64WordSize)));
1877 // lr();
1878 __ Blr(lr);
1879 } else {
1880 __ Bl(&frame_entry_label_);
1881 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001882
Andreas Gampe878d58c2015-01-15 23:24:00 -08001883 RecordPcInfo(invoke, invoke->GetDexPc());
1884 DCHECK(!IsLeafMethod());
1885}
1886
1887void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1888 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1889 return;
1890 }
1891
1892 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
1893 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01001894}
1895
1896void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001897 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1898 return;
1899 }
1900
Alexandre Rames5319def2014-10-23 10:03:10 +01001901 LocationSummary* locations = invoke->GetLocations();
1902 Location receiver = locations->InAt(0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001903 Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01001904 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1905 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1906 Offset class_offset = mirror::Object::ClassOffset();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001907 Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01001908
1909 // temp = object->GetClass();
1910 if (receiver.IsStackSlot()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001911 __ Ldr(temp, MemOperand(sp, receiver.GetStackIndex()));
1912 __ Ldr(temp, HeapOperand(temp, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001913 } else {
1914 DCHECK(receiver.IsRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001915 __ Ldr(temp, HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001916 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001917 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01001918 // temp = temp->GetMethodAt(method_offset);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001919 __ Ldr(temp, HeapOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01001920 // lr = temp->GetEntryPoint();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001921 __ Ldr(lr, HeapOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001922 // lr();
1923 __ Blr(lr);
1924 DCHECK(!codegen_->IsLeafMethod());
1925 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1926}
1927
Alexandre Rames67555f72014-11-18 10:55:16 +00001928void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
1929 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
1930 : LocationSummary::kNoCall;
1931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
1932 locations->SetOut(Location::RequiresRegister());
1933}
1934
1935void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
1936 Register out = OutputRegister(cls);
1937 if (cls->IsReferrersClass()) {
1938 DCHECK(!cls->CanCallRuntime());
1939 DCHECK(!cls->MustGenerateClinitCheck());
1940 codegen_->LoadCurrentMethod(out);
1941 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
1942 } else {
1943 DCHECK(cls->CanCallRuntime());
1944 codegen_->LoadCurrentMethod(out);
1945 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DexCacheResolvedTypesOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001946 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00001947
1948 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1949 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
1950 codegen_->AddSlowPath(slow_path);
1951 __ Cbz(out, slow_path->GetEntryLabel());
1952 if (cls->MustGenerateClinitCheck()) {
1953 GenerateClassInitializationCheck(slow_path, out);
1954 } else {
1955 __ Bind(slow_path->GetExitLabel());
1956 }
1957 }
1958}
1959
1960void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
1961 LocationSummary* locations =
1962 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
1963 locations->SetOut(Location::RequiresRegister());
1964}
1965
1966void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
1967 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
1968 __ Ldr(OutputRegister(instruction), exception);
1969 __ Str(wzr, exception);
1970}
1971
Alexandre Rames5319def2014-10-23 10:03:10 +01001972void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
1973 load->SetLocations(nullptr);
1974}
1975
1976void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
1977 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001978 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01001979}
1980
Alexandre Rames67555f72014-11-18 10:55:16 +00001981void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
1982 LocationSummary* locations =
1983 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
1984 locations->SetOut(Location::RequiresRegister());
1985}
1986
1987void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
1988 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
1989 codegen_->AddSlowPath(slow_path);
1990
1991 Register out = OutputRegister(load);
1992 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08001993 __ Ldr(out, HeapOperand(out, mirror::ArtMethod::DeclaringClassOffset()));
1994 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001995 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00001996 __ Cbz(out, slow_path->GetEntryLabel());
1997 __ Bind(slow_path->GetExitLabel());
1998}
1999
Alexandre Rames5319def2014-10-23 10:03:10 +01002000void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2001 local->SetLocations(nullptr);
2002}
2003
2004void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2005 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2006}
2007
2008void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2009 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2010 locations->SetOut(Location::ConstantLocation(constant));
2011}
2012
2013void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2014 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002015 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002016}
2017
Alexandre Rames67555f72014-11-18 10:55:16 +00002018void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2021 InvokeRuntimeCallingConvention calling_convention;
2022 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2023}
2024
2025void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2026 codegen_->InvokeRuntime(instruction->IsEnter()
2027 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2028 instruction,
2029 instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002030 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002031}
2032
Alexandre Rames42d641b2014-10-27 14:00:51 +00002033void LocationsBuilderARM64::VisitMul(HMul* mul) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2036 switch (mul->GetResultType()) {
2037 case Primitive::kPrimInt:
2038 case Primitive::kPrimLong:
2039 locations->SetInAt(0, Location::RequiresRegister());
2040 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002042 break;
2043
2044 case Primitive::kPrimFloat:
2045 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002046 locations->SetInAt(0, Location::RequiresFpuRegister());
2047 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002048 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002049 break;
2050
2051 default:
2052 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2053 }
2054}
2055
2056void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2057 switch (mul->GetResultType()) {
2058 case Primitive::kPrimInt:
2059 case Primitive::kPrimLong:
2060 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2061 break;
2062
2063 case Primitive::kPrimFloat:
2064 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002065 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002066 break;
2067
2068 default:
2069 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2070 }
2071}
2072
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002073void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2074 LocationSummary* locations =
2075 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2076 switch (neg->GetResultType()) {
2077 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002078 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002079 locations->SetInAt(0, Location::RegisterOrConstant(neg->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002081 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002082
2083 case Primitive::kPrimFloat:
2084 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002085 locations->SetInAt(0, Location::RequiresFpuRegister());
2086 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002087 break;
2088
2089 default:
2090 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2091 }
2092}
2093
2094void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2095 switch (neg->GetResultType()) {
2096 case Primitive::kPrimInt:
2097 case Primitive::kPrimLong:
2098 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2099 break;
2100
2101 case Primitive::kPrimFloat:
2102 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002103 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002104 break;
2105
2106 default:
2107 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2108 }
2109}
2110
2111void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2112 LocationSummary* locations =
2113 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2114 InvokeRuntimeCallingConvention calling_convention;
2115 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002116 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002117 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002118 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2119 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2120 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002121}
2122
2123void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2124 LocationSummary* locations = instruction->GetLocations();
2125 InvokeRuntimeCallingConvention calling_convention;
2126 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2127 DCHECK(type_index.Is(w0));
2128 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002129 DCHECK(current_method.Is(w2));
Alexandre Rames67555f72014-11-18 10:55:16 +00002130 codegen_->LoadCurrentMethod(current_method);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002131 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002132 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002133 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2134 instruction,
2135 instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002136 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
2137 void*, uint32_t, int32_t, mirror::ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002138}
2139
Alexandre Rames5319def2014-10-23 10:03:10 +01002140void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2141 LocationSummary* locations =
2142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2143 InvokeRuntimeCallingConvention calling_convention;
2144 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2145 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2146 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002147 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002148}
2149
2150void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2151 LocationSummary* locations = instruction->GetLocations();
2152 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2153 DCHECK(type_index.Is(w0));
2154 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2155 DCHECK(current_method.Is(w1));
Alexandre Rames67555f72014-11-18 10:55:16 +00002156 codegen_->LoadCurrentMethod(current_method);
Alexandre Rames5319def2014-10-23 10:03:10 +01002157 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002158 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002159 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2160 instruction,
2161 instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002162 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002163}
2164
2165void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2166 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002167 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002168 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002169}
2170
2171void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
2172 switch (instruction->InputAt(0)->GetType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002173 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002175 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002176 break;
2177
2178 default:
2179 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2180 }
2181}
2182
2183void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2184 LocationSummary* locations =
2185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2186 locations->SetInAt(0, Location::RequiresRegister());
2187 if (instruction->HasUses()) {
2188 locations->SetOut(Location::SameAsFirstInput());
2189 }
2190}
2191
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002192void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002193 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2194 return;
2195 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002196 Location obj = instruction->GetLocations()->InAt(0);
2197
2198 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2199 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2200}
2201
2202void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002203 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2204 codegen_->AddSlowPath(slow_path);
2205
2206 LocationSummary* locations = instruction->GetLocations();
2207 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002208
2209 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002210}
2211
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002212void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2213 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2214 GenerateImplicitNullCheck(instruction);
2215 } else {
2216 GenerateExplicitNullCheck(instruction);
2217 }
2218}
2219
Alexandre Rames67555f72014-11-18 10:55:16 +00002220void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2221 HandleBinaryOp(instruction);
2222}
2223
2224void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2225 HandleBinaryOp(instruction);
2226}
2227
Alexandre Rames3e69f162014-12-10 10:36:50 +00002228void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2229 LOG(FATAL) << "Unreachable";
2230}
2231
2232void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2233 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2234}
2235
Alexandre Rames5319def2014-10-23 10:03:10 +01002236void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2238 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2239 if (location.IsStackSlot()) {
2240 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2241 } else if (location.IsDoubleStackSlot()) {
2242 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2243 }
2244 locations->SetOut(location);
2245}
2246
2247void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) {
2248 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002249 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002250}
2251
2252void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2254 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2255 locations->SetInAt(i, Location::Any());
2256 }
2257 locations->SetOut(Location::Any());
2258}
2259
2260void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002261 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002262 LOG(FATAL) << "Unreachable";
2263}
2264
Serban Constantinescu02164b32014-11-13 14:05:07 +00002265void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002266 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002267 LocationSummary::CallKind call_kind =
2268 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2270
2271 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002272 case Primitive::kPrimInt:
2273 case Primitive::kPrimLong:
2274 locations->SetInAt(0, Location::RequiresRegister());
2275 locations->SetInAt(1, Location::RequiresRegister());
2276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2277 break;
2278
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002279 case Primitive::kPrimFloat:
2280 case Primitive::kPrimDouble: {
2281 InvokeRuntimeCallingConvention calling_convention;
2282 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2283 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2284 locations->SetOut(calling_convention.GetReturnLocation(type));
2285
2286 break;
2287 }
2288
Serban Constantinescu02164b32014-11-13 14:05:07 +00002289 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002290 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002291 }
2292}
2293
2294void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2295 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002296
Serban Constantinescu02164b32014-11-13 14:05:07 +00002297 switch (type) {
2298 case Primitive::kPrimInt:
2299 case Primitive::kPrimLong: {
2300 UseScratchRegisterScope temps(GetVIXLAssembler());
2301 Register dividend = InputRegisterAt(rem, 0);
2302 Register divisor = InputRegisterAt(rem, 1);
2303 Register output = OutputRegister(rem);
2304 Register temp = temps.AcquireSameSizeAs(output);
2305
2306 __ Sdiv(temp, dividend, divisor);
2307 __ Msub(output, temp, divisor, dividend);
2308 break;
2309 }
2310
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002311 case Primitive::kPrimFloat:
2312 case Primitive::kPrimDouble: {
2313 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2314 : QUICK_ENTRY_POINT(pFmod);
2315 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc());
2316 break;
2317 }
2318
Serban Constantinescu02164b32014-11-13 14:05:07 +00002319 default:
2320 LOG(FATAL) << "Unexpected rem type " << type;
2321 }
2322}
2323
Alexandre Rames5319def2014-10-23 10:03:10 +01002324void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2325 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2326 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002327 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002328}
2329
2330void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002331 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002332 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002333 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002334}
2335
2336void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2337 instruction->SetLocations(nullptr);
2338}
2339
2340void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002341 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002342 codegen_->GenerateFrameExit();
Alexandre Rames3e69f162014-12-10 10:36:50 +00002343 __ Ret();
Alexandre Rames5319def2014-10-23 10:03:10 +01002344}
2345
Serban Constantinescu02164b32014-11-13 14:05:07 +00002346void LocationsBuilderARM64::VisitShl(HShl* shl) {
2347 HandleShift(shl);
2348}
2349
2350void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2351 HandleShift(shl);
2352}
2353
2354void LocationsBuilderARM64::VisitShr(HShr* shr) {
2355 HandleShift(shr);
2356}
2357
2358void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2359 HandleShift(shr);
2360}
2361
Alexandre Rames5319def2014-10-23 10:03:10 +01002362void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2364 Primitive::Type field_type = store->InputAt(1)->GetType();
2365 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002366 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002367 case Primitive::kPrimBoolean:
2368 case Primitive::kPrimByte:
2369 case Primitive::kPrimChar:
2370 case Primitive::kPrimShort:
2371 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002372 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002373 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2374 break;
2375
2376 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002377 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002378 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2379 break;
2380
2381 default:
2382 LOG(FATAL) << "Unimplemented local type " << field_type;
2383 }
2384}
2385
2386void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002387 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002388}
2389
2390void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002391 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002392}
2393
2394void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002395 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002396}
2397
Alexandre Rames67555f72014-11-18 10:55:16 +00002398void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2399 LocationSummary* locations =
2400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2401 locations->SetInAt(0, Location::RequiresRegister());
2402 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2403}
2404
2405void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002406 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), instruction->GetFieldOffset());
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002407
2408 if (instruction->IsVolatile()) {
2409 if (kUseAcquireRelease) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002410 // NB: LoadAcquire will record the pc info if needed.
2411 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002412 } else {
2413 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2414 // For IRIW sequential consistency kLoadAny is not sufficient.
2415 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2416 }
2417 } else {
2418 codegen_->Load(instruction->GetType(), OutputCPURegister(instruction), field);
2419 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002420}
2421
2422void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002423 LocationSummary* locations =
2424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2425 locations->SetInAt(0, Location::RequiresRegister());
2426 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01002427}
2428
Alexandre Rames67555f72014-11-18 10:55:16 +00002429void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002430 Register cls = InputRegisterAt(instruction, 0);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002431 CPURegister value = InputCPURegisterAt(instruction, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002432 Offset offset = instruction->GetFieldOffset();
Alexandre Rames67555f72014-11-18 10:55:16 +00002433 Primitive::Type field_type = instruction->GetFieldType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002434
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002435 if (instruction->IsVolatile()) {
2436 if (kUseAcquireRelease) {
2437 codegen_->StoreRelease(field_type, value, HeapOperand(cls, offset));
2438 } else {
2439 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2440 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2441 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2442 }
2443 } else {
2444 codegen_->Store(field_type, value, HeapOperand(cls, offset));
2445 }
2446
2447 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 codegen_->MarkGCCard(cls, Register(value));
2449 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002450}
2451
2452void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2454}
2455
2456void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002457 HBasicBlock* block = instruction->GetBlock();
2458 if (block->GetLoopInformation() != nullptr) {
2459 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2460 // The back edge will generate the suspend check.
2461 return;
2462 }
2463 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2464 // The goto will generate the suspend check.
2465 return;
2466 }
2467 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002468}
2469
2470void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2471 temp->SetLocations(nullptr);
2472}
2473
2474void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2475 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002476 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002477}
2478
Alexandre Rames67555f72014-11-18 10:55:16 +00002479void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2480 LocationSummary* locations =
2481 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2482 InvokeRuntimeCallingConvention calling_convention;
2483 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2484}
2485
2486void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2487 codegen_->InvokeRuntime(
2488 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002489 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002490}
2491
2492void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2493 LocationSummary* locations =
2494 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2495 Primitive::Type input_type = conversion->GetInputType();
2496 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002497 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002498 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2499 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2501 }
2502
Alexandre Rames542361f2015-01-29 16:57:31 +00002503 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002504 locations->SetInAt(0, Location::RequiresFpuRegister());
2505 } else {
2506 locations->SetInAt(0, Location::RequiresRegister());
2507 }
2508
Alexandre Rames542361f2015-01-29 16:57:31 +00002509 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002510 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2511 } else {
2512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2513 }
2514}
2515
2516void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2517 Primitive::Type result_type = conversion->GetResultType();
2518 Primitive::Type input_type = conversion->GetInputType();
2519
2520 DCHECK_NE(input_type, result_type);
2521
Alexandre Rames542361f2015-01-29 16:57:31 +00002522 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002523 int result_size = Primitive::ComponentSize(result_type);
2524 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002525 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002526 Register output = OutputRegister(conversion);
2527 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002528 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2529 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2530 } else if ((result_type == Primitive::kPrimChar) ||
2531 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2532 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002533 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002534 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002535 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002536 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002537 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002538 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002539 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2540 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002541 } else if (Primitive::IsFloatingPointType(result_type) &&
2542 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002543 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2544 } else {
2545 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2546 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002547 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002548}
Alexandre Rames67555f72014-11-18 10:55:16 +00002549
Serban Constantinescu02164b32014-11-13 14:05:07 +00002550void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2551 HandleShift(ushr);
2552}
2553
2554void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2555 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002556}
2557
2558void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2559 HandleBinaryOp(instruction);
2560}
2561
2562void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2563 HandleBinaryOp(instruction);
2564}
2565
2566#undef __
2567#undef QUICK_ENTRY_POINT
2568
Alexandre Rames5319def2014-10-23 10:03:10 +01002569} // namespace arm64
2570} // namespace art