blob: d6169b057f43aa4e2a979d7c659dfef6ed7ac2e9 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080022#include "common_arm64.h"
Vladimir Marko9b688a02015-05-06 14:12:42 +010023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000031#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "thread.h"
33#include "utils/arm64/assembler_arm64.h"
34#include "utils/assembler.h"
35#include "utils/stack_checks.h"
36
37
38using namespace vixl; // NOLINT(build/namespaces)
39
40#ifdef __
41#error "ARM64 Codegen VIXL macro-assembler macro already defined."
42#endif
43
Alexandre Rames5319def2014-10-23 10:03:10 +010044namespace art {
45
46namespace arm64 {
47
Andreas Gampe878d58c2015-01-15 23:24:00 -080048using helpers::CPURegisterFrom;
49using helpers::DRegisterFrom;
50using helpers::FPRegisterFrom;
51using helpers::HeapOperand;
52using helpers::HeapOperandFrom;
53using helpers::InputCPURegisterAt;
54using helpers::InputFPRegisterAt;
55using helpers::InputRegisterAt;
56using helpers::InputOperandAt;
57using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::LocationFrom;
59using helpers::OperandFromMemOperand;
60using helpers::OutputCPURegister;
61using helpers::OutputFPRegister;
62using helpers::OutputRegister;
63using helpers::RegisterFrom;
64using helpers::StackOperandFrom;
65using helpers::VIXLRegCodeFromART;
66using helpers::WRegisterFrom;
67using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000068using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080069using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070
Alexandre Rames5319def2014-10-23 10:03:10 +010071static constexpr int kCurrentMethodStackOffset = 0;
72
Alexandre Rames5319def2014-10-23 10:03:10 +010073inline Condition ARM64Condition(IfCondition cond) {
74 switch (cond) {
75 case kCondEQ: return eq;
76 case kCondNE: return ne;
77 case kCondLT: return lt;
78 case kCondLE: return le;
79 case kCondGT: return gt;
80 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010081 }
Roland Levillain7f63c522015-07-13 15:54:55 +000082 LOG(FATAL) << "Unreachable";
83 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010084}
85
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000087 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010096 } else if (return_type == Primitive::kPrimVoid) {
97 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000098 } else {
99 return LocationFrom(w0);
100 }
101}
102
Alexandre Rames5319def2014-10-23 10:03:10 +0100103Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000104 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105}
106
Alexandre Rames67555f72014-11-18 10:55:16 +0000107#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
108#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100109
Zheng Xuda403092015-04-24 17:35:39 +0800110// Calculate memory accessing operand for save/restore live registers.
111static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
112 RegisterSet* register_set,
113 int64_t spill_offset,
114 bool is_save) {
115 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
116 codegen->GetNumberOfCoreRegisters(),
117 register_set->GetFloatingPointRegisters(),
118 codegen->GetNumberOfFloatingPointRegisters()));
119
120 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
121 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
122 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
123 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
124
125 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
126 UseScratchRegisterScope temps(masm);
127
128 Register base = masm->StackPointer();
129 int64_t core_spill_size = core_list.TotalSizeInBytes();
130 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
131 int64_t reg_size = kXRegSizeInBytes;
132 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
133 uint32_t ls_access_size = WhichPowerOf2(reg_size);
134 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
135 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
136 // If the offset does not fit in the instruction's immediate field, use an alternate register
137 // to compute the base address(float point registers spill base address).
138 Register new_base = temps.AcquireSameSizeAs(base);
139 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
140 base = new_base;
141 spill_offset = -core_spill_size;
142 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
143 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
144 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
145 }
146
147 if (is_save) {
148 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
149 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
150 } else {
151 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
152 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
153 }
154}
155
156void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
157 RegisterSet* register_set = locations->GetLiveRegisters();
158 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
159 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
160 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
161 // If the register holds an object, update the stack mask.
162 if (locations->RegisterContainsObject(i)) {
163 locations->SetStackBit(stack_offset / kVRegSize);
164 }
165 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
166 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
167 saved_core_stack_offsets_[i] = stack_offset;
168 stack_offset += kXRegSizeInBytes;
169 }
170 }
171
172 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
173 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
174 register_set->ContainsFloatingPointRegister(i)) {
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_fpu_stack_offsets_[i] = stack_offset;
178 stack_offset += kDRegSizeInBytes;
179 }
180 }
181
182 SaveRestoreLiveRegistersHelper(codegen, register_set,
183 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
184}
185
186void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 SaveRestoreLiveRegistersHelper(codegen, register_set,
189 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
190}
191
Alexandre Rames5319def2014-10-23 10:03:10 +0100192class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
193 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000194 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
195 Location index_location,
196 Location length_location)
197 : instruction_(instruction),
198 index_location_(index_location),
199 length_location_(length_location) {}
200
Alexandre Rames5319def2014-10-23 10:03:10 +0100201
Alexandre Rames67555f72014-11-18 10:55:16 +0000202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000203 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100204 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000205 // We're moving two locations to locations that could overlap, so we need a parallel
206 // move resolver.
207 InvokeRuntimeCallingConvention calling_convention;
208 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100209 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
210 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000211 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000212 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800213 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 }
215
Alexandre Rames8158f282015-08-07 10:26:17 +0100216 bool IsFatal() const OVERRIDE { return true; }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221 HBoundsCheck* const instruction_;
222 const Location index_location_;
223 const Location length_location_;
224
Alexandre Rames5319def2014-10-23 10:03:10 +0100225 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
226};
227
Alexandre Rames67555f72014-11-18 10:55:16 +0000228class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
229 public:
230 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
231
232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
233 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
234 __ Bind(GetEntryLabel());
235 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000236 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800237 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000238 }
239
Alexandre Rames8158f282015-08-07 10:26:17 +0100240 bool IsFatal() const OVERRIDE { return true; }
241
Alexandre Rames9931f312015-06-19 14:47:01 +0100242 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
243
Alexandre Rames67555f72014-11-18 10:55:16 +0000244 private:
245 HDivZeroCheck* const instruction_;
246 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
247};
248
249class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
250 public:
251 LoadClassSlowPathARM64(HLoadClass* cls,
252 HInstruction* at,
253 uint32_t dex_pc,
254 bool do_clinit)
255 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
256 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
257 }
258
259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
260 LocationSummary* locations = at_->GetLocations();
261 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
262
263 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000265
266 InvokeRuntimeCallingConvention calling_convention;
267 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
269 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000270 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100272 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800273 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100274 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000276
277 // Move the class to the desired location.
278 Location out = locations->Out();
279 if (out.IsValid()) {
280 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
281 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000282 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000283 }
284
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 __ B(GetExitLabel());
287 }
288
Alexandre Rames9931f312015-06-19 14:47:01 +0100289 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
290
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 private:
292 // The class this slow path will load.
293 HLoadClass* const cls_;
294
295 // The instruction where this slow path is happening.
296 // (Might be the load class or an initialization check).
297 HInstruction* const at_;
298
299 // The dex PC of `at_`.
300 const uint32_t dex_pc_;
301
302 // Whether to initialize the class.
303 const bool do_clinit_;
304
305 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
306};
307
308class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
309 public:
310 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
311
312 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
313 LocationSummary* locations = instruction_->GetLocations();
314 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
315 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
316
317 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000318 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000319
320 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800321 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000323 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100324 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000325 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000326 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000327
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000328 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000329 __ B(GetExitLabel());
330 }
331
Alexandre Rames9931f312015-06-19 14:47:01 +0100332 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
333
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 private:
335 HLoadString* const instruction_;
336
337 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
338};
339
Alexandre Rames5319def2014-10-23 10:03:10 +0100340class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
341 public:
342 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
343
Alexandre Rames67555f72014-11-18 10:55:16 +0000344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
345 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100346 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000347 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000348 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100350 }
351
Alexandre Rames8158f282015-08-07 10:26:17 +0100352 bool IsFatal() const OVERRIDE { return true; }
353
Alexandre Rames9931f312015-06-19 14:47:01 +0100354 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
355
Alexandre Rames5319def2014-10-23 10:03:10 +0100356 private:
357 HNullCheck* const instruction_;
358
359 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
360};
361
362class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
363 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100364 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100365 : instruction_(instruction), successor_(successor) {}
366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
368 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100369 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000370 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000372 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800373 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000374 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 if (successor_ == nullptr) {
376 __ B(GetReturnLabel());
377 } else {
378 __ B(arm64_codegen->GetLabelOf(successor_));
379 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 }
381
382 vixl::Label* GetReturnLabel() {
383 DCHECK(successor_ == nullptr);
384 return &return_label_;
385 }
386
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100387 HBasicBlock* GetSuccessor() const {
388 return successor_;
389 }
390
Alexandre Rames9931f312015-06-19 14:47:01 +0100391 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
392
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 private:
394 HSuspendCheck* const instruction_;
395 // If not null, the block to branch to after the suspend check.
396 HBasicBlock* const successor_;
397
398 // If `successor_` is null, the label to branch to after the suspend check.
399 vixl::Label return_label_;
400
401 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
402};
403
Alexandre Rames67555f72014-11-18 10:55:16 +0000404class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
405 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000406 TypeCheckSlowPathARM64(HInstruction* instruction,
407 Location class_to_check,
408 Location object_class,
409 uint32_t dex_pc)
410 : instruction_(instruction),
411 class_to_check_(class_to_check),
412 object_class_(object_class),
413 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000416 LocationSummary* locations = instruction_->GetLocations();
417 DCHECK(instruction_->IsCheckCast()
418 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
419 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
420
Alexandre Rames67555f72014-11-18 10:55:16 +0000421 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000422 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
424 // We're moving two locations to locations that could overlap, so we need a parallel
425 // move resolver.
426 InvokeRuntimeCallingConvention calling_convention;
427 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100428 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
429 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430
431 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000432 arm64_codegen->InvokeRuntime(
433 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 Primitive::Type ret_type = instruction_->GetType();
435 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
436 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800437 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
438 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000439 } else {
440 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000441 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800442 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443 }
444
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000445 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000446 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 }
448
Alexandre Rames9931f312015-06-19 14:47:01 +0100449 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
450
Alexandre Rames67555f72014-11-18 10:55:16 +0000451 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452 HInstruction* const instruction_;
453 const Location class_to_check_;
454 const Location object_class_;
455 uint32_t dex_pc_;
456
Alexandre Rames67555f72014-11-18 10:55:16 +0000457 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
458};
459
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700460class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
462 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
463 : instruction_(instruction) {}
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 __ Bind(GetEntryLabel());
467 SaveLiveRegisters(codegen, instruction_->GetLocations());
468 DCHECK(instruction_->IsDeoptimize());
469 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
470 uint32_t dex_pc = deoptimize->GetDexPc();
471 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
472 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
473 }
474
Alexandre Rames9931f312015-06-19 14:47:01 +0100475 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700477 private:
478 HInstruction* const instruction_;
479 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
480};
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482#undef __
483
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100484Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 Location next_location;
486 if (type == Primitive::kPrimVoid) {
487 LOG(FATAL) << "Unreachable type " << type;
488 }
489
Alexandre Rames542361f2015-01-29 16:57:31 +0000490 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100491 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
492 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000493 } else if (!Primitive::IsFloatingPointType(type) &&
494 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000495 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
496 } else {
497 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000498 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
499 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100500 }
501
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000502 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000503 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 return next_location;
505}
506
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100507Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100508 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100509}
510
Serban Constantinescu579885a2015-02-22 20:51:33 +0000511CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
512 const Arm64InstructionSetFeatures& isa_features,
513 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100514 : CodeGenerator(graph,
515 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000516 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000517 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000518 callee_saved_core_registers.list(),
519 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000520 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100521 block_labels_(nullptr),
522 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000523 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000524 move_resolver_(graph->GetArena(), this),
Vladimir Marko9b688a02015-05-06 14:12:42 +0100525 isa_features_(isa_features),
526 uint64_literals_(std::less<uint64_t>(), graph->GetArena()->Adapter()),
527 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
528 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
529 relative_call_patches_(graph->GetArena()->Adapter()),
530 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000532 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533}
Alexandre Rames5319def2014-10-23 10:03:10 +0100534
Alexandre Rames67555f72014-11-18 10:55:16 +0000535#undef __
536#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100537
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000538void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
539 // Ensure we emit the literal pool.
540 __ FinalizeCode();
Vladimir Marko9b688a02015-05-06 14:12:42 +0100541
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000542 CodeGenerator::Finalize(allocator);
543}
544
Zheng Xuad4450e2015-04-17 18:48:56 +0800545void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
546 // Note: There are 6 kinds of moves:
547 // 1. constant -> GPR/FPR (non-cycle)
548 // 2. constant -> stack (non-cycle)
549 // 3. GPR/FPR -> GPR/FPR
550 // 4. GPR/FPR -> stack
551 // 5. stack -> GPR/FPR
552 // 6. stack -> stack (non-cycle)
553 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
554 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
555 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
556 // dependency.
557 vixl_temps_.Open(GetVIXLAssembler());
558}
559
560void ParallelMoveResolverARM64::FinishEmitNativeCode() {
561 vixl_temps_.Close();
562}
563
564Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
565 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
566 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
567 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
568 Location scratch = GetScratchLocation(kind);
569 if (!scratch.Equals(Location::NoLocation())) {
570 return scratch;
571 }
572 // Allocate from VIXL temp registers.
573 if (kind == Location::kRegister) {
574 scratch = LocationFrom(vixl_temps_.AcquireX());
575 } else {
576 DCHECK(kind == Location::kFpuRegister);
577 scratch = LocationFrom(vixl_temps_.AcquireD());
578 }
579 AddScratchLocation(scratch);
580 return scratch;
581}
582
583void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
584 if (loc.IsRegister()) {
585 vixl_temps_.Release(XRegisterFrom(loc));
586 } else {
587 DCHECK(loc.IsFpuRegister());
588 vixl_temps_.Release(DRegisterFrom(loc));
589 }
590 RemoveScratchLocation(loc);
591}
592
Alexandre Rames3e69f162014-12-10 10:36:50 +0000593void ParallelMoveResolverARM64::EmitMove(size_t index) {
594 MoveOperands* move = moves_.Get(index);
595 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
596}
597
Alexandre Rames5319def2014-10-23 10:03:10 +0100598void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100599 MacroAssembler* masm = GetVIXLAssembler();
600 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000601 __ Bind(&frame_entry_label_);
602
Serban Constantinescu02164b32014-11-13 14:05:07 +0000603 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
604 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100605 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000606 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000607 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000608 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000609 __ Ldr(wzr, MemOperand(temp, 0));
610 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000611 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100612
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000613 if (!HasEmptyFrame()) {
614 int frame_size = GetFrameSize();
615 // Stack layout:
616 // sp[frame_size - 8] : lr.
617 // ... : other preserved core registers.
618 // ... : other preserved fp registers.
619 // ... : reserved frame space.
620 // sp[0] : current method.
621 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100622 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800623 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
624 frame_size - GetCoreSpillSize());
625 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
626 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000627 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100628}
629
630void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100631 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100632 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000633 if (!HasEmptyFrame()) {
634 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800635 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
636 frame_size - FrameEntrySpillSize());
637 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
638 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000639 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100640 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000641 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100642 __ Ret();
643 GetAssembler()->cfi().RestoreState();
644 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100645}
646
Zheng Xuda403092015-04-24 17:35:39 +0800647vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
648 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
649 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
650 core_spill_mask_);
651}
652
653vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
654 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
655 GetNumberOfFloatingPointRegisters()));
656 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
657 fpu_spill_mask_);
658}
659
Alexandre Rames5319def2014-10-23 10:03:10 +0100660void CodeGeneratorARM64::Bind(HBasicBlock* block) {
661 __ Bind(GetLabelOf(block));
662}
663
Alexandre Rames5319def2014-10-23 10:03:10 +0100664void CodeGeneratorARM64::Move(HInstruction* instruction,
665 Location location,
666 HInstruction* move_for) {
667 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100668 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000669 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100670
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100671 if (instruction->IsFakeString()) {
672 // The fake string is an alias for null.
673 DCHECK(IsBaseline());
674 instruction = locations->Out().GetConstant();
675 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
676 }
677
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100678 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700679 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100680 } else if (locations != nullptr && locations->Out().Equals(location)) {
681 return;
682 } else if (instruction->IsIntConstant()
683 || instruction->IsLongConstant()
684 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000685 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100686 if (location.IsRegister()) {
687 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000688 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100689 (instruction->IsLongConstant() && dst.Is64Bits()));
690 __ Mov(dst, value);
691 } else {
692 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000693 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000694 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
695 ? temps.AcquireW()
696 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100697 __ Mov(temp, value);
698 __ Str(temp, StackOperandFrom(location));
699 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000700 } else if (instruction->IsTemporary()) {
701 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000702 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100703 } else if (instruction->IsLoadLocal()) {
704 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000705 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000706 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000707 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000708 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100709 }
710
711 } else {
712 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000713 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100714 }
715}
716
Alexandre Rames5319def2014-10-23 10:03:10 +0100717Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
718 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000719
Alexandre Rames5319def2014-10-23 10:03:10 +0100720 switch (type) {
721 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000722 case Primitive::kPrimInt:
723 case Primitive::kPrimFloat:
724 return Location::StackSlot(GetStackSlot(load->GetLocal()));
725
726 case Primitive::kPrimLong:
727 case Primitive::kPrimDouble:
728 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
729
Alexandre Rames5319def2014-10-23 10:03:10 +0100730 case Primitive::kPrimBoolean:
731 case Primitive::kPrimByte:
732 case Primitive::kPrimChar:
733 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100734 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100735 LOG(FATAL) << "Unexpected type " << type;
736 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000737
Alexandre Rames5319def2014-10-23 10:03:10 +0100738 LOG(FATAL) << "Unreachable";
739 return Location::NoLocation();
740}
741
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100742void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000743 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100744 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000745 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100746 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100747 if (value_can_be_null) {
748 __ Cbz(value, &done);
749 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100750 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
751 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000752 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100753 if (value_can_be_null) {
754 __ Bind(&done);
755 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100756}
757
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000758void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
759 // Blocked core registers:
760 // lr : Runtime reserved.
761 // tr : Runtime reserved.
762 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
763 // ip1 : VIXL core temp.
764 // ip0 : VIXL core temp.
765 //
766 // Blocked fp registers:
767 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100768 CPURegList reserved_core_registers = vixl_reserved_core_registers;
769 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100770 while (!reserved_core_registers.IsEmpty()) {
771 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
772 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000773
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000774 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800775 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000776 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
777 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000778
779 if (is_baseline) {
780 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
781 while (!reserved_core_baseline_registers.IsEmpty()) {
782 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
783 }
784
785 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
786 while (!reserved_fp_baseline_registers.IsEmpty()) {
787 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
788 }
789 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100790}
791
792Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
793 if (type == Primitive::kPrimVoid) {
794 LOG(FATAL) << "Unreachable type " << type;
795 }
796
Alexandre Rames542361f2015-01-29 16:57:31 +0000797 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000798 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
799 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100800 return Location::FpuRegisterLocation(reg);
801 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000802 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
803 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100804 return Location::RegisterLocation(reg);
805 }
806}
807
Alexandre Rames3e69f162014-12-10 10:36:50 +0000808size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
809 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
810 __ Str(reg, MemOperand(sp, stack_index));
811 return kArm64WordSize;
812}
813
814size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
815 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
816 __ Ldr(reg, MemOperand(sp, stack_index));
817 return kArm64WordSize;
818}
819
820size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
821 FPRegister reg = FPRegister(reg_id, kDRegSize);
822 __ Str(reg, MemOperand(sp, stack_index));
823 return kArm64WordSize;
824}
825
826size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
827 FPRegister reg = FPRegister(reg_id, kDRegSize);
828 __ Ldr(reg, MemOperand(sp, stack_index));
829 return kArm64WordSize;
830}
831
Alexandre Rames5319def2014-10-23 10:03:10 +0100832void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100833 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100834}
835
836void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100837 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100838}
839
Alexandre Rames67555f72014-11-18 10:55:16 +0000840void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000841 if (constant->IsIntConstant()) {
842 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
843 } else if (constant->IsLongConstant()) {
844 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
845 } else if (constant->IsNullConstant()) {
846 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000847 } else if (constant->IsFloatConstant()) {
848 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
849 } else {
850 DCHECK(constant->IsDoubleConstant());
851 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
852 }
853}
854
Alexandre Rames3e69f162014-12-10 10:36:50 +0000855
856static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
857 DCHECK(constant.IsConstant());
858 HConstant* cst = constant.GetConstant();
859 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000860 // Null is mapped to a core W register, which we associate with kPrimInt.
861 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000862 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
863 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
864 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
865}
866
867void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000868 if (source.Equals(destination)) {
869 return;
870 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000871
872 // A valid move can always be inferred from the destination and source
873 // locations. When moving from and to a register, the argument type can be
874 // used to generate 32bit instead of 64bit moves. In debug mode we also
875 // checks the coherency of the locations and the type.
876 bool unspecified_type = (type == Primitive::kPrimVoid);
877
878 if (destination.IsRegister() || destination.IsFpuRegister()) {
879 if (unspecified_type) {
880 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
881 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000882 (src_cst != nullptr && (src_cst->IsIntConstant()
883 || src_cst->IsFloatConstant()
884 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000885 // For stack slots and 32bit constants, a 64bit type is appropriate.
886 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000887 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000888 // If the source is a double stack slot or a 64bit constant, a 64bit
889 // type is appropriate. Else the source is a register, and since the
890 // type has not been specified, we chose a 64bit type to force a 64bit
891 // move.
892 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000893 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000894 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000895 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
896 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000897 CPURegister dst = CPURegisterFrom(destination, type);
898 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
899 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
900 __ Ldr(dst, StackOperandFrom(source));
901 } else if (source.IsConstant()) {
902 DCHECK(CoherentConstantAndType(source, type));
903 MoveConstant(dst, source.GetConstant());
904 } else {
905 if (destination.IsRegister()) {
906 __ Mov(Register(dst), RegisterFrom(source, type));
907 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800908 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000909 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
910 }
911 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000912 } else { // The destination is not a register. It must be a stack slot.
913 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
914 if (source.IsRegister() || source.IsFpuRegister()) {
915 if (unspecified_type) {
916 if (source.IsRegister()) {
917 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
918 } else {
919 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
920 }
921 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000922 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
923 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000924 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
925 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100926 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000927 UseScratchRegisterScope temps(GetVIXLAssembler());
928 HConstant* src_cst = source.GetConstant();
929 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000930 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000931 temp = temps.AcquireW();
932 } else if (src_cst->IsLongConstant()) {
933 temp = temps.AcquireX();
934 } else if (src_cst->IsFloatConstant()) {
935 temp = temps.AcquireS();
936 } else {
937 DCHECK(src_cst->IsDoubleConstant());
938 temp = temps.AcquireD();
939 }
940 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000941 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000942 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000943 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000944 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000945 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000946 // There is generally less pressure on FP registers.
947 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000948 __ Ldr(temp, StackOperandFrom(source));
949 __ Str(temp, StackOperandFrom(destination));
950 }
951 }
952}
953
954void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000955 CPURegister dst,
956 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000957 switch (type) {
958 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000959 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000960 break;
961 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000962 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000963 break;
964 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000965 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000966 break;
967 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000968 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000969 break;
970 case Primitive::kPrimInt:
971 case Primitive::kPrimNot:
972 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000973 case Primitive::kPrimFloat:
974 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000975 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000976 __ Ldr(dst, src);
977 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000978 case Primitive::kPrimVoid:
979 LOG(FATAL) << "Unreachable type " << type;
980 }
981}
982
Calin Juravle77520bc2015-01-12 18:45:46 +0000983void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000984 CPURegister dst,
985 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100986 MacroAssembler* masm = GetVIXLAssembler();
987 BlockPoolsScope block_pools(masm);
988 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000989 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000990 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000991
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000992 DCHECK(!src.IsPreIndex());
993 DCHECK(!src.IsPostIndex());
994
995 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800996 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000997 MemOperand base = MemOperand(temp_base);
998 switch (type) {
999 case Primitive::kPrimBoolean:
1000 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001001 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001002 break;
1003 case Primitive::kPrimByte:
1004 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001005 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001006 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1007 break;
1008 case Primitive::kPrimChar:
1009 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001010 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001011 break;
1012 case Primitive::kPrimShort:
1013 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001014 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001015 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1016 break;
1017 case Primitive::kPrimInt:
1018 case Primitive::kPrimNot:
1019 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001020 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001021 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001022 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001023 break;
1024 case Primitive::kPrimFloat:
1025 case Primitive::kPrimDouble: {
1026 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001027 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001028
1029 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1030 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001031 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001032 __ Fmov(FPRegister(dst), temp);
1033 break;
1034 }
1035 case Primitive::kPrimVoid:
1036 LOG(FATAL) << "Unreachable type " << type;
1037 }
1038}
1039
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001040void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001041 CPURegister src,
1042 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001043 switch (type) {
1044 case Primitive::kPrimBoolean:
1045 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001046 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001047 break;
1048 case Primitive::kPrimChar:
1049 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001050 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001051 break;
1052 case Primitive::kPrimInt:
1053 case Primitive::kPrimNot:
1054 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001055 case Primitive::kPrimFloat:
1056 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001057 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001058 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001059 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001060 case Primitive::kPrimVoid:
1061 LOG(FATAL) << "Unreachable type " << type;
1062 }
1063}
1064
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001065void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1066 CPURegister src,
1067 const MemOperand& dst) {
1068 UseScratchRegisterScope temps(GetVIXLAssembler());
1069 Register temp_base = temps.AcquireX();
1070
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001071 DCHECK(!dst.IsPreIndex());
1072 DCHECK(!dst.IsPostIndex());
1073
1074 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001075 Operand op = OperandFromMemOperand(dst);
1076 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001077 MemOperand base = MemOperand(temp_base);
1078 switch (type) {
1079 case Primitive::kPrimBoolean:
1080 case Primitive::kPrimByte:
1081 __ Stlrb(Register(src), base);
1082 break;
1083 case Primitive::kPrimChar:
1084 case Primitive::kPrimShort:
1085 __ Stlrh(Register(src), base);
1086 break;
1087 case Primitive::kPrimInt:
1088 case Primitive::kPrimNot:
1089 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001090 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001091 __ Stlr(Register(src), base);
1092 break;
1093 case Primitive::kPrimFloat:
1094 case Primitive::kPrimDouble: {
1095 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001096 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001097
1098 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1099 __ Fmov(temp, FPRegister(src));
1100 __ Stlr(temp, base);
1101 break;
1102 }
1103 case Primitive::kPrimVoid:
1104 LOG(FATAL) << "Unreachable type " << type;
1105 }
1106}
1107
Alexandre Rames67555f72014-11-18 10:55:16 +00001108void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1109 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001110 uint32_t dex_pc,
1111 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001112 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001113 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001114 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1115 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001116 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001117}
1118
1119void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1120 vixl::Register class_reg) {
1121 UseScratchRegisterScope temps(GetVIXLAssembler());
1122 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001123 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001124 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125
Serban Constantinescu02164b32014-11-13 14:05:07 +00001126 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001127 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001128 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1129 __ Add(temp, class_reg, status_offset);
1130 __ Ldar(temp, HeapOperand(temp));
1131 __ Cmp(temp, mirror::Class::kStatusInitialized);
1132 __ B(lt, slow_path->GetEntryLabel());
1133 } else {
1134 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1135 __ Cmp(temp, mirror::Class::kStatusInitialized);
1136 __ B(lt, slow_path->GetEntryLabel());
1137 __ Dmb(InnerShareable, BarrierReads);
1138 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001139 __ Bind(slow_path->GetExitLabel());
1140}
Alexandre Rames5319def2014-10-23 10:03:10 +01001141
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001142void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1143 BarrierType type = BarrierAll;
1144
1145 switch (kind) {
1146 case MemBarrierKind::kAnyAny:
1147 case MemBarrierKind::kAnyStore: {
1148 type = BarrierAll;
1149 break;
1150 }
1151 case MemBarrierKind::kLoadAny: {
1152 type = BarrierReads;
1153 break;
1154 }
1155 case MemBarrierKind::kStoreStore: {
1156 type = BarrierWrites;
1157 break;
1158 }
1159 default:
1160 LOG(FATAL) << "Unexpected memory barrier " << kind;
1161 }
1162 __ Dmb(InnerShareable, type);
1163}
1164
Serban Constantinescu02164b32014-11-13 14:05:07 +00001165void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1166 HBasicBlock* successor) {
1167 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001168 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1169 if (slow_path == nullptr) {
1170 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1171 instruction->SetSlowPath(slow_path);
1172 codegen_->AddSlowPath(slow_path);
1173 if (successor != nullptr) {
1174 DCHECK(successor->IsLoopHeader());
1175 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1176 }
1177 } else {
1178 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1179 }
1180
Serban Constantinescu02164b32014-11-13 14:05:07 +00001181 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1182 Register temp = temps.AcquireW();
1183
1184 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1185 if (successor == nullptr) {
1186 __ Cbnz(temp, slow_path->GetEntryLabel());
1187 __ Bind(slow_path->GetReturnLabel());
1188 } else {
1189 __ Cbz(temp, codegen_->GetLabelOf(successor));
1190 __ B(slow_path->GetEntryLabel());
1191 // slow_path will return to GetLabelOf(successor).
1192 }
1193}
1194
Alexandre Rames5319def2014-10-23 10:03:10 +01001195InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1196 CodeGeneratorARM64* codegen)
1197 : HGraphVisitor(graph),
1198 assembler_(codegen->GetAssembler()),
1199 codegen_(codegen) {}
1200
1201#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001202 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001203
1204#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1205
1206enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001207 // Using a base helps identify when we hit such breakpoints.
1208 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001209#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1210 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1211#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1212};
1213
1214#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1215 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001216 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001217 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1218 } \
1219 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1220 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1221 locations->SetOut(Location::Any()); \
1222 }
1223 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1224#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1225
1226#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001227#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001228
Alexandre Rames67555f72014-11-18 10:55:16 +00001229void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001230 DCHECK_EQ(instr->InputCount(), 2U);
1231 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1232 Primitive::Type type = instr->GetResultType();
1233 switch (type) {
1234 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001235 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001236 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001237 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001239 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001240
1241 case Primitive::kPrimFloat:
1242 case Primitive::kPrimDouble:
1243 locations->SetInAt(0, Location::RequiresFpuRegister());
1244 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001245 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001246 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001247
Alexandre Rames5319def2014-10-23 10:03:10 +01001248 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001249 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001250 }
1251}
1252
Alexandre Rames09a99962015-04-15 11:47:56 +01001253void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1254 LocationSummary* locations =
1255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1256 locations->SetInAt(0, Location::RequiresRegister());
1257 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1258 locations->SetOut(Location::RequiresFpuRegister());
1259 } else {
1260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1261 }
1262}
1263
1264void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1265 const FieldInfo& field_info) {
1266 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001267 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001268 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001269
1270 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1271 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1272
1273 if (field_info.IsVolatile()) {
1274 if (use_acquire_release) {
1275 // NB: LoadAcquire will record the pc info if needed.
1276 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1277 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001278 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001279 codegen_->MaybeRecordImplicitNullCheck(instruction);
1280 // For IRIW sequential consistency kLoadAny is not sufficient.
1281 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1282 }
1283 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001284 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001285 codegen_->MaybeRecordImplicitNullCheck(instruction);
1286 }
Roland Levillain4d027112015-07-01 15:41:14 +01001287
1288 if (field_type == Primitive::kPrimNot) {
1289 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1290 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001291}
1292
1293void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1294 LocationSummary* locations =
1295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1296 locations->SetInAt(0, Location::RequiresRegister());
1297 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1298 locations->SetInAt(1, Location::RequiresFpuRegister());
1299 } else {
1300 locations->SetInAt(1, Location::RequiresRegister());
1301 }
1302}
1303
1304void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001305 const FieldInfo& field_info,
1306 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001307 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001308 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001309
1310 Register obj = InputRegisterAt(instruction, 0);
1311 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001312 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001313 Offset offset = field_info.GetFieldOffset();
1314 Primitive::Type field_type = field_info.GetFieldType();
1315 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1316
Roland Levillain4d027112015-07-01 15:41:14 +01001317 {
1318 // We use a block to end the scratch scope before the write barrier, thus
1319 // freeing the temporary registers so they can be used in `MarkGCCard`.
1320 UseScratchRegisterScope temps(GetVIXLAssembler());
1321
1322 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1323 DCHECK(value.IsW());
1324 Register temp = temps.AcquireW();
1325 __ Mov(temp, value.W());
1326 GetAssembler()->PoisonHeapReference(temp.W());
1327 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001328 }
Roland Levillain4d027112015-07-01 15:41:14 +01001329
1330 if (field_info.IsVolatile()) {
1331 if (use_acquire_release) {
1332 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1333 codegen_->MaybeRecordImplicitNullCheck(instruction);
1334 } else {
1335 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1336 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1337 codegen_->MaybeRecordImplicitNullCheck(instruction);
1338 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1339 }
1340 } else {
1341 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1342 codegen_->MaybeRecordImplicitNullCheck(instruction);
1343 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001344 }
1345
1346 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001347 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001348 }
1349}
1350
Alexandre Rames67555f72014-11-18 10:55:16 +00001351void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001353
1354 switch (type) {
1355 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001356 case Primitive::kPrimLong: {
1357 Register dst = OutputRegister(instr);
1358 Register lhs = InputRegisterAt(instr, 0);
1359 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 if (instr->IsAdd()) {
1361 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001362 } else if (instr->IsAnd()) {
1363 __ And(dst, lhs, rhs);
1364 } else if (instr->IsOr()) {
1365 __ Orr(dst, lhs, rhs);
1366 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001367 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001368 } else {
1369 DCHECK(instr->IsXor());
1370 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001371 }
1372 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001373 }
1374 case Primitive::kPrimFloat:
1375 case Primitive::kPrimDouble: {
1376 FPRegister dst = OutputFPRegister(instr);
1377 FPRegister lhs = InputFPRegisterAt(instr, 0);
1378 FPRegister rhs = InputFPRegisterAt(instr, 1);
1379 if (instr->IsAdd()) {
1380 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001381 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001382 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001383 } else {
1384 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001385 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001386 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001387 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001388 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001389 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001390 }
1391}
1392
Serban Constantinescu02164b32014-11-13 14:05:07 +00001393void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1394 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1395
1396 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1397 Primitive::Type type = instr->GetResultType();
1398 switch (type) {
1399 case Primitive::kPrimInt:
1400 case Primitive::kPrimLong: {
1401 locations->SetInAt(0, Location::RequiresRegister());
1402 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1403 locations->SetOut(Location::RequiresRegister());
1404 break;
1405 }
1406 default:
1407 LOG(FATAL) << "Unexpected shift type " << type;
1408 }
1409}
1410
1411void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1412 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1413
1414 Primitive::Type type = instr->GetType();
1415 switch (type) {
1416 case Primitive::kPrimInt:
1417 case Primitive::kPrimLong: {
1418 Register dst = OutputRegister(instr);
1419 Register lhs = InputRegisterAt(instr, 0);
1420 Operand rhs = InputOperandAt(instr, 1);
1421 if (rhs.IsImmediate()) {
1422 uint32_t shift_value = (type == Primitive::kPrimInt)
1423 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1424 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1425 if (instr->IsShl()) {
1426 __ Lsl(dst, lhs, shift_value);
1427 } else if (instr->IsShr()) {
1428 __ Asr(dst, lhs, shift_value);
1429 } else {
1430 __ Lsr(dst, lhs, shift_value);
1431 }
1432 } else {
1433 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1434
1435 if (instr->IsShl()) {
1436 __ Lsl(dst, lhs, rhs_reg);
1437 } else if (instr->IsShr()) {
1438 __ Asr(dst, lhs, rhs_reg);
1439 } else {
1440 __ Lsr(dst, lhs, rhs_reg);
1441 }
1442 }
1443 break;
1444 }
1445 default:
1446 LOG(FATAL) << "Unexpected shift operation type " << type;
1447 }
1448}
1449
Alexandre Rames5319def2014-10-23 10:03:10 +01001450void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001451 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001452}
1453
1454void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001455 HandleBinaryOp(instruction);
1456}
1457
1458void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1459 HandleBinaryOp(instruction);
1460}
1461
1462void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1463 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001464}
1465
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001466void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1467 LocationSummary* locations =
1468 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1469 locations->SetInAt(0, Location::RequiresRegister());
1470 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001471 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1472 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1473 } else {
1474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1475 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001476}
1477
1478void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1479 LocationSummary* locations = instruction->GetLocations();
1480 Primitive::Type type = instruction->GetType();
1481 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001482 Location index = locations->InAt(1);
1483 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001484 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001485 MacroAssembler* masm = GetVIXLAssembler();
1486 UseScratchRegisterScope temps(masm);
1487 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001488
1489 if (index.IsConstant()) {
1490 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001491 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001492 } else {
1493 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001494 __ Add(temp, obj, offset);
1495 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001496 }
1497
Alexandre Rames67555f72014-11-18 10:55:16 +00001498 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001499 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001500
1501 if (type == Primitive::kPrimNot) {
1502 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1503 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001504}
1505
Alexandre Rames5319def2014-10-23 10:03:10 +01001506void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1508 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001510}
1511
1512void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001513 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001514 __ Ldr(OutputRegister(instruction),
1515 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001517}
1518
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001519void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001520 if (instruction->NeedsTypeCheck()) {
1521 LocationSummary* locations =
1522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001523 InvokeRuntimeCallingConvention calling_convention;
1524 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1525 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1526 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1527 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001528 LocationSummary* locations =
1529 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001530 locations->SetInAt(0, Location::RequiresRegister());
1531 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001532 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1533 locations->SetInAt(2, Location::RequiresFpuRegister());
1534 } else {
1535 locations->SetInAt(2, Location::RequiresRegister());
1536 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001537 }
1538}
1539
1540void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1541 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001542 LocationSummary* locations = instruction->GetLocations();
1543 bool needs_runtime_call = locations->WillCall();
1544
1545 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001546 // Note: if heap poisoning is enabled, pAputObject takes cares
1547 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001548 codegen_->InvokeRuntime(
1549 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001550 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001551 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001552 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001553 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001554 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001555 Location index = locations->InAt(1);
1556 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001557 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001558 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001559 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001560 {
1561 // We use a block to end the scratch scope before the write barrier, thus
1562 // freeing the temporary registers so they can be used in `MarkGCCard`.
1563 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001564
Roland Levillain4d027112015-07-01 15:41:14 +01001565 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1566 DCHECK(value.IsW());
1567 Register temp = temps.AcquireW();
1568 __ Mov(temp, value.W());
1569 GetAssembler()->PoisonHeapReference(temp.W());
1570 source = temp;
1571 }
1572
Alexandre Rames97833a02015-04-16 15:07:12 +01001573 if (index.IsConstant()) {
1574 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1575 destination = HeapOperand(obj, offset);
1576 } else {
1577 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001578 __ Add(temp, obj, offset);
1579 destination = HeapOperand(temp,
1580 XRegisterFrom(index),
1581 LSL,
1582 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001583 }
1584
Roland Levillain4d027112015-07-01 15:41:14 +01001585 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001586 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001587 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001588 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001589 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001590 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001591 }
1592}
1593
Alexandre Rames67555f72014-11-18 10:55:16 +00001594void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1595 LocationSummary* locations =
1596 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1597 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001598 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001599 if (instruction->HasUses()) {
1600 locations->SetOut(Location::SameAsFirstInput());
1601 }
1602}
1603
1604void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001605 LocationSummary* locations = instruction->GetLocations();
1606 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1607 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001608 codegen_->AddSlowPath(slow_path);
1609
1610 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1611 __ B(slow_path->GetEntryLabel(), hs);
1612}
1613
1614void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1616 instruction, LocationSummary::kCallOnSlowPath);
1617 locations->SetInAt(0, Location::RequiresRegister());
1618 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001619 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001620}
1621
1622void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001623 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001624 Register obj = InputRegisterAt(instruction, 0);;
1625 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001626 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001627
Alexandre Rames3e69f162014-12-10 10:36:50 +00001628 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1629 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001630 codegen_->AddSlowPath(slow_path);
1631
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001632 // Avoid null check if we know obj is not null.
1633 if (instruction->MustDoNullCheck()) {
1634 __ Cbz(obj, slow_path->GetExitLabel());
1635 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001636 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001637 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001638 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001639 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001640 // The checkcast succeeds if the classes are equal (fast path).
1641 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001642 __ B(ne, slow_path->GetEntryLabel());
1643 __ Bind(slow_path->GetExitLabel());
1644}
1645
1646void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1647 LocationSummary* locations =
1648 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1649 locations->SetInAt(0, Location::RequiresRegister());
1650 if (check->HasUses()) {
1651 locations->SetOut(Location::SameAsFirstInput());
1652 }
1653}
1654
1655void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1656 // We assume the class is not null.
1657 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1658 check->GetLoadClass(), check, check->GetDexPc(), true);
1659 codegen_->AddSlowPath(slow_path);
1660 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1661}
1662
Roland Levillain7f63c522015-07-13 15:54:55 +00001663static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1664 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1665 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1666}
1667
Serban Constantinescu02164b32014-11-13 14:05:07 +00001668void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001669 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001670 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1671 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001672 switch (in_type) {
1673 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001674 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001675 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1677 break;
1678 }
1679 case Primitive::kPrimFloat:
1680 case Primitive::kPrimDouble: {
1681 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001682 locations->SetInAt(1,
1683 IsFloatingPointZeroConstant(compare->InputAt(1))
1684 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1685 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001686 locations->SetOut(Location::RequiresRegister());
1687 break;
1688 }
1689 default:
1690 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1691 }
1692}
1693
1694void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1695 Primitive::Type in_type = compare->InputAt(0)->GetType();
1696
1697 // 0 if: left == right
1698 // 1 if: left > right
1699 // -1 if: left < right
1700 switch (in_type) {
1701 case Primitive::kPrimLong: {
1702 Register result = OutputRegister(compare);
1703 Register left = InputRegisterAt(compare, 0);
1704 Operand right = InputOperandAt(compare, 1);
1705
1706 __ Cmp(left, right);
1707 __ Cset(result, ne);
1708 __ Cneg(result, result, lt);
1709 break;
1710 }
1711 case Primitive::kPrimFloat:
1712 case Primitive::kPrimDouble: {
1713 Register result = OutputRegister(compare);
1714 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001715 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001716 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1717 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001718 __ Fcmp(left, 0.0);
1719 } else {
1720 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1721 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001722 if (compare->IsGtBias()) {
1723 __ Cset(result, ne);
1724 } else {
1725 __ Csetm(result, ne);
1726 }
1727 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001728 break;
1729 }
1730 default:
1731 LOG(FATAL) << "Unimplemented compare type " << in_type;
1732 }
1733}
1734
1735void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1736 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001737
1738 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1739 locations->SetInAt(0, Location::RequiresFpuRegister());
1740 locations->SetInAt(1,
1741 IsFloatingPointZeroConstant(instruction->InputAt(1))
1742 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1743 : Location::RequiresFpuRegister());
1744 } else {
1745 // Integer cases.
1746 locations->SetInAt(0, Location::RequiresRegister());
1747 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1748 }
1749
Alexandre Rames5319def2014-10-23 10:03:10 +01001750 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001752 }
1753}
1754
1755void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1756 if (!instruction->NeedsMaterialization()) {
1757 return;
1758 }
1759
1760 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001761 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001762 IfCondition if_cond = instruction->GetCondition();
1763 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001764
Roland Levillain7f63c522015-07-13 15:54:55 +00001765 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1766 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1767 if (locations->InAt(1).IsConstant()) {
1768 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1769 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1770 __ Fcmp(lhs, 0.0);
1771 } else {
1772 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1773 }
1774 __ Cset(res, arm64_cond);
1775 if (instruction->IsFPConditionTrueIfNaN()) {
1776 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1777 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1778 } else if (instruction->IsFPConditionFalseIfNaN()) {
1779 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1780 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1781 }
1782 } else {
1783 // Integer cases.
1784 Register lhs = InputRegisterAt(instruction, 0);
1785 Operand rhs = InputOperandAt(instruction, 1);
1786 __ Cmp(lhs, rhs);
1787 __ Cset(res, arm64_cond);
1788 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001789}
1790
1791#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1792 M(Equal) \
1793 M(NotEqual) \
1794 M(LessThan) \
1795 M(LessThanOrEqual) \
1796 M(GreaterThan) \
1797 M(GreaterThanOrEqual)
1798#define DEFINE_CONDITION_VISITORS(Name) \
1799void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1800void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1801FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001802#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001803#undef FOR_EACH_CONDITION_INSTRUCTION
1804
Zheng Xuc6667102015-05-15 16:08:45 +08001805void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1806 DCHECK(instruction->IsDiv() || instruction->IsRem());
1807
1808 LocationSummary* locations = instruction->GetLocations();
1809 Location second = locations->InAt(1);
1810 DCHECK(second.IsConstant());
1811
1812 Register out = OutputRegister(instruction);
1813 Register dividend = InputRegisterAt(instruction, 0);
1814 int64_t imm = Int64FromConstant(second.GetConstant());
1815 DCHECK(imm == 1 || imm == -1);
1816
1817 if (instruction->IsRem()) {
1818 __ Mov(out, 0);
1819 } else {
1820 if (imm == 1) {
1821 __ Mov(out, dividend);
1822 } else {
1823 __ Neg(out, dividend);
1824 }
1825 }
1826}
1827
1828void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1829 DCHECK(instruction->IsDiv() || instruction->IsRem());
1830
1831 LocationSummary* locations = instruction->GetLocations();
1832 Location second = locations->InAt(1);
1833 DCHECK(second.IsConstant());
1834
1835 Register out = OutputRegister(instruction);
1836 Register dividend = InputRegisterAt(instruction, 0);
1837 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001838 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001839 DCHECK(IsPowerOfTwo(abs_imm));
1840 int ctz_imm = CTZ(abs_imm);
1841
1842 UseScratchRegisterScope temps(GetVIXLAssembler());
1843 Register temp = temps.AcquireSameSizeAs(out);
1844
1845 if (instruction->IsDiv()) {
1846 __ Add(temp, dividend, abs_imm - 1);
1847 __ Cmp(dividend, 0);
1848 __ Csel(out, temp, dividend, lt);
1849 if (imm > 0) {
1850 __ Asr(out, out, ctz_imm);
1851 } else {
1852 __ Neg(out, Operand(out, ASR, ctz_imm));
1853 }
1854 } else {
1855 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1856 __ Asr(temp, dividend, bits - 1);
1857 __ Lsr(temp, temp, bits - ctz_imm);
1858 __ Add(out, dividend, temp);
1859 __ And(out, out, abs_imm - 1);
1860 __ Sub(out, out, temp);
1861 }
1862}
1863
1864void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1865 DCHECK(instruction->IsDiv() || instruction->IsRem());
1866
1867 LocationSummary* locations = instruction->GetLocations();
1868 Location second = locations->InAt(1);
1869 DCHECK(second.IsConstant());
1870
1871 Register out = OutputRegister(instruction);
1872 Register dividend = InputRegisterAt(instruction, 0);
1873 int64_t imm = Int64FromConstant(second.GetConstant());
1874
1875 Primitive::Type type = instruction->GetResultType();
1876 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1877
1878 int64_t magic;
1879 int shift;
1880 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1881
1882 UseScratchRegisterScope temps(GetVIXLAssembler());
1883 Register temp = temps.AcquireSameSizeAs(out);
1884
1885 // temp = get_high(dividend * magic)
1886 __ Mov(temp, magic);
1887 if (type == Primitive::kPrimLong) {
1888 __ Smulh(temp, dividend, temp);
1889 } else {
1890 __ Smull(temp.X(), dividend, temp);
1891 __ Lsr(temp.X(), temp.X(), 32);
1892 }
1893
1894 if (imm > 0 && magic < 0) {
1895 __ Add(temp, temp, dividend);
1896 } else if (imm < 0 && magic > 0) {
1897 __ Sub(temp, temp, dividend);
1898 }
1899
1900 if (shift != 0) {
1901 __ Asr(temp, temp, shift);
1902 }
1903
1904 if (instruction->IsDiv()) {
1905 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1906 } else {
1907 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1908 // TODO: Strength reduction for msub.
1909 Register temp_imm = temps.AcquireSameSizeAs(out);
1910 __ Mov(temp_imm, imm);
1911 __ Msub(out, temp, temp_imm, dividend);
1912 }
1913}
1914
1915void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1916 DCHECK(instruction->IsDiv() || instruction->IsRem());
1917 Primitive::Type type = instruction->GetResultType();
1918 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1919
1920 LocationSummary* locations = instruction->GetLocations();
1921 Register out = OutputRegister(instruction);
1922 Location second = locations->InAt(1);
1923
1924 if (second.IsConstant()) {
1925 int64_t imm = Int64FromConstant(second.GetConstant());
1926
1927 if (imm == 0) {
1928 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1929 } else if (imm == 1 || imm == -1) {
1930 DivRemOneOrMinusOne(instruction);
1931 } else if (IsPowerOfTwo(std::abs(imm))) {
1932 DivRemByPowerOfTwo(instruction);
1933 } else {
1934 DCHECK(imm <= -2 || imm >= 2);
1935 GenerateDivRemWithAnyConstant(instruction);
1936 }
1937 } else {
1938 Register dividend = InputRegisterAt(instruction, 0);
1939 Register divisor = InputRegisterAt(instruction, 1);
1940 if (instruction->IsDiv()) {
1941 __ Sdiv(out, dividend, divisor);
1942 } else {
1943 UseScratchRegisterScope temps(GetVIXLAssembler());
1944 Register temp = temps.AcquireSameSizeAs(out);
1945 __ Sdiv(temp, dividend, divisor);
1946 __ Msub(out, temp, divisor, dividend);
1947 }
1948 }
1949}
1950
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001951void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1952 LocationSummary* locations =
1953 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1954 switch (div->GetResultType()) {
1955 case Primitive::kPrimInt:
1956 case Primitive::kPrimLong:
1957 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001958 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1960 break;
1961
1962 case Primitive::kPrimFloat:
1963 case Primitive::kPrimDouble:
1964 locations->SetInAt(0, Location::RequiresFpuRegister());
1965 locations->SetInAt(1, Location::RequiresFpuRegister());
1966 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1967 break;
1968
1969 default:
1970 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1971 }
1972}
1973
1974void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1975 Primitive::Type type = div->GetResultType();
1976 switch (type) {
1977 case Primitive::kPrimInt:
1978 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001979 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001980 break;
1981
1982 case Primitive::kPrimFloat:
1983 case Primitive::kPrimDouble:
1984 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1985 break;
1986
1987 default:
1988 LOG(FATAL) << "Unexpected div type " << type;
1989 }
1990}
1991
Alexandre Rames67555f72014-11-18 10:55:16 +00001992void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1993 LocationSummary* locations =
1994 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1995 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1996 if (instruction->HasUses()) {
1997 locations->SetOut(Location::SameAsFirstInput());
1998 }
1999}
2000
2001void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2002 SlowPathCodeARM64* slow_path =
2003 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2004 codegen_->AddSlowPath(slow_path);
2005 Location value = instruction->GetLocations()->InAt(0);
2006
Alexandre Rames3e69f162014-12-10 10:36:50 +00002007 Primitive::Type type = instruction->GetType();
2008
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002009 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2010 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002011 return;
2012 }
2013
Alexandre Rames67555f72014-11-18 10:55:16 +00002014 if (value.IsConstant()) {
2015 int64_t divisor = Int64ConstantFrom(value);
2016 if (divisor == 0) {
2017 __ B(slow_path->GetEntryLabel());
2018 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002019 // A division by a non-null constant is valid. We don't need to perform
2020 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002021 }
2022 } else {
2023 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2024 }
2025}
2026
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002027void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2028 LocationSummary* locations =
2029 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2030 locations->SetOut(Location::ConstantLocation(constant));
2031}
2032
2033void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2034 UNUSED(constant);
2035 // Will be generated at use site.
2036}
2037
Alexandre Rames5319def2014-10-23 10:03:10 +01002038void LocationsBuilderARM64::VisitExit(HExit* exit) {
2039 exit->SetLocations(nullptr);
2040}
2041
2042void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002043 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002044}
2045
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002046void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2047 LocationSummary* locations =
2048 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2049 locations->SetOut(Location::ConstantLocation(constant));
2050}
2051
2052void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2053 UNUSED(constant);
2054 // Will be generated at use site.
2055}
2056
David Brazdilfc6a86a2015-06-26 10:33:45 +00002057void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002058 DCHECK(!successor->IsExitBlock());
2059 HBasicBlock* block = got->GetBlock();
2060 HInstruction* previous = got->GetPrevious();
2061 HLoopInformation* info = block->GetLoopInformation();
2062
David Brazdil46e2a392015-03-16 17:31:52 +00002063 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002064 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2065 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2066 return;
2067 }
2068 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2069 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2070 }
2071 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002072 __ B(codegen_->GetLabelOf(successor));
2073 }
2074}
2075
David Brazdilfc6a86a2015-06-26 10:33:45 +00002076void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2077 got->SetLocations(nullptr);
2078}
2079
2080void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2081 HandleGoto(got, got->GetSuccessor());
2082}
2083
2084void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2085 try_boundary->SetLocations(nullptr);
2086}
2087
2088void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2089 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2090 if (!successor->IsExitBlock()) {
2091 HandleGoto(try_boundary, successor);
2092 }
2093}
2094
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002095void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2096 vixl::Label* true_target,
2097 vixl::Label* false_target,
2098 vixl::Label* always_true_target) {
2099 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002100 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002101
Serban Constantinescu02164b32014-11-13 14:05:07 +00002102 if (cond->IsIntConstant()) {
2103 int32_t cond_value = cond->AsIntConstant()->GetValue();
2104 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002105 if (always_true_target != nullptr) {
2106 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002107 }
2108 return;
2109 } else {
2110 DCHECK_EQ(cond_value, 0);
2111 }
2112 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002113 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002114 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002115 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002116 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002117 } else {
2118 // The condition instruction has not been materialized, use its inputs as
2119 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002120 Primitive::Type type =
2121 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2122
2123 if (Primitive::IsFloatingPointType(type)) {
2124 // FP compares don't like null false_targets.
2125 if (false_target == nullptr) {
2126 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002127 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002128 FPRegister lhs = InputFPRegisterAt(condition, 0);
2129 if (condition->GetLocations()->InAt(1).IsConstant()) {
2130 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2131 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2132 __ Fcmp(lhs, 0.0);
2133 } else {
2134 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2135 }
2136 if (condition->IsFPConditionTrueIfNaN()) {
2137 __ B(vs, true_target); // VS for unordered.
2138 } else if (condition->IsFPConditionFalseIfNaN()) {
2139 __ B(vs, false_target); // VS for unordered.
2140 }
2141 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002142 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002143 // Integer cases.
2144 Register lhs = InputRegisterAt(condition, 0);
2145 Operand rhs = InputOperandAt(condition, 1);
2146 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2147 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2148 switch (arm64_cond) {
2149 case eq:
2150 __ Cbz(lhs, true_target);
2151 break;
2152 case ne:
2153 __ Cbnz(lhs, true_target);
2154 break;
2155 case lt:
2156 // Test the sign bit and branch accordingly.
2157 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2158 break;
2159 case ge:
2160 // Test the sign bit and branch accordingly.
2161 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2162 break;
2163 default:
2164 // Without the `static_cast` the compiler throws an error for
2165 // `-Werror=sign-promo`.
2166 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2167 }
2168 } else {
2169 __ Cmp(lhs, rhs);
2170 __ B(arm64_cond, true_target);
2171 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 }
2173 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002174 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002175 __ B(false_target);
2176 }
2177}
2178
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002179void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2180 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2181 HInstruction* cond = if_instr->InputAt(0);
2182 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2183 locations->SetInAt(0, Location::RequiresRegister());
2184 }
2185}
2186
2187void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2188 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2189 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2190 vixl::Label* always_true_target = true_target;
2191 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2192 if_instr->IfTrueSuccessor())) {
2193 always_true_target = nullptr;
2194 }
2195 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2196 if_instr->IfFalseSuccessor())) {
2197 false_target = nullptr;
2198 }
2199 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2200}
2201
2202void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2203 LocationSummary* locations = new (GetGraph()->GetArena())
2204 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2205 HInstruction* cond = deoptimize->InputAt(0);
2206 DCHECK(cond->IsCondition());
2207 if (cond->AsCondition()->NeedsMaterialization()) {
2208 locations->SetInAt(0, Location::RequiresRegister());
2209 }
2210}
2211
2212void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2213 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2214 DeoptimizationSlowPathARM64(deoptimize);
2215 codegen_->AddSlowPath(slow_path);
2216 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2217 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2218}
2219
Alexandre Rames5319def2014-10-23 10:03:10 +01002220void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002221 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002222}
2223
2224void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002225 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002226}
2227
2228void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002229 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002230}
2231
2232void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002233 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002234}
2235
Alexandre Rames67555f72014-11-18 10:55:16 +00002236void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2237 LocationSummary::CallKind call_kind =
2238 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2239 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2240 locations->SetInAt(0, Location::RequiresRegister());
2241 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002242 // The output does overlap inputs.
2243 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002244}
2245
2246void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2247 LocationSummary* locations = instruction->GetLocations();
2248 Register obj = InputRegisterAt(instruction, 0);;
2249 Register cls = InputRegisterAt(instruction, 1);;
2250 Register out = OutputRegister(instruction);
2251
2252 vixl::Label done;
2253
2254 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002255 // Avoid null check if we know `obj` is not null.
2256 if (instruction->MustDoNullCheck()) {
2257 __ Mov(out, 0);
2258 __ Cbz(obj, &done);
2259 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002260
2261 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002262 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002263 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002264 __ Cmp(out, cls);
2265 if (instruction->IsClassFinal()) {
2266 // Classes must be equal for the instanceof to succeed.
2267 __ Cset(out, eq);
2268 } else {
2269 // If the classes are not equal, we go into a slow path.
2270 DCHECK(locations->OnlyCallsOnSlowPath());
2271 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002272 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2273 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002274 codegen_->AddSlowPath(slow_path);
2275 __ B(ne, slow_path->GetEntryLabel());
2276 __ Mov(out, 1);
2277 __ Bind(slow_path->GetExitLabel());
2278 }
2279
2280 __ Bind(&done);
2281}
2282
Alexandre Rames5319def2014-10-23 10:03:10 +01002283void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2285 locations->SetOut(Location::ConstantLocation(constant));
2286}
2287
2288void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2289 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002290 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002291}
2292
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002293void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2294 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2295 locations->SetOut(Location::ConstantLocation(constant));
2296}
2297
2298void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2299 // Will be generated at use site.
2300 UNUSED(constant);
2301}
2302
Alexandre Rames5319def2014-10-23 10:03:10 +01002303void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002304 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002305 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002306}
2307
Alexandre Rames67555f72014-11-18 10:55:16 +00002308void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2309 HandleInvoke(invoke);
2310}
2311
2312void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2313 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002314 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2315 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2316 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002317 Location receiver = invoke->GetLocations()->InAt(0);
2318 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002319 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002320
2321 // The register ip1 is required to be used for the hidden argument in
2322 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002323 MacroAssembler* masm = GetVIXLAssembler();
2324 UseScratchRegisterScope scratch_scope(masm);
2325 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002326 scratch_scope.Exclude(ip1);
2327 __ Mov(ip1, invoke->GetDexMethodIndex());
2328
2329 // temp = object->GetClass();
2330 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002331 __ Ldr(temp.W(), StackOperandFrom(receiver));
2332 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002333 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002334 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002335 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002336 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002337 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002338 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002339 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002340 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002341 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002342 // lr();
2343 __ Blr(lr);
2344 DCHECK(!codegen_->IsLeafMethod());
2345 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2346}
2347
2348void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002349 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2350 if (intrinsic.TryDispatch(invoke)) {
2351 return;
2352 }
2353
Alexandre Rames67555f72014-11-18 10:55:16 +00002354 HandleInvoke(invoke);
2355}
2356
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002357void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002358 // When we do not run baseline, explicit clinit checks triggered by static
2359 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2360 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002361
Andreas Gampe878d58c2015-01-15 23:24:00 -08002362 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2363 if (intrinsic.TryDispatch(invoke)) {
2364 return;
2365 }
2366
Alexandre Rames67555f72014-11-18 10:55:16 +00002367 HandleInvoke(invoke);
2368}
2369
Andreas Gampe878d58c2015-01-15 23:24:00 -08002370static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2371 if (invoke->GetLocations()->Intrinsified()) {
2372 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2373 intrinsic.Dispatch(invoke);
2374 return true;
2375 }
2376 return false;
2377}
2378
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002379void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko9b688a02015-05-06 14:12:42 +01002380 // For better instruction scheduling we load the direct code pointer before the method pointer.
2381 bool direct_code_loaded = false;
2382 switch (invoke->GetCodePtrLocation()) {
2383 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2384 // LR = code address from literal pool with link-time patch.
2385 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2386 direct_code_loaded = true;
2387 break;
2388 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2389 // LR = invoke->GetDirectCodePtr();
2390 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2391 direct_code_loaded = true;
2392 break;
2393 default:
2394 break;
2395 }
2396
Andreas Gampe878d58c2015-01-15 23:24:00 -08002397 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko9b688a02015-05-06 14:12:42 +01002398 switch (invoke->GetMethodLoadKind()) {
2399 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2400 // temp = thread->string_init_entrypoint
2401 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2402 break;
2403 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2404 // Nothing to do.
2405 break;
2406 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2407 // Load method address from literal pool.
2408 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2409 break;
2410 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2411 // Load method address from literal pool with a link-time patch.
2412 __ Ldr(XRegisterFrom(temp).X(),
2413 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2414 break;
2415 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2416 // Add ADRP with its PC-relative DexCache access patch.
2417 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2418 invoke->GetDexCacheArrayOffset());
2419 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2420 {
2421 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2422 __ adrp(XRegisterFrom(temp).X(), 0);
2423 }
2424 __ Bind(pc_insn_label); // Bind after ADRP.
2425 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2426 // Add LDR with its PC-relative DexCache access patch.
2427 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2428 invoke->GetDexCacheArrayOffset());
2429 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2430 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2431 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2432 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002433 }
Vladimir Marko9b688a02015-05-06 14:12:42 +01002434 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2435 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2436 Register reg = XRegisterFrom(temp);
2437 Register method_reg;
2438 if (current_method.IsRegister()) {
2439 method_reg = XRegisterFrom(current_method);
2440 } else {
2441 DCHECK(invoke->GetLocations()->Intrinsified());
2442 DCHECK(!current_method.IsValid());
2443 method_reg = reg;
2444 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2445 }
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002446
Vladimir Marko9b688a02015-05-06 14:12:42 +01002447 // temp = current_method->dex_cache_resolved_methods_;
2448 __ Ldr(reg.W(), MemOperand(method_reg.X(),
2449 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2450 // temp = temp[index_in_cache];
2451 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2452 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2453 break;
2454 }
2455 }
2456
2457 switch (invoke->GetCodePtrLocation()) {
2458 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2459 __ Bl(&frame_entry_label_);
2460 break;
2461 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2462 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2463 vixl::Label* label = &relative_call_patches_.back().label;
2464 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2465 __ Bind(label); // Bind after BL.
2466 break;
2467 }
2468 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2469 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2470 // LR prepared above for better instruction scheduling.
2471 DCHECK(direct_code_loaded);
2472 // lr()
2473 __ Blr(lr);
2474 break;
2475 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2476 // LR = temp->entry_point_from_quick_compiled_code_;
2477 __ Ldr(lr, MemOperand(
2478 XRegisterFrom(temp).X(),
2479 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2480 // lr()
2481 __ Blr(lr);
2482 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002483 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002484
Andreas Gampe878d58c2015-01-15 23:24:00 -08002485 DCHECK(!IsLeafMethod());
2486}
2487
Vladimir Marko9b688a02015-05-06 14:12:42 +01002488void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2489 DCHECK(linker_patches->empty());
2490 size_t size =
2491 method_patches_.size() +
2492 call_patches_.size() +
2493 relative_call_patches_.size() +
2494 pc_rel_dex_cache_patches_.size();
2495 linker_patches->reserve(size);
2496 for (const auto& entry : method_patches_) {
2497 const MethodReference& target_method = entry.first;
2498 vixl::Literal<uint64_t>* literal = entry.second;
2499 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2500 target_method.dex_file,
2501 target_method.dex_method_index));
2502 }
2503 for (const auto& entry : call_patches_) {
2504 const MethodReference& target_method = entry.first;
2505 vixl::Literal<uint64_t>* literal = entry.second;
2506 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2507 target_method.dex_file,
2508 target_method.dex_method_index));
2509 }
2510 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2511 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2512 info.target_method.dex_file,
2513 info.target_method.dex_method_index));
2514 }
2515 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2516 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2517 &info.target_dex_file,
2518 info.pc_insn_label->location() - 4u,
2519 info.element_offset));
2520 }
2521}
2522
2523vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2524 // Look up the literal for value.
2525 auto lb = uint64_literals_.lower_bound(value);
2526 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2527 return lb->second;
2528 }
2529 // We don't have a literal for this value, insert a new one.
2530 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2531 uint64_literals_.PutBefore(lb, value, literal);
2532 return literal;
2533}
2534
2535vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2536 MethodReference target_method,
2537 MethodToLiteralMap* map) {
2538 // Look up the literal for target_method.
2539 auto lb = map->lower_bound(target_method);
2540 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2541 return lb->second;
2542 }
2543 // We don't have a literal for this method yet, insert a new one.
2544 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2545 map->PutBefore(lb, target_method, literal);
2546 return literal;
2547}
2548
2549vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2550 MethodReference target_method) {
2551 return DeduplicateMethodLiteral(target_method, &method_patches_);
2552}
2553
2554vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2555 MethodReference target_method) {
2556 return DeduplicateMethodLiteral(target_method, &call_patches_);
2557}
2558
2559
Andreas Gampe878d58c2015-01-15 23:24:00 -08002560void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002561 // When we do not run baseline, explicit clinit checks triggered by static
2562 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2563 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002564
Andreas Gampe878d58c2015-01-15 23:24:00 -08002565 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2566 return;
2567 }
2568
Alexandre Ramesd921d642015-04-16 15:07:16 +01002569 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002570 LocationSummary* locations = invoke->GetLocations();
2571 codegen_->GenerateStaticOrDirectCall(
2572 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002573 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002574}
2575
2576void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002577 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2578 return;
2579 }
2580
Alexandre Rames5319def2014-10-23 10:03:10 +01002581 LocationSummary* locations = invoke->GetLocations();
2582 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002583 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2584 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2585 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002586 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002587 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002588
Alexandre Ramesd921d642015-04-16 15:07:16 +01002589 BlockPoolsScope block_pools(GetVIXLAssembler());
2590
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002591 DCHECK(receiver.IsRegister());
2592 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002593 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002594 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002595 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002596 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002597 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002598 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002599 // lr();
2600 __ Blr(lr);
2601 DCHECK(!codegen_->IsLeafMethod());
2602 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2603}
2604
Alexandre Rames67555f72014-11-18 10:55:16 +00002605void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2606 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2607 : LocationSummary::kNoCall;
2608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002609 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002610 locations->SetOut(Location::RequiresRegister());
2611}
2612
2613void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2614 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002615 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002616 if (cls->IsReferrersClass()) {
2617 DCHECK(!cls->CanCallRuntime());
2618 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002619 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002620 } else {
2621 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002622 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002623 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002624 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002625
2626 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2627 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2628 codegen_->AddSlowPath(slow_path);
2629 __ Cbz(out, slow_path->GetEntryLabel());
2630 if (cls->MustGenerateClinitCheck()) {
2631 GenerateClassInitializationCheck(slow_path, out);
2632 } else {
2633 __ Bind(slow_path->GetExitLabel());
2634 }
2635 }
2636}
2637
David Brazdilcb1c0552015-08-04 16:22:25 +01002638static MemOperand GetExceptionTlsAddress() {
2639 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2640}
2641
Alexandre Rames67555f72014-11-18 10:55:16 +00002642void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2643 LocationSummary* locations =
2644 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2645 locations->SetOut(Location::RequiresRegister());
2646}
2647
2648void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002649 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2650}
2651
2652void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2653 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2654}
2655
2656void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2657 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002658}
2659
Alexandre Rames5319def2014-10-23 10:03:10 +01002660void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2661 load->SetLocations(nullptr);
2662}
2663
2664void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2665 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002666 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002667}
2668
Alexandre Rames67555f72014-11-18 10:55:16 +00002669void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2670 LocationSummary* locations =
2671 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002672 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002673 locations->SetOut(Location::RequiresRegister());
2674}
2675
2676void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2677 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2678 codegen_->AddSlowPath(slow_path);
2679
2680 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002681 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002682 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002683 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002684 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002685 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002686 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002687 __ Cbz(out, slow_path->GetEntryLabel());
2688 __ Bind(slow_path->GetExitLabel());
2689}
2690
Alexandre Rames5319def2014-10-23 10:03:10 +01002691void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2692 local->SetLocations(nullptr);
2693}
2694
2695void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2696 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2697}
2698
2699void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2701 locations->SetOut(Location::ConstantLocation(constant));
2702}
2703
2704void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2705 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002706 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002707}
2708
Alexandre Rames67555f72014-11-18 10:55:16 +00002709void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2710 LocationSummary* locations =
2711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2712 InvokeRuntimeCallingConvention calling_convention;
2713 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2714}
2715
2716void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2717 codegen_->InvokeRuntime(instruction->IsEnter()
2718 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2719 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002720 instruction->GetDexPc(),
2721 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002722 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002723}
2724
Alexandre Rames42d641b2014-10-27 14:00:51 +00002725void LocationsBuilderARM64::VisitMul(HMul* mul) {
2726 LocationSummary* locations =
2727 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2728 switch (mul->GetResultType()) {
2729 case Primitive::kPrimInt:
2730 case Primitive::kPrimLong:
2731 locations->SetInAt(0, Location::RequiresRegister());
2732 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002734 break;
2735
2736 case Primitive::kPrimFloat:
2737 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002738 locations->SetInAt(0, Location::RequiresFpuRegister());
2739 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002740 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002741 break;
2742
2743 default:
2744 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2745 }
2746}
2747
2748void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2749 switch (mul->GetResultType()) {
2750 case Primitive::kPrimInt:
2751 case Primitive::kPrimLong:
2752 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2753 break;
2754
2755 case Primitive::kPrimFloat:
2756 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002757 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002758 break;
2759
2760 default:
2761 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2762 }
2763}
2764
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002765void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2766 LocationSummary* locations =
2767 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2768 switch (neg->GetResultType()) {
2769 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002770 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002771 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002773 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002774
2775 case Primitive::kPrimFloat:
2776 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002777 locations->SetInAt(0, Location::RequiresFpuRegister());
2778 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002779 break;
2780
2781 default:
2782 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2783 }
2784}
2785
2786void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2787 switch (neg->GetResultType()) {
2788 case Primitive::kPrimInt:
2789 case Primitive::kPrimLong:
2790 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2791 break;
2792
2793 case Primitive::kPrimFloat:
2794 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002795 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002796 break;
2797
2798 default:
2799 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2800 }
2801}
2802
2803void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2804 LocationSummary* locations =
2805 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2806 InvokeRuntimeCallingConvention calling_convention;
2807 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002808 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002809 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002810 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002811 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002812 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002813}
2814
2815void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2816 LocationSummary* locations = instruction->GetLocations();
2817 InvokeRuntimeCallingConvention calling_convention;
2818 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2819 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002820 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002821 // Note: if heap poisoning is enabled, the entry point takes cares
2822 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002823 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002824 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2825 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002826 instruction->GetDexPc(),
2827 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002828 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002829}
2830
Alexandre Rames5319def2014-10-23 10:03:10 +01002831void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2832 LocationSummary* locations =
2833 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2834 InvokeRuntimeCallingConvention calling_convention;
2835 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002836 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002837 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002838 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002839}
2840
2841void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2842 LocationSummary* locations = instruction->GetLocations();
2843 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2844 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002845 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002846 // Note: if heap poisoning is enabled, the entry point takes cares
2847 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002848 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002849 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2850 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002851 instruction->GetDexPc(),
2852 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002853 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002854}
2855
2856void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002858 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002860}
2861
2862void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002863 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002864 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002865 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002866 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002867 break;
2868
2869 default:
2870 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2871 }
2872}
2873
David Brazdil66d126e2015-04-03 16:02:44 +01002874void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2876 locations->SetInAt(0, Location::RequiresRegister());
2877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2878}
2879
2880void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002881 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2882}
2883
Alexandre Rames5319def2014-10-23 10:03:10 +01002884void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2885 LocationSummary* locations =
2886 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2887 locations->SetInAt(0, Location::RequiresRegister());
2888 if (instruction->HasUses()) {
2889 locations->SetOut(Location::SameAsFirstInput());
2890 }
2891}
2892
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002893void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002894 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2895 return;
2896 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002897
Alexandre Ramesd921d642015-04-16 15:07:16 +01002898 BlockPoolsScope block_pools(GetVIXLAssembler());
2899 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002900 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2901 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2902}
2903
2904void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002905 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2906 codegen_->AddSlowPath(slow_path);
2907
2908 LocationSummary* locations = instruction->GetLocations();
2909 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002910
2911 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002912}
2913
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002914void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2915 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2916 GenerateImplicitNullCheck(instruction);
2917 } else {
2918 GenerateExplicitNullCheck(instruction);
2919 }
2920}
2921
Alexandre Rames67555f72014-11-18 10:55:16 +00002922void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2923 HandleBinaryOp(instruction);
2924}
2925
2926void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2927 HandleBinaryOp(instruction);
2928}
2929
Alexandre Rames3e69f162014-12-10 10:36:50 +00002930void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2931 LOG(FATAL) << "Unreachable";
2932}
2933
2934void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2935 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2936}
2937
Alexandre Rames5319def2014-10-23 10:03:10 +01002938void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2939 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2940 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2941 if (location.IsStackSlot()) {
2942 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2943 } else if (location.IsDoubleStackSlot()) {
2944 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2945 }
2946 locations->SetOut(location);
2947}
2948
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002949void InstructionCodeGeneratorARM64::VisitParameterValue(
2950 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002951 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002952}
2953
2954void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2955 LocationSummary* locations =
2956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002957 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002958}
2959
2960void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2961 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2962 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002963}
2964
2965void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2967 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2968 locations->SetInAt(i, Location::Any());
2969 }
2970 locations->SetOut(Location::Any());
2971}
2972
2973void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002974 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002975 LOG(FATAL) << "Unreachable";
2976}
2977
Serban Constantinescu02164b32014-11-13 14:05:07 +00002978void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002979 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002980 LocationSummary::CallKind call_kind =
2981 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2983
2984 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002985 case Primitive::kPrimInt:
2986 case Primitive::kPrimLong:
2987 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002988 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2990 break;
2991
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002992 case Primitive::kPrimFloat:
2993 case Primitive::kPrimDouble: {
2994 InvokeRuntimeCallingConvention calling_convention;
2995 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2996 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2997 locations->SetOut(calling_convention.GetReturnLocation(type));
2998
2999 break;
3000 }
3001
Serban Constantinescu02164b32014-11-13 14:05:07 +00003002 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003003 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003004 }
3005}
3006
3007void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3008 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003009
Serban Constantinescu02164b32014-11-13 14:05:07 +00003010 switch (type) {
3011 case Primitive::kPrimInt:
3012 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003013 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003014 break;
3015 }
3016
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003017 case Primitive::kPrimFloat:
3018 case Primitive::kPrimDouble: {
3019 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3020 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003021 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003022 break;
3023 }
3024
Serban Constantinescu02164b32014-11-13 14:05:07 +00003025 default:
3026 LOG(FATAL) << "Unexpected rem type " << type;
3027 }
3028}
3029
Calin Juravle27df7582015-04-17 19:12:31 +01003030void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3031 memory_barrier->SetLocations(nullptr);
3032}
3033
3034void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3035 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3036}
3037
Alexandre Rames5319def2014-10-23 10:03:10 +01003038void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3040 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003041 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003042}
3043
3044void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003045 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003046 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003047}
3048
3049void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3050 instruction->SetLocations(nullptr);
3051}
3052
3053void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003054 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003055 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003056}
3057
Serban Constantinescu02164b32014-11-13 14:05:07 +00003058void LocationsBuilderARM64::VisitShl(HShl* shl) {
3059 HandleShift(shl);
3060}
3061
3062void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3063 HandleShift(shl);
3064}
3065
3066void LocationsBuilderARM64::VisitShr(HShr* shr) {
3067 HandleShift(shr);
3068}
3069
3070void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3071 HandleShift(shr);
3072}
3073
Alexandre Rames5319def2014-10-23 10:03:10 +01003074void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3076 Primitive::Type field_type = store->InputAt(1)->GetType();
3077 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003078 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003079 case Primitive::kPrimBoolean:
3080 case Primitive::kPrimByte:
3081 case Primitive::kPrimChar:
3082 case Primitive::kPrimShort:
3083 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003084 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003085 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3086 break;
3087
3088 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003089 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003090 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3091 break;
3092
3093 default:
3094 LOG(FATAL) << "Unimplemented local type " << field_type;
3095 }
3096}
3097
3098void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003099 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003100}
3101
3102void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003103 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003104}
3105
3106void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003107 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003108}
3109
Alexandre Rames67555f72014-11-18 10:55:16 +00003110void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003111 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003112}
3113
3114void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003115 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003116}
3117
3118void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003119 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003120}
3121
Alexandre Rames67555f72014-11-18 10:55:16 +00003122void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003123 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003124}
3125
3126void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3127 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3128}
3129
3130void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003131 HBasicBlock* block = instruction->GetBlock();
3132 if (block->GetLoopInformation() != nullptr) {
3133 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3134 // The back edge will generate the suspend check.
3135 return;
3136 }
3137 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3138 // The goto will generate the suspend check.
3139 return;
3140 }
3141 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003142}
3143
3144void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3145 temp->SetLocations(nullptr);
3146}
3147
3148void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3149 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003150 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003151}
3152
Alexandre Rames67555f72014-11-18 10:55:16 +00003153void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3154 LocationSummary* locations =
3155 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3156 InvokeRuntimeCallingConvention calling_convention;
3157 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3158}
3159
3160void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3161 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003162 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003163 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003164}
3165
3166void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3167 LocationSummary* locations =
3168 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3169 Primitive::Type input_type = conversion->GetInputType();
3170 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003171 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003172 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3173 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3174 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3175 }
3176
Alexandre Rames542361f2015-01-29 16:57:31 +00003177 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003178 locations->SetInAt(0, Location::RequiresFpuRegister());
3179 } else {
3180 locations->SetInAt(0, Location::RequiresRegister());
3181 }
3182
Alexandre Rames542361f2015-01-29 16:57:31 +00003183 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003184 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3185 } else {
3186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3187 }
3188}
3189
3190void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3191 Primitive::Type result_type = conversion->GetResultType();
3192 Primitive::Type input_type = conversion->GetInputType();
3193
3194 DCHECK_NE(input_type, result_type);
3195
Alexandre Rames542361f2015-01-29 16:57:31 +00003196 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003197 int result_size = Primitive::ComponentSize(result_type);
3198 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003199 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003200 Register output = OutputRegister(conversion);
3201 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003202 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3203 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3204 } else if ((result_type == Primitive::kPrimChar) ||
3205 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3206 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003207 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003208 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003209 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003210 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003211 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003212 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003213 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3214 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003215 } else if (Primitive::IsFloatingPointType(result_type) &&
3216 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003217 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3218 } else {
3219 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3220 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003221 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003222}
Alexandre Rames67555f72014-11-18 10:55:16 +00003223
Serban Constantinescu02164b32014-11-13 14:05:07 +00003224void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3225 HandleShift(ushr);
3226}
3227
3228void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3229 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003230}
3231
3232void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3233 HandleBinaryOp(instruction);
3234}
3235
3236void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3237 HandleBinaryOp(instruction);
3238}
3239
Calin Juravleb1498f62015-02-16 13:13:29 +00003240void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3241 // Nothing to do, this should be removed during prepare for register allocator.
3242 UNUSED(instruction);
3243 LOG(FATAL) << "Unreachable";
3244}
3245
3246void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3247 // Nothing to do, this should be removed during prepare for register allocator.
3248 UNUSED(instruction);
3249 LOG(FATAL) << "Unreachable";
3250}
3251
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003252void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3253 DCHECK(codegen_->IsBaseline());
3254 LocationSummary* locations =
3255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3256 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3257}
3258
3259void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3260 DCHECK(codegen_->IsBaseline());
3261 // Will be generated at use site.
3262}
3263
Alexandre Rames67555f72014-11-18 10:55:16 +00003264#undef __
3265#undef QUICK_ENTRY_POINT
3266
Alexandre Rames5319def2014-10-23 10:03:10 +01003267} // namespace arm64
3268} // namespace art