blob: f76ba4be67f20de36c477df424b98215efe9a896 [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"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
Roland Levillain22ccc3a2015-11-24 13:10:05 +000045template<class MirrorType>
46class GcRoot;
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace arm64 {
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050using helpers::CPURegisterFrom;
51using helpers::DRegisterFrom;
52using helpers::FPRegisterFrom;
53using helpers::HeapOperand;
54using helpers::HeapOperandFrom;
55using helpers::InputCPURegisterAt;
56using helpers::InputFPRegisterAt;
57using helpers::InputRegisterAt;
58using helpers::InputOperandAt;
59using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080060using helpers::LocationFrom;
61using helpers::OperandFromMemOperand;
62using helpers::OutputCPURegister;
63using helpers::OutputFPRegister;
64using helpers::OutputRegister;
65using helpers::RegisterFrom;
66using helpers::StackOperandFrom;
67using helpers::VIXLRegCodeFromART;
68using helpers::WRegisterFrom;
69using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000070using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080071using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
Alexandre Rames5319def2014-10-23 10:03:10 +010073static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000074// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080075// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000077static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Vladimir Markod6e069b2016-01-18 11:11:01 +000096inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
97 // The ARM64 condition codes can express all the necessary branches, see the
98 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
99 // There is no dex instruction or HIR that would need the missing conditions
100 // "equal or unordered" or "not equal".
101 switch (cond) {
102 case kCondEQ: return eq;
103 case kCondNE: return ne /* unordered */;
104 case kCondLT: return gt_bias ? cc : lt /* unordered */;
105 case kCondLE: return gt_bias ? ls : le /* unordered */;
106 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
107 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
108 default:
109 LOG(FATAL) << "UNREACHABLE";
110 UNREACHABLE();
111 }
112}
113
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
116 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
117 // but we use the exact registers for clarity.
118 if (return_type == Primitive::kPrimFloat) {
119 return LocationFrom(s0);
120 } else if (return_type == Primitive::kPrimDouble) {
121 return LocationFrom(d0);
122 } else if (return_type == Primitive::kPrimLong) {
123 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100124 } else if (return_type == Primitive::kPrimVoid) {
125 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000126 } else {
127 return LocationFrom(w0);
128 }
129}
130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000132 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133}
134
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700135// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
136#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Alexandre Rames67555f72014-11-18 10:55:16 +0000137#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100138
Zheng Xuda403092015-04-24 17:35:39 +0800139// Calculate memory accessing operand for save/restore live registers.
140static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
141 RegisterSet* register_set,
142 int64_t spill_offset,
143 bool is_save) {
144 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
145 codegen->GetNumberOfCoreRegisters(),
146 register_set->GetFloatingPointRegisters(),
147 codegen->GetNumberOfFloatingPointRegisters()));
148
149 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
150 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000151 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
152 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800153
154 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
155 UseScratchRegisterScope temps(masm);
156
157 Register base = masm->StackPointer();
158 int64_t core_spill_size = core_list.TotalSizeInBytes();
159 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
160 int64_t reg_size = kXRegSizeInBytes;
161 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
162 uint32_t ls_access_size = WhichPowerOf2(reg_size);
163 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
164 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
165 // If the offset does not fit in the instruction's immediate field, use an alternate register
166 // to compute the base address(float point registers spill base address).
167 Register new_base = temps.AcquireSameSizeAs(base);
168 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
169 base = new_base;
170 spill_offset = -core_spill_size;
171 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
172 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
173 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
174 }
175
176 if (is_save) {
177 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
178 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
179 } else {
180 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
181 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
182 }
183}
184
185void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
188 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
189 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
193 }
194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
198 }
199 }
200
201 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
202 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
203 register_set->ContainsFloatingPointRegister(i)) {
204 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
205 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
206 saved_fpu_stack_offsets_[i] = stack_offset;
207 stack_offset += kDRegSizeInBytes;
208 }
209 }
210
211 SaveRestoreLiveRegistersHelper(codegen, register_set,
212 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
213}
214
215void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
216 RegisterSet* register_set = locations->GetLiveRegisters();
217 SaveRestoreLiveRegistersHelper(codegen, register_set,
218 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
219}
220
Alexandre Rames5319def2014-10-23 10:03:10 +0100221class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
222 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000223 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000227 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100228
Alexandre Rames5319def2014-10-23 10:03:10 +0100229 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000230 if (instruction_->CanThrowIntoCatchBlock()) {
231 // Live registers will be restored in the catch block if caught.
232 SaveLiveRegisters(codegen, instruction_->GetLocations());
233 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000234 // We're moving two locations to locations that could overlap, so we need a parallel
235 // move resolver.
236 InvokeRuntimeCallingConvention calling_convention;
237 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100238 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
239 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100240 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
241 ? QUICK_ENTRY_POINT(pThrowStringBounds)
242 : QUICK_ENTRY_POINT(pThrowArrayBounds);
243 arm64_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
244 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800245 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100246 }
247
Alexandre Rames8158f282015-08-07 10:26:17 +0100248 bool IsFatal() const OVERRIDE { return true; }
249
Alexandre Rames9931f312015-06-19 14:47:01 +0100250 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
251
Alexandre Rames5319def2014-10-23 10:03:10 +0100252 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
254};
255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
257 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000258 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000259
260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
261 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
262 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000263 if (instruction_->CanThrowIntoCatchBlock()) {
264 // Live registers will be restored in the catch block if caught.
265 SaveLiveRegisters(codegen, instruction_->GetLocations());
266 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000268 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 }
271
Alexandre Rames8158f282015-08-07 10:26:17 +0100272 bool IsFatal() const OVERRIDE { return true; }
273
Alexandre Rames9931f312015-06-19 14:47:01 +0100274 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
278};
279
280class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
281 public:
282 LoadClassSlowPathARM64(HLoadClass* cls,
283 HInstruction* at,
284 uint32_t dex_pc,
285 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
288 }
289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 LocationSummary* locations = at_->GetLocations();
292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
293
294 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000295 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000296
297 InvokeRuntimeCallingConvention calling_convention;
298 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
300 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000301 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800302 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100303 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800304 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100305 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800306 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000307
308 // Move the class to the desired location.
309 Location out = locations->Out();
310 if (out.IsValid()) {
311 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
312 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000313 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 }
315
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 __ B(GetExitLabel());
318 }
319
Alexandre Rames9931f312015-06-19 14:47:01 +0100320 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 private:
323 // The class this slow path will load.
324 HLoadClass* const cls_;
325
326 // The instruction where this slow path is happening.
327 // (Might be the load class or an initialization check).
328 HInstruction* const at_;
329
330 // The dex PC of `at_`.
331 const uint32_t dex_pc_;
332
333 // Whether to initialize the class.
334 const bool do_clinit_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
337};
338
339class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000341 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
345 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
347
348 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000349 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000355 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100356 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000357 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000358 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000359
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000360 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000361 __ B(GetExitLabel());
362 }
363
Alexandre Rames9931f312015-06-19 14:47:01 +0100364 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
365
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
368};
369
Alexandre Rames5319def2014-10-23 10:03:10 +0100370class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000372 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100373
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000377 if (instruction_->CanThrowIntoCatchBlock()) {
378 // Live registers will be restored in the catch block if caught.
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
380 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000381 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000382 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800383 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 }
385
Alexandre Rames8158f282015-08-07 10:26:17 +0100386 bool IsFatal() const OVERRIDE { return true; }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
389
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100391 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
392};
393
394class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
395 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100396 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000397 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100398
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
400 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100401 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000403 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000404 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800405 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000406 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000407 if (successor_ == nullptr) {
408 __ B(GetReturnLabel());
409 } else {
410 __ B(arm64_codegen->GetLabelOf(successor_));
411 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100412 }
413
414 vixl::Label* GetReturnLabel() {
415 DCHECK(successor_ == nullptr);
416 return &return_label_;
417 }
418
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100419 HBasicBlock* GetSuccessor() const {
420 return successor_;
421 }
422
Alexandre Rames9931f312015-06-19 14:47:01 +0100423 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
424
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100426 // If not null, the block to branch to after the suspend check.
427 HBasicBlock* const successor_;
428
429 // If `successor_` is null, the label to branch to after the suspend check.
430 vixl::Label return_label_;
431
432 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
433};
434
Alexandre Rames67555f72014-11-18 10:55:16 +0000435class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
436 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000437 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000441 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100442 Location class_to_check = locations->InAt(1);
443 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
444 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445 DCHECK(instruction_->IsCheckCast()
446 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
447 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100448 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000449
Alexandre Rames67555f72014-11-18 10:55:16 +0000450 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000451
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000452 if (!is_fatal_) {
453 SaveLiveRegisters(codegen, locations);
454 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455
456 // We're moving two locations to locations that could overlap, so we need a parallel
457 // move resolver.
458 InvokeRuntimeCallingConvention calling_convention;
459 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100460 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
461 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000462
463 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000464 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100465 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000466 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
467 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000468 Primitive::Type ret_type = instruction_->GetType();
469 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
470 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
471 } else {
472 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100473 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800474 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475 }
476
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477 if (!is_fatal_) {
478 RestoreLiveRegisters(codegen, locations);
479 __ B(GetExitLabel());
480 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000481 }
482
Alexandre Rames9931f312015-06-19 14:47:01 +0100483 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000488
Alexandre Rames67555f72014-11-18 10:55:16 +0000489 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
490};
491
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700492class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
493 public:
Aart Bik42249c32016-01-07 15:33:50 -0800494 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000495 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496
497 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800498 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700499 __ Bind(GetEntryLabel());
500 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800501 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
502 instruction_,
503 instruction_->GetDexPc(),
504 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000505 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700506 }
507
Alexandre Rames9931f312015-06-19 14:47:01 +0100508 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
509
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700511 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
512};
513
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100514class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
515 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000516 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100517
518 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
519 LocationSummary* locations = instruction_->GetLocations();
520 __ Bind(GetEntryLabel());
521 SaveLiveRegisters(codegen, locations);
522
523 InvokeRuntimeCallingConvention calling_convention;
524 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
525 parallel_move.AddMove(
526 locations->InAt(0),
527 LocationFrom(calling_convention.GetRegisterAt(0)),
528 Primitive::kPrimNot,
529 nullptr);
530 parallel_move.AddMove(
531 locations->InAt(1),
532 LocationFrom(calling_convention.GetRegisterAt(1)),
533 Primitive::kPrimInt,
534 nullptr);
535 parallel_move.AddMove(
536 locations->InAt(2),
537 LocationFrom(calling_convention.GetRegisterAt(2)),
538 Primitive::kPrimNot,
539 nullptr);
540 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
541
542 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
543 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
544 instruction_,
545 instruction_->GetDexPc(),
546 this);
547 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
548 RestoreLiveRegisters(codegen, locations);
549 __ B(GetExitLabel());
550 }
551
552 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
553
554 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100555 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
556};
557
Zheng Xu3927c8b2015-11-18 17:46:25 +0800558void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
559 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000560 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800561
562 // We are about to use the assembler to place literals directly. Make sure we have enough
563 // underlying code buffer and we have generated the jump table with right size.
564 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
565 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
566
567 __ Bind(&table_start_);
568 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
569 for (uint32_t i = 0; i < num_entries; i++) {
570 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
571 DCHECK(target_label->IsBound());
572 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
573 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
574 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
575 Literal<int32_t> literal(jump_offset);
576 __ place(&literal);
577 }
578}
579
Roland Levillain44015862016-01-22 11:47:17 +0000580// Slow path marking an object during a read barrier.
581class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
582 public:
583 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000584 : SlowPathCodeARM64(instruction), out_(out), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000585 DCHECK(kEmitCompilerReadBarrier);
586 }
587
588 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
589
590 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
591 LocationSummary* locations = instruction_->GetLocations();
592 Primitive::Type type = Primitive::kPrimNot;
593 DCHECK(locations->CanCall());
594 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
595 DCHECK(instruction_->IsInstanceFieldGet() ||
596 instruction_->IsStaticFieldGet() ||
597 instruction_->IsArrayGet() ||
598 instruction_->IsLoadClass() ||
599 instruction_->IsLoadString() ||
600 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100601 instruction_->IsCheckCast() ||
602 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
603 instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000604 << "Unexpected instruction in read barrier marking slow path: "
605 << instruction_->DebugName();
606
607 __ Bind(GetEntryLabel());
608 SaveLiveRegisters(codegen, locations);
609
610 InvokeRuntimeCallingConvention calling_convention;
611 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
612 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)), obj_, type);
613 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
614 instruction_,
615 instruction_->GetDexPc(),
616 this);
617 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
618 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
619
620 RestoreLiveRegisters(codegen, locations);
621 __ B(GetExitLabel());
622 }
623
624 private:
Roland Levillain44015862016-01-22 11:47:17 +0000625 const Location out_;
626 const Location obj_;
627
628 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
629};
630
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000631// Slow path generating a read barrier for a heap reference.
632class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
633 public:
634 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
635 Location out,
636 Location ref,
637 Location obj,
638 uint32_t offset,
639 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000640 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000641 out_(out),
642 ref_(ref),
643 obj_(obj),
644 offset_(offset),
645 index_(index) {
646 DCHECK(kEmitCompilerReadBarrier);
647 // If `obj` is equal to `out` or `ref`, it means the initial object
648 // has been overwritten by (or after) the heap object reference load
649 // to be instrumented, e.g.:
650 //
651 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000652 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000653 //
654 // In that case, we have lost the information about the original
655 // object, and the emitted read barrier cannot work properly.
656 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
657 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
658 }
659
660 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
661 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
662 LocationSummary* locations = instruction_->GetLocations();
663 Primitive::Type type = Primitive::kPrimNot;
664 DCHECK(locations->CanCall());
665 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100666 DCHECK(instruction_->IsInstanceFieldGet() ||
667 instruction_->IsStaticFieldGet() ||
668 instruction_->IsArrayGet() ||
669 instruction_->IsInstanceOf() ||
670 instruction_->IsCheckCast() ||
671 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain44015862016-01-22 11:47:17 +0000672 instruction_->GetLocations()->Intrinsified()))
673 << "Unexpected instruction in read barrier for heap reference slow path: "
674 << instruction_->DebugName();
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000675 // The read barrier instrumentation does not support the
676 // HArm64IntermediateAddress instruction yet.
677 DCHECK(!(instruction_->IsArrayGet() &&
678 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000679
680 __ Bind(GetEntryLabel());
681
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000682 SaveLiveRegisters(codegen, locations);
683
684 // We may have to change the index's value, but as `index_` is a
685 // constant member (like other "inputs" of this slow path),
686 // introduce a copy of it, `index`.
687 Location index = index_;
688 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100689 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000690 if (instruction_->IsArrayGet()) {
691 // Compute the actual memory offset and store it in `index`.
692 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
693 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
694 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
695 // We are about to change the value of `index_reg` (see the
696 // calls to vixl::MacroAssembler::Lsl and
697 // vixl::MacroAssembler::Mov below), but it has
698 // not been saved by the previous call to
699 // art::SlowPathCode::SaveLiveRegisters, as it is a
700 // callee-save register --
701 // art::SlowPathCode::SaveLiveRegisters does not consider
702 // callee-save registers, as it has been designed with the
703 // assumption that callee-save registers are supposed to be
704 // handled by the called function. So, as a callee-save
705 // register, `index_reg` _would_ eventually be saved onto
706 // the stack, but it would be too late: we would have
707 // changed its value earlier. Therefore, we manually save
708 // it here into another freely available register,
709 // `free_reg`, chosen of course among the caller-save
710 // registers (as a callee-save `free_reg` register would
711 // exhibit the same problem).
712 //
713 // Note we could have requested a temporary register from
714 // the register allocator instead; but we prefer not to, as
715 // this is a slow path, and we know we can find a
716 // caller-save register that is available.
717 Register free_reg = FindAvailableCallerSaveRegister(codegen);
718 __ Mov(free_reg.W(), index_reg);
719 index_reg = free_reg;
720 index = LocationFrom(index_reg);
721 } else {
722 // The initial register stored in `index_` has already been
723 // saved in the call to art::SlowPathCode::SaveLiveRegisters
724 // (as it is not a callee-save register), so we can freely
725 // use it.
726 }
727 // Shifting the index value contained in `index_reg` by the scale
728 // factor (2) cannot overflow in practice, as the runtime is
729 // unable to allocate object arrays with a size larger than
730 // 2^26 - 1 (that is, 2^28 - 4 bytes).
731 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
732 static_assert(
733 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
734 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
735 __ Add(index_reg, index_reg, Operand(offset_));
736 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100737 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
738 // intrinsics, `index_` is not shifted by a scale factor of 2
739 // (as in the case of ArrayGet), as it is actually an offset
740 // to an object field within an object.
741 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000742 DCHECK(instruction_->GetLocations()->Intrinsified());
743 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
744 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
745 << instruction_->AsInvoke()->GetIntrinsic();
746 DCHECK_EQ(offset_, 0U);
747 DCHECK(index_.IsRegisterPair());
748 // UnsafeGet's offset location is a register pair, the low
749 // part contains the correct offset.
750 index = index_.ToLow();
751 }
752 }
753
754 // We're moving two or three locations to locations that could
755 // overlap, so we need a parallel move resolver.
756 InvokeRuntimeCallingConvention calling_convention;
757 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
758 parallel_move.AddMove(ref_,
759 LocationFrom(calling_convention.GetRegisterAt(0)),
760 type,
761 nullptr);
762 parallel_move.AddMove(obj_,
763 LocationFrom(calling_convention.GetRegisterAt(1)),
764 type,
765 nullptr);
766 if (index.IsValid()) {
767 parallel_move.AddMove(index,
768 LocationFrom(calling_convention.GetRegisterAt(2)),
769 Primitive::kPrimInt,
770 nullptr);
771 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
772 } else {
773 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
774 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
775 }
776 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
777 instruction_,
778 instruction_->GetDexPc(),
779 this);
780 CheckEntrypointTypes<
781 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
782 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
783
784 RestoreLiveRegisters(codegen, locations);
785
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000786 __ B(GetExitLabel());
787 }
788
789 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
790
791 private:
792 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
793 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
794 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
795 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
796 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
797 return Register(VIXLRegCodeFromART(i), kXRegSize);
798 }
799 }
800 // We shall never fail to find a free caller-save register, as
801 // there are more than two core caller-save registers on ARM64
802 // (meaning it is possible to find one which is different from
803 // `ref` and `obj`).
804 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
805 LOG(FATAL) << "Could not find a free register";
806 UNREACHABLE();
807 }
808
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000809 const Location out_;
810 const Location ref_;
811 const Location obj_;
812 const uint32_t offset_;
813 // An additional location containing an index to an array.
814 // Only used for HArrayGet and the UnsafeGetObject &
815 // UnsafeGetObjectVolatile intrinsics.
816 const Location index_;
817
818 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
819};
820
821// Slow path generating a read barrier for a GC root.
822class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
823 public:
824 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000825 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000826 DCHECK(kEmitCompilerReadBarrier);
827 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000828
829 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
830 LocationSummary* locations = instruction_->GetLocations();
831 Primitive::Type type = Primitive::kPrimNot;
832 DCHECK(locations->CanCall());
833 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000834 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
835 << "Unexpected instruction in read barrier for GC root slow path: "
836 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000837
838 __ Bind(GetEntryLabel());
839 SaveLiveRegisters(codegen, locations);
840
841 InvokeRuntimeCallingConvention calling_convention;
842 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
843 // The argument of the ReadBarrierForRootSlow is not a managed
844 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
845 // thus we need a 64-bit move here, and we cannot use
846 //
847 // arm64_codegen->MoveLocation(
848 // LocationFrom(calling_convention.GetRegisterAt(0)),
849 // root_,
850 // type);
851 //
852 // which would emit a 32-bit move, as `type` is a (32-bit wide)
853 // reference type (`Primitive::kPrimNot`).
854 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
855 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
856 instruction_,
857 instruction_->GetDexPc(),
858 this);
859 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
860 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
861
862 RestoreLiveRegisters(codegen, locations);
863 __ B(GetExitLabel());
864 }
865
866 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
867
868 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000869 const Location out_;
870 const Location root_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
873};
874
Alexandre Rames5319def2014-10-23 10:03:10 +0100875#undef __
876
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100877Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100878 Location next_location;
879 if (type == Primitive::kPrimVoid) {
880 LOG(FATAL) << "Unreachable type " << type;
881 }
882
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100884 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
885 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000886 } else if (!Primitive::IsFloatingPointType(type) &&
887 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000888 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
889 } else {
890 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000891 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
892 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100893 }
894
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000896 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100897 return next_location;
898}
899
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100900Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100901 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100902}
903
Serban Constantinescu579885a2015-02-22 20:51:33 +0000904CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
905 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100906 const CompilerOptions& compiler_options,
907 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100908 : CodeGenerator(graph,
909 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000910 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000911 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000912 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000913 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100914 compiler_options,
915 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100916 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800917 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100918 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000919 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000920 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100921 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000922 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000923 uint32_literals_(std::less<uint32_t>(),
924 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100925 uint64_literals_(std::less<uint64_t>(),
926 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
927 method_patches_(MethodReferenceComparator(),
928 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
929 call_patches_(MethodReferenceComparator(),
930 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
931 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000932 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
933 boot_image_string_patches_(StringReferenceValueComparator(),
934 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
935 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100936 boot_image_type_patches_(TypeReferenceValueComparator(),
937 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
938 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939 boot_image_address_patches_(std::less<uint32_t>(),
940 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000941 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000942 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000943}
Alexandre Rames5319def2014-10-23 10:03:10 +0100944
Alexandre Rames67555f72014-11-18 10:55:16 +0000945#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100946
Zheng Xu3927c8b2015-11-18 17:46:25 +0800947void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100948 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800949 jump_table->EmitTable(this);
950 }
951}
952
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000953void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800954 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000955 // Ensure we emit the literal pool.
956 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000957
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000958 CodeGenerator::Finalize(allocator);
959}
960
Zheng Xuad4450e2015-04-17 18:48:56 +0800961void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
962 // Note: There are 6 kinds of moves:
963 // 1. constant -> GPR/FPR (non-cycle)
964 // 2. constant -> stack (non-cycle)
965 // 3. GPR/FPR -> GPR/FPR
966 // 4. GPR/FPR -> stack
967 // 5. stack -> GPR/FPR
968 // 6. stack -> stack (non-cycle)
969 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
970 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
971 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
972 // dependency.
973 vixl_temps_.Open(GetVIXLAssembler());
974}
975
976void ParallelMoveResolverARM64::FinishEmitNativeCode() {
977 vixl_temps_.Close();
978}
979
980Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
981 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
982 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
983 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
984 Location scratch = GetScratchLocation(kind);
985 if (!scratch.Equals(Location::NoLocation())) {
986 return scratch;
987 }
988 // Allocate from VIXL temp registers.
989 if (kind == Location::kRegister) {
990 scratch = LocationFrom(vixl_temps_.AcquireX());
991 } else {
992 DCHECK(kind == Location::kFpuRegister);
993 scratch = LocationFrom(vixl_temps_.AcquireD());
994 }
995 AddScratchLocation(scratch);
996 return scratch;
997}
998
999void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1000 if (loc.IsRegister()) {
1001 vixl_temps_.Release(XRegisterFrom(loc));
1002 } else {
1003 DCHECK(loc.IsFpuRegister());
1004 vixl_temps_.Release(DRegisterFrom(loc));
1005 }
1006 RemoveScratchLocation(loc);
1007}
1008
Alexandre Rames3e69f162014-12-10 10:36:50 +00001009void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001010 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001011 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001012}
1013
Alexandre Rames5319def2014-10-23 10:03:10 +01001014void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001015 MacroAssembler* masm = GetVIXLAssembler();
1016 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001017 __ Bind(&frame_entry_label_);
1018
Serban Constantinescu02164b32014-11-13 14:05:07 +00001019 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1020 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001021 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001022 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001023 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001024 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001025 __ Ldr(wzr, MemOperand(temp, 0));
1026 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001027 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001028
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001029 if (!HasEmptyFrame()) {
1030 int frame_size = GetFrameSize();
1031 // Stack layout:
1032 // sp[frame_size - 8] : lr.
1033 // ... : other preserved core registers.
1034 // ... : other preserved fp registers.
1035 // ... : reserved frame space.
1036 // sp[0] : current method.
1037 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001039 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1040 frame_size - GetCoreSpillSize());
1041 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1042 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001043 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001044}
1045
1046void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001047 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001048 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001049 if (!HasEmptyFrame()) {
1050 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001051 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1052 frame_size - FrameEntrySpillSize());
1053 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1054 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001055 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001057 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001058 __ Ret();
1059 GetAssembler()->cfi().RestoreState();
1060 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001061}
1062
Zheng Xuda403092015-04-24 17:35:39 +08001063vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1064 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1065 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1066 core_spill_mask_);
1067}
1068
1069vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1070 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1071 GetNumberOfFloatingPointRegisters()));
1072 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1073 fpu_spill_mask_);
1074}
1075
Alexandre Rames5319def2014-10-23 10:03:10 +01001076void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1077 __ Bind(GetLabelOf(block));
1078}
1079
Calin Juravle175dc732015-08-25 15:42:32 +01001080void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1081 DCHECK(location.IsRegister());
1082 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1083}
1084
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1086 if (location.IsRegister()) {
1087 locations->AddTemp(location);
1088 } else {
1089 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1090 }
1091}
1092
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001093void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001094 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001096 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001097 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001098 if (value_can_be_null) {
1099 __ Cbz(value, &done);
1100 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001101 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1102 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001104 if (value_can_be_null) {
1105 __ Bind(&done);
1106 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001107}
1108
David Brazdil58282f42016-01-14 12:45:10 +00001109void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001110 // Blocked core registers:
1111 // lr : Runtime reserved.
1112 // tr : Runtime reserved.
1113 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1114 // ip1 : VIXL core temp.
1115 // ip0 : VIXL core temp.
1116 //
1117 // Blocked fp registers:
1118 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1120 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 while (!reserved_core_registers.IsEmpty()) {
1122 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1123 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001124
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001125 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001126 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001127 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1128 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001129
David Brazdil58282f42016-01-14 12:45:10 +00001130 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001131 // Stubs do not save callee-save floating point registers. If the graph
1132 // is debuggable, we need to deal with these registers differently. For
1133 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001134 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1135 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1136 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001137 }
1138 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001139}
1140
Alexandre Rames3e69f162014-12-10 10:36:50 +00001141size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1142 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1143 __ Str(reg, MemOperand(sp, stack_index));
1144 return kArm64WordSize;
1145}
1146
1147size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1148 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1149 __ Ldr(reg, MemOperand(sp, stack_index));
1150 return kArm64WordSize;
1151}
1152
1153size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1154 FPRegister reg = FPRegister(reg_id, kDRegSize);
1155 __ Str(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 FPRegister reg = FPRegister(reg_id, kDRegSize);
1161 __ Ldr(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
Alexandre Rames5319def2014-10-23 10:03:10 +01001165void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001166 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001167}
1168
1169void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001170 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001171}
1172
Alexandre Rames67555f72014-11-18 10:55:16 +00001173void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001174 if (constant->IsIntConstant()) {
1175 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1176 } else if (constant->IsLongConstant()) {
1177 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1178 } else if (constant->IsNullConstant()) {
1179 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001180 } else if (constant->IsFloatConstant()) {
1181 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1182 } else {
1183 DCHECK(constant->IsDoubleConstant());
1184 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1185 }
1186}
1187
Alexandre Rames3e69f162014-12-10 10:36:50 +00001188
1189static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1190 DCHECK(constant.IsConstant());
1191 HConstant* cst = constant.GetConstant();
1192 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001193 // Null is mapped to a core W register, which we associate with kPrimInt.
1194 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1196 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1197 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1198}
1199
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200void CodeGeneratorARM64::MoveLocation(Location destination,
1201 Location source,
1202 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001203 if (source.Equals(destination)) {
1204 return;
1205 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206
1207 // A valid move can always be inferred from the destination and source
1208 // locations. When moving from and to a register, the argument type can be
1209 // used to generate 32bit instead of 64bit moves. In debug mode we also
1210 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212
1213 if (destination.IsRegister() || destination.IsFpuRegister()) {
1214 if (unspecified_type) {
1215 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1216 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001217 (src_cst != nullptr && (src_cst->IsIntConstant()
1218 || src_cst->IsFloatConstant()
1219 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001220 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001222 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001223 // If the source is a double stack slot or a 64bit constant, a 64bit
1224 // type is appropriate. Else the source is a register, and since the
1225 // type has not been specified, we chose a 64bit type to force a 64bit
1226 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001230 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1231 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1232 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001233 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1234 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1235 __ Ldr(dst, StackOperandFrom(source));
1236 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001242 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001243 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1245 ? Primitive::kPrimLong
1246 : Primitive::kPrimInt;
1247 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1248 }
1249 } else {
1250 DCHECK(source.IsFpuRegister());
1251 if (destination.IsRegister()) {
1252 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1253 ? Primitive::kPrimDouble
1254 : Primitive::kPrimFloat;
1255 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1256 } else {
1257 DCHECK(destination.IsFpuRegister());
1258 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001259 }
1260 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 } else { // The destination is not a register. It must be a stack slot.
1262 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1263 if (source.IsRegister() || source.IsFpuRegister()) {
1264 if (unspecified_type) {
1265 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001269 }
1270 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1272 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1273 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1276 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277 UseScratchRegisterScope temps(GetVIXLAssembler());
1278 HConstant* src_cst = source.GetConstant();
1279 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001280 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 temp = temps.AcquireW();
1282 } else if (src_cst->IsLongConstant()) {
1283 temp = temps.AcquireX();
1284 } else if (src_cst->IsFloatConstant()) {
1285 temp = temps.AcquireS();
1286 } else {
1287 DCHECK(src_cst->IsDoubleConstant());
1288 temp = temps.AcquireD();
1289 }
1290 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001291 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001292 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001293 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001294 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001295 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001296 // There is generally less pressure on FP registers.
1297 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001298 __ Ldr(temp, StackOperandFrom(source));
1299 __ Str(temp, StackOperandFrom(destination));
1300 }
1301 }
1302}
1303
1304void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001305 CPURegister dst,
1306 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001307 switch (type) {
1308 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001309 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001310 break;
1311 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001312 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001313 break;
1314 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001315 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316 break;
1317 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001319 break;
1320 case Primitive::kPrimInt:
1321 case Primitive::kPrimNot:
1322 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 case Primitive::kPrimFloat:
1324 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001325 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 __ Ldr(dst, src);
1327 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001328 case Primitive::kPrimVoid:
1329 LOG(FATAL) << "Unreachable type " << type;
1330 }
1331}
1332
Calin Juravle77520bc2015-01-12 18:45:46 +00001333void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001334 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001335 const MemOperand& src,
1336 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001337 MacroAssembler* masm = GetVIXLAssembler();
1338 BlockPoolsScope block_pools(masm);
1339 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001340 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001341 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001342
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001343 DCHECK(!src.IsPreIndex());
1344 DCHECK(!src.IsPostIndex());
1345
1346 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001347 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001348 MemOperand base = MemOperand(temp_base);
1349 switch (type) {
1350 case Primitive::kPrimBoolean:
1351 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001352 if (needs_null_check) {
1353 MaybeRecordImplicitNullCheck(instruction);
1354 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001355 break;
1356 case Primitive::kPrimByte:
1357 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001358 if (needs_null_check) {
1359 MaybeRecordImplicitNullCheck(instruction);
1360 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001361 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1362 break;
1363 case Primitive::kPrimChar:
1364 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001365 if (needs_null_check) {
1366 MaybeRecordImplicitNullCheck(instruction);
1367 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001368 break;
1369 case Primitive::kPrimShort:
1370 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001371 if (needs_null_check) {
1372 MaybeRecordImplicitNullCheck(instruction);
1373 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001374 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1375 break;
1376 case Primitive::kPrimInt:
1377 case Primitive::kPrimNot:
1378 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001379 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001380 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001381 if (needs_null_check) {
1382 MaybeRecordImplicitNullCheck(instruction);
1383 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 break;
1385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble: {
1387 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001388 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001389
1390 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1391 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001392 if (needs_null_check) {
1393 MaybeRecordImplicitNullCheck(instruction);
1394 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001395 __ Fmov(FPRegister(dst), temp);
1396 break;
1397 }
1398 case Primitive::kPrimVoid:
1399 LOG(FATAL) << "Unreachable type " << type;
1400 }
1401}
1402
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001403void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001404 CPURegister src,
1405 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001406 switch (type) {
1407 case Primitive::kPrimBoolean:
1408 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001409 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410 break;
1411 case Primitive::kPrimChar:
1412 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001413 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414 break;
1415 case Primitive::kPrimInt:
1416 case Primitive::kPrimNot:
1417 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001418 case Primitive::kPrimFloat:
1419 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001420 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001421 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001422 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001423 case Primitive::kPrimVoid:
1424 LOG(FATAL) << "Unreachable type " << type;
1425 }
1426}
1427
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001428void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1429 CPURegister src,
1430 const MemOperand& dst) {
1431 UseScratchRegisterScope temps(GetVIXLAssembler());
1432 Register temp_base = temps.AcquireX();
1433
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001434 DCHECK(!dst.IsPreIndex());
1435 DCHECK(!dst.IsPostIndex());
1436
1437 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001438 Operand op = OperandFromMemOperand(dst);
1439 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001440 MemOperand base = MemOperand(temp_base);
1441 switch (type) {
1442 case Primitive::kPrimBoolean:
1443 case Primitive::kPrimByte:
1444 __ Stlrb(Register(src), base);
1445 break;
1446 case Primitive::kPrimChar:
1447 case Primitive::kPrimShort:
1448 __ Stlrh(Register(src), base);
1449 break;
1450 case Primitive::kPrimInt:
1451 case Primitive::kPrimNot:
1452 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001453 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001454 __ Stlr(Register(src), base);
1455 break;
1456 case Primitive::kPrimFloat:
1457 case Primitive::kPrimDouble: {
1458 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001459 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001460
1461 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1462 __ Fmov(temp, FPRegister(src));
1463 __ Stlr(temp, base);
1464 break;
1465 }
1466 case Primitive::kPrimVoid:
1467 LOG(FATAL) << "Unreachable type " << type;
1468 }
1469}
1470
Calin Juravle175dc732015-08-25 15:42:32 +01001471void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1472 HInstruction* instruction,
1473 uint32_t dex_pc,
1474 SlowPathCode* slow_path) {
1475 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1476 instruction,
1477 dex_pc,
1478 slow_path);
1479}
1480
Alexandre Rames67555f72014-11-18 10:55:16 +00001481void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1482 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001483 uint32_t dex_pc,
1484 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001485 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001486 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001487 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1488 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001489 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001490}
1491
1492void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1493 vixl::Register class_reg) {
1494 UseScratchRegisterScope temps(GetVIXLAssembler());
1495 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001496 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1497
Serban Constantinescu02164b32014-11-13 14:05:07 +00001498 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001499 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1500 __ Add(temp, class_reg, status_offset);
1501 __ Ldar(temp, HeapOperand(temp));
1502 __ Cmp(temp, mirror::Class::kStatusInitialized);
1503 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 __ Bind(slow_path->GetExitLabel());
1505}
Alexandre Rames5319def2014-10-23 10:03:10 +01001506
Roland Levillain44015862016-01-22 11:47:17 +00001507void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001508 BarrierType type = BarrierAll;
1509
1510 switch (kind) {
1511 case MemBarrierKind::kAnyAny:
1512 case MemBarrierKind::kAnyStore: {
1513 type = BarrierAll;
1514 break;
1515 }
1516 case MemBarrierKind::kLoadAny: {
1517 type = BarrierReads;
1518 break;
1519 }
1520 case MemBarrierKind::kStoreStore: {
1521 type = BarrierWrites;
1522 break;
1523 }
1524 default:
1525 LOG(FATAL) << "Unexpected memory barrier " << kind;
1526 }
1527 __ Dmb(InnerShareable, type);
1528}
1529
Serban Constantinescu02164b32014-11-13 14:05:07 +00001530void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1531 HBasicBlock* successor) {
1532 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001533 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1534 if (slow_path == nullptr) {
1535 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1536 instruction->SetSlowPath(slow_path);
1537 codegen_->AddSlowPath(slow_path);
1538 if (successor != nullptr) {
1539 DCHECK(successor->IsLoopHeader());
1540 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1541 }
1542 } else {
1543 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1544 }
1545
Serban Constantinescu02164b32014-11-13 14:05:07 +00001546 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1547 Register temp = temps.AcquireW();
1548
1549 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1550 if (successor == nullptr) {
1551 __ Cbnz(temp, slow_path->GetEntryLabel());
1552 __ Bind(slow_path->GetReturnLabel());
1553 } else {
1554 __ Cbz(temp, codegen_->GetLabelOf(successor));
1555 __ B(slow_path->GetEntryLabel());
1556 // slow_path will return to GetLabelOf(successor).
1557 }
1558}
1559
Alexandre Rames5319def2014-10-23 10:03:10 +01001560InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1561 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001562 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001563 assembler_(codegen->GetAssembler()),
1564 codegen_(codegen) {}
1565
1566#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001567 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001568
1569#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1570
1571enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001572 // Using a base helps identify when we hit such breakpoints.
1573 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001574#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1575 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1576#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1577};
1578
1579#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001580 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001581 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1582 } \
1583 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1585 locations->SetOut(Location::Any()); \
1586 }
1587 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1588#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1589
1590#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001591#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001592
Alexandre Rames67555f72014-11-18 10:55:16 +00001593void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001594 DCHECK_EQ(instr->InputCount(), 2U);
1595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1596 Primitive::Type type = instr->GetResultType();
1597 switch (type) {
1598 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001599 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001601 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001603 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001604
1605 case Primitive::kPrimFloat:
1606 case Primitive::kPrimDouble:
1607 locations->SetInAt(0, Location::RequiresFpuRegister());
1608 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001610 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001611
Alexandre Rames5319def2014-10-23 10:03:10 +01001612 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001613 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001614 }
1615}
1616
Alexandre Rames09a99962015-04-15 11:47:56 +01001617void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001618 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1619
1620 bool object_field_get_with_read_barrier =
1621 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001622 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001623 new (GetGraph()->GetArena()) LocationSummary(instruction,
1624 object_field_get_with_read_barrier ?
1625 LocationSummary::kCallOnSlowPath :
1626 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001627 locations->SetInAt(0, Location::RequiresRegister());
1628 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1629 locations->SetOut(Location::RequiresFpuRegister());
1630 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001631 // The output overlaps for an object field get when read barriers
1632 // are enabled: we do not want the load to overwrite the object's
1633 // location, as we need it to emit the read barrier.
1634 locations->SetOut(
1635 Location::RequiresRegister(),
1636 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001637 }
1638}
1639
1640void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1641 const FieldInfo& field_info) {
1642 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001643 LocationSummary* locations = instruction->GetLocations();
1644 Location base_loc = locations->InAt(0);
1645 Location out = locations->Out();
1646 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001647 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001648 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001649 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001650
Roland Levillain44015862016-01-22 11:47:17 +00001651 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1652 // Object FieldGet with Baker's read barrier case.
1653 MacroAssembler* masm = GetVIXLAssembler();
1654 UseScratchRegisterScope temps(masm);
1655 // /* HeapReference<Object> */ out = *(base + offset)
1656 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1657 Register temp = temps.AcquireW();
1658 // Note that potential implicit null checks are handled in this
1659 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1660 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1661 instruction,
1662 out,
1663 base,
1664 offset,
1665 temp,
1666 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001667 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001668 } else {
1669 // General case.
1670 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001671 // Note that a potential implicit null check is handled in this
1672 // CodeGeneratorARM64::LoadAcquire call.
1673 // NB: LoadAcquire will record the pc info if needed.
1674 codegen_->LoadAcquire(
1675 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001676 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001677 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001679 }
Roland Levillain44015862016-01-22 11:47:17 +00001680 if (field_type == Primitive::kPrimNot) {
1681 // If read barriers are enabled, emit read barriers other than
1682 // Baker's using a slow path (and also unpoison the loaded
1683 // reference, if heap poisoning is enabled).
1684 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1685 }
Roland Levillain4d027112015-07-01 15:41:14 +01001686 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001687}
1688
1689void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1690 LocationSummary* locations =
1691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1692 locations->SetInAt(0, Location::RequiresRegister());
1693 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1694 locations->SetInAt(1, Location::RequiresFpuRegister());
1695 } else {
1696 locations->SetInAt(1, Location::RequiresRegister());
1697 }
1698}
1699
1700void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001701 const FieldInfo& field_info,
1702 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001703 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001704 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001705
1706 Register obj = InputRegisterAt(instruction, 0);
1707 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001708 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001709 Offset offset = field_info.GetFieldOffset();
1710 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001711
Roland Levillain4d027112015-07-01 15:41:14 +01001712 {
1713 // We use a block to end the scratch scope before the write barrier, thus
1714 // freeing the temporary registers so they can be used in `MarkGCCard`.
1715 UseScratchRegisterScope temps(GetVIXLAssembler());
1716
1717 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1718 DCHECK(value.IsW());
1719 Register temp = temps.AcquireW();
1720 __ Mov(temp, value.W());
1721 GetAssembler()->PoisonHeapReference(temp.W());
1722 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001723 }
Roland Levillain4d027112015-07-01 15:41:14 +01001724
1725 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001726 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1727 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001728 } else {
1729 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1730 codegen_->MaybeRecordImplicitNullCheck(instruction);
1731 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001732 }
1733
1734 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001735 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001736 }
1737}
1738
Alexandre Rames67555f72014-11-18 10:55:16 +00001739void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001740 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001741
1742 switch (type) {
1743 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001744 case Primitive::kPrimLong: {
1745 Register dst = OutputRegister(instr);
1746 Register lhs = InputRegisterAt(instr, 0);
1747 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001748 if (instr->IsAdd()) {
1749 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001750 } else if (instr->IsAnd()) {
1751 __ And(dst, lhs, rhs);
1752 } else if (instr->IsOr()) {
1753 __ Orr(dst, lhs, rhs);
1754 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001755 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001756 } else if (instr->IsRor()) {
1757 if (rhs.IsImmediate()) {
1758 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1759 __ Ror(dst, lhs, shift);
1760 } else {
1761 // Ensure shift distance is in the same size register as the result. If
1762 // we are rotating a long and the shift comes in a w register originally,
1763 // we don't need to sxtw for use as an x since the shift distances are
1764 // all & reg_bits - 1.
1765 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1766 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001767 } else {
1768 DCHECK(instr->IsXor());
1769 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001770 }
1771 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001772 }
1773 case Primitive::kPrimFloat:
1774 case Primitive::kPrimDouble: {
1775 FPRegister dst = OutputFPRegister(instr);
1776 FPRegister lhs = InputFPRegisterAt(instr, 0);
1777 FPRegister rhs = InputFPRegisterAt(instr, 1);
1778 if (instr->IsAdd()) {
1779 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001780 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001781 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001782 } else {
1783 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001784 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001785 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001786 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001787 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001788 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001789 }
1790}
1791
Serban Constantinescu02164b32014-11-13 14:05:07 +00001792void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1793 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1794
1795 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1796 Primitive::Type type = instr->GetResultType();
1797 switch (type) {
1798 case Primitive::kPrimInt:
1799 case Primitive::kPrimLong: {
1800 locations->SetInAt(0, Location::RequiresRegister());
1801 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1802 locations->SetOut(Location::RequiresRegister());
1803 break;
1804 }
1805 default:
1806 LOG(FATAL) << "Unexpected shift type " << type;
1807 }
1808}
1809
1810void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1811 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1812
1813 Primitive::Type type = instr->GetType();
1814 switch (type) {
1815 case Primitive::kPrimInt:
1816 case Primitive::kPrimLong: {
1817 Register dst = OutputRegister(instr);
1818 Register lhs = InputRegisterAt(instr, 0);
1819 Operand rhs = InputOperandAt(instr, 1);
1820 if (rhs.IsImmediate()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001821 uint32_t shift_value = rhs.immediate() &
1822 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001823 if (instr->IsShl()) {
1824 __ Lsl(dst, lhs, shift_value);
1825 } else if (instr->IsShr()) {
1826 __ Asr(dst, lhs, shift_value);
1827 } else {
1828 __ Lsr(dst, lhs, shift_value);
1829 }
1830 } else {
1831 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1832
1833 if (instr->IsShl()) {
1834 __ Lsl(dst, lhs, rhs_reg);
1835 } else if (instr->IsShr()) {
1836 __ Asr(dst, lhs, rhs_reg);
1837 } else {
1838 __ Lsr(dst, lhs, rhs_reg);
1839 }
1840 }
1841 break;
1842 }
1843 default:
1844 LOG(FATAL) << "Unexpected shift operation type " << type;
1845 }
1846}
1847
Alexandre Rames5319def2014-10-23 10:03:10 +01001848void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001849 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001850}
1851
1852void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001853 HandleBinaryOp(instruction);
1854}
1855
1856void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1857 HandleBinaryOp(instruction);
1858}
1859
1860void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1861 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001862}
1863
Artem Serov7fc63502016-02-09 17:15:29 +00001864void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001865 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1866 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1867 locations->SetInAt(0, Location::RequiresRegister());
1868 // There is no immediate variant of negated bitwise instructions in AArch64.
1869 locations->SetInAt(1, Location::RequiresRegister());
1870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1871}
1872
Artem Serov7fc63502016-02-09 17:15:29 +00001873void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001874 Register dst = OutputRegister(instr);
1875 Register lhs = InputRegisterAt(instr, 0);
1876 Register rhs = InputRegisterAt(instr, 1);
1877
1878 switch (instr->GetOpKind()) {
1879 case HInstruction::kAnd:
1880 __ Bic(dst, lhs, rhs);
1881 break;
1882 case HInstruction::kOr:
1883 __ Orn(dst, lhs, rhs);
1884 break;
1885 case HInstruction::kXor:
1886 __ Eon(dst, lhs, rhs);
1887 break;
1888 default:
1889 LOG(FATAL) << "Unreachable";
1890 }
1891}
1892
Alexandre Rames8626b742015-11-25 16:28:08 +00001893void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1894 HArm64DataProcWithShifterOp* instruction) {
1895 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1896 instruction->GetType() == Primitive::kPrimLong);
1897 LocationSummary* locations =
1898 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1899 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1900 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1901 } else {
1902 locations->SetInAt(0, Location::RequiresRegister());
1903 }
1904 locations->SetInAt(1, Location::RequiresRegister());
1905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1906}
1907
1908void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1909 HArm64DataProcWithShifterOp* instruction) {
1910 Primitive::Type type = instruction->GetType();
1911 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1912 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1913 Register out = OutputRegister(instruction);
1914 Register left;
1915 if (kind != HInstruction::kNeg) {
1916 left = InputRegisterAt(instruction, 0);
1917 }
1918 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1919 // shifter operand operation, the IR generating `right_reg` (input to the type
1920 // conversion) can have a different type from the current instruction's type,
1921 // so we manually indicate the type.
1922 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001923 int64_t shift_amount = instruction->GetShiftAmount() &
1924 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001925
1926 Operand right_operand(0);
1927
1928 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1929 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1930 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1931 } else {
1932 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1933 }
1934
1935 // Logical binary operations do not support extension operations in the
1936 // operand. Note that VIXL would still manage if it was passed by generating
1937 // the extension as a separate instruction.
1938 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1939 DCHECK(!right_operand.IsExtendedRegister() ||
1940 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1941 kind != HInstruction::kNeg));
1942 switch (kind) {
1943 case HInstruction::kAdd:
1944 __ Add(out, left, right_operand);
1945 break;
1946 case HInstruction::kAnd:
1947 __ And(out, left, right_operand);
1948 break;
1949 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001950 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001951 __ Neg(out, right_operand);
1952 break;
1953 case HInstruction::kOr:
1954 __ Orr(out, left, right_operand);
1955 break;
1956 case HInstruction::kSub:
1957 __ Sub(out, left, right_operand);
1958 break;
1959 case HInstruction::kXor:
1960 __ Eor(out, left, right_operand);
1961 break;
1962 default:
1963 LOG(FATAL) << "Unexpected operation kind: " << kind;
1964 UNREACHABLE();
1965 }
1966}
1967
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001968void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001969 // The read barrier instrumentation does not support the
1970 // HArm64IntermediateAddress instruction yet.
1971 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001972 LocationSummary* locations =
1973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1974 locations->SetInAt(0, Location::RequiresRegister());
1975 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1976 locations->SetOut(Location::RequiresRegister());
1977}
1978
1979void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1980 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001981 // The read barrier instrumentation does not support the
1982 // HArm64IntermediateAddress instruction yet.
1983 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001984 __ Add(OutputRegister(instruction),
1985 InputRegisterAt(instruction, 0),
1986 Operand(InputOperandAt(instruction, 1)));
1987}
1988
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001989void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001990 LocationSummary* locations =
1991 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001992 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1993 if (instr->GetOpKind() == HInstruction::kSub &&
1994 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00001995 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001996 // Don't allocate register for Mneg instruction.
1997 } else {
1998 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1999 Location::RequiresRegister());
2000 }
2001 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2002 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002003 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2004}
2005
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002006void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002007 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002008 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2009 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002010
2011 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2012 // This fixup should be carried out for all multiply-accumulate instructions:
2013 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2014 if (instr->GetType() == Primitive::kPrimLong &&
2015 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2016 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2017 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2018 if (prev->IsLoadOrStore()) {
2019 // Make sure we emit only exactly one nop.
2020 vixl::CodeBufferCheckScope scope(masm,
2021 vixl::kInstructionSize,
2022 vixl::CodeBufferCheckScope::kCheck,
2023 vixl::CodeBufferCheckScope::kExactSize);
2024 __ nop();
2025 }
2026 }
2027
2028 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002029 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002030 __ Madd(res, mul_left, mul_right, accumulator);
2031 } else {
2032 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002033 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002034 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002035 __ Mneg(res, mul_left, mul_right);
2036 } else {
2037 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2038 __ Msub(res, mul_left, mul_right, accumulator);
2039 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002040 }
2041}
2042
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002043void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002044 bool object_array_get_with_read_barrier =
2045 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002046 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002047 new (GetGraph()->GetArena()) LocationSummary(instruction,
2048 object_array_get_with_read_barrier ?
2049 LocationSummary::kCallOnSlowPath :
2050 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002051 locations->SetInAt(0, Location::RequiresRegister());
2052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002053 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2054 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2055 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002056 // The output overlaps in the case of an object array get with
2057 // read barriers enabled: we do not want the move to overwrite the
2058 // array's location, as we need it to emit the read barrier.
2059 locations->SetOut(
2060 Location::RequiresRegister(),
2061 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002062 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002063}
2064
2065void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002066 Primitive::Type type = instruction->GetType();
2067 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002068 LocationSummary* locations = instruction->GetLocations();
2069 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002070 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002071 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002072
Alexandre Ramesd921d642015-04-16 15:07:16 +01002073 MacroAssembler* masm = GetVIXLAssembler();
2074 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002075 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002076 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002077
Roland Levillain44015862016-01-22 11:47:17 +00002078 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2079 // Object ArrayGet with Baker's read barrier case.
2080 Register temp = temps.AcquireW();
2081 // The read barrier instrumentation does not support the
2082 // HArm64IntermediateAddress instruction yet.
2083 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2084 // Note that a potential implicit null check is handled in the
2085 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2086 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2087 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002088 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002089 // General case.
2090 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002091 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002092 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2093 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002094 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002095 Register temp = temps.AcquireSameSizeAs(obj);
2096 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2097 // The read barrier instrumentation does not support the
2098 // HArm64IntermediateAddress instruction yet.
2099 DCHECK(!kEmitCompilerReadBarrier);
2100 // We do not need to compute the intermediate address from the array: the
2101 // input instruction has done it already. See the comment in
2102 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2103 if (kIsDebugBuild) {
2104 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2105 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2106 }
2107 temp = obj;
2108 } else {
2109 __ Add(temp, obj, offset);
2110 }
2111 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2112 }
2113
2114 codegen_->Load(type, OutputCPURegister(instruction), source);
2115 codegen_->MaybeRecordImplicitNullCheck(instruction);
2116
2117 if (type == Primitive::kPrimNot) {
2118 static_assert(
2119 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2120 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2121 Location obj_loc = locations->InAt(0);
2122 if (index.IsConstant()) {
2123 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2124 } else {
2125 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2126 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002127 }
Roland Levillain4d027112015-07-01 15:41:14 +01002128 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002129}
2130
Alexandre Rames5319def2014-10-23 10:03:10 +01002131void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2132 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2133 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002135}
2136
2137void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002138 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002139 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002140 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002141 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002142}
2143
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002144void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002145 Primitive::Type value_type = instruction->GetComponentType();
2146
2147 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2148 bool object_array_set_with_read_barrier =
2149 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2151 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002152 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2153 LocationSummary::kCallOnSlowPath :
2154 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002155 locations->SetInAt(0, Location::RequiresRegister());
2156 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002157 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002158 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002159 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002160 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002161 }
2162}
2163
2164void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2165 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002166 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002167 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002168 bool needs_write_barrier =
2169 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002170
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002171 Register array = InputRegisterAt(instruction, 0);
2172 CPURegister value = InputCPURegisterAt(instruction, 2);
2173 CPURegister source = value;
2174 Location index = locations->InAt(1);
2175 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2176 MemOperand destination = HeapOperand(array);
2177 MacroAssembler* masm = GetVIXLAssembler();
2178 BlockPoolsScope block_pools(masm);
2179
2180 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002181 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002182 if (index.IsConstant()) {
2183 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2184 destination = HeapOperand(array, offset);
2185 } else {
2186 UseScratchRegisterScope temps(masm);
2187 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002188 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002189 // The read barrier instrumentation does not support the
2190 // HArm64IntermediateAddress instruction yet.
2191 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002192 // We do not need to compute the intermediate address from the array: the
2193 // input instruction has done it already. See the comment in
2194 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2195 if (kIsDebugBuild) {
2196 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2197 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2198 }
2199 temp = array;
2200 } else {
2201 __ Add(temp, array, offset);
2202 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002203 destination = HeapOperand(temp,
2204 XRegisterFrom(index),
2205 LSL,
2206 Primitive::ComponentSizeShift(value_type));
2207 }
2208 codegen_->Store(value_type, value, destination);
2209 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002210 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002211 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002212 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002213 vixl::Label done;
2214 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002215 {
2216 // We use a block to end the scratch scope before the write barrier, thus
2217 // freeing the temporary registers so they can be used in `MarkGCCard`.
2218 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002219 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002220 if (index.IsConstant()) {
2221 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002222 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002223 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002224 destination = HeapOperand(temp,
2225 XRegisterFrom(index),
2226 LSL,
2227 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002228 }
2229
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002230 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2231 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2232 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2233
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002234 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002235 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2236 codegen_->AddSlowPath(slow_path);
2237 if (instruction->GetValueCanBeNull()) {
2238 vixl::Label non_zero;
2239 __ Cbnz(Register(value), &non_zero);
2240 if (!index.IsConstant()) {
2241 __ Add(temp, array, offset);
2242 }
2243 __ Str(wzr, destination);
2244 codegen_->MaybeRecordImplicitNullCheck(instruction);
2245 __ B(&done);
2246 __ Bind(&non_zero);
2247 }
2248
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002249 if (kEmitCompilerReadBarrier) {
2250 // When read barriers are enabled, the type checking
2251 // instrumentation requires two read barriers:
2252 //
2253 // __ Mov(temp2, temp);
2254 // // /* HeapReference<Class> */ temp = temp->component_type_
2255 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002256 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002257 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2258 //
2259 // // /* HeapReference<Class> */ temp2 = value->klass_
2260 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002261 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002262 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2263 //
2264 // __ Cmp(temp, temp2);
2265 //
2266 // However, the second read barrier may trash `temp`, as it
2267 // is a temporary register, and as such would not be saved
2268 // along with live registers before calling the runtime (nor
2269 // restored afterwards). So in this case, we bail out and
2270 // delegate the work to the array set slow path.
2271 //
2272 // TODO: Extend the register allocator to support a new
2273 // "(locally) live temp" location so as to avoid always
2274 // going into the slow path when read barriers are enabled.
2275 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002276 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002277 Register temp2 = temps.AcquireSameSizeAs(array);
2278 // /* HeapReference<Class> */ temp = array->klass_
2279 __ Ldr(temp, HeapOperand(array, class_offset));
2280 codegen_->MaybeRecordImplicitNullCheck(instruction);
2281 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2282
2283 // /* HeapReference<Class> */ temp = temp->component_type_
2284 __ Ldr(temp, HeapOperand(temp, component_offset));
2285 // /* HeapReference<Class> */ temp2 = value->klass_
2286 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2287 // If heap poisoning is enabled, no need to unpoison `temp`
2288 // nor `temp2`, as we are comparing two poisoned references.
2289 __ Cmp(temp, temp2);
2290
2291 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2292 vixl::Label do_put;
2293 __ B(eq, &do_put);
2294 // If heap poisoning is enabled, the `temp` reference has
2295 // not been unpoisoned yet; unpoison it now.
2296 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2297
2298 // /* HeapReference<Class> */ temp = temp->super_class_
2299 __ Ldr(temp, HeapOperand(temp, super_offset));
2300 // If heap poisoning is enabled, no need to unpoison
2301 // `temp`, as we are comparing against null below.
2302 __ Cbnz(temp, slow_path->GetEntryLabel());
2303 __ Bind(&do_put);
2304 } else {
2305 __ B(ne, slow_path->GetEntryLabel());
2306 }
2307 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002308 }
2309 }
2310
2311 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002312 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002313 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002314 __ Mov(temp2, value.W());
2315 GetAssembler()->PoisonHeapReference(temp2);
2316 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002317 }
2318
2319 if (!index.IsConstant()) {
2320 __ Add(temp, array, offset);
2321 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002322 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002323
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002324 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002325 codegen_->MaybeRecordImplicitNullCheck(instruction);
2326 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002327 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002328
2329 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2330
2331 if (done.IsLinked()) {
2332 __ Bind(&done);
2333 }
2334
2335 if (slow_path != nullptr) {
2336 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002337 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002338 }
2339}
2340
Alexandre Rames67555f72014-11-18 10:55:16 +00002341void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002342 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2343 ? LocationSummary::kCallOnSlowPath
2344 : LocationSummary::kNoCall;
2345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002346 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002347 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002348 if (instruction->HasUses()) {
2349 locations->SetOut(Location::SameAsFirstInput());
2350 }
2351}
2352
2353void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002354 BoundsCheckSlowPathARM64* slow_path =
2355 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002356 codegen_->AddSlowPath(slow_path);
2357
2358 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2359 __ B(slow_path->GetEntryLabel(), hs);
2360}
2361
Alexandre Rames67555f72014-11-18 10:55:16 +00002362void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2363 LocationSummary* locations =
2364 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2365 locations->SetInAt(0, Location::RequiresRegister());
2366 if (check->HasUses()) {
2367 locations->SetOut(Location::SameAsFirstInput());
2368 }
2369}
2370
2371void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2372 // We assume the class is not null.
2373 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2374 check->GetLoadClass(), check, check->GetDexPc(), true);
2375 codegen_->AddSlowPath(slow_path);
2376 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2377}
2378
Roland Levillain1a653882016-03-18 18:05:57 +00002379static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2380 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2381 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2382}
2383
2384void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2385 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2386 Location rhs_loc = instruction->GetLocations()->InAt(1);
2387 if (rhs_loc.IsConstant()) {
2388 // 0.0 is the only immediate that can be encoded directly in
2389 // an FCMP instruction.
2390 //
2391 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2392 // specify that in a floating-point comparison, positive zero
2393 // and negative zero are considered equal, so we can use the
2394 // literal 0.0 for both cases here.
2395 //
2396 // Note however that some methods (Float.equal, Float.compare,
2397 // Float.compareTo, Double.equal, Double.compare,
2398 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2399 // StrictMath.min) consider 0.0 to be (strictly) greater than
2400 // -0.0. So if we ever translate calls to these methods into a
2401 // HCompare instruction, we must handle the -0.0 case with
2402 // care here.
2403 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2404 __ Fcmp(lhs_reg, 0.0);
2405 } else {
2406 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2407 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002408}
2409
Serban Constantinescu02164b32014-11-13 14:05:07 +00002410void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002411 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002412 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2413 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002414 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002415 case Primitive::kPrimBoolean:
2416 case Primitive::kPrimByte:
2417 case Primitive::kPrimShort:
2418 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002419 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002420 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002421 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002422 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2424 break;
2425 }
2426 case Primitive::kPrimFloat:
2427 case Primitive::kPrimDouble: {
2428 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002429 locations->SetInAt(1,
2430 IsFloatingPointZeroConstant(compare->InputAt(1))
2431 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2432 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002433 locations->SetOut(Location::RequiresRegister());
2434 break;
2435 }
2436 default:
2437 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2438 }
2439}
2440
2441void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2442 Primitive::Type in_type = compare->InputAt(0)->GetType();
2443
2444 // 0 if: left == right
2445 // 1 if: left > right
2446 // -1 if: left < right
2447 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002448 case Primitive::kPrimBoolean:
2449 case Primitive::kPrimByte:
2450 case Primitive::kPrimShort:
2451 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002452 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002453 case Primitive::kPrimLong: {
2454 Register result = OutputRegister(compare);
2455 Register left = InputRegisterAt(compare, 0);
2456 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002457 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002458 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2459 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002460 break;
2461 }
2462 case Primitive::kPrimFloat:
2463 case Primitive::kPrimDouble: {
2464 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002465 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002466 __ Cset(result, ne);
2467 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 break;
2469 }
2470 default:
2471 LOG(FATAL) << "Unimplemented compare type " << in_type;
2472 }
2473}
2474
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002475void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002477
2478 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2479 locations->SetInAt(0, Location::RequiresFpuRegister());
2480 locations->SetInAt(1,
2481 IsFloatingPointZeroConstant(instruction->InputAt(1))
2482 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2483 : Location::RequiresFpuRegister());
2484 } else {
2485 // Integer cases.
2486 locations->SetInAt(0, Location::RequiresRegister());
2487 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2488 }
2489
David Brazdilb3e773e2016-01-26 11:28:37 +00002490 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002492 }
2493}
2494
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002495void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002496 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002497 return;
2498 }
2499
2500 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002501 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002502 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002503
Roland Levillain7f63c522015-07-13 15:54:55 +00002504 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002505 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002506 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002507 } else {
2508 // Integer cases.
2509 Register lhs = InputRegisterAt(instruction, 0);
2510 Operand rhs = InputOperandAt(instruction, 1);
2511 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002512 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002513 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002514}
2515
2516#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2517 M(Equal) \
2518 M(NotEqual) \
2519 M(LessThan) \
2520 M(LessThanOrEqual) \
2521 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002522 M(GreaterThanOrEqual) \
2523 M(Below) \
2524 M(BelowOrEqual) \
2525 M(Above) \
2526 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002527#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002528void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2529void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002530FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002531#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002532#undef FOR_EACH_CONDITION_INSTRUCTION
2533
Zheng Xuc6667102015-05-15 16:08:45 +08002534void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2535 DCHECK(instruction->IsDiv() || instruction->IsRem());
2536
2537 LocationSummary* locations = instruction->GetLocations();
2538 Location second = locations->InAt(1);
2539 DCHECK(second.IsConstant());
2540
2541 Register out = OutputRegister(instruction);
2542 Register dividend = InputRegisterAt(instruction, 0);
2543 int64_t imm = Int64FromConstant(second.GetConstant());
2544 DCHECK(imm == 1 || imm == -1);
2545
2546 if (instruction->IsRem()) {
2547 __ Mov(out, 0);
2548 } else {
2549 if (imm == 1) {
2550 __ Mov(out, dividend);
2551 } else {
2552 __ Neg(out, dividend);
2553 }
2554 }
2555}
2556
2557void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2558 DCHECK(instruction->IsDiv() || instruction->IsRem());
2559
2560 LocationSummary* locations = instruction->GetLocations();
2561 Location second = locations->InAt(1);
2562 DCHECK(second.IsConstant());
2563
2564 Register out = OutputRegister(instruction);
2565 Register dividend = InputRegisterAt(instruction, 0);
2566 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002567 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002568 int ctz_imm = CTZ(abs_imm);
2569
2570 UseScratchRegisterScope temps(GetVIXLAssembler());
2571 Register temp = temps.AcquireSameSizeAs(out);
2572
2573 if (instruction->IsDiv()) {
2574 __ Add(temp, dividend, abs_imm - 1);
2575 __ Cmp(dividend, 0);
2576 __ Csel(out, temp, dividend, lt);
2577 if (imm > 0) {
2578 __ Asr(out, out, ctz_imm);
2579 } else {
2580 __ Neg(out, Operand(out, ASR, ctz_imm));
2581 }
2582 } else {
2583 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2584 __ Asr(temp, dividend, bits - 1);
2585 __ Lsr(temp, temp, bits - ctz_imm);
2586 __ Add(out, dividend, temp);
2587 __ And(out, out, abs_imm - 1);
2588 __ Sub(out, out, temp);
2589 }
2590}
2591
2592void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2593 DCHECK(instruction->IsDiv() || instruction->IsRem());
2594
2595 LocationSummary* locations = instruction->GetLocations();
2596 Location second = locations->InAt(1);
2597 DCHECK(second.IsConstant());
2598
2599 Register out = OutputRegister(instruction);
2600 Register dividend = InputRegisterAt(instruction, 0);
2601 int64_t imm = Int64FromConstant(second.GetConstant());
2602
2603 Primitive::Type type = instruction->GetResultType();
2604 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2605
2606 int64_t magic;
2607 int shift;
2608 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2609
2610 UseScratchRegisterScope temps(GetVIXLAssembler());
2611 Register temp = temps.AcquireSameSizeAs(out);
2612
2613 // temp = get_high(dividend * magic)
2614 __ Mov(temp, magic);
2615 if (type == Primitive::kPrimLong) {
2616 __ Smulh(temp, dividend, temp);
2617 } else {
2618 __ Smull(temp.X(), dividend, temp);
2619 __ Lsr(temp.X(), temp.X(), 32);
2620 }
2621
2622 if (imm > 0 && magic < 0) {
2623 __ Add(temp, temp, dividend);
2624 } else if (imm < 0 && magic > 0) {
2625 __ Sub(temp, temp, dividend);
2626 }
2627
2628 if (shift != 0) {
2629 __ Asr(temp, temp, shift);
2630 }
2631
2632 if (instruction->IsDiv()) {
2633 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2634 } else {
2635 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2636 // TODO: Strength reduction for msub.
2637 Register temp_imm = temps.AcquireSameSizeAs(out);
2638 __ Mov(temp_imm, imm);
2639 __ Msub(out, temp, temp_imm, dividend);
2640 }
2641}
2642
2643void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2644 DCHECK(instruction->IsDiv() || instruction->IsRem());
2645 Primitive::Type type = instruction->GetResultType();
2646 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2647
2648 LocationSummary* locations = instruction->GetLocations();
2649 Register out = OutputRegister(instruction);
2650 Location second = locations->InAt(1);
2651
2652 if (second.IsConstant()) {
2653 int64_t imm = Int64FromConstant(second.GetConstant());
2654
2655 if (imm == 0) {
2656 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2657 } else if (imm == 1 || imm == -1) {
2658 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002659 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002660 DivRemByPowerOfTwo(instruction);
2661 } else {
2662 DCHECK(imm <= -2 || imm >= 2);
2663 GenerateDivRemWithAnyConstant(instruction);
2664 }
2665 } else {
2666 Register dividend = InputRegisterAt(instruction, 0);
2667 Register divisor = InputRegisterAt(instruction, 1);
2668 if (instruction->IsDiv()) {
2669 __ Sdiv(out, dividend, divisor);
2670 } else {
2671 UseScratchRegisterScope temps(GetVIXLAssembler());
2672 Register temp = temps.AcquireSameSizeAs(out);
2673 __ Sdiv(temp, dividend, divisor);
2674 __ Msub(out, temp, divisor, dividend);
2675 }
2676 }
2677}
2678
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002679void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2680 LocationSummary* locations =
2681 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2682 switch (div->GetResultType()) {
2683 case Primitive::kPrimInt:
2684 case Primitive::kPrimLong:
2685 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002686 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2688 break;
2689
2690 case Primitive::kPrimFloat:
2691 case Primitive::kPrimDouble:
2692 locations->SetInAt(0, Location::RequiresFpuRegister());
2693 locations->SetInAt(1, Location::RequiresFpuRegister());
2694 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2695 break;
2696
2697 default:
2698 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2699 }
2700}
2701
2702void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2703 Primitive::Type type = div->GetResultType();
2704 switch (type) {
2705 case Primitive::kPrimInt:
2706 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002707 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002708 break;
2709
2710 case Primitive::kPrimFloat:
2711 case Primitive::kPrimDouble:
2712 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2713 break;
2714
2715 default:
2716 LOG(FATAL) << "Unexpected div type " << type;
2717 }
2718}
2719
Alexandre Rames67555f72014-11-18 10:55:16 +00002720void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002721 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2722 ? LocationSummary::kCallOnSlowPath
2723 : LocationSummary::kNoCall;
2724 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002725 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2726 if (instruction->HasUses()) {
2727 locations->SetOut(Location::SameAsFirstInput());
2728 }
2729}
2730
2731void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2732 SlowPathCodeARM64* slow_path =
2733 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2734 codegen_->AddSlowPath(slow_path);
2735 Location value = instruction->GetLocations()->InAt(0);
2736
Alexandre Rames3e69f162014-12-10 10:36:50 +00002737 Primitive::Type type = instruction->GetType();
2738
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002739 if (!Primitive::IsIntegralType(type)) {
2740 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002741 return;
2742 }
2743
Alexandre Rames67555f72014-11-18 10:55:16 +00002744 if (value.IsConstant()) {
2745 int64_t divisor = Int64ConstantFrom(value);
2746 if (divisor == 0) {
2747 __ B(slow_path->GetEntryLabel());
2748 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002749 // A division by a non-null constant is valid. We don't need to perform
2750 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002751 }
2752 } else {
2753 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2754 }
2755}
2756
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002757void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2758 LocationSummary* locations =
2759 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2760 locations->SetOut(Location::ConstantLocation(constant));
2761}
2762
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002763void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2764 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002765 // Will be generated at use site.
2766}
2767
Alexandre Rames5319def2014-10-23 10:03:10 +01002768void LocationsBuilderARM64::VisitExit(HExit* exit) {
2769 exit->SetLocations(nullptr);
2770}
2771
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002772void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002773}
2774
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002775void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2776 LocationSummary* locations =
2777 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2778 locations->SetOut(Location::ConstantLocation(constant));
2779}
2780
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002781void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002782 // Will be generated at use site.
2783}
2784
David Brazdilfc6a86a2015-06-26 10:33:45 +00002785void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002786 DCHECK(!successor->IsExitBlock());
2787 HBasicBlock* block = got->GetBlock();
2788 HInstruction* previous = got->GetPrevious();
2789 HLoopInformation* info = block->GetLoopInformation();
2790
David Brazdil46e2a392015-03-16 17:31:52 +00002791 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002792 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2793 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2794 return;
2795 }
2796 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2797 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2798 }
2799 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002800 __ B(codegen_->GetLabelOf(successor));
2801 }
2802}
2803
David Brazdilfc6a86a2015-06-26 10:33:45 +00002804void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2805 got->SetLocations(nullptr);
2806}
2807
2808void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2809 HandleGoto(got, got->GetSuccessor());
2810}
2811
2812void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2813 try_boundary->SetLocations(nullptr);
2814}
2815
2816void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2817 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2818 if (!successor->IsExitBlock()) {
2819 HandleGoto(try_boundary, successor);
2820 }
2821}
2822
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002823void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002824 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002825 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002826 vixl::Label* false_target) {
2827 // FP branching requires both targets to be explicit. If either of the targets
2828 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2829 vixl::Label fallthrough_target;
2830 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002831
David Brazdil0debae72015-11-12 18:37:00 +00002832 if (true_target == nullptr && false_target == nullptr) {
2833 // Nothing to do. The code always falls through.
2834 return;
2835 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002836 // Constant condition, statically compared against "true" (integer value 1).
2837 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002838 if (true_target != nullptr) {
2839 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002840 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002841 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002842 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002843 if (false_target != nullptr) {
2844 __ B(false_target);
2845 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002846 }
David Brazdil0debae72015-11-12 18:37:00 +00002847 return;
2848 }
2849
2850 // The following code generates these patterns:
2851 // (1) true_target == nullptr && false_target != nullptr
2852 // - opposite condition true => branch to false_target
2853 // (2) true_target != nullptr && false_target == nullptr
2854 // - condition true => branch to true_target
2855 // (3) true_target != nullptr && false_target != nullptr
2856 // - condition true => branch to true_target
2857 // - branch to false_target
2858 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002859 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002860 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002861 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002862 if (true_target == nullptr) {
2863 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2864 } else {
2865 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2866 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002867 } else {
2868 // The condition instruction has not been materialized, use its inputs as
2869 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002870 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002871
David Brazdil0debae72015-11-12 18:37:00 +00002872 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002873 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002874 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002875 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002876 IfCondition opposite_condition = condition->GetOppositeCondition();
2877 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002878 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002879 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002880 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002881 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002882 // Integer cases.
2883 Register lhs = InputRegisterAt(condition, 0);
2884 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002885
2886 Condition arm64_cond;
2887 vixl::Label* non_fallthrough_target;
2888 if (true_target == nullptr) {
2889 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2890 non_fallthrough_target = false_target;
2891 } else {
2892 arm64_cond = ARM64Condition(condition->GetCondition());
2893 non_fallthrough_target = true_target;
2894 }
2895
Aart Bik086d27e2016-01-20 17:02:00 -08002896 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2897 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002898 switch (arm64_cond) {
2899 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002900 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002901 break;
2902 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002903 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002904 break;
2905 case lt:
2906 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002907 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002908 break;
2909 case ge:
2910 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002911 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002912 break;
2913 default:
2914 // Without the `static_cast` the compiler throws an error for
2915 // `-Werror=sign-promo`.
2916 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2917 }
2918 } else {
2919 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002920 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002921 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002922 }
2923 }
David Brazdil0debae72015-11-12 18:37:00 +00002924
2925 // If neither branch falls through (case 3), the conditional branch to `true_target`
2926 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2927 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002928 __ B(false_target);
2929 }
David Brazdil0debae72015-11-12 18:37:00 +00002930
2931 if (fallthrough_target.IsLinked()) {
2932 __ Bind(&fallthrough_target);
2933 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002934}
2935
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002936void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002938 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002939 locations->SetInAt(0, Location::RequiresRegister());
2940 }
2941}
2942
2943void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002944 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2945 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2946 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2947 nullptr : codegen_->GetLabelOf(true_successor);
2948 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2949 nullptr : codegen_->GetLabelOf(false_successor);
2950 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002951}
2952
2953void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2954 LocationSummary* locations = new (GetGraph()->GetArena())
2955 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002956 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002957 locations->SetInAt(0, Location::RequiresRegister());
2958 }
2959}
2960
2961void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002962 SlowPathCodeARM64* slow_path =
2963 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002964 GenerateTestAndBranch(deoptimize,
2965 /* condition_input_index */ 0,
2966 slow_path->GetEntryLabel(),
2967 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002968}
2969
David Brazdilc0b601b2016-02-08 14:20:45 +00002970static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2971 return condition->IsCondition() &&
2972 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2973}
2974
Alexandre Rames880f1192016-06-13 16:04:50 +01002975static inline Condition GetConditionForSelect(HCondition* condition) {
2976 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002977 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2978 : ARM64Condition(cond);
2979}
2980
David Brazdil74eb1b22015-12-14 11:44:01 +00002981void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002983 if (Primitive::IsFloatingPointType(select->GetType())) {
2984 locations->SetInAt(0, Location::RequiresFpuRegister());
2985 locations->SetInAt(1, Location::RequiresFpuRegister());
2986 locations->SetOut(Location::RequiresFpuRegister());
2987 } else {
2988 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2989 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2990 bool is_true_value_constant = cst_true_value != nullptr;
2991 bool is_false_value_constant = cst_false_value != nullptr;
2992 // Ask VIXL whether we should synthesize constants in registers.
2993 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
2994 Operand true_op = is_true_value_constant ?
2995 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
2996 Operand false_op = is_false_value_constant ?
2997 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
2998 bool true_value_in_register = false;
2999 bool false_value_in_register = false;
3000 MacroAssembler::GetCselSynthesisInformation(
3001 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3002 true_value_in_register |= !is_true_value_constant;
3003 false_value_in_register |= !is_false_value_constant;
3004
3005 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3006 : Location::ConstantLocation(cst_true_value));
3007 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3008 : Location::ConstantLocation(cst_false_value));
3009 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003010 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003011
David Brazdil74eb1b22015-12-14 11:44:01 +00003012 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3013 locations->SetInAt(2, Location::RequiresRegister());
3014 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003015}
3016
3017void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003018 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003019 Condition csel_cond;
3020
3021 if (IsBooleanValueOrMaterializedCondition(cond)) {
3022 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003023 // Use the condition flags set by the previous instruction.
3024 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003025 } else {
3026 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003027 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003028 }
3029 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003030 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003031 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003032 } else {
3033 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003034 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003035 }
3036
Alexandre Rames880f1192016-06-13 16:04:50 +01003037 if (Primitive::IsFloatingPointType(select->GetType())) {
3038 __ Fcsel(OutputFPRegister(select),
3039 InputFPRegisterAt(select, 1),
3040 InputFPRegisterAt(select, 0),
3041 csel_cond);
3042 } else {
3043 __ Csel(OutputRegister(select),
3044 InputOperandAt(select, 1),
3045 InputOperandAt(select, 0),
3046 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003047 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003048}
3049
David Srbecky0cf44932015-12-09 14:09:59 +00003050void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3051 new (GetGraph()->GetArena()) LocationSummary(info);
3052}
3053
David Srbeckyd28f4a02016-03-14 17:14:24 +00003054void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3055 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003056}
3057
3058void CodeGeneratorARM64::GenerateNop() {
3059 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003060}
3061
Alexandre Rames5319def2014-10-23 10:03:10 +01003062void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003063 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003064}
3065
3066void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003067 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003068}
3069
3070void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003071 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003072}
3073
3074void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003075 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003076}
3077
Roland Levillain44015862016-01-22 11:47:17 +00003078static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3079 return kEmitCompilerReadBarrier &&
3080 (kUseBakerReadBarrier ||
3081 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3082 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3083 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3084}
3085
Alexandre Rames67555f72014-11-18 10:55:16 +00003086void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003087 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003088 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3089 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003090 case TypeCheckKind::kExactCheck:
3091 case TypeCheckKind::kAbstractClassCheck:
3092 case TypeCheckKind::kClassHierarchyCheck:
3093 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003094 call_kind =
3095 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003096 break;
3097 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003098 case TypeCheckKind::kUnresolvedCheck:
3099 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003100 call_kind = LocationSummary::kCallOnSlowPath;
3101 break;
3102 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003103
Alexandre Rames67555f72014-11-18 10:55:16 +00003104 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003105 locations->SetInAt(0, Location::RequiresRegister());
3106 locations->SetInAt(1, Location::RequiresRegister());
3107 // The "out" register is used as a temporary, so it overlaps with the inputs.
3108 // Note that TypeCheckSlowPathARM64 uses this register too.
3109 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3110 // When read barriers are enabled, we need a temporary register for
3111 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003112 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003113 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003114 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003115}
3116
3117void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003118 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003119 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003120 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003121 Register obj = InputRegisterAt(instruction, 0);
3122 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003123 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003124 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003125 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3126 locations->GetTemp(0) :
3127 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003128 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3129 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3130 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3131 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003132
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003133 vixl::Label done, zero;
3134 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003135
3136 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003137 // Avoid null check if we know `obj` is not null.
3138 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 __ Cbz(obj, &zero);
3140 }
3141
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003142 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003143 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003144
Roland Levillain44015862016-01-22 11:47:17 +00003145 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003146 case TypeCheckKind::kExactCheck: {
3147 __ Cmp(out, cls);
3148 __ Cset(out, eq);
3149 if (zero.IsLinked()) {
3150 __ B(&done);
3151 }
3152 break;
3153 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003154
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003155 case TypeCheckKind::kAbstractClassCheck: {
3156 // If the class is abstract, we eagerly fetch the super class of the
3157 // object to avoid doing a comparison we know will fail.
3158 vixl::Label loop, success;
3159 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003160 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003161 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003162 // If `out` is null, we use it for the result, and jump to `done`.
3163 __ Cbz(out, &done);
3164 __ Cmp(out, cls);
3165 __ B(ne, &loop);
3166 __ Mov(out, 1);
3167 if (zero.IsLinked()) {
3168 __ B(&done);
3169 }
3170 break;
3171 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003172
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003173 case TypeCheckKind::kClassHierarchyCheck: {
3174 // Walk over the class hierarchy to find a match.
3175 vixl::Label loop, success;
3176 __ Bind(&loop);
3177 __ Cmp(out, cls);
3178 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003179 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003180 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003181 __ Cbnz(out, &loop);
3182 // If `out` is null, we use it for the result, and jump to `done`.
3183 __ B(&done);
3184 __ Bind(&success);
3185 __ Mov(out, 1);
3186 if (zero.IsLinked()) {
3187 __ B(&done);
3188 }
3189 break;
3190 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003192 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003193 // Do an exact check.
3194 vixl::Label exact_check;
3195 __ Cmp(out, cls);
3196 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003197 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003198 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003199 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200 // If `out` is null, we use it for the result, and jump to `done`.
3201 __ Cbz(out, &done);
3202 __ Ldrh(out, HeapOperand(out, primitive_offset));
3203 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3204 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003205 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003206 __ Mov(out, 1);
3207 __ B(&done);
3208 break;
3209 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003211 case TypeCheckKind::kArrayCheck: {
3212 __ Cmp(out, cls);
3213 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003214 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3215 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003216 codegen_->AddSlowPath(slow_path);
3217 __ B(ne, slow_path->GetEntryLabel());
3218 __ Mov(out, 1);
3219 if (zero.IsLinked()) {
3220 __ B(&done);
3221 }
3222 break;
3223 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003224
Calin Juravle98893e12015-10-02 21:05:03 +01003225 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003226 case TypeCheckKind::kInterfaceCheck: {
3227 // Note that we indeed only call on slow path, but we always go
3228 // into the slow path for the unresolved and interface check
3229 // cases.
3230 //
3231 // We cannot directly call the InstanceofNonTrivial runtime
3232 // entry point without resorting to a type checking slow path
3233 // here (i.e. by calling InvokeRuntime directly), as it would
3234 // require to assign fixed registers for the inputs of this
3235 // HInstanceOf instruction (following the runtime calling
3236 // convention), which might be cluttered by the potential first
3237 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003238 //
3239 // TODO: Introduce a new runtime entry point taking the object
3240 // to test (instead of its class) as argument, and let it deal
3241 // with the read barrier issues. This will let us refactor this
3242 // case of the `switch` code as it was previously (with a direct
3243 // call to the runtime not using a type checking slow path).
3244 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003245 DCHECK(locations->OnlyCallsOnSlowPath());
3246 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3247 /* is_fatal */ false);
3248 codegen_->AddSlowPath(slow_path);
3249 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003250 if (zero.IsLinked()) {
3251 __ B(&done);
3252 }
3253 break;
3254 }
3255 }
3256
3257 if (zero.IsLinked()) {
3258 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003259 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003260 }
3261
3262 if (done.IsLinked()) {
3263 __ Bind(&done);
3264 }
3265
3266 if (slow_path != nullptr) {
3267 __ Bind(slow_path->GetExitLabel());
3268 }
3269}
3270
3271void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3272 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3273 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3274
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003275 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3276 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003277 case TypeCheckKind::kExactCheck:
3278 case TypeCheckKind::kAbstractClassCheck:
3279 case TypeCheckKind::kClassHierarchyCheck:
3280 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003281 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3282 LocationSummary::kCallOnSlowPath :
3283 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003284 break;
3285 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003286 case TypeCheckKind::kUnresolvedCheck:
3287 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003288 call_kind = LocationSummary::kCallOnSlowPath;
3289 break;
3290 }
3291
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3293 locations->SetInAt(0, Location::RequiresRegister());
3294 locations->SetInAt(1, Location::RequiresRegister());
3295 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3296 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003297 // When read barriers are enabled, we need an additional temporary
3298 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003299 if (TypeCheckNeedsATemporary(type_check_kind)) {
3300 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003301 }
3302}
3303
3304void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003305 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003306 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003307 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003308 Register obj = InputRegisterAt(instruction, 0);
3309 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003310 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003311 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3312 locations->GetTemp(1) :
3313 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003314 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003315 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3316 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3317 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3318 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003320 bool is_type_check_slow_path_fatal =
3321 (type_check_kind == TypeCheckKind::kExactCheck ||
3322 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3323 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3324 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3325 !instruction->CanThrowIntoCatchBlock();
3326 SlowPathCodeARM64* type_check_slow_path =
3327 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3328 is_type_check_slow_path_fatal);
3329 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003330
3331 vixl::Label done;
3332 // Avoid null check if we know obj is not null.
3333 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003334 __ Cbz(obj, &done);
3335 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003336
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003338 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003339
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003340 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003341 case TypeCheckKind::kExactCheck:
3342 case TypeCheckKind::kArrayCheck: {
3343 __ Cmp(temp, cls);
3344 // Jump to slow path for throwing the exception or doing a
3345 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003346 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003347 break;
3348 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003349
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003350 case TypeCheckKind::kAbstractClassCheck: {
3351 // If the class is abstract, we eagerly fetch the super class of the
3352 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003353 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003354 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003355 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003356 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003357
3358 // If the class reference currently in `temp` is not null, jump
3359 // to the `compare_classes` label to compare it with the checked
3360 // class.
3361 __ Cbnz(temp, &compare_classes);
3362 // Otherwise, jump to the slow path to throw the exception.
3363 //
3364 // But before, move back the object's class into `temp` before
3365 // going into the slow path, as it has been overwritten in the
3366 // meantime.
3367 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003368 GenerateReferenceLoadTwoRegisters(
3369 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003370 __ B(type_check_slow_path->GetEntryLabel());
3371
3372 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003373 __ Cmp(temp, cls);
3374 __ B(ne, &loop);
3375 break;
3376 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003377
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003378 case TypeCheckKind::kClassHierarchyCheck: {
3379 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003380 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003381 __ Bind(&loop);
3382 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003383 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003384
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003385 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003386 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003387
3388 // If the class reference currently in `temp` is not null, jump
3389 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003390 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391 // Otherwise, jump to the slow path to throw the exception.
3392 //
3393 // But before, move back the object's class into `temp` before
3394 // going into the slow path, as it has been overwritten in the
3395 // meantime.
3396 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003397 GenerateReferenceLoadTwoRegisters(
3398 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003399 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003400 break;
3401 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003402
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003403 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003404 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003405 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003406 __ Cmp(temp, cls);
3407 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003408
3409 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003411 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003412
3413 // If the component type is not null (i.e. the object is indeed
3414 // an array), jump to label `check_non_primitive_component_type`
3415 // to further check that this component type is not a primitive
3416 // type.
3417 __ Cbnz(temp, &check_non_primitive_component_type);
3418 // Otherwise, jump to the slow path to throw the exception.
3419 //
3420 // But before, move back the object's class into `temp` before
3421 // going into the slow path, as it has been overwritten in the
3422 // meantime.
3423 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003424 GenerateReferenceLoadTwoRegisters(
3425 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426 __ B(type_check_slow_path->GetEntryLabel());
3427
3428 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003429 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3430 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431 __ Cbz(temp, &done);
3432 // Same comment as above regarding `temp` and the slow path.
3433 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003434 GenerateReferenceLoadTwoRegisters(
3435 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003436 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003437 break;
3438 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003439
Calin Juravle98893e12015-10-02 21:05:03 +01003440 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003441 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003442 // We always go into the type check slow path for the unresolved
3443 // and interface check cases.
3444 //
3445 // We cannot directly call the CheckCast runtime entry point
3446 // without resorting to a type checking slow path here (i.e. by
3447 // calling InvokeRuntime directly), as it would require to
3448 // assign fixed registers for the inputs of this HInstanceOf
3449 // instruction (following the runtime calling convention), which
3450 // might be cluttered by the potential first read barrier
3451 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003452 //
3453 // TODO: Introduce a new runtime entry point taking the object
3454 // to test (instead of its class) as argument, and let it deal
3455 // with the read barrier issues. This will let us refactor this
3456 // case of the `switch` code as it was previously (with a direct
3457 // call to the runtime not using a type checking slow path).
3458 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003459 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003460 break;
3461 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003462 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003463
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003464 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003465}
3466
Alexandre Rames5319def2014-10-23 10:03:10 +01003467void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3468 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3469 locations->SetOut(Location::ConstantLocation(constant));
3470}
3471
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003472void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003473 // Will be generated at use site.
3474}
3475
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003476void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3478 locations->SetOut(Location::ConstantLocation(constant));
3479}
3480
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003481void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003482 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003483}
3484
Calin Juravle175dc732015-08-25 15:42:32 +01003485void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3486 // The trampoline uses the same calling convention as dex calling conventions,
3487 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3488 // the method_idx.
3489 HandleInvoke(invoke);
3490}
3491
3492void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3493 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3494}
3495
Alexandre Rames5319def2014-10-23 10:03:10 +01003496void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003497 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003498 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003499}
3500
Alexandre Rames67555f72014-11-18 10:55:16 +00003501void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3502 HandleInvoke(invoke);
3503}
3504
3505void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3506 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003507 LocationSummary* locations = invoke->GetLocations();
3508 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003509 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003510 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003511 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003512
3513 // The register ip1 is required to be used for the hidden argument in
3514 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003515 MacroAssembler* masm = GetVIXLAssembler();
3516 UseScratchRegisterScope scratch_scope(masm);
3517 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003518 scratch_scope.Exclude(ip1);
3519 __ Mov(ip1, invoke->GetDexMethodIndex());
3520
Alexandre Rames67555f72014-11-18 10:55:16 +00003521 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003522 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003523 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003524 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003525 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003526 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003527 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003528 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003529 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003530 // Instead of simply (possibly) unpoisoning `temp` here, we should
3531 // emit a read barrier for the previous class reference load.
3532 // However this is not required in practice, as this is an
3533 // intermediate/temporary reference and because the current
3534 // concurrent copying collector keeps the from-space memory
3535 // intact/accessible until the end of the marking phase (the
3536 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003537 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003538 __ Ldr(temp,
3539 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3540 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3541 invoke->GetImtIndex() % ImTable::kSize, kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003542 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003543 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003544 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003545 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003546 // lr();
3547 __ Blr(lr);
3548 DCHECK(!codegen_->IsLeafMethod());
3549 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3550}
3551
3552void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003553 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3554 if (intrinsic.TryDispatch(invoke)) {
3555 return;
3556 }
3557
Alexandre Rames67555f72014-11-18 10:55:16 +00003558 HandleInvoke(invoke);
3559}
3560
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003561void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003562 // Explicit clinit checks triggered by static invokes must have been pruned by
3563 // art::PrepareForRegisterAllocation.
3564 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003565
Andreas Gampe878d58c2015-01-15 23:24:00 -08003566 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3567 if (intrinsic.TryDispatch(invoke)) {
3568 return;
3569 }
3570
Alexandre Rames67555f72014-11-18 10:55:16 +00003571 HandleInvoke(invoke);
3572}
3573
Andreas Gampe878d58c2015-01-15 23:24:00 -08003574static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3575 if (invoke->GetLocations()->Intrinsified()) {
3576 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3577 intrinsic.Dispatch(invoke);
3578 return true;
3579 }
3580 return false;
3581}
3582
Vladimir Markodc151b22015-10-15 18:02:30 +01003583HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3584 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3585 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003586 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003587 return desired_dispatch_info;
3588}
3589
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003590void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003591 // For better instruction scheduling we load the direct code pointer before the method pointer.
3592 bool direct_code_loaded = false;
3593 switch (invoke->GetCodePtrLocation()) {
3594 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3595 // LR = code address from literal pool with link-time patch.
3596 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3597 direct_code_loaded = true;
3598 break;
3599 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3600 // LR = invoke->GetDirectCodePtr();
3601 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3602 direct_code_loaded = true;
3603 break;
3604 default:
3605 break;
3606 }
3607
Andreas Gampe878d58c2015-01-15 23:24:00 -08003608 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003609 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3610 switch (invoke->GetMethodLoadKind()) {
3611 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3612 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003613 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003614 break;
3615 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003616 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003617 break;
3618 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3619 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003620 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003621 break;
3622 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3623 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003624 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003625 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3626 break;
3627 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3628 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003629 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3630 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
3631 vixl::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003632 {
3633 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003634 __ Bind(adrp_label);
3635 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003636 }
Vladimir Marko58155012015-08-19 12:49:41 +00003637 // Add LDR with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003638 vixl::Label* ldr_label =
3639 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003640 {
3641 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003642 __ Bind(ldr_label);
3643 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003644 }
Vladimir Marko58155012015-08-19 12:49:41 +00003645 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003646 }
Vladimir Marko58155012015-08-19 12:49:41 +00003647 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003648 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003649 Register reg = XRegisterFrom(temp);
3650 Register method_reg;
3651 if (current_method.IsRegister()) {
3652 method_reg = XRegisterFrom(current_method);
3653 } else {
3654 DCHECK(invoke->GetLocations()->Intrinsified());
3655 DCHECK(!current_method.IsValid());
3656 method_reg = reg;
3657 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3658 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003659
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003660 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003661 __ Ldr(reg.X(),
3662 MemOperand(method_reg.X(),
3663 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003664 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003665 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3666 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003667 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3668 break;
3669 }
3670 }
3671
3672 switch (invoke->GetCodePtrLocation()) {
3673 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3674 __ Bl(&frame_entry_label_);
3675 break;
3676 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3677 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3678 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003679 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3680 __ Bind(label);
3681 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003682 break;
3683 }
3684 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3685 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3686 // LR prepared above for better instruction scheduling.
3687 DCHECK(direct_code_loaded);
3688 // lr()
3689 __ Blr(lr);
3690 break;
3691 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3692 // LR = callee_method->entry_point_from_quick_compiled_code_;
3693 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003694 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003695 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3696 // lr()
3697 __ Blr(lr);
3698 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003699 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003700
Andreas Gampe878d58c2015-01-15 23:24:00 -08003701 DCHECK(!IsLeafMethod());
3702}
3703
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003704void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003705 // Use the calling convention instead of the location of the receiver, as
3706 // intrinsics may have put the receiver in a different register. In the intrinsics
3707 // slow path, the arguments have been moved to the right place, so here we are
3708 // guaranteed that the receiver is the first register of the calling convention.
3709 InvokeDexCallingConvention calling_convention;
3710 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003711 Register temp = XRegisterFrom(temp_in);
3712 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3713 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3714 Offset class_offset = mirror::Object::ClassOffset();
3715 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3716
3717 BlockPoolsScope block_pools(GetVIXLAssembler());
3718
3719 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003720 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003721 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003722 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003723 // Instead of simply (possibly) unpoisoning `temp` here, we should
3724 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003725 // intermediate/temporary reference and because the current
3726 // concurrent copying collector keeps the from-space memory
3727 // intact/accessible until the end of the marking phase (the
3728 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003729 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3730 // temp = temp->GetMethodAt(method_offset);
3731 __ Ldr(temp, MemOperand(temp, method_offset));
3732 // lr = temp->GetEntryPoint();
3733 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3734 // lr();
3735 __ Blr(lr);
3736}
3737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003738vixl::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(const DexFile& dex_file,
3739 uint32_t string_index,
3740 vixl::Label* adrp_label) {
3741 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3742}
3743
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003744vixl::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(const DexFile& dex_file,
3745 uint32_t type_index,
3746 vixl::Label* adrp_label) {
3747 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3748}
3749
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003750vixl::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
3751 uint32_t element_offset,
3752 vixl::Label* adrp_label) {
3753 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3754}
3755
3756vixl::Label* CodeGeneratorARM64::NewPcRelativePatch(const DexFile& dex_file,
3757 uint32_t offset_or_index,
3758 vixl::Label* adrp_label,
3759 ArenaDeque<PcRelativePatchInfo>* patches) {
3760 // Add a patch entry and return the label.
3761 patches->emplace_back(dex_file, offset_or_index);
3762 PcRelativePatchInfo* info = &patches->back();
3763 vixl::Label* label = &info->label;
3764 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3765 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3766 return label;
3767}
3768
3769vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
3770 const DexFile& dex_file, uint32_t string_index) {
3771 return boot_image_string_patches_.GetOrCreate(
3772 StringReference(&dex_file, string_index),
3773 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3774}
3775
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003776vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
3777 const DexFile& dex_file, uint32_t type_index) {
3778 return boot_image_type_patches_.GetOrCreate(
3779 TypeReference(&dex_file, type_index),
3780 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3781}
3782
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003783vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(uint64_t address) {
3784 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3785 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3786 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3787}
3788
3789vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(uint64_t address) {
3790 return DeduplicateUint64Literal(address);
3791}
3792
Vladimir Marko58155012015-08-19 12:49:41 +00003793void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3794 DCHECK(linker_patches->empty());
3795 size_t size =
3796 method_patches_.size() +
3797 call_patches_.size() +
3798 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003799 pc_relative_dex_cache_patches_.size() +
3800 boot_image_string_patches_.size() +
3801 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003802 boot_image_type_patches_.size() +
3803 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003804 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003805 linker_patches->reserve(size);
3806 for (const auto& entry : method_patches_) {
3807 const MethodReference& target_method = entry.first;
3808 vixl::Literal<uint64_t>* literal = entry.second;
3809 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3810 target_method.dex_file,
3811 target_method.dex_method_index));
3812 }
3813 for (const auto& entry : call_patches_) {
3814 const MethodReference& target_method = entry.first;
3815 vixl::Literal<uint64_t>* literal = entry.second;
3816 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3817 target_method.dex_file,
3818 target_method.dex_method_index));
3819 }
3820 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003821 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003822 info.target_method.dex_file,
3823 info.target_method.dex_method_index));
3824 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003825 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003826 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003827 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003828 info.pc_insn_label->location(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003829 info.offset_or_index));
3830 }
3831 for (const auto& entry : boot_image_string_patches_) {
3832 const StringReference& target_string = entry.first;
3833 vixl::Literal<uint32_t>* literal = entry.second;
3834 linker_patches->push_back(LinkerPatch::StringPatch(literal->offset(),
3835 target_string.dex_file,
3836 target_string.string_index));
3837 }
3838 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
3839 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.location(),
3840 &info.target_dex_file,
3841 info.pc_insn_label->location(),
3842 info.offset_or_index));
3843 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003844 for (const auto& entry : boot_image_type_patches_) {
3845 const TypeReference& target_type = entry.first;
3846 vixl::Literal<uint32_t>* literal = entry.second;
3847 linker_patches->push_back(LinkerPatch::TypePatch(literal->offset(),
3848 target_type.dex_file,
3849 target_type.type_index));
3850 }
3851 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
3852 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.location(),
3853 &info.target_dex_file,
3854 info.pc_insn_label->location(),
3855 info.offset_or_index));
3856 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003857 for (const auto& entry : boot_image_address_patches_) {
3858 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
3859 vixl::Literal<uint32_t>* literal = entry.second;
3860 linker_patches->push_back(LinkerPatch::RecordPosition(literal->offset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003861 }
3862}
3863
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003864vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
3865 Uint32ToLiteralMap* map) {
3866 return map->GetOrCreate(
3867 value,
3868 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3869}
3870
Vladimir Marko58155012015-08-19 12:49:41 +00003871vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003872 return uint64_literals_.GetOrCreate(
3873 value,
3874 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003875}
3876
3877vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3878 MethodReference target_method,
3879 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003880 return map->GetOrCreate(
3881 target_method,
3882 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003883}
3884
3885vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3886 MethodReference target_method) {
3887 return DeduplicateMethodLiteral(target_method, &method_patches_);
3888}
3889
3890vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3891 MethodReference target_method) {
3892 return DeduplicateMethodLiteral(target_method, &call_patches_);
3893}
3894
3895
Andreas Gampe878d58c2015-01-15 23:24:00 -08003896void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003897 // Explicit clinit checks triggered by static invokes must have been pruned by
3898 // art::PrepareForRegisterAllocation.
3899 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003900
Andreas Gampe878d58c2015-01-15 23:24:00 -08003901 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3902 return;
3903 }
3904
Alexandre Ramesd921d642015-04-16 15:07:16 +01003905 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003906 LocationSummary* locations = invoke->GetLocations();
3907 codegen_->GenerateStaticOrDirectCall(
3908 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003909 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003910}
3911
3912void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003913 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3914 return;
3915 }
3916
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003917 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003918 DCHECK(!codegen_->IsLeafMethod());
3919 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3920}
3921
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003922HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3923 HLoadClass::LoadKind desired_class_load_kind) {
3924 if (kEmitCompilerReadBarrier) {
3925 switch (desired_class_load_kind) {
3926 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3927 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3928 case HLoadClass::LoadKind::kBootImageAddress:
3929 // TODO: Implement for read barrier.
3930 return HLoadClass::LoadKind::kDexCacheViaMethod;
3931 default:
3932 break;
3933 }
3934 }
3935 switch (desired_class_load_kind) {
3936 case HLoadClass::LoadKind::kReferrersClass:
3937 break;
3938 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3939 DCHECK(!GetCompilerOptions().GetCompilePic());
3940 break;
3941 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3942 DCHECK(GetCompilerOptions().GetCompilePic());
3943 break;
3944 case HLoadClass::LoadKind::kBootImageAddress:
3945 break;
3946 case HLoadClass::LoadKind::kDexCacheAddress:
3947 DCHECK(Runtime::Current()->UseJitCompilation());
3948 break;
3949 case HLoadClass::LoadKind::kDexCachePcRelative:
3950 DCHECK(!Runtime::Current()->UseJitCompilation());
3951 break;
3952 case HLoadClass::LoadKind::kDexCacheViaMethod:
3953 break;
3954 }
3955 return desired_class_load_kind;
3956}
3957
Alexandre Rames67555f72014-11-18 10:55:16 +00003958void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003959 if (cls->NeedsAccessCheck()) {
3960 InvokeRuntimeCallingConvention calling_convention;
3961 CodeGenerator::CreateLoadClassLocationSummary(
3962 cls,
3963 LocationFrom(calling_convention.GetRegisterAt(0)),
3964 LocationFrom(vixl::x0),
3965 /* code_generator_supports_read_barrier */ true);
3966 return;
3967 }
3968
3969 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3970 ? LocationSummary::kCallOnSlowPath
3971 : LocationSummary::kNoCall;
3972 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3973 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3974 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3975 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3976 locations->SetInAt(0, Location::RequiresRegister());
3977 }
3978 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003979}
3980
3981void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003982 if (cls->NeedsAccessCheck()) {
3983 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3984 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3985 cls,
3986 cls->GetDexPc(),
3987 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003988 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003989 return;
3990 }
3991
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003992 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003993 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00003994
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003995 bool generate_null_check = false;
3996 switch (cls->GetLoadKind()) {
3997 case HLoadClass::LoadKind::kReferrersClass: {
3998 DCHECK(!cls->CanCallRuntime());
3999 DCHECK(!cls->MustGenerateClinitCheck());
4000 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4001 Register current_method = InputRegisterAt(cls, 0);
4002 GenerateGcRootFieldLoad(
4003 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4004 break;
4005 }
4006 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4007 DCHECK(!kEmitCompilerReadBarrier);
4008 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4009 cls->GetTypeIndex()));
4010 break;
4011 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4012 DCHECK(!kEmitCompilerReadBarrier);
4013 // Add ADRP with its PC-relative type patch.
4014 const DexFile& dex_file = cls->GetDexFile();
4015 uint32_t type_index = cls->GetTypeIndex();
4016 vixl::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
4017 {
4018 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4019 __ Bind(adrp_label);
4020 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004021 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004022 // Add ADD with its PC-relative type patch.
4023 vixl::Label* add_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
4024 {
4025 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4026 __ Bind(add_label);
4027 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004028 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004029 break;
4030 }
4031 case HLoadClass::LoadKind::kBootImageAddress: {
4032 DCHECK(!kEmitCompilerReadBarrier);
4033 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4034 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4035 break;
4036 }
4037 case HLoadClass::LoadKind::kDexCacheAddress: {
4038 DCHECK_NE(cls->GetAddress(), 0u);
4039 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4040 // that gives a 16KiB range. To try and reduce the number of literals if we load
4041 // multiple types, simply split the dex cache address to a 16KiB aligned base
4042 // loaded from a literal and the remaining offset embedded in the load.
4043 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4044 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4045 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4046 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4047 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4048 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4049 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4050 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4051 generate_null_check = !cls->IsInDexCache();
4052 break;
4053 }
4054 case HLoadClass::LoadKind::kDexCachePcRelative: {
4055 // Add ADRP with its PC-relative DexCache access patch.
4056 const DexFile& dex_file = cls->GetDexFile();
4057 uint32_t element_offset = cls->GetDexCacheElementOffset();
4058 vixl::Label* adrp_label = codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
4059 {
4060 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4061 __ Bind(adrp_label);
4062 __ adrp(out.X(), /* offset placeholder */ 0);
4063 }
4064 // Add LDR with its PC-relative DexCache access patch.
4065 vixl::Label* ldr_label =
4066 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4067 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4068 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4069 generate_null_check = !cls->IsInDexCache();
4070 break;
4071 }
4072 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4073 MemberOffset resolved_types_offset =
4074 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4075 // /* GcRoot<mirror::Class>[] */ out =
4076 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4077 Register current_method = InputRegisterAt(cls, 0);
4078 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4079 // /* GcRoot<mirror::Class> */ out = out[type_index]
4080 GenerateGcRootFieldLoad(
4081 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4082 generate_null_check = !cls->IsInDexCache();
4083 break;
4084 }
4085 }
4086
4087 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4088 DCHECK(cls->CanCallRuntime());
4089 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4090 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4091 codegen_->AddSlowPath(slow_path);
4092 if (generate_null_check) {
4093 __ Cbz(out, slow_path->GetEntryLabel());
4094 }
4095 if (cls->MustGenerateClinitCheck()) {
4096 GenerateClassInitializationCheck(slow_path, out);
4097 } else {
4098 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004099 }
4100 }
4101}
4102
David Brazdilcb1c0552015-08-04 16:22:25 +01004103static MemOperand GetExceptionTlsAddress() {
4104 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
4105}
4106
Alexandre Rames67555f72014-11-18 10:55:16 +00004107void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4108 LocationSummary* locations =
4109 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4110 locations->SetOut(Location::RequiresRegister());
4111}
4112
4113void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004114 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4115}
4116
4117void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4118 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4119}
4120
4121void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4122 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004123}
4124
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004125HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4126 HLoadString::LoadKind desired_string_load_kind) {
4127 if (kEmitCompilerReadBarrier) {
4128 switch (desired_string_load_kind) {
4129 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4130 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4131 case HLoadString::LoadKind::kBootImageAddress:
4132 // TODO: Implement for read barrier.
4133 return HLoadString::LoadKind::kDexCacheViaMethod;
4134 default:
4135 break;
4136 }
4137 }
4138 switch (desired_string_load_kind) {
4139 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4140 DCHECK(!GetCompilerOptions().GetCompilePic());
4141 break;
4142 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4143 DCHECK(GetCompilerOptions().GetCompilePic());
4144 break;
4145 case HLoadString::LoadKind::kBootImageAddress:
4146 break;
4147 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004148 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004149 break;
4150 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004151 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004152 break;
4153 case HLoadString::LoadKind::kDexCacheViaMethod:
4154 break;
4155 }
4156 return desired_string_load_kind;
4157}
4158
Alexandre Rames67555f72014-11-18 10:55:16 +00004159void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004160 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004161 ? LocationSummary::kCallOnSlowPath
4162 : LocationSummary::kNoCall;
4163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004164 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4165 locations->SetInAt(0, Location::RequiresRegister());
4166 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004167 locations->SetOut(Location::RequiresRegister());
4168}
4169
4170void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004171 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004172 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004173
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004174 switch (load->GetLoadKind()) {
4175 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4176 DCHECK(!kEmitCompilerReadBarrier);
4177 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4178 load->GetStringIndex()));
4179 return; // No dex cache slow path.
4180 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4181 DCHECK(!kEmitCompilerReadBarrier);
4182 // Add ADRP with its PC-relative String patch.
4183 const DexFile& dex_file = load->GetDexFile();
4184 uint32_t string_index = load->GetStringIndex();
4185 vixl::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4186 {
4187 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4188 __ Bind(adrp_label);
4189 __ adrp(out.X(), /* offset placeholder */ 0);
4190 }
4191 // Add ADD with its PC-relative String patch.
4192 vixl::Label* add_label =
4193 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4194 {
4195 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4196 __ Bind(add_label);
4197 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4198 }
4199 return; // No dex cache slow path.
4200 }
4201 case HLoadString::LoadKind::kBootImageAddress: {
4202 DCHECK(!kEmitCompilerReadBarrier);
4203 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4204 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4205 return; // No dex cache slow path.
4206 }
4207 case HLoadString::LoadKind::kDexCacheAddress: {
4208 DCHECK_NE(load->GetAddress(), 0u);
4209 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4210 // that gives a 16KiB range. To try and reduce the number of literals if we load
4211 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4212 // loaded from a literal and the remaining offset embedded in the load.
4213 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4214 DCHECK_ALIGNED(load->GetAddress(), 4u);
4215 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4216 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4217 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4218 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004219 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004220 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4221 break;
4222 }
4223 case HLoadString::LoadKind::kDexCachePcRelative: {
4224 // Add ADRP with its PC-relative DexCache access patch.
4225 const DexFile& dex_file = load->GetDexFile();
4226 uint32_t element_offset = load->GetDexCacheElementOffset();
4227 vixl::Label* adrp_label = codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
4228 {
4229 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4230 __ Bind(adrp_label);
4231 __ adrp(out.X(), /* offset placeholder */ 0);
4232 }
4233 // Add LDR with its PC-relative DexCache access patch.
4234 vixl::Label* ldr_label =
4235 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004236 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004237 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4238 break;
4239 }
4240 case HLoadString::LoadKind::kDexCacheViaMethod: {
4241 Register current_method = InputRegisterAt(load, 0);
4242 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4243 GenerateGcRootFieldLoad(
4244 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4245 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4246 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4247 // /* GcRoot<mirror::String> */ out = out[string_index]
4248 GenerateGcRootFieldLoad(
4249 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4250 break;
4251 }
4252 default:
4253 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4254 UNREACHABLE();
4255 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004256
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004257 if (!load->IsInDexCache()) {
4258 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4259 codegen_->AddSlowPath(slow_path);
4260 __ Cbz(out, slow_path->GetEntryLabel());
4261 __ Bind(slow_path->GetExitLabel());
4262 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004263}
4264
Alexandre Rames5319def2014-10-23 10:03:10 +01004265void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4267 locations->SetOut(Location::ConstantLocation(constant));
4268}
4269
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004270void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004271 // Will be generated at use site.
4272}
4273
Alexandre Rames67555f72014-11-18 10:55:16 +00004274void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4275 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004276 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004277 InvokeRuntimeCallingConvention calling_convention;
4278 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4279}
4280
4281void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4282 codegen_->InvokeRuntime(instruction->IsEnter()
4283 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4284 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004285 instruction->GetDexPc(),
4286 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004287 if (instruction->IsEnter()) {
4288 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4289 } else {
4290 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4291 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004292}
4293
Alexandre Rames42d641b2014-10-27 14:00:51 +00004294void LocationsBuilderARM64::VisitMul(HMul* mul) {
4295 LocationSummary* locations =
4296 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4297 switch (mul->GetResultType()) {
4298 case Primitive::kPrimInt:
4299 case Primitive::kPrimLong:
4300 locations->SetInAt(0, Location::RequiresRegister());
4301 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004303 break;
4304
4305 case Primitive::kPrimFloat:
4306 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004307 locations->SetInAt(0, Location::RequiresFpuRegister());
4308 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004309 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004310 break;
4311
4312 default:
4313 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4314 }
4315}
4316
4317void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4318 switch (mul->GetResultType()) {
4319 case Primitive::kPrimInt:
4320 case Primitive::kPrimLong:
4321 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4322 break;
4323
4324 case Primitive::kPrimFloat:
4325 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004326 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004327 break;
4328
4329 default:
4330 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4331 }
4332}
4333
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004334void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4335 LocationSummary* locations =
4336 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4337 switch (neg->GetResultType()) {
4338 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004339 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004340 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004342 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004343
4344 case Primitive::kPrimFloat:
4345 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004346 locations->SetInAt(0, Location::RequiresFpuRegister());
4347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004348 break;
4349
4350 default:
4351 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4352 }
4353}
4354
4355void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4356 switch (neg->GetResultType()) {
4357 case Primitive::kPrimInt:
4358 case Primitive::kPrimLong:
4359 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4360 break;
4361
4362 case Primitive::kPrimFloat:
4363 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004364 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004365 break;
4366
4367 default:
4368 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4369 }
4370}
4371
4372void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4373 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004375 InvokeRuntimeCallingConvention calling_convention;
4376 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004377 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004378 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004379 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004380}
4381
4382void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4383 LocationSummary* locations = instruction->GetLocations();
4384 InvokeRuntimeCallingConvention calling_convention;
4385 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4386 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004387 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004388 // Note: if heap poisoning is enabled, the entry point takes cares
4389 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004390 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4391 instruction,
4392 instruction->GetDexPc(),
4393 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004394 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004395}
4396
Alexandre Rames5319def2014-10-23 10:03:10 +01004397void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4398 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004399 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004400 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004401 if (instruction->IsStringAlloc()) {
4402 locations->AddTemp(LocationFrom(kArtMethodRegister));
4403 } else {
4404 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4405 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4406 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004407 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4408}
4409
4410void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004411 // Note: if heap poisoning is enabled, the entry point takes cares
4412 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004413 if (instruction->IsStringAlloc()) {
4414 // String is allocated through StringFactory. Call NewEmptyString entry point.
4415 Location temp = instruction->GetLocations()->GetTemp(0);
4416 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4417 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4418 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4419 __ Blr(lr);
4420 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4421 } else {
4422 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4423 instruction,
4424 instruction->GetDexPc(),
4425 nullptr);
4426 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4427 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004428}
4429
4430void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4431 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004432 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004433 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004434}
4435
4436void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004437 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004438 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004439 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004440 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004441 break;
4442
4443 default:
4444 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4445 }
4446}
4447
David Brazdil66d126e2015-04-03 16:02:44 +01004448void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4450 locations->SetInAt(0, Location::RequiresRegister());
4451 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4452}
4453
4454void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004455 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4456}
4457
Alexandre Rames5319def2014-10-23 10:03:10 +01004458void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004459 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4460 ? LocationSummary::kCallOnSlowPath
4461 : LocationSummary::kNoCall;
4462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004463 locations->SetInAt(0, Location::RequiresRegister());
4464 if (instruction->HasUses()) {
4465 locations->SetOut(Location::SameAsFirstInput());
4466 }
4467}
4468
Calin Juravle2ae48182016-03-16 14:05:09 +00004469void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4470 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004471 return;
4472 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004473
Alexandre Ramesd921d642015-04-16 15:07:16 +01004474 BlockPoolsScope block_pools(GetVIXLAssembler());
4475 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004476 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004477 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004478}
4479
Calin Juravle2ae48182016-03-16 14:05:09 +00004480void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004481 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004482 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004483
4484 LocationSummary* locations = instruction->GetLocations();
4485 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004486
4487 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004488}
4489
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004490void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004491 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004492}
4493
Alexandre Rames67555f72014-11-18 10:55:16 +00004494void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4495 HandleBinaryOp(instruction);
4496}
4497
4498void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4499 HandleBinaryOp(instruction);
4500}
4501
Alexandre Rames3e69f162014-12-10 10:36:50 +00004502void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4503 LOG(FATAL) << "Unreachable";
4504}
4505
4506void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4507 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4508}
4509
Alexandre Rames5319def2014-10-23 10:03:10 +01004510void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4512 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4513 if (location.IsStackSlot()) {
4514 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4515 } else if (location.IsDoubleStackSlot()) {
4516 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4517 }
4518 locations->SetOut(location);
4519}
4520
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004521void InstructionCodeGeneratorARM64::VisitParameterValue(
4522 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004523 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004524}
4525
4526void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4527 LocationSummary* locations =
4528 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004529 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004530}
4531
4532void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4533 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4534 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004535}
4536
4537void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004539 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004540 locations->SetInAt(i, Location::Any());
4541 }
4542 locations->SetOut(Location::Any());
4543}
4544
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004545void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004546 LOG(FATAL) << "Unreachable";
4547}
4548
Serban Constantinescu02164b32014-11-13 14:05:07 +00004549void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004550 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004551 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004552 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4553 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4555
4556 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004557 case Primitive::kPrimInt:
4558 case Primitive::kPrimLong:
4559 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004560 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4562 break;
4563
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004564 case Primitive::kPrimFloat:
4565 case Primitive::kPrimDouble: {
4566 InvokeRuntimeCallingConvention calling_convention;
4567 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4568 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4569 locations->SetOut(calling_convention.GetReturnLocation(type));
4570
4571 break;
4572 }
4573
Serban Constantinescu02164b32014-11-13 14:05:07 +00004574 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004575 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004576 }
4577}
4578
4579void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4580 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004581
Serban Constantinescu02164b32014-11-13 14:05:07 +00004582 switch (type) {
4583 case Primitive::kPrimInt:
4584 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004585 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004586 break;
4587 }
4588
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004589 case Primitive::kPrimFloat:
4590 case Primitive::kPrimDouble: {
4591 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4592 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004593 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004594 if (type == Primitive::kPrimFloat) {
4595 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4596 } else {
4597 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4598 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004599 break;
4600 }
4601
Serban Constantinescu02164b32014-11-13 14:05:07 +00004602 default:
4603 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004604 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004605 }
4606}
4607
Calin Juravle27df7582015-04-17 19:12:31 +01004608void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4609 memory_barrier->SetLocations(nullptr);
4610}
4611
4612void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004613 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004614}
4615
Alexandre Rames5319def2014-10-23 10:03:10 +01004616void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4618 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004619 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004620}
4621
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004622void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004623 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004624}
4625
4626void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4627 instruction->SetLocations(nullptr);
4628}
4629
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004630void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004631 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004632}
4633
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004634void LocationsBuilderARM64::VisitRor(HRor* ror) {
4635 HandleBinaryOp(ror);
4636}
4637
4638void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4639 HandleBinaryOp(ror);
4640}
4641
Serban Constantinescu02164b32014-11-13 14:05:07 +00004642void LocationsBuilderARM64::VisitShl(HShl* shl) {
4643 HandleShift(shl);
4644}
4645
4646void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4647 HandleShift(shl);
4648}
4649
4650void LocationsBuilderARM64::VisitShr(HShr* shr) {
4651 HandleShift(shr);
4652}
4653
4654void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4655 HandleShift(shr);
4656}
4657
Alexandre Rames5319def2014-10-23 10:03:10 +01004658void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004659 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004660}
4661
4662void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004663 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004664}
4665
Alexandre Rames67555f72014-11-18 10:55:16 +00004666void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004667 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004668}
4669
4670void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004671 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004672}
4673
4674void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004675 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004676}
4677
Alexandre Rames67555f72014-11-18 10:55:16 +00004678void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004679 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004680}
4681
Calin Juravlee460d1d2015-09-29 04:52:17 +01004682void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4683 HUnresolvedInstanceFieldGet* instruction) {
4684 FieldAccessCallingConventionARM64 calling_convention;
4685 codegen_->CreateUnresolvedFieldLocationSummary(
4686 instruction, instruction->GetFieldType(), calling_convention);
4687}
4688
4689void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4690 HUnresolvedInstanceFieldGet* instruction) {
4691 FieldAccessCallingConventionARM64 calling_convention;
4692 codegen_->GenerateUnresolvedFieldAccess(instruction,
4693 instruction->GetFieldType(),
4694 instruction->GetFieldIndex(),
4695 instruction->GetDexPc(),
4696 calling_convention);
4697}
4698
4699void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4700 HUnresolvedInstanceFieldSet* instruction) {
4701 FieldAccessCallingConventionARM64 calling_convention;
4702 codegen_->CreateUnresolvedFieldLocationSummary(
4703 instruction, instruction->GetFieldType(), calling_convention);
4704}
4705
4706void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4707 HUnresolvedInstanceFieldSet* instruction) {
4708 FieldAccessCallingConventionARM64 calling_convention;
4709 codegen_->GenerateUnresolvedFieldAccess(instruction,
4710 instruction->GetFieldType(),
4711 instruction->GetFieldIndex(),
4712 instruction->GetDexPc(),
4713 calling_convention);
4714}
4715
4716void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4717 HUnresolvedStaticFieldGet* instruction) {
4718 FieldAccessCallingConventionARM64 calling_convention;
4719 codegen_->CreateUnresolvedFieldLocationSummary(
4720 instruction, instruction->GetFieldType(), calling_convention);
4721}
4722
4723void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4724 HUnresolvedStaticFieldGet* instruction) {
4725 FieldAccessCallingConventionARM64 calling_convention;
4726 codegen_->GenerateUnresolvedFieldAccess(instruction,
4727 instruction->GetFieldType(),
4728 instruction->GetFieldIndex(),
4729 instruction->GetDexPc(),
4730 calling_convention);
4731}
4732
4733void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4734 HUnresolvedStaticFieldSet* instruction) {
4735 FieldAccessCallingConventionARM64 calling_convention;
4736 codegen_->CreateUnresolvedFieldLocationSummary(
4737 instruction, instruction->GetFieldType(), calling_convention);
4738}
4739
4740void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4741 HUnresolvedStaticFieldSet* instruction) {
4742 FieldAccessCallingConventionARM64 calling_convention;
4743 codegen_->GenerateUnresolvedFieldAccess(instruction,
4744 instruction->GetFieldType(),
4745 instruction->GetFieldIndex(),
4746 instruction->GetDexPc(),
4747 calling_convention);
4748}
4749
Alexandre Rames5319def2014-10-23 10:03:10 +01004750void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4752}
4753
4754void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004755 HBasicBlock* block = instruction->GetBlock();
4756 if (block->GetLoopInformation() != nullptr) {
4757 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4758 // The back edge will generate the suspend check.
4759 return;
4760 }
4761 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4762 // The goto will generate the suspend check.
4763 return;
4764 }
4765 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004766}
4767
Alexandre Rames67555f72014-11-18 10:55:16 +00004768void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4769 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004770 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004771 InvokeRuntimeCallingConvention calling_convention;
4772 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4773}
4774
4775void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4776 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004777 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004778 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004779}
4780
4781void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4782 LocationSummary* locations =
4783 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4784 Primitive::Type input_type = conversion->GetInputType();
4785 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004786 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004787 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4788 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4789 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4790 }
4791
Alexandre Rames542361f2015-01-29 16:57:31 +00004792 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004793 locations->SetInAt(0, Location::RequiresFpuRegister());
4794 } else {
4795 locations->SetInAt(0, Location::RequiresRegister());
4796 }
4797
Alexandre Rames542361f2015-01-29 16:57:31 +00004798 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004799 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4800 } else {
4801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4802 }
4803}
4804
4805void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4806 Primitive::Type result_type = conversion->GetResultType();
4807 Primitive::Type input_type = conversion->GetInputType();
4808
4809 DCHECK_NE(input_type, result_type);
4810
Alexandre Rames542361f2015-01-29 16:57:31 +00004811 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004812 int result_size = Primitive::ComponentSize(result_type);
4813 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004814 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004815 Register output = OutputRegister(conversion);
4816 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004817 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004818 // 'int' values are used directly as W registers, discarding the top
4819 // bits, so we don't need to sign-extend and can just perform a move.
4820 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4821 // top 32 bits of the target register. We theoretically could leave those
4822 // bits unchanged, but we would have to make sure that no code uses a
4823 // 32bit input value as a 64bit value assuming that the top 32 bits are
4824 // zero.
4825 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004826 } else if (result_type == Primitive::kPrimChar ||
4827 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4828 __ Ubfx(output,
4829 output.IsX() ? source.X() : source.W(),
4830 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004831 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004832 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004833 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004834 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004835 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004836 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004837 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4838 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004839 } else if (Primitive::IsFloatingPointType(result_type) &&
4840 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004841 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4842 } else {
4843 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4844 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004845 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004846}
Alexandre Rames67555f72014-11-18 10:55:16 +00004847
Serban Constantinescu02164b32014-11-13 14:05:07 +00004848void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4849 HandleShift(ushr);
4850}
4851
4852void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4853 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004854}
4855
4856void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4857 HandleBinaryOp(instruction);
4858}
4859
4860void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4861 HandleBinaryOp(instruction);
4862}
4863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004864void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004865 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004866 LOG(FATAL) << "Unreachable";
4867}
4868
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004869void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004870 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004871 LOG(FATAL) << "Unreachable";
4872}
4873
Mark Mendellfe57faa2015-09-18 09:26:15 -04004874// Simple implementation of packed switch - generate cascaded compare/jumps.
4875void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4876 LocationSummary* locations =
4877 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4878 locations->SetInAt(0, Location::RequiresRegister());
4879}
4880
4881void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4882 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004883 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004884 Register value_reg = InputRegisterAt(switch_instr, 0);
4885 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4886
Zheng Xu3927c8b2015-11-18 17:46:25 +08004887 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4888 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4889 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4890 // make sure we don't emit it if the target may run out of range.
4891 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4892 // ranges and emit the tables only as required.
4893 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004894
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004895 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004896 // Current instruction id is an upper bound of the number of HIRs in the graph.
4897 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4898 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004899 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4900 Register temp = temps.AcquireW();
4901 __ Subs(temp, value_reg, Operand(lower_bound));
4902
Zheng Xu3927c8b2015-11-18 17:46:25 +08004903 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004904 // Jump to successors[0] if value == lower_bound.
4905 __ B(eq, codegen_->GetLabelOf(successors[0]));
4906 int32_t last_index = 0;
4907 for (; num_entries - last_index > 2; last_index += 2) {
4908 __ Subs(temp, temp, Operand(2));
4909 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4910 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4911 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4912 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4913 }
4914 if (num_entries - last_index == 2) {
4915 // The last missing case_value.
4916 __ Cmp(temp, Operand(1));
4917 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004918 }
4919
4920 // And the default for any other value.
4921 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4922 __ B(codegen_->GetLabelOf(default_block));
4923 }
4924 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004925 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004926
4927 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4928
4929 // Below instructions should use at most one blocked register. Since there are two blocked
4930 // registers, we are free to block one.
4931 Register temp_w = temps.AcquireW();
4932 Register index;
4933 // Remove the bias.
4934 if (lower_bound != 0) {
4935 index = temp_w;
4936 __ Sub(index, value_reg, Operand(lower_bound));
4937 } else {
4938 index = value_reg;
4939 }
4940
4941 // Jump to default block if index is out of the range.
4942 __ Cmp(index, Operand(num_entries));
4943 __ B(hs, codegen_->GetLabelOf(default_block));
4944
4945 // In current VIXL implementation, it won't require any blocked registers to encode the
4946 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4947 // register pressure.
4948 Register table_base = temps.AcquireX();
4949 // Load jump offset from the table.
4950 __ Adr(table_base, jump_table->GetTableStartLabel());
4951 Register jump_offset = temp_w;
4952 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4953
4954 // Jump to target block by branching to table_base(pc related) + offset.
4955 Register target_address = table_base;
4956 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4957 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004958 }
4959}
4960
Roland Levillain44015862016-01-22 11:47:17 +00004961void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4962 Location out,
4963 uint32_t offset,
4964 Location maybe_temp) {
4965 Primitive::Type type = Primitive::kPrimNot;
4966 Register out_reg = RegisterFrom(out, type);
4967 if (kEmitCompilerReadBarrier) {
4968 Register temp_reg = RegisterFrom(maybe_temp, type);
4969 if (kUseBakerReadBarrier) {
4970 // Load with fast path based Baker's read barrier.
4971 // /* HeapReference<Object> */ out = *(out + offset)
4972 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4973 out,
4974 out_reg,
4975 offset,
4976 temp_reg,
4977 /* needs_null_check */ false,
4978 /* use_load_acquire */ false);
4979 } else {
4980 // Load with slow path based read barrier.
4981 // Save the value of `out` into `maybe_temp` before overwriting it
4982 // in the following move operation, as we will need it for the
4983 // read barrier below.
4984 __ Mov(temp_reg, out_reg);
4985 // /* HeapReference<Object> */ out = *(out + offset)
4986 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4987 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4988 }
4989 } else {
4990 // Plain load with no read barrier.
4991 // /* HeapReference<Object> */ out = *(out + offset)
4992 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4993 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4994 }
4995}
4996
4997void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4998 Location out,
4999 Location obj,
5000 uint32_t offset,
5001 Location maybe_temp) {
5002 Primitive::Type type = Primitive::kPrimNot;
5003 Register out_reg = RegisterFrom(out, type);
5004 Register obj_reg = RegisterFrom(obj, type);
5005 if (kEmitCompilerReadBarrier) {
5006 if (kUseBakerReadBarrier) {
5007 // Load with fast path based Baker's read barrier.
5008 Register temp_reg = RegisterFrom(maybe_temp, type);
5009 // /* HeapReference<Object> */ out = *(obj + offset)
5010 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5011 out,
5012 obj_reg,
5013 offset,
5014 temp_reg,
5015 /* needs_null_check */ false,
5016 /* use_load_acquire */ false);
5017 } else {
5018 // Load with slow path based read barrier.
5019 // /* HeapReference<Object> */ out = *(obj + offset)
5020 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5021 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5022 }
5023 } else {
5024 // Plain load with no read barrier.
5025 // /* HeapReference<Object> */ out = *(obj + offset)
5026 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5027 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5028 }
5029}
5030
5031void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5032 Location root,
5033 vixl::Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005034 uint32_t offset,
5035 vixl::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005036 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5037 if (kEmitCompilerReadBarrier) {
5038 if (kUseBakerReadBarrier) {
5039 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5040 // Baker's read barrier are used:
5041 //
5042 // root = obj.field;
5043 // if (Thread::Current()->GetIsGcMarking()) {
5044 // root = ReadBarrier::Mark(root)
5045 // }
5046
5047 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005048 if (fixup_label == nullptr) {
5049 __ Ldr(root_reg, MemOperand(obj, offset));
5050 } else {
5051 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
5052 __ Bind(fixup_label);
5053 __ ldr(root_reg, MemOperand(obj, offset));
5054 }
Roland Levillain44015862016-01-22 11:47:17 +00005055 static_assert(
5056 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5057 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5058 "have different sizes.");
5059 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5060 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5061 "have different sizes.");
5062
5063 // Slow path used to mark the GC root `root`.
5064 SlowPathCodeARM64* slow_path =
5065 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root, root);
5066 codegen_->AddSlowPath(slow_path);
5067
5068 MacroAssembler* masm = GetVIXLAssembler();
5069 UseScratchRegisterScope temps(masm);
5070 Register temp = temps.AcquireW();
5071 // temp = Thread::Current()->GetIsGcMarking()
5072 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
5073 __ Cbnz(temp, slow_path->GetEntryLabel());
5074 __ Bind(slow_path->GetExitLabel());
5075 } else {
5076 // GC root loaded through a slow path for read barriers other
5077 // than Baker's.
5078 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 if (fixup_label == nullptr) {
5080 __ Add(root_reg.X(), obj.X(), offset);
5081 } else {
5082 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
5083 __ Bind(fixup_label);
5084 __ add(root_reg.X(), obj.X(), offset);
5085 }
Roland Levillain44015862016-01-22 11:47:17 +00005086 // /* mirror::Object* */ root = root->Read()
5087 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5088 }
5089 } else {
5090 // Plain GC root load with no read barrier.
5091 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 if (fixup_label == nullptr) {
5093 __ Ldr(root_reg, MemOperand(obj, offset));
5094 } else {
5095 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
5096 __ Bind(fixup_label);
5097 __ ldr(root_reg, MemOperand(obj, offset));
5098 }
Roland Levillain44015862016-01-22 11:47:17 +00005099 // Note that GC roots are not affected by heap poisoning, thus we
5100 // do not have to unpoison `root_reg` here.
5101 }
5102}
5103
5104void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5105 Location ref,
5106 vixl::Register obj,
5107 uint32_t offset,
5108 Register temp,
5109 bool needs_null_check,
5110 bool use_load_acquire) {
5111 DCHECK(kEmitCompilerReadBarrier);
5112 DCHECK(kUseBakerReadBarrier);
5113
5114 // /* HeapReference<Object> */ ref = *(obj + offset)
5115 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005116 size_t no_scale_factor = 0U;
5117 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5118 ref,
5119 obj,
5120 offset,
5121 no_index,
5122 no_scale_factor,
5123 temp,
5124 needs_null_check,
5125 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005126}
5127
5128void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5129 Location ref,
5130 vixl::Register obj,
5131 uint32_t data_offset,
5132 Location index,
5133 Register temp,
5134 bool needs_null_check) {
5135 DCHECK(kEmitCompilerReadBarrier);
5136 DCHECK(kUseBakerReadBarrier);
5137
5138 // Array cells are never volatile variables, therefore array loads
5139 // never use Load-Acquire instructions on ARM64.
5140 const bool use_load_acquire = false;
5141
Roland Levillainbfea3352016-06-23 13:48:47 +01005142 static_assert(
5143 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5144 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005145 // /* HeapReference<Object> */ ref =
5146 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005147 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5148 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5149 ref,
5150 obj,
5151 data_offset,
5152 index,
5153 scale_factor,
5154 temp,
5155 needs_null_check,
5156 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005157}
5158
5159void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5160 Location ref,
5161 vixl::Register obj,
5162 uint32_t offset,
5163 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005164 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005165 Register temp,
5166 bool needs_null_check,
5167 bool use_load_acquire) {
5168 DCHECK(kEmitCompilerReadBarrier);
5169 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005170 // If we are emitting an array load, we should not be using a
5171 // Load Acquire instruction. In other words:
5172 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5173 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005174
5175 MacroAssembler* masm = GetVIXLAssembler();
5176 UseScratchRegisterScope temps(masm);
5177
5178 // In slow path based read barriers, the read barrier call is
5179 // inserted after the original load. However, in fast path based
5180 // Baker's read barriers, we need to perform the load of
5181 // mirror::Object::monitor_ *before* the original reference load.
5182 // This load-load ordering is required by the read barrier.
5183 // The fast path/slow path (for Baker's algorithm) should look like:
5184 //
5185 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5186 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5187 // HeapReference<Object> ref = *src; // Original reference load.
5188 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5189 // if (is_gray) {
5190 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5191 // }
5192 //
5193 // Note: the original implementation in ReadBarrier::Barrier is
5194 // slightly more complex as it performs additional checks that we do
5195 // not do here for performance reasons.
5196
5197 Primitive::Type type = Primitive::kPrimNot;
5198 Register ref_reg = RegisterFrom(ref, type);
5199 DCHECK(obj.IsW());
5200 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5201
5202 // /* int32_t */ monitor = obj->monitor_
5203 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5204 if (needs_null_check) {
5205 MaybeRecordImplicitNullCheck(instruction);
5206 }
5207 // /* LockWord */ lock_word = LockWord(monitor)
5208 static_assert(sizeof(LockWord) == sizeof(int32_t),
5209 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005210
Vladimir Marko877a0332016-07-11 19:30:56 +01005211 // Introduce a dependency on the lock_word including rb_state,
5212 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005213 // a memory barrier (which would be more expensive).
Vladimir Marko877a0332016-07-11 19:30:56 +01005214 // obj is unchanged by this operation, but its value now depends on temp.
5215 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005216
5217 // The actual reference load.
5218 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005219 // Load types involving an "index".
5220 if (use_load_acquire) {
5221 // UnsafeGetObjectVolatile intrinsic case.
5222 // Register `index` is not an index in an object array, but an
5223 // offset to an object reference field within object `obj`.
5224 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5225 DCHECK(instruction->GetLocations()->Intrinsified());
5226 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5227 << instruction->AsInvoke()->GetIntrinsic();
5228 DCHECK_EQ(offset, 0U);
5229 DCHECK_EQ(scale_factor, 0U);
5230 DCHECK_EQ(needs_null_check, 0U);
5231 // /* HeapReference<Object> */ ref = *(obj + index)
5232 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5233 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005234 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005235 // ArrayGet and UnsafeGetObject intrinsics cases.
5236 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5237 if (index.IsConstant()) {
5238 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5239 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5240 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005241 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005242 __ Add(temp2, obj, offset);
5243 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5244 temps.Release(temp2);
5245 }
Roland Levillain44015862016-01-22 11:47:17 +00005246 }
Roland Levillain44015862016-01-22 11:47:17 +00005247 } else {
5248 // /* HeapReference<Object> */ ref = *(obj + offset)
5249 MemOperand field = HeapOperand(obj, offset);
5250 if (use_load_acquire) {
5251 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5252 } else {
5253 Load(type, ref_reg, field);
5254 }
5255 }
5256
5257 // Object* ref = ref_addr->AsMirrorPtr()
5258 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5259
5260 // Slow path used to mark the object `ref` when it is gray.
5261 SlowPathCodeARM64* slow_path =
5262 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref, ref);
5263 AddSlowPath(slow_path);
5264
5265 // if (rb_state == ReadBarrier::gray_ptr_)
5266 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005267 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5268 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5269 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5270 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5271 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005272 __ Bind(slow_path->GetExitLabel());
5273}
5274
5275void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5276 Location out,
5277 Location ref,
5278 Location obj,
5279 uint32_t offset,
5280 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005281 DCHECK(kEmitCompilerReadBarrier);
5282
Roland Levillain44015862016-01-22 11:47:17 +00005283 // Insert a slow path based read barrier *after* the reference load.
5284 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005285 // If heap poisoning is enabled, the unpoisoning of the loaded
5286 // reference will be carried out by the runtime within the slow
5287 // path.
5288 //
5289 // Note that `ref` currently does not get unpoisoned (when heap
5290 // poisoning is enabled), which is alright as the `ref` argument is
5291 // not used by the artReadBarrierSlow entry point.
5292 //
5293 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5294 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5295 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5296 AddSlowPath(slow_path);
5297
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005298 __ B(slow_path->GetEntryLabel());
5299 __ Bind(slow_path->GetExitLabel());
5300}
5301
Roland Levillain44015862016-01-22 11:47:17 +00005302void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5303 Location out,
5304 Location ref,
5305 Location obj,
5306 uint32_t offset,
5307 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005308 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005309 // Baker's read barriers shall be handled by the fast path
5310 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5311 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005312 // If heap poisoning is enabled, unpoisoning will be taken care of
5313 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005314 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005315 } else if (kPoisonHeapReferences) {
5316 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5317 }
5318}
5319
Roland Levillain44015862016-01-22 11:47:17 +00005320void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5321 Location out,
5322 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005323 DCHECK(kEmitCompilerReadBarrier);
5324
Roland Levillain44015862016-01-22 11:47:17 +00005325 // Insert a slow path based read barrier *after* the GC root load.
5326 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005327 // Note that GC roots are not affected by heap poisoning, so we do
5328 // not need to do anything special for this here.
5329 SlowPathCodeARM64* slow_path =
5330 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5331 AddSlowPath(slow_path);
5332
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005333 __ B(slow_path->GetEntryLabel());
5334 __ Bind(slow_path->GetExitLabel());
5335}
5336
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005337void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5338 LocationSummary* locations =
5339 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5340 locations->SetInAt(0, Location::RequiresRegister());
5341 locations->SetOut(Location::RequiresRegister());
5342}
5343
5344void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5345 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005346 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005347 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005348 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005349 __ Ldr(XRegisterFrom(locations->Out()),
5350 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005351 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005352 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
5353 instruction->GetIndex() % ImTable::kSize, kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005354 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5355 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005356 __ Ldr(XRegisterFrom(locations->Out()),
5357 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005358 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005359}
5360
5361
5362
Alexandre Rames67555f72014-11-18 10:55:16 +00005363#undef __
5364#undef QUICK_ENTRY_POINT
5365
Alexandre Rames5319def2014-10-23 10:03:10 +01005366} // namespace arm64
5367} // namespace art